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

406 lines
13 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_notice.dart';
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
import 'chat_thread_fixtures.dart';
Widget host(Widget child) => MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(body: child),
);
Widget cupertinoHost(Widget child) => CupertinoApp(
theme: const CupertinoThemeData(brightness: Brightness.dark),
home: CupertinoPageScaffold(child: child),
);
JsonMap compactionTurn(
String messageId,
String id, [
String state = 'running',
]) => chatMessage(
messageId,
'assistant',
parts: [
{'type': 'compaction', 'kind': 'compaction', 'id': id, 'status': state},
],
);
JsonMap runningCompaction(String id) => compactionTurn('a1', id);
const compactionDismissKey = ValueKey<String>('chat-notice-dismiss-compaction');
void main() {
test('each recorded condition keeps its own exact wording', () {
final notices = chatNotices(
historyError: 'Bad state: Connection closed',
liveError: 'Exception: live watch refused',
messages: [runningCompaction('c1')],
);
expect(notices, hasLength(3));
expect(notices[0].kind, ChatNoticeKind.connectionRefused);
expect(notices[0].text, 'Chat history unavailable\nConnection closed');
expect(notices[1].kind, ChatNoticeKind.liveFailure);
expect(notices[1].text, 'Live updates unavailable\nlive watch refused');
// The phase words are the web's own (`turnPhaseLabel`).
expect(notices[2].kind, ChatNoticeKind.compaction);
expect(notices[2].text, 'Compacting context…');
expect(notices[2].detail, isEmpty);
expect(
[for (final kind in ChatNoticeKind.values) chatNoticeDismissLabel(kind)],
[
'Dismiss connection notice',
'Dismiss live updates notice',
'Dismiss compaction notice',
'Dismiss command notice',
],
);
// A later compaction has its own recorded condition.
final next = chatNotices(messages: [runningCompaction('c2')]).single;
expect(next.condition, isNot(notices[2].condition));
});
test('a dismissal hides one condition until that condition changes', () {
final dismissals = ChatNoticeDismissals();
final first = chatNotices(liveError: 'first refusal').single;
expect(dismissals.visible([first]), [first]);
dismissals.dismiss(first);
expect(dismissals.visible([first]), isEmpty);
// The same recorded condition stays hidden.
expect(
dismissals.visible(chatNotices(liveError: 'first refusal')),
isEmpty,
);
// A different recorded reason is a new notice.
final second = dismissals
.visible(chatNotices(liveError: 'second refusal'))
.single;
expect(second.text, 'Live updates unavailable\nsecond refusal');
// A condition that disappears and comes back is recorded afresh.
expect(dismissals.visible(const []), isEmpty);
expect(
dismissals.visible(chatNotices(liveError: 'first refusal')).single.text,
'Live updates unavailable\nfirst refusal',
);
});
test('a reconnect forgets every earlier dismissal', () {
final dismissals = ChatNoticeDismissals();
final notice = chatNotices(historyError: 'Connection closed').single;
dismissals.dismiss(notice);
expect(dismissals.visible([notice]), isEmpty);
dismissals.clear();
expect(dismissals.visible([notice]), [notice]);
});
test('an unchanged transcript invents no notice', () {
expect(chatNotices(), isEmpty);
expect(chatNotices(messages: const []), isEmpty);
expect(
chatNotices(
messages: [
chatMessage(
'a1',
'assistant',
parts: [
{'type': 'text', 'text': 'Just an answer.'},
{'type': 'reasoning', 'text': 'Thinking.'},
{
'type': 'tool',
'state': {'status': 'completed'},
},
],
),
],
),
isEmpty,
);
// A compaction that already ended is recorded history, not a live notice.
expect(
chatNotices(
messages: [
chatMessage(
'a1',
'assistant',
parts: [
{
'type': 'compaction',
'kind': 'compaction',
'id': 'c1',
'status': 'completed',
},
],
),
],
),
isEmpty,
);
});
test('a later terminal record supersedes an earlier running compaction', () {
final messages = [
compactionTurn('a1', 'c1'),
compactionTurn('a2', 'c2', 'completed'),
];
expect(chatRunningCompaction(messages), isNull);
expect(chatNotices(messages: messages), isEmpty);
// The line itself is still the recorded activity: the compaction ended.
expect(transcriptRows(messages).single['state'], 'completed');
expect(
chatNoticeActivityCondition(transcriptRows(messages).single),
isNull,
);
});
test('each running compaction row carries its own notice condition', () {
final rows = transcriptRows([
compactionTurn('a1', 'c1'),
chatMessage('u1', 'user', parts: const []),
compactionTurn('a2', 'c2'),
]);
final conditions = [
for (final row in rows)
if (row['kind'] == 'activity') chatNoticeActivityCondition(row),
];
expect(conditions, ['compaction\nc1', 'compaction\nc2']);
// The transcript's newest running compaction is the one it reports.
expect(
chatNotices(
messages: [
compactionTurn('a1', 'c1'),
chatMessage('u1', 'user', parts: const []),
compactionTurn('a2', 'c2'),
],
).single.condition,
'compaction\nc2',
);
});
testWidgets('the live failure notice has a native dismiss that hides it', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.subscribe = () async => throw StateError('live watch refused');
gateway.history = (_) async => chatPage(const []);
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-live-error')), findsOneWidget);
expect(find.textContaining('live watch refused'), findsOneWidget);
await tester.tap(
find.byKey(const ValueKey<String>('chat-notice-dismiss-liveFailure')),
);
await tester.pump();
expect(find.byKey(const Key('chat-live-error')), findsNothing);
expect(find.textContaining('live watch refused'), findsNothing);
// A later transcript frame changes no condition, so it stays hidden.
gateway.emitEvent(chatTextFrame('An answer.'));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-live-error')), findsNothing);
});
testWidgets('a reconnect clears the stale notice and its dismissal', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.subscribe = () async => throw StateError('live watch refused');
gateway.history = (_) async => chatPage(const []);
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
await tester.tap(
find.byKey(const ValueKey<String>('chat-notice-dismiss-liveFailure')),
);
await tester.pump();
expect(find.byKey(const Key('chat-live-error')), findsNothing);
// The socket comes back and the watch is accepted: the recorded condition
// is gone, so the old notice cannot linger.
gateway.subscribe = () async => const {};
await tester.pumpWidget(
host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
syncToken: 1,
),
),
);
// The pre-gap notice is stale as soon as the socket is replaced.
await tester.pump();
expect(find.byKey(const Key('chat-live-error')), findsNothing);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-live-error')), findsNothing);
// A failure recorded after the reconnect is shown again: the old dismissal
// did not survive the gap, even though the words repeat.
gateway.subscribe = () async => throw StateError('live watch refused');
await tester.pumpWidget(
host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
syncToken: 2,
),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-live-error')), findsOneWidget);
expect(find.textContaining('live watch refused'), findsOneWidget);
});
testWidgets('a reconnect hides a stale running compaction until fresh state', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([runningCompaction('c1')]);
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
expect(find.text('Compacting context…'), findsOneWidget);
await tester.tap(find.byKey(compactionDismissKey));
await tester.pump();
expect(find.text('Compacting context…'), findsNothing);
// The socket is replaced and the repair's history read has not answered:
// the pre-gap running state is stale, and the earlier dismissal does not
// make it visible again.
gateway.history = null;
await tester.pumpWidget(
host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
syncToken: 1,
),
),
);
await tester.pump();
expect(find.text('Compacting context…'), findsNothing);
expect(find.byKey(compactionDismissKey), findsNothing);
// The repair reports the same compaction still running: the fresh record
// shows again with its dismiss control; the gap cleared the old dismissal.
gateway.historyRequests.last.complete(chatPage([runningCompaction('c1')]));
await tester.pumpAndSettle();
expect(find.text('Compacting context…'), findsOneWidget);
expect(find.byKey(compactionDismissKey), findsOneWidget);
});
testWidgets('a refused repair keeps the pre-gap compaction hidden', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([runningCompaction('c1')]);
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(compactionDismissKey));
await tester.pump();
expect(find.text('Compacting context…'), findsNothing);
gateway.history = (_) async => throw StateError('history refused');
await tester.pumpWidget(
host(
ChatThreadView(
gateway: gateway,
sessionId: 's',
onBack: () {},
syncToken: 1,
),
),
);
await tester.pumpAndSettle();
// The refused repair is a fresh failure notice; the held transcript's
// running compaction is not painted again as if it were current.
expect(find.byKey(const Key('chat-history-error')), findsOneWidget);
expect(find.textContaining('history refused'), findsOneWidget);
expect(find.text('Compacting context…'), findsNothing);
expect(find.byKey(compactionDismissKey), findsNothing);
});
testWidgets('dismissing one running compaction leaves the other line alone', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([
compactionTurn('a1', 'c1'),
chatMessage('u1', 'user', parts: const []),
compactionTurn('a2', 'c2'),
]);
await tester.pumpWidget(
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
);
await tester.pumpAndSettle();
expect(find.text('Compacting context…'), findsNWidgets(2));
// Only the transcript's newest running compaction is the reported notice.
expect(find.byKey(compactionDismissKey), findsOneWidget);
await tester.tap(find.byKey(compactionDismissKey));
await tester.pump();
// c2's line is hidden; the separately recorded c1 line keeps its words.
expect(find.text('Compacting context…'), findsOneWidget);
expect(find.byKey(compactionDismissKey), findsNothing);
});
testWidgets('a running compaction notice is native, dismissed and exact', (
tester,
) async {
final gateway = FakeChatGateway();
addTearDown(gateway.close);
gateway.history = (_) async => chatPage([runningCompaction('c1')]);
await tester.pumpWidget(
cupertinoHost(
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
),
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-compaction-notice')), findsNothing);
expect(find.text('Compacting context…'), findsOneWidget);
expect(find.byType(Material), findsNothing);
expect(find.bySemanticsLabel('Dismiss compaction notice'), findsOneWidget);
await tester.tap(
find.byKey(const ValueKey<String>('chat-notice-dismiss-compaction')),
);
await tester.pump();
expect(find.text('Compacting context…'), findsNothing);
});
}