import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'chat_thread_fixtures.dart'; /// A gateway for one open fixture conversation with a composer. History pages /// are tests' own; prompt/interrupt replies are controlled per test. class ComposerBarGateway implements ApiGateway { final calls = <({String type, JsonMap fields})>[]; final _frames = StreamController.broadcast(); JsonMap page = chatPage(const []); Future Function(String text, String operationId)? prompt; Future Function()? interrupt; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add((type: type, fields: fields)); switch (type) { case 'chat_subscribe': return {}; case 'chat_messages': return page; case 'chat_prompt': final body = fields['chat_prompt'] as JsonMap? ?? const {}; return prompt == null ? {} : prompt!('${body['text'] ?? ''}', '${body['operation_id'] ?? ''}'); case 'chat_interrupt': return interrupt == null ? {} : interrupt!(); } return {}; } @override Stream get frames => _frames.stream; List get promptBodies => calls .where((call) => call.type == 'chat_prompt') .map((call) => call.fields['chat_prompt'] as JsonMap) .toList(); List get interruptBodies => calls .where((call) => call.type == 'chat_interrupt') .map((call) => call.fields['chat_query'] as JsonMap) .toList(); void emitEvent(JsonMap body) => _frames.add({'type': 'chat_event', 'chat_event': body}); void close() => _frames.close(); } Widget materialHost(Widget child) => MaterialApp( theme: ThemeData( platform: TargetPlatform.android, brightness: Brightness.light, ), home: Scaffold(body: child), ); Widget cupertinoHost(Widget child) => CupertinoApp( theme: const CupertinoThemeData(brightness: Brightness.dark), home: CupertinoPageScaffold(child: child), ); Widget composerThread( ComposerBarGateway gateway, { bool cupertino = false, int syncToken = 0, ChatDraftStore? drafts, String Function()? newOperationId, VoidCallback? onBack, }) { final thread = ChatThreadView( gateway: gateway, sessionId: 's', syncToken: syncToken, onBack: onBack ?? () {}, drafts: drafts ?? MemoryChatDraftStore(), newOperationId: newOperationId, ); return cupertino ? cupertinoHost(thread) : materialHost(thread); } /// The common fixture turn, so a rebuild has real transcript content to move. JsonMap fixtureTurn({ String user = 'Stored question', String answer = 'Stored answer', }) => chatPage([ chatMessage( 'u1', 'user', parts: [ {'type': 'text', 'text': user}, ], ), chatMessage( 'a1', 'assistant', parts: [ {'type': 'text', 'text': answer}, ], ), ]); String fieldText(WidgetTester tester) => tester .widget( find.descendant( of: find.byKey(const Key('chat-composer-field')), matching: find.byType(EditableText), ), ) .controller .text;