import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/core/transport/connection_state.dart'; import 'package:pipelines_app/features/chat/session_screen.dart'; /// Stateful chat-session fixture for the real-browser probe: every accepted /// mutation changes the list the next `chat_sessions` read returns, so the /// screen must refresh from the server rather than guess. A title of /// `Refused title` is refused with the server's words; the first create returns /// a reply without `chat_session`; `?unseen=1` reports newer recency after the /// first read so the `New` marker can be observed. class _FixtureGateway implements ApiGateway { _FixtureGateway({this.bumpAfterFirstRead = false}); /// `?unseen=1`: the first list read seeds the device watermark and later /// reads report newer activity, so the browser can prove the `New` marker. final bool bumpAfterFirstRead; int _reads = 0; int _creates = 0; final _sessions = [ { 'id': 'probe-open', 'title': 'Open fixture', 'directory': '/workspace/demo', 'starred': true, }, { 'id': 'probe-closed', 'title': 'Closed fixture', 'directory': '/workspace/archive', 'closed_at': '2026-01-01', }, { 'id': 'probe-stream', 'title': 'Streaming fixture', 'directory': '/workspace/demo', 'running': true, }, ]; int _nextId = 1; @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async { switch (type) { case 'chat_sessions': _reads++; return { 'chat_sessions': [for (final session in _sessions) _row(session)], }; case 'chat_directories': return { 'chat_directories': [ '/workspace/demo', '/workspace/archive', '/home/user/project', ], }; case 'chat_backends': return { 'chat_backends': ['opencode', 'codex'], }; case 'chat_create': final body = fields['chat_create']! as JsonMap; // Exercise the missing-session refusal independently of the fixed // title the create dialog now submits. if (_creates++ == 0) return const {}; final session = { 'id': 'probe-new-${_nextId++}', 'title': body['title'], 'directory': body['directory'], }; _sessions.insert(0, session); return {'chat_session': Map.from(session)}; case 'chat_rename': final body = fields['chat_rename']! as JsonMap; if ('${body['title']}' == 'Refused title') { throw const ProtocolError('a title with usable text is required'); } _update('${body['session_id']}', 'title', body['title']); return const {}; case 'chat_star': final body = fields['chat_query']! as JsonMap; _update('${body['session_id']}', 'starred', body['starred']); return const {}; case 'chat_close': final body = fields['chat_query']! as JsonMap; _update('${body['session_id']}', 'closed_at', '2026-01-02'); return const {}; case 'chat_reopen': final body = fields['chat_query']! as JsonMap; // A refusal long enough to need several lines at 320px with 2x text. if ('${body['session_id']}' == 'probe-closed') { throw const ProtocolError( 'the chat is locked by another operator and cannot be reopened ' 'from here; refresh the list to see its current state', ); } _update('${body['session_id']}', 'closed_at', null); return const {}; default: return {'chat_session': {}}; } } void _update(String id, String key, Object? value) { for (final session in _sessions) { if (session['id'] == id) session[key] = value; } } JsonMap _row(JsonMap session) { final row = Map.from(session); if (bumpAfterFirstRead && (row['id'] == 'probe-open' || row['id'] == 'probe-closed')) { row['updated_at'] = _reads > 1 ? 2000 : 1000; } return row; } } const _probeStyle = String.fromEnvironment( 'CHAT_PROBE_STYLE', defaultValue: 'material', ); void main() { final dark = Uri.base.queryParameters['dark'] == '1'; final unseen = Uri.base.queryParameters['unseen'] == '1'; final home = ChatSessionsScreen( gateway: _FixtureGateway(bumpAfterFirstRead: unseen), onOpen: (_) {}, ); if (_probeStyle == 'cupertino') { runApp( CupertinoApp( theme: CupertinoThemeData( brightness: dark ? Brightness.dark : Brightness.light, ), home: home, ), ); return; } runApp( MaterialApp( theme: ThemeData( platform: TargetPlatform.android, brightness: dark ? Brightness.dark : Brightness.light, ), home: home, ), ); }