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/domain/chat_cell_numbers.dart'; import 'package:pipelines_app/features/chat/domain/chat_transcript.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_message_bubble.dart'; import 'package:pipelines_app/features/chat/ui/transcript/chat_message_view.dart'; import 'package:pipelines_app/features/chat/ui/transcript/notebook/notebook_input_cell.dart'; import 'package:pipelines_app/features/chat/ui/transcript/notebook/notebook_tool_cell.dart'; import 'package:pipelines_app/features/chat/ui/transcript/notebook/notebook_tool_cells.dart'; import 'package:pipelines_app/features/chat/ui/transcript/paper/paper_quote.dart'; import 'package:pipelines_app/features/chat/ui/transcript/terminal/terminal_activity_rows.dart'; import 'package:pipelines_app/features/chat/ui/transcript/terminal/terminal_prompt_line.dart'; import 'package:pipelines_app/ui/theme/app_design.dart'; JsonMap _row(JsonMap message) => transcriptRows([message]).single; final JsonMap _operator = _row({ 'id': 'u1', 'role': 'user', 'parts': [ {'type': 'text', 'text': 'Fix the Windows paths.'}, ], }); JsonMap _tool(String id, String name, String status, {String output = ''}) => { 'type': 'tool', 'id': id, 'name': name, 'time': {'created': 1000, 'completed': 2200}, 'state': { 'status': status, 'input': {'command': 'go test ./...'}, if (output.isNotEmpty) 'content': [ {'type': 'text', 'text': output}, ], if (status == 'error') 'error': {'message': 'exit status 1'}, }, }; const String _longOutput = 'ok pkg/a\nok pkg/b\nok pkg/c\nok pkg/d\nPASS'; JsonMap _agent({bool streaming = false}) => { ..._row({ 'id': 'a1', 'role': 'assistant', 'parts': [ _tool('t1', 'Bash', 'completed', output: _longOutput), _tool('t2', 'Read', 'error'), {'type': 'text', 'text': 'Fixed the separators.'}, ], }), if (streaming) 'streaming': true, }; final JsonMap _followup = _row({ 'id': 'f1', 'role': 'user', 'origin': 'followup', 'client': 'Agent follow-up', 'parts': [ {'type': 'text', 'text': 'Check whether run 42 finished.'}, ], }); Widget _host( AppDesign design, List<(JsonMap, int?)> rows, { double width = 900, }) => MaterialApp( theme: design.light(), home: Scaffold( body: Center( child: SizedBox( width: width, child: ListView( children: [ for (final (row, cell) in rows) ChatMessageView( row: row, cell: cell, textBuilder: chatMarkdownText, ), ], ), ), ), ), ); Finder _label(String text) => find.byWidgetPredicate( (widget) => widget is Text && widget.key == const Key('chat-notebook-gutter-label') && widget.data == text, ); void main() { test('cells number each operator turn and pair its answers', () { final rows = [ {'kind': 'assistant', 'key': 'early'}, {'kind': 'user', 'key': 'u1'}, {'kind': 'assistant', 'key': 'a1'}, {'kind': 'system_notice', 'key': 'n1'}, {'kind': 'assistant', 'key': 'a1b'}, {'kind': 'user', 'key': 'f1'}, {'kind': 'assistant', 'key': 'chat-pending-response'}, ]; expect(chatCellNumbers(rows), { 'u1': 1, 'a1': 1, 'a1b': 1, 'f1': 2, 'chat-pending-response': 2, }); // Derived from the turn order alone: the same history numbers the same. expect(chatCellNumbers(rows), chatCellNumbers([...rows])); }); testWidgets('Notebook: In and Out cells with right-aligned gutter labels', ( tester, ) async { await tester.pumpWidget( _host(AppDesign.notebook, [(_operator, 3), (_agent(), 3)]), ); expect(find.byType(NotebookInputCell), findsOneWidget); expect(find.byType(TerminalPromptLine), findsNothing); expect(find.byType(PaperQuote), findsNothing); final inLabel = _label('In [3]:'); final outLabel = _label('Out [3]:'); expect(inLabel, findsOneWidget); expect(outLabel, findsOneWidget); // One gutter: both labels end in the same column, left of the cells. expect( tester.getTopRight(inLabel).dx, closeTo(tester.getTopRight(outLabel).dx, 0.5), ); final cell = find.byType(NotebookInputCell); expect( tester.getTopRight(inLabel).dx, lessThan(tester.getTopLeft(cell).dx), ); // The prompt's and the answer's text share one column. expect( tester.getTopLeft(find.text('Fix the Windows paths.')).dx, closeTo(tester.getTopLeft(find.text('Fixed the separators.')).dx, 0.5), ); // The label sits on the prompt's first line. final labelBox = tester.getRect(inLabel); final textBox = tester.getRect(find.text('Fix the Windows paths.')); expect(labelBox.bottom, closeTo(textBox.bottom, 3)); }); testWidgets('Notebook: a prompt not yet run reads In [ ]:', (tester) async { await tester.pumpWidget(_host(AppDesign.notebook, [(_operator, null)])); expect(_label('In [ ]:'), findsOneWidget); }); testWidgets('Notebook: tool calls are collapsed sub-cells that expand in ' 'place', (tester) async { await tester.pumpWidget(_host(AppDesign.notebook, [(_agent(), 1)])); expect(find.byType(NotebookToolCells), findsOneWidget); expect(find.byType(TerminalActivityRows), findsNothing); final cells = find.byType(NotebookToolCell); expect(cells, findsNWidgets(2)); // Collapsed: the first lines of the output, "+N lines", the duration and // the status on the right. expect(find.byKey(const Key('chat-notebook-tool-preview')), findsWidgets); expect(find.text('+4 lines'), findsOneWidget); expect(find.text('1.2s'), findsNWidgets(2)); expect(find.byKey(const Key('chat-notebook-tool-output')), findsNothing); expect(find.textContaining('PASS'), findsNothing); await tester.tap(find.byKey(const Key('chat-notebook-tool-header')).first); await tester.pump(); expect(find.byKey(const Key('chat-notebook-tool-output')), findsOneWidget); expect(find.textContaining('PASS'), findsOneWidget); expect(find.text('+4 lines'), findsNothing); await tester.tap(find.byKey(const Key('chat-notebook-tool-header')).first); await tester.pump(); expect(find.byKey(const Key('chat-notebook-tool-output')), findsNothing); // The status line still opens the activity details. await tester.tap(find.byKey(const Key('chat-turn-activity'))); await tester.pumpAndSettle(); expect(find.text('Activity details'), findsOneWidget); }); testWidgets('Notebook: a long turn folds its earlier calls', (tester) async { final row = _row({ 'id': 'a9', 'role': 'assistant', 'parts': [ for (var i = 0; i < notebookToolMaxCalls + 3; i++) _tool('t$i', 'Read', 'completed', output: 'line $i'), ], }); await tester.pumpWidget(_host(AppDesign.notebook, [(row, 1)])); expect(find.byType(NotebookToolCell), findsNWidgets(notebookToolMaxCalls)); await tester.tap(find.byKey(const Key('chat-notebook-tool-earlier'))); await tester.pump(); expect( find.byType(NotebookToolCell), findsNWidgets(notebookToolMaxCalls + 3), ); }); testWidgets('Notebook: a running turn shows Jupyter\'s [*] busy marker', ( tester, ) async { await tester.pumpWidget( _host(AppDesign.notebook, [(_agent(streaming: true), 2)]), ); expect(_label('Out [2]:'), findsOneWidget); final busy = find.byKey(const Key('chat-notebook-busy')); expect(busy, findsOneWidget); expect(tester.widget(busy).data, '[*]'); expect( tester.getTopLeft(busy).dy, greaterThan(tester.getTopLeft(_label('Out [2]:')).dy), ); await tester.pumpWidget(_host(AppDesign.notebook, [(_agent(), 2)])); expect(find.byKey(const Key('chat-notebook-busy')), findsNothing); }); testWidgets('Notebook: a follow-up is an In cell with a scheduled tag', ( tester, ) async { await tester.pumpWidget(_host(AppDesign.notebook, [(_followup, 4)])); expect(_label('In [4]:'), findsOneWidget); expect(find.byType(NotebookScheduledCell), findsOneWidget); final tag = find.byKey(const Key('chat-notebook-scheduled-tag')); expect( find.descendant(of: tag, matching: find.text('scheduled')), findsOneWidget, ); expect(find.text('Follow-up from agent'), findsOneWidget); expect(find.text('Check whether run 42 finished.'), findsOneWidget); }); testWidgets('Notebook: at phone width the gutter collapses to an inline ' 'label above the cell', (tester) async { await tester.pumpWidget( _host(AppDesign.notebook, [(_operator, 1), (_agent(), 1)], width: 380), ); final label = _label('In [1]:'); final cell = find.byType(NotebookInputCell); expect( tester.getBottomLeft(label).dy, lessThanOrEqualTo(tester.getTopLeft(cell).dy), ); expect(tester.getTopLeft(label).dx, closeTo(tester.getTopLeft(cell).dx, 4)); }); for (final design in [ AppDesign.standard, AppDesign.terminal, AppDesign.paper, ]) { testWidgets('${design.label} is unchanged: no cells, even with a number', ( tester, ) async { await tester.pumpWidget( _host(design, [ (_operator, 1), (_agent(streaming: true), 1), (_followup, 2), ]), ); expect(find.byKey(const Key('chat-notebook-gutter-label')), findsNothing); expect(find.byType(NotebookInputCell), findsNothing); expect(find.byType(NotebookToolCells), findsNothing); expect(find.byType(NotebookScheduledCell), findsNothing); expect(find.byKey(const Key('chat-notebook-busy')), findsNothing); if (design == AppDesign.standard) { expect(find.byType(ChatUserBubble), findsOneWidget); } }); } }