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/core/transport/connection_state.dart'; import 'package:pipelines_app/features/chat/conversation/chat_background_tasks.dart'; import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart'; import 'package:pipelines_app/features/chat/domain/chat_shells.dart'; /// A gateway that scripts the session snapshot and bounded output reads and can /// push a shell frame for the selected conversation. class _TasksGateway implements ApiGateway { final calls = <({String type, JsonMap fields})>[]; final _frames = StreamController.broadcast(); Future Function()? shells; Future Function(JsonMap fields)? output; @override Stream get frames => _frames.stream; @override Future call(String type, [JsonMap fields = const {}]) async { calls.add((type: type, fields: fields)); switch (type) { case 'chat_shells': return shells == null ? {'chat_shells': const []} : shells!(); case 'chat_shell_output': return output == null ? {'chat_shell_output': const {}} : output!(fields); case 'chat_subscribe': return {}; case 'chat_messages': return { 'chat_history': { 'messages': const [], 'next_cursor': '', 'has_more': false, }, }; } return {}; } List fieldsFor(String type) => [ for (final call in calls) if (call.type == type) call.fields, ]; void emitShell( String id, String status, { String command = '', String session = 's', }) => _frames.add({ 'type': 'chat_event', 'chat_event': { 'session_id': session, 'kind': 'shell', 'shell': {'id': id, 'status': status, 'command': command}, }, }); void close() => _frames.close(); } JsonMap _running(String id, {String command = 'go test ./...'}) => { 'id': id, 'status': 'running', 'command': command, }; JsonMap _snapshot(List shells) => {'chat_shells': shells}; Widget _host( _TasksGateway gateway, { bool cupertino = false, int syncToken = 0, }) { final child = ChatBackgroundTasks( gateway: gateway, sessionId: 's', syncToken: syncToken, ); if (cupertino) { return CupertinoApp( home: CupertinoPageScaffold(child: SingleChildScrollView(child: child)), ); } return MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold(body: SingleChildScrollView(child: child)), ); } Widget _thread(_TasksGateway gateway) => MaterialApp( theme: ThemeData(platform: TargetPlatform.android), home: Scaffold( body: ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}), ), ); Future _tapTask(WidgetTester tester, String id) async { await tester.tap(find.byKey(ValueKey('chat-bg-task-$id'))); await tester.pumpAndSettle(); } void main() { testWidgets('shows only the session snapshot tasks that are still running', ( tester, ) async { final gateway = _TasksGateway(); gateway.shells = () async => _snapshot([ _running('sh1'), {'id': 'sh2', 'status': 'exited', 'command': 'pnpm build'}, {'id': '', 'status': 'running', 'command': 'no identity'}, ]); addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); expect(find.text('Background tasks'), findsOneWidget); expect(find.text('1 running'), findsOneWidget); expect(find.text('Running'), findsOneWidget); expect(find.text('go test ./...'), findsOneWidget); expect(find.text('pnpm build'), findsNothing); expect(find.text('no identity'), findsNothing); expect(gateway.fieldsFor('chat_shells'), [ { 'chat_query': {'session_id': 's'}, }, ]); }); testWidgets('an honest empty snapshot draws no task surface', (tester) async { final gateway = _TasksGateway(); addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); expect(find.byKey(const Key('chat-background-tasks')), findsNothing); expect(find.text('Background tasks'), findsNothing); expect(gateway.fieldsFor('chat_shells'), hasLength(1)); }); testWidgets('pushed shell frames add and remove this session tasks', ( tester, ) async { final gateway = _TasksGateway(); addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); expect(find.text('Background tasks'), findsNothing); gateway.emitShell('sh1', 'running', command: 'tail -f server.log'); await tester.pumpAndSettle(); expect(find.text('Background tasks'), findsOneWidget); expect(find.text('1 running'), findsOneWidget); expect(find.text('tail -f server.log'), findsOneWidget); gateway.emitShell('sh1', 'exited'); await tester.pumpAndSettle(); expect(find.text('Background tasks'), findsNothing); gateway.emitShell('other', 'running', session: 'other-session'); await tester.pumpAndSettle(); expect(find.text('Background tasks'), findsNothing); }); testWidgets('more than three running tasks scroll inside the bar', ( tester, ) async { final gateway = _TasksGateway() ..shells = () async => _snapshot([ for (var index = 0; index < 4; index++) _running('sh$index'), ]); addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); expect(find.text('4 running'), findsOneWidget); expect( find.descendant( of: find.byType(ChatBackgroundTasks), matching: find.byType(SingleChildScrollView), ), findsOneWidget, ); for (var index = 0; index < 4; index++) { expect( find.byKey(ValueKey('chat-bg-task-sh$index')), findsOneWidget, ); } }); testWidgets('selecting a task reads one bounded session-linked page', ( tester, ) async { final gateway = _TasksGateway(); gateway.shells = () async => _snapshot([_running('sh1')]); gateway.output = (fields) async => { 'chat_shell_output': { 'shell': _running('sh1'), 'output': 'ok example\n--- PASS: TestOne\n', 'cursor': 0, 'size': 8192, 'truncated': true, }, }; addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); await _tapTask(tester, 'sh1'); expect(gateway.fieldsFor('chat_shell_output'), [ { 'chat_shell_output_query': { 'session_id': 's', 'shell_id': 'sh1', 'cursor': 0, 'limit': 64 * 1024, }, }, ]); expect(find.byKey(const Key('chat-bg-task-detail')), findsOneWidget); expect(find.text('Command'), findsOneWidget); expect(find.text('Task identity'), findsOneWidget); expect(find.text('sh1'), findsOneWidget); expect(find.text('Status: running'), findsOneWidget); expect(find.text('Output'), findsOneWidget); expect( tester .widget(find.byKey(const Key('chat-bg-task-output'))) .data, 'ok example\n--- PASS: TestOne\n', ); expect(find.text('Output is bounded to 64 KiB.'), findsOneWidget); expect(find.text('Refresh'), findsOneWidget); // Selecting the open task again closes its detail without another read. await _tapTask(tester, 'sh1'); expect(find.byKey(const Key('chat-bg-task-detail')), findsNothing); expect(gateway.fieldsFor('chat_shell_output'), hasLength(1)); }); testWidgets('the output pane reports empty, unavailable and a refusal', ( tester, ) async { final gateway = _TasksGateway() ..shells = () async => _snapshot([_running('sh1')]); addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); gateway.output = (_) async => { 'chat_shell_output': {'shell': _running('sh1')}, }; await _tapTask(tester, 'sh1'); expect(find.text('No output yet.'), findsOneWidget); gateway.output = (_) async => { 'chat_shell_output': {'shell': _running('sh1'), 'unavailable': true}, }; await _tapTask(tester, 'sh1'); await _tapTask(tester, 'sh1'); expect(find.text('Output unavailable for this task.'), findsOneWidget); gateway.output = (_) async => throw ProtocolError('output refused'); await _tapTask(tester, 'sh1'); await _tapTask(tester, 'sh1'); expect(find.byKey(const Key('chat-bg-task-output-error')), findsOneWidget); expect(find.text('output refused'), findsOneWidget); expect(find.text('Retry'), findsOneWidget); gateway.output = (_) async => { 'chat_shell_output': {'shell': _running('sh1'), 'output': 'fresh output'}, }; await tester.tap(find.text('Retry')); await tester.pumpAndSettle(); expect(find.byKey(const Key('chat-bg-task-output-error')), findsNothing); expect( tester .widget(find.byKey(const Key('chat-bg-task-output'))) .data, 'fresh output', ); }); testWidgets('a refused snapshot is reported and Retry rereads it', ( tester, ) async { final gateway = _TasksGateway() ..shells = () async => throw ProtocolError('server down'); addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); expect( find.textContaining('Background tasks unavailable. server down'), findsOneWidget, ); expect(find.text('0 running'), findsNothing); gateway.shells = () async => _snapshot([_running('sh1')]); await tester.tap(find.text('Retry')); await tester.pumpAndSettle(); expect(find.byKey(const Key('chat-background-tasks-error')), findsNothing); expect(find.text('Background tasks'), findsOneWidget); expect(find.text('1 running'), findsOneWidget); expect(gateway.fieldsFor('chat_shells'), hasLength(2)); }); testWidgets('an exit closes the selected pane and the bar', (tester) async { final gateway = _TasksGateway(); gateway.shells = () async => _snapshot([_running('sh1')]); gateway.output = (_) async => { 'chat_shell_output': { 'shell': _running('sh1'), 'output': 'still running', }, }; addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); await _tapTask(tester, 'sh1'); expect(find.byKey(const Key('chat-bg-task-detail')), findsOneWidget); gateway.emitShell('sh1', 'exited'); await tester.pumpAndSettle(); expect(find.byKey(const Key('chat-bg-task-detail')), findsNothing); expect(find.text('Background tasks'), findsNothing); }); testWidgets('a replaced socket drops held tasks and re-learns the snapshot', ( tester, ) async { final gateway = _TasksGateway() ..shells = () async => _snapshot([_running('sh1')]); addTearDown(gateway.close); await tester.pumpWidget(_host(gateway)); await tester.pumpAndSettle(); expect(find.text('1 running'), findsOneWidget); gateway.shells = () async => _snapshot(const []); await tester.pumpWidget(_host(gateway, syncToken: 1)); await tester.pumpAndSettle(); expect(find.text('Background tasks'), findsNothing); expect(gateway.fieldsFor('chat_shells'), hasLength(2)); }); testWidgets('the Cupertino host keeps native buttons and the same facts', ( tester, ) async { final gateway = _TasksGateway(); gateway.shells = () async => _snapshot([_running('sh1')]); gateway.output = (_) async => { 'chat_shell_output': { 'shell': _running('sh1'), 'output': 'native output', }, }; addTearDown(gateway.close); await tester.pumpWidget(_host(gateway, cupertino: true)); await tester.pumpAndSettle(); expect(find.byType(CupertinoButton), findsWidgets); expect(find.text('1 running'), findsOneWidget); await _tapTask(tester, 'sh1'); expect(find.text('Command'), findsOneWidget); expect(find.text('Status: running'), findsOneWidget); expect( tester .widget(find.byKey(const Key('chat-bg-task-output'))) .data, 'native output', ); }); testWidgets('the conversation mounts the session task surface', ( tester, ) async { final gateway = _TasksGateway() ..shells = () async => _snapshot([_running('sh1')]); addTearDown(gateway.close); await tester.pumpWidget(_thread(gateway)); await tester.pumpAndSettle(); expect(find.byKey(const Key('chat-background-tasks')), findsOneWidget); expect(find.text('Background tasks'), findsOneWidget); expect(find.text('1 running'), findsOneWidget); }); }