302 lines
9.2 KiB
Dart
302 lines
9.2 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_question_card_model.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_question_group.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
|
|
|
import 'chat_thread_fixtures.dart';
|
|
|
|
JsonMap _turn(String id) => {
|
|
'kind': 'assistant',
|
|
'key': id,
|
|
'messageIds': [id],
|
|
'parts': const [],
|
|
};
|
|
|
|
JsonMap _card(
|
|
String id, {
|
|
String? raisedIn,
|
|
bool resolved = false,
|
|
bool cancelled = false,
|
|
String? error,
|
|
}) => {
|
|
'kind': 'question',
|
|
'key': 'chat-question-$id',
|
|
'form': {
|
|
'id': id,
|
|
'resolved': resolved,
|
|
'cancelled': cancelled,
|
|
chatQuestionRaisedIn: ?raisedIn,
|
|
},
|
|
'error': error,
|
|
};
|
|
|
|
JsonMap _form({String id = 'form-1'}) => {
|
|
'id': id,
|
|
'session_id': 's',
|
|
'kind': 'question',
|
|
'title': 'Database',
|
|
'fields': [
|
|
{
|
|
'key': 'q0',
|
|
'title': 'Which database?',
|
|
'type': 'string',
|
|
'required': true,
|
|
'options': [
|
|
{'value': 'pg', 'label': 'Postgres'},
|
|
{'value': 'lite', 'label': 'SQLite'},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
const _chip = 'Which database? · Postgres';
|
|
|
|
/// A chat whose agent turn `a1` is the one the question arrives during.
|
|
Future<FakeChatGateway> _open(
|
|
WidgetTester tester,
|
|
Size size, {
|
|
bool cupertino = false,
|
|
}) async {
|
|
await tester.binding.setSurfaceSize(size);
|
|
addTearDown(() => tester.binding.setSurfaceSize(null));
|
|
final gateway = FakeChatGateway()
|
|
..history = (_) async => chatPage([
|
|
chatMessage(
|
|
'u1',
|
|
'user',
|
|
parts: [
|
|
{'type': 'text', 'text': 'Ship the release.'},
|
|
],
|
|
),
|
|
chatMessage(
|
|
'a1',
|
|
'assistant',
|
|
parts: [
|
|
{'type': 'text', 'text': 'Which database first?'},
|
|
],
|
|
),
|
|
]);
|
|
addTearDown(gateway.close);
|
|
final view = ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {});
|
|
await tester.pumpWidget(
|
|
cupertino
|
|
? CupertinoApp(home: CupertinoPageScaffold(child: view))
|
|
: MaterialApp(home: Scaffold(body: view)),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
gateway.emitEvent({'session_id': 's', 'kind': 'form', 'form': _form()});
|
|
await tester.pumpAndSettle();
|
|
expect(find.text(chatQuestionWaiting), findsOneWidget);
|
|
return gateway;
|
|
}
|
|
|
|
Future<void> _answerPostgres(WidgetTester tester) async {
|
|
await tester.tap(find.text('Postgres'));
|
|
await tester.pump();
|
|
await tester.tap(find.text(chatQuestionSubmit));
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
void _expectCollapsed() {
|
|
expect(find.text(chatQuestionWaiting), findsNothing);
|
|
expect(find.text(chatQuestionSubmit), findsNothing);
|
|
expect(find.text(chatQuestionAnswered), findsNothing);
|
|
expect(find.text(_chip), findsOneWidget);
|
|
// The chip sits in its turn's footer, under the turn that asked.
|
|
expect(
|
|
find.descendant(
|
|
of: find.byKey(const Key('chat-message-footer')),
|
|
matching: find.text(_chip),
|
|
),
|
|
findsOneWidget,
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
test('settled questions join the turn they were raised during', () {
|
|
final rows = groupSettledQuestions([
|
|
_turn('m1'),
|
|
_turn('m2'),
|
|
_card('a', raisedIn: 'm1', resolved: true),
|
|
_card('b', raisedIn: 'm2', cancelled: true),
|
|
]);
|
|
expect(rows.map((r) => r['kind']), ['assistant', 'assistant']);
|
|
expect(chatRowQuestions(rows[0]).map((f) => f['id']), ['a']);
|
|
expect(chatRowQuestions(rows[1]).map((f) => f['id']), ['b']);
|
|
});
|
|
|
|
test('questions naming no turn share one row; open ones stay cards', () {
|
|
final rows = groupSettledQuestions([
|
|
_turn('m1'),
|
|
_card('loose-1', resolved: true),
|
|
_card('pending', raisedIn: 'm1'),
|
|
_card('refused', raisedIn: 'm1', error: 'no longer awaiting'),
|
|
_card('loose-2', raisedIn: 'paged-out', resolved: true),
|
|
]);
|
|
expect(rows.map((r) => r['kind']), [
|
|
'assistant',
|
|
'questions',
|
|
'question',
|
|
'question',
|
|
]);
|
|
expect(rows[0].containsKey('questions'), isFalse);
|
|
expect(chatRowQuestions(rows[1]).map((f) => f['id']), [
|
|
'loose-1',
|
|
'loose-2',
|
|
]);
|
|
});
|
|
|
|
test('the summary names the question and its outcome', () {
|
|
final form = _form();
|
|
expect(
|
|
chatQuestionSummary({
|
|
...form,
|
|
'resolved': true,
|
|
'answers': {'q0': 'pg'},
|
|
}),
|
|
_chip,
|
|
);
|
|
expect(
|
|
chatQuestionSummary({
|
|
...form,
|
|
'resolved': true,
|
|
'answers': {'q0': 'pg', 'extra': 'x'},
|
|
}),
|
|
'$_chip · +1 more',
|
|
);
|
|
expect(
|
|
chatQuestionSummary({...form, 'cancelled': true}),
|
|
'Which database? · Expired',
|
|
);
|
|
expect(
|
|
chatQuestionSummary({...form, 'resolved': true}),
|
|
'Which database? · Closed',
|
|
);
|
|
});
|
|
|
|
for (final (name, size) in [
|
|
('narrow', const Size(390, 844)),
|
|
('wide', const Size(1280, 900)),
|
|
]) {
|
|
testWidgets('$name: an acknowledged answer collapses into its turn at once '
|
|
'(Claude Code: the ack is the only signal)', (tester) async {
|
|
final gateway = await _open(tester, size);
|
|
final held = Completer<JsonMap>();
|
|
gateway.answer = (_) => held.future;
|
|
await tester.tap(find.text('Postgres'));
|
|
await tester.pump();
|
|
await tester.tap(find.text(chatQuestionSubmit));
|
|
await tester.pump();
|
|
// Still the card while the reply is in flight.
|
|
expect(find.text(chatQuestionWaiting), findsOneWidget);
|
|
held.complete(const {'type': 'chat_answer'});
|
|
await tester.pumpAndSettle();
|
|
_expectCollapsed();
|
|
|
|
// A late replay of the pending frame cannot reopen it.
|
|
gateway.emitEvent({'session_id': 's', 'kind': 'form', 'form': _form()});
|
|
await tester.pumpAndSettle();
|
|
_expectCollapsed();
|
|
|
|
await tester.tap(find.text(_chip));
|
|
await tester.pumpAndSettle();
|
|
expect(find.byKey(const Key('chat-question-sheet')), findsOneWidget);
|
|
expect(find.text('Which database?'), findsOneWidget);
|
|
expect(find.text('Postgres'), findsOneWidget);
|
|
expect(find.text(chatQuestionAnswered), findsOneWidget);
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
|
|
testWidgets('$name: a refused answer keeps the full card with the reason', (
|
|
tester,
|
|
) async {
|
|
final gateway = await _open(tester, size);
|
|
gateway.answer = (_) async =>
|
|
throw StateError('the question is no longer awaiting an answer');
|
|
await _answerPostgres(tester);
|
|
expect(find.textContaining('no longer awaiting'), findsOneWidget);
|
|
expect(find.text(chatQuestionWaiting), findsOneWidget);
|
|
expect(find.text(chatQuestionSubmit), findsOneWidget);
|
|
expect(find.text(_chip), findsNothing);
|
|
});
|
|
}
|
|
|
|
testWidgets('OpenCode/Codex: the server resolution collapses a question '
|
|
'answered elsewhere', (tester) async {
|
|
final gateway = await _open(tester, const Size(390, 844));
|
|
gateway.emitEvent({
|
|
'session_id': 's',
|
|
'kind': 'form',
|
|
'form': {'id': 'form-1', 'session_id': 's', 'resolved': true},
|
|
});
|
|
await tester.pumpAndSettle();
|
|
expect(find.text(chatQuestionWaiting), findsNothing);
|
|
expect(find.text('Which database? · Closed'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a cancelled question collapses as expired', (tester) async {
|
|
final gateway = await _open(tester, const Size(390, 844));
|
|
gateway.emitEvent({
|
|
'session_id': 's',
|
|
'kind': 'form',
|
|
'form': {'id': 'form-1', 'session_id': 's', 'cancelled': true},
|
|
});
|
|
await tester.pumpAndSettle();
|
|
expect(find.text(chatQuestionWaiting), findsNothing);
|
|
expect(find.text('Which database? · Expired'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('OpenCode: ack then form.replied leaves one collapsed row', (
|
|
tester,
|
|
) async {
|
|
final gateway = await _open(tester, const Size(390, 844));
|
|
gateway.answer = (_) async {
|
|
gateway.emitEvent({
|
|
'session_id': 's',
|
|
'kind': 'form',
|
|
'form': {'id': 'form-1', 'session_id': 's', 'resolved': true},
|
|
});
|
|
return const {'type': 'chat_answer'};
|
|
};
|
|
await _answerPostgres(tester);
|
|
_expectCollapsed();
|
|
});
|
|
|
|
testWidgets('Cupertino: the answered question collapses to the native chip', (
|
|
tester,
|
|
) async {
|
|
final gateway = await _open(tester, const Size(390, 844), cupertino: true);
|
|
gateway.answer = (_) async => const {'type': 'chat_answer'};
|
|
await _answerPostgres(tester);
|
|
_expectCollapsed();
|
|
expect(tester.takeException(), isNull);
|
|
});
|
|
|
|
testWidgets('after a reload the answered question stays gone', (
|
|
tester,
|
|
) async {
|
|
final gateway = await _open(tester, const Size(390, 844));
|
|
gateway.answer = (_) async => const {'type': 'chat_answer'};
|
|
await _answerPostgres(tester);
|
|
_expectCollapsed();
|
|
// A fresh view over the same server: the subscribe replays only pending
|
|
// forms, and this one is no longer pending.
|
|
await tester.pumpWidget(const SizedBox());
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Which database first?'), findsOneWidget);
|
|
expect(find.text(chatQuestionWaiting), findsNothing);
|
|
});
|
|
}
|