369 lines
11 KiB
Dart
369 lines
11 KiB
Dart
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_thread_view.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),
|
|
);
|
|
|
|
void main() {
|
|
testWidgets('live-subscription refusal is separate from readable history', (
|
|
tester,
|
|
) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.subscribe = () async => throw StateError('live watch refused');
|
|
gateway.history = (_) async => chatPage([
|
|
chatMessage(
|
|
'u1',
|
|
'user',
|
|
parts: [
|
|
{'type': 'text', 'text': 'History still renders.'},
|
|
],
|
|
),
|
|
]);
|
|
|
|
await tester.pumpWidget(
|
|
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('History still renders.'), findsOneWidget);
|
|
expect(find.byKey(const Key('chat-live-error')), findsOneWidget);
|
|
expect(find.textContaining('Live updates unavailable'), findsOneWidget);
|
|
expect(find.textContaining('live watch refused'), findsOneWidget);
|
|
expect(find.byKey(const Key('chat-history-error')), findsNothing);
|
|
|
|
// A later resync that attaches clears only the live notice.
|
|
gateway.subscribe = () async => const {};
|
|
gateway.history = (_) async => chatPage([
|
|
chatMessage(
|
|
'u1',
|
|
'user',
|
|
parts: [
|
|
{'type': 'text', 'text': 'History still renders.'},
|
|
],
|
|
),
|
|
]);
|
|
var attempts = 0;
|
|
for (
|
|
var i = 0;
|
|
i < 80 && find.byKey(const Key('chat-live-error')).evaluate().isNotEmpty;
|
|
i++
|
|
) {
|
|
if (attempts == 0) {
|
|
await tester.tap(find.text('Retry').last);
|
|
attempts++;
|
|
}
|
|
await tester.pump(const Duration(milliseconds: 5));
|
|
}
|
|
expect(find.byKey(const Key('chat-live-error')), findsNothing);
|
|
});
|
|
|
|
testWidgets('Load older passes the server cursor back and shows older text', (
|
|
tester,
|
|
) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => chatPage(
|
|
[
|
|
chatMessage(
|
|
'a2',
|
|
'assistant',
|
|
parts: [
|
|
{'type': 'text', 'text': 'Newest reply.'},
|
|
],
|
|
),
|
|
],
|
|
cursor: 'opaque/next?x=1',
|
|
hasMore: true,
|
|
);
|
|
|
|
await tester.pumpWidget(
|
|
host(ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {})),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Newest reply.'), findsOneWidget);
|
|
expect(find.text('Load older'), findsOneWidget);
|
|
|
|
final older = Completer<JsonMap>();
|
|
gateway.history = (_) => older.future;
|
|
await tester.tap(find.text('Load older'));
|
|
await tester.pump();
|
|
|
|
expect(gateway.calls.last.fields['chat_query'], {
|
|
'session_id': 's',
|
|
'limit': 200,
|
|
'cursor': 'opaque/next?x=1',
|
|
});
|
|
older.complete(
|
|
chatPage(
|
|
[
|
|
chatMessage(
|
|
'a1',
|
|
'assistant',
|
|
parts: [
|
|
{'type': 'text', 'text': 'Older stored reply.'},
|
|
],
|
|
),
|
|
],
|
|
cursor: '',
|
|
hasMore: false,
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Older stored reply.'), findsOneWidget);
|
|
expect(find.text('Newest reply.'), findsOneWidget);
|
|
expect(find.text('Load older'), findsNothing);
|
|
});
|
|
|
|
// Paging prepends messages, so an unkeyed transcript would reuse the element
|
|
// at a message's old index: the operator's open reasoning would move to the
|
|
// previous answer, or be dropped when `Load older` disappears.
|
|
for (final cupertino in [false, true]) {
|
|
testWidgets(
|
|
'an open reasoning block stays on its answer while has_more remains '
|
|
'($cupertino)',
|
|
(tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
var reads = 0;
|
|
gateway.history = (_) async => ++reads == 1
|
|
? pagedChatPage(
|
|
'a2',
|
|
'NEWEST reasoning body that belongs to the newest answer.',
|
|
cursor: 'opaque/older',
|
|
hasMore: true,
|
|
)
|
|
: pagedChatPage(
|
|
'a1',
|
|
'OLDER reasoning body that must not steal the newest disclosure.',
|
|
cursor: 'opaque/older-2',
|
|
hasMore: true,
|
|
);
|
|
|
|
await tester.pumpWidget(
|
|
(cupertino ? cupertinoHost : host)(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.tap(find.text('Reasoning'));
|
|
await tester.pumpAndSettle();
|
|
expect(
|
|
find.text('NEWEST reasoning body that belongs to the newest answer.'),
|
|
findsOneWidget,
|
|
);
|
|
|
|
await tester.tap(find.text('Load older'));
|
|
await tester.pumpAndSettle();
|
|
|
|
// Both answers are present; only the one the operator opened is open.
|
|
expect(find.text('Reasoning'), findsNWidgets(2));
|
|
expect(find.text('Load older'), findsOneWidget);
|
|
expect(
|
|
find.text('NEWEST reasoning body that belongs to the newest answer.'),
|
|
findsOneWidget,
|
|
);
|
|
expect(
|
|
find.text(
|
|
'OLDER reasoning body that must not steal the newest disclosure.',
|
|
),
|
|
findsNothing,
|
|
);
|
|
},
|
|
);
|
|
|
|
testWidgets(
|
|
'an open reasoning block survives the removal of Load older ($cupertino)',
|
|
(tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
var reads = 0;
|
|
gateway.history = (_) async => ++reads == 1
|
|
? pagedChatPage(
|
|
'a2',
|
|
'NEWEST reasoning body that belongs to the newest answer.',
|
|
cursor: 'opaque/older',
|
|
hasMore: true,
|
|
)
|
|
: pagedChatPage(
|
|
'a1',
|
|
'OLDER reasoning body that must not steal the newest disclosure.',
|
|
cursor: '',
|
|
hasMore: false,
|
|
);
|
|
|
|
await tester.pumpWidget(
|
|
(cupertino ? cupertinoHost : host)(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.tap(find.text('Reasoning'));
|
|
await tester.pumpAndSettle();
|
|
expect(
|
|
find.text('NEWEST reasoning body that belongs to the newest answer.'),
|
|
findsOneWidget,
|
|
);
|
|
|
|
await tester.tap(find.text('Load older'));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Reasoning'), findsNWidgets(2));
|
|
expect(find.text('Load older'), findsNothing);
|
|
expect(
|
|
find.text('NEWEST reasoning body that belongs to the newest answer.'),
|
|
findsOneWidget,
|
|
);
|
|
expect(
|
|
find.text(
|
|
'OLDER reasoning body that must not steal the newest disclosure.',
|
|
),
|
|
findsNothing,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
// The repository's own paged-history fixture pages consecutive assistant
|
|
// messages, so an older page can prepend into the one folded turn. Its first
|
|
// message id changes; the row must still be the same element and keep the
|
|
// operator's open disclosure.
|
|
for (final cupertino in [false, true]) {
|
|
for (final keepsControl in [true, false]) {
|
|
testWidgets(
|
|
'an open reasoning block in a folded assistant turn survives paging '
|
|
'(keeps Load older: $keepsControl, $cupertino)',
|
|
(tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
var reads = 0;
|
|
gateway.history = (_) async => ++reads == 1
|
|
? assistantFoldPage(
|
|
const ['msg_6', 'msg_4'],
|
|
const [
|
|
'NEWEST folded reasoning body.',
|
|
'FOURTH folded reasoning body.',
|
|
],
|
|
cursor: 'opaque/fold',
|
|
hasMore: true,
|
|
)
|
|
: assistantFoldPage(
|
|
const ['msg_2', 'msg_0'],
|
|
const [
|
|
'SECOND folded reasoning body.',
|
|
'OLDEST folded reasoning body.',
|
|
],
|
|
cursor: keepsControl ? 'opaque/fold-2' : '',
|
|
hasMore: keepsControl,
|
|
);
|
|
|
|
await tester.pumpWidget(
|
|
(cupertino ? cupertinoHost : host)(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
|
|
// Before paging the turn holds two messages, so two disclosures.
|
|
expect(find.text('Reasoning'), findsNWidgets(2));
|
|
await tester.tap(find.text('Reasoning').first);
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('NEWEST folded reasoning body.'), findsOneWidget);
|
|
|
|
await tester.tap(find.text('Load older'));
|
|
await tester.pumpAndSettle();
|
|
|
|
// All four assistant messages now fold into the same turn. The
|
|
// newest block stays open; no older block may open.
|
|
expect(find.text('Reasoning'), findsNWidgets(4));
|
|
expect(find.text('NEWEST folded reasoning body.'), findsOneWidget);
|
|
expect(find.text('FOURTH folded reasoning body.'), findsNothing);
|
|
expect(find.text('SECOND folded reasoning body.'), findsNothing);
|
|
expect(find.text('OLDEST folded reasoning body.'), findsNothing);
|
|
expect(
|
|
find.text('Load older'),
|
|
keepsControl ? findsOneWidget : findsNothing,
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The paged-history fixture shape: a page of consecutive assistant messages,
|
|
/// each with its own reasoning and text part.
|
|
JsonMap assistantFoldPage(
|
|
List<String> ids,
|
|
List<String> reasoning, {
|
|
required String cursor,
|
|
required bool hasMore,
|
|
}) => chatPage(
|
|
[
|
|
for (var index = 0; index < ids.length; index++)
|
|
chatMessage(
|
|
ids[index],
|
|
'assistant',
|
|
parts: [
|
|
{
|
|
'type': 'reasoning',
|
|
'id': 'r-${ids[index]}',
|
|
'text': reasoning[index],
|
|
},
|
|
{
|
|
'type': 'text',
|
|
'id': 't-${ids[index]}',
|
|
'text': 'Answer stored as ${ids[index]}.',
|
|
},
|
|
],
|
|
),
|
|
],
|
|
cursor: cursor,
|
|
hasMore: hasMore,
|
|
);
|
|
|
|
/// A newest page with one user prompt and one answer carrying [reasoning].
|
|
JsonMap pagedChatPage(
|
|
String answerId,
|
|
String reasoning, {
|
|
required String cursor,
|
|
required bool hasMore,
|
|
}) => chatPage(
|
|
[
|
|
chatMessage(
|
|
'u-$answerId',
|
|
'user',
|
|
parts: [
|
|
{'type': 'text', 'text': 'A question for $answerId.'},
|
|
],
|
|
),
|
|
chatMessage(
|
|
answerId,
|
|
'assistant',
|
|
parts: [
|
|
{'type': 'reasoning', 'text': reasoning},
|
|
{'type': 'text', 'text': 'The answer stored as $answerId.'},
|
|
],
|
|
),
|
|
],
|
|
cursor: cursor,
|
|
hasMore: hasMore,
|
|
);
|