Files
pipelines/app/test/features/chat/conversation/chat_activity_line_test.dart
T

376 lines
11 KiB
Dart

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_thread_view.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
import 'chat_thread_fixtures.dart';
JsonMap _record({
String type = 'activity',
String? id,
String? kind,
String? state,
String? label,
}) => {
'type': type,
'id': ?id,
'kind': ?kind,
'state': ?state,
'label': ?label,
};
JsonMap _turn(String id, List<JsonMap> parts) => {
'id': id,
'role': 'assistant',
'parts': parts,
};
List<JsonMap> _activityRows(List<JsonMap> rows) =>
rows.where((row) => row['kind'] == 'activity').toList();
void main() {
group('recorded activity wording', () {
test('running kinds use the web phase words', () {
expect(
chatActivityLabel(_record(kind: 'compaction', state: 'running')),
'Compacting context…',
);
expect(
chatActivityLabel(_record(kind: 'thinking', state: 'running')),
'Thinking…',
);
expect(
chatActivityLabel(_record(kind: 'tool', state: 'running')),
'Running a tool…',
);
expect(
chatActivityLabel(_record(kind: 'waiting', state: 'running')),
'Waiting…',
);
});
test('the part type is the kind when the record does not name one', () {
expect(
chatActivityLabel(_record(type: 'compaction', state: 'running')),
'Compacting context…',
);
});
test('terminal records keep their recorded label', () {
expect(
chatActivityLabel(
_record(
kind: 'compaction',
state: 'completed',
label: 'Context compacted',
),
),
'Context compacted',
);
expect(
chatActivityLabel(
_record(
kind: 'compaction',
state: 'failed',
label: 'Compaction failed',
),
),
'Compaction failed',
);
});
test('an unknown kind keeps its recorded label and state', () {
expect(
chatActivityLabel(
_record(kind: 'weaving', state: 'running', label: 'Weaving context'),
),
'Weaving context',
);
expect(
chatActivityLabel(
_record(kind: 'weaving', state: 'failed', label: 'Weave failed'),
),
'Weave failed',
);
});
test('no recorded label keeps the recorded kind and state verbatim', () {
expect(
chatActivityLabel(_record(kind: 'weaving', state: 'completed')),
'weaving · completed',
);
expect(chatActivityLabel(_record()), 'activity');
});
});
group('recorded activity rows', () {
test('one line per sequence, worded from its latest recorded update', () {
final rows = transcriptRows([
_turn('a1', [
_record(kind: 'thinking', state: 'running', id: 'e1'),
{'type': 'text', 'text': 'answer'},
_record(type: 'compaction', state: 'running', id: 'e2'),
_record(
type: 'compaction',
state: 'completed',
id: 'e3',
label: 'Context compacted',
),
]),
]);
expect(rows.map((row) => row['kind']), ['activity', 'assistant']);
final activity = rows.first;
expect(activity['records'], hasLength(3));
expect(activity['label'], 'Context compacted');
expect(activity['state'], 'completed');
expect((rows.last['parts'] as List).single['text'], 'answer');
});
test('a sequence can span consecutive assistant messages', () {
final rows = transcriptRows([
_turn('a1', [_record(kind: 'thinking', state: 'running', id: 'e1')]),
_turn('a2', [
_record(
kind: 'thinking',
state: 'completed',
id: 'e2',
label: 'Thought',
),
{'type': 'text', 'text': 'answer'},
]),
]);
expect(_activityRows(rows), hasLength(1));
expect(_activityRows(rows).single['label'], 'Thought');
});
test('separate turns keep separate lines', () {
final rows = transcriptRows([
_turn('a1', [_record(kind: 'compaction', state: 'running', id: 'e1')]),
{'id': 'u1', 'role': 'user', 'parts': <JsonMap>[]},
_turn('a2', [_record(kind: 'compaction', state: 'running', id: 'e2')]),
]);
expect(_activityRows(rows), hasLength(2));
expect(_activityRows(rows).map((row) => row['key']), [
'chat-activity-a1',
'chat-activity-a2',
]);
});
test('an activity-only turn renders no empty assistant row', () {
final rows = transcriptRows([
_turn('a1', [_record(kind: 'compaction', state: 'running')]),
]);
expect(rows.map((row) => row['kind']), ['activity']);
});
});
group('recorded activity rendering', () {
Future<FakeChatGateway> pumpThread(
WidgetTester tester, {
required bool cupertino,
List<JsonMap>? activityParts,
}) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([
chatMessage(
'u1',
'user',
parts: [
{'type': 'text', 'text': 'Prompt'},
],
),
chatMessage(
'a1',
'assistant',
parts:
activityParts ??
[
{
'type': 'activity',
'id': 'e1',
'kind': 'compaction',
'state': 'running',
},
{'type': 'text', 'id': 't1', 'text': 'Answer'},
],
),
]);
final view = ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
);
await tester.pumpWidget(
cupertino
? CupertinoApp(
theme: const CupertinoThemeData(brightness: Brightness.dark),
home: CupertinoPageScaffold(child: view),
)
: MaterialApp(
theme: ThemeData(),
home: Scaffold(body: view),
),
);
await tester.pumpAndSettle();
return gateway;
}
testWidgets('Material shows the recorded line once beside the answer', (
tester,
) async {
await pumpThread(tester, cupertino: false);
expect(find.text('Compacting context…'), findsOneWidget);
expect(find.text('Answer'), findsOneWidget);
expect(find.textContaining('Unrecognised part'), findsNothing);
});
testWidgets('Cupertino shows the same recorded line', (tester) async {
await pumpThread(tester, cupertino: true);
expect(find.text('Compacting context…'), findsOneWidget);
expect(find.text('Answer'), findsOneWidget);
expect(find.textContaining('Unrecognised part'), findsNothing);
});
group('expanded detail', () {
final recorded = <JsonMap>[
{
'type': 'activity',
'id': 'e1',
'kind': 'tool',
'state': 'completed',
'label': 'Ran a tool',
'input': {'command': 'ls'},
'output': 'two files',
'error': 'exit 1',
'duration': 1200,
'diff': {
'file': 'lib/main.dart',
'added': 1,
'removed': 1,
'lines': [
{'kind': 'remove', 'text': 'old'},
{'kind': 'add', 'text': 'new'},
],
},
},
{'type': 'text', 'id': 't1', 'text': 'Answer'},
];
final sparse = <JsonMap>[
{
'type': 'activity',
'id': 'e1',
'kind': 'compaction',
'state': 'running',
},
{'type': 'text', 'id': 't1', 'text': 'Answer'},
];
Future<void> toggle(WidgetTester tester) async {
final control = find.byKey(
const ValueKey<String>('chat-activity-toggle'),
);
// Drag like an operator: a programmatic scroll is undone by the
// view's follow-to-latest, while a user drag stops following.
final list = find.byKey(const ValueKey<String>('chat-thread-s'));
final viewport = tester.getRect(list);
for (var i = 0; i < 10; i++) {
final rect = tester.getRect(control);
if (rect.top >= viewport.top && rect.bottom <= viewport.bottom) break;
await tester.drag(
list,
Offset(0, rect.top < viewport.top ? 200 : -200),
);
await tester.pumpAndSettle();
}
await tester.tap(control);
await tester.pumpAndSettle();
}
for (final cupertino in [false, true]) {
final host = cupertino ? 'Cupertino' : 'Material';
testWidgets('$host shows only the recorded detail values', (
tester,
) async {
await pumpThread(
tester,
cupertino: cupertino,
activityParts: recorded,
);
await toggle(tester);
expect(
find.byKey(const ValueKey<String>('chat-activity-details')),
findsOneWidget,
);
expect(find.text('Lifecycle'), findsOneWidget);
expect(find.text('completed'), findsOneWidget);
expect(find.textContaining('"command": "ls"'), findsOneWidget);
expect(find.text('two files'), findsOneWidget);
expect(find.text('exit 1'), findsOneWidget);
expect(find.text('1.2s'), findsOneWidget);
expect(
find.text('lib/main.dart\n+1 -1\n- old\n+ new'),
findsOneWidget,
);
for (final absent in const [
'No lifecycle recorded.',
'No input recorded.',
'No output recorded.',
'No error recorded.',
'No duration recorded.',
'No edit diff recorded.',
]) {
expect(find.text(absent), findsNothing, reason: absent);
}
});
testWidgets('$host words every absent field exactly', (tester) async {
await pumpThread(tester, cupertino: cupertino, activityParts: sparse);
await toggle(tester);
expect(find.text('running'), findsOneWidget);
expect(find.text('No input recorded.'), findsOneWidget);
expect(find.text('No output recorded.'), findsOneWidget);
expect(find.text('No error recorded.'), findsOneWidget);
expect(find.text('No duration recorded.'), findsOneWidget);
expect(find.text('No edit diff recorded.'), findsOneWidget);
expect(find.text('No lifecycle recorded.'), findsNothing);
});
testWidgets('$host collapses back to the single line', (tester) async {
await pumpThread(
tester,
cupertino: cupertino,
activityParts: recorded,
);
await toggle(tester);
expect(
find.byKey(const ValueKey<String>('chat-activity-details')),
findsOneWidget,
);
await toggle(tester);
expect(
find.byKey(const ValueKey<String>('chat-activity-details')),
findsNothing,
);
expect(find.text('Input'), findsNothing);
expect(find.text('Ran a tool'), findsOneWidget);
expect(find.text('Answer'), findsOneWidget);
});
}
});
});
}