import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/screen.dart'; /// Browser fixture for the Chat destination wiring: the real session list opens /// the real controller/view against a scripted gateway. Server data only; no /// composer, routes, sockets or production store are involved. /// /// Sessions page with different server answers so the driver can reproduce the /// disclosure-state variants: alternating turns with the older page keeping or /// ending history, and pages of consecutive assistant messages that fold into /// one turn at the page boundary. class _FixtureGateway implements ApiGateway { final _frames = StreamController.broadcast(); @override Stream get frames => _frames.stream; void _pushLiveDelta() { Timer(const Duration(milliseconds: 500), () { _frames.add({ 'type': 'chat_event', 'chat_event': { 'session_id': 'probe-session', 'message_id': 'a-NEWEST', 'kind': 'text', 'part': {'id': 'p-live', 'type': 'text'}, 'delta': 'A live delta updated this thread.', }, }); }); } @override Future call(String type, [JsonMap fields = const {}]) async { switch (type) { case 'chat_sessions': return { 'chat_sessions': [ { 'id': 'probe-session', 'title': 'Probe conversation', 'directory': '/workspace/demo', 'starred': true, }, { 'id': 'probe-more', 'title': 'Probe paging session', 'directory': '/workspace/demo', }, { 'id': 'probe-other', 'title': 'Another stored chat', 'directory': '/workspace/demo', }, { 'id': 'probe-fold', 'title': 'Probe assistant fold session', 'directory': '/workspace/demo', }, { 'id': 'probe-fold-end', 'title': 'Probe assistant fold last page', 'directory': '/workspace/demo', }, ], }; case 'chat_subscribe': return {}; case 'chat_messages': final query = fields['chat_query'] as Map? ?? const {}; final cursor = '${query['cursor'] ?? ''}'; if (query['session_id'] == 'probe-fold' || query['session_id'] == 'probe-fold-end') { final keepsPaging = query['session_id'] == 'probe-fold'; if (cursor.isEmpty) { return _foldPage( prefix: 'FOLD_NEWEST', ids: const ['fold-newest', 'fold-fourth'], reasoning: const [ 'FOLD NEWEST reasoning body.', 'FOLD FOURTH reasoning body.', ], cursor: 'probe/opaque?fold=2', hasMore: true, ); } return _foldPage( prefix: 'FOLD_OLDER', ids: const ['fold-second', 'fold-oldest'], reasoning: const [ 'FOLD SECOND reasoning body.', 'FOLD OLDEST reasoning body.', ], cursor: keepsPaging ? 'probe/opaque?fold=3' : '', hasMore: keepsPaging, ); } if (query['session_id'] == 'probe-more') { return cursor.isEmpty ? _page( prefix: 'MORE_NEWEST', user: 'A second read-only question from the server.', reasoning: 'MORE NEWEST reasoning body.', reply: 'The second stored assistant reply.', cursor: 'probe/opaque?more=2', hasMore: true, ) : _page( prefix: 'MORE_OLDER', user: 'An even older message for the second chat.', reasoning: 'MORE OLDER reasoning body that must stay collapsed.', reply: 'Second older reply.', cursor: 'probe/opaque?more=3', hasMore: true, ); } if (cursor.isEmpty) { // Deliver the live delta only after the initial history page, so the // merge cannot be mistaken for the delta being lost. _pushLiveDelta(); return _page( prefix: 'NEWEST', user: 'Please keep this exact read-only message from the server.', reasoning: 'NEWEST reasoning body that belongs to the newest answer.', reply: 'The stored assistant reply is rendered by the existing ' 'native message view.', cursor: 'probe/opaque?page=2', hasMore: true, ); } return _page( prefix: 'OLDER', user: 'This older stored message came from the opaque cursor page.', reasoning: 'OLDER reasoning body that must not steal the newest ' 'disclosure.', reply: 'Older reply kept in order.', cursor: '', hasMore: false, ); } return {}; } JsonMap _page({ required String prefix, required String user, required String reasoning, required String reply, required String cursor, required bool hasMore, }) => { 'chat_history': { 'messages': [ { 'id': 'u-$prefix', 'role': 'user', 'client': 'Probe host', 'parts': [ {'type': 'text', 'text': user}, ], }, { 'id': 'a-$prefix', 'role': 'assistant', 'parts': [ {'type': 'reasoning', 'text': reasoning}, {'type': 'text', 'text': reply}, {'type': 'tool', 'name': 'shell'}, ], }, ], 'next_cursor': cursor, 'has_more': hasMore, }, }; /// The paged-history fixture shape: a page of consecutive assistant messages /// with no user message at the page boundary, so paging folds into one turn. JsonMap _foldPage({ required String prefix, required List ids, required List reasoning, required String cursor, required bool hasMore, }) => { 'chat_history': { 'messages': [ for (var index = 0; index < ids.length; index++) { 'id': ids[index], 'role': 'assistant', 'parts': [ { 'type': 'reasoning', 'id': 'r-${ids[index]}', 'text': reasoning[index], }, { 'type': 'text', 'id': 't-${ids[index]}', 'text': 'Answer $prefix stored as ${ids[index]}.', }, ], }, ], 'next_cursor': cursor, 'has_more': hasMore, }, }; } const _style = String.fromEnvironment( 'CHAT_WIRE_PROBE_STYLE', defaultValue: 'material', ); void main() { final screen = ChatScreen( gateway: _FixtureGateway(), onSessionSelected: null, ); if (_style == 'cupertino') { runApp( CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), home: CupertinoPageScaffold( navigationBar: const CupertinoNavigationBar(middle: Text('Chat')), child: SafeArea(child: screen), ), ), ); return; } runApp( MaterialApp( theme: ThemeData( brightness: Brightness.dark, platform: TargetPlatform.android, ), home: Scaffold( appBar: AppBar(title: const Text('Chat')), body: screen, ), ), ); }