merge(app): bring main into chat-cmd-session; /new /close /rename tests

This commit is contained in:
2026-10-22 22:29:33 +01:00
58 changed files with 4326 additions and 159 deletions
@@ -152,7 +152,7 @@ void main() {
(tester) async {
final gateway = _ScreenGateway()
..sessions = [
{'id': 's', 'title': 'Wire chat', 'backend': 'claude'},
{'id': 's', 'title': 'Wire chat', 'backend': 'other'},
];
await _pumpScreen(tester, gateway, initialSessionId: 's');
@@ -195,41 +195,111 @@ void main() {
expect(palette(), findsNothing);
expect(gateway.promptBodies, isEmpty);
// A second Enter re-reports the command instead of sending it.
// A second Enter runs the command: its confirm, never a prompt.
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close');
expect(
find.text(
'“/close” is not available yet, so nothing was sent to the agent.',
),
findsOneWidget,
);
// The primary control and the explicit send shortcut take the same path.
await tester.tap(find.byKey(const Key('chat-composer-primary')));
expect(fieldText(tester), '');
expect(find.text('Close chat?'), findsOneWidget);
await tester.tap(find.text('Cancel'));
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close');
expect(gateway.calls.where((call) => call.type == 'chat_close'), isEmpty);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close');
// An argument does not make the slash draft sendable either.
// An argument is validated locally, then consumed too.
await tester.enterText(field, '/close now');
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close now');
expect(fieldText(tester), '');
expect(find.text('/close does not take an argument.'), findsOneWidget);
});
testWidgets('/help consumes the draft and lists commands in recorded order', (
tester,
) async {
await pumpComposer(tester);
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'/help',
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(fieldText(tester), '');
expect(gateway.promptBodies, isEmpty);
expect(
find.text(
'/new — Start a fresh session in this directory\n'
'/close — Finish this session for every client, keeping its history\n'
'/rename <title> — Rename this session on this device\n'
'/stop — Stop the turn that is running\n'
'/help — List every command',
),
findsOneWidget,
);
expect(find.text('Command'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-command-dismiss')));
await tester.pump();
expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing);
});
testWidgets('/stop without a running turn and extra arguments fail locally', (
tester,
) async {
await pumpComposer(tester);
final field = find.byKey(const Key('chat-composer-field'));
await tester.enterText(field, '/stop');
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(fieldText(tester), '');
expect(gateway.interruptBodies, isEmpty);
expect(find.text('Command failed'), findsOneWidget);
expect(find.text('No turn is running in this chat.'), findsOneWidget);
await tester.enterText(field, '/help x');
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(fieldText(tester), '');
expect(find.text('/help does not take an argument.'), findsOneWidget);
expect(gateway.promptBodies, isEmpty);
});
testWidgets('/stop interrupts the current session through the composer', (
tester,
) async {
await pumpComposer(tester);
gateway.emitEvent({
'session_id': 's',
'kind': 'step',
'step': {'status': 'started', 'turn_id': 'turn', 'intermediate': false},
});
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'/stop',
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(gateway.interruptBodies, [
<String, dynamic>{'session_id': 's'},
]);
expect(fieldText(tester), '');
expect(gateway.promptBodies, isEmpty);
});
testWidgets('a command picked from the palette is never sent either', (
@@ -244,17 +314,15 @@ void main() {
await tester.pumpAndSettle();
expect(fieldText(tester), '/help');
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/help');
expect(
find.text(
'“/help” is not available yet, so nothing was sent to the agent.',
),
findsOneWidget,
);
expect(fieldText(tester), '');
expect(find.text('Command'), findsOneWidget);
expect(find.textContaining('/help — List every command'), findsOneWidget);
});
testWidgets('a row completes the command and never prompts', (tester) async {
@@ -0,0 +1,164 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/core/transport/connection_state.dart';
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'chat_composer_bar_fixtures.dart';
/// Answers the session commands' create, close and rename requests.
class _SessionGateway extends ComposerBarGateway {
String? refuse;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
if (type == 'chat_create' ||
type == 'chat_close' ||
type == 'chat_rename') {
calls.add((type: type, fields: fields));
if (refuse != null) throw ProtocolError(refuse!);
if (type == 'chat_create') {
return {
'chat_session': {'id': 'n1', 'title': 'New chat', 'directory': '/w'},
};
}
return <String, dynamic>{};
}
return super.call(type, fields);
}
List<JsonMap> sent(String type) => [
for (final call in calls)
if (call.type == type) call.fields,
];
}
void main() {
late _SessionGateway gateway;
JsonMap? created;
String? createdNotice;
String? renamed;
var closed = 0;
setUp(() {
gateway = _SessionGateway();
created = null;
createdNotice = null;
renamed = null;
closed = 0;
});
Future<void> pump(WidgetTester tester, {String directory = '/w'}) async {
await tester.pumpWidget(
materialHost(
ChatThreadView(
gateway: gateway,
sessionId: 's',
title: 'Old',
session: {'id': 's', 'title': 'Old', 'directory': directory},
onBack: () {},
drafts: MemoryChatDraftStore(),
newOperationId: () => 'op-1',
onCreatedSession: (session, notice) {
created = session;
createdNotice = notice;
},
onSessionRenamed: (title) => renamed = title,
onSessionClosed: () => closed++,
),
),
);
await tester.pumpAndSettle();
}
Future<void> run(WidgetTester tester, String draft) async {
await tester.enterText(find.byKey(const Key('chat-composer-field')), draft);
await tester.pump();
// The first Enter may complete the palette row; the second runs it.
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
if (fieldText(tester).isNotEmpty) {
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
}
await tester.pumpAndSettle();
}
testWidgets('/new creates in this directory and hands the row over', (
tester,
) async {
await pump(tester);
await run(tester, '/new');
final body = gateway.sent('chat_create').single['chat_create'] as JsonMap;
expect(body['title'], 'New chat');
expect(body['directory'], '/w');
expect(body['operation_id'], 'op-1');
expect(created?['id'], 'n1');
expect(createdNotice, 'Started a new session “New chat” in /w.');
expect(gateway.promptBodies, isEmpty);
});
testWidgets('/new without a directory sends nothing and reports it', (
tester,
) async {
await pump(tester, directory: '');
await run(tester, '/new');
expect(gateway.sent('chat_create'), isEmpty);
expect(
find.text(
"Can't start a new session here: the server reported no directory "
'for this chat.',
),
findsOneWidget,
);
});
testWidgets('/rename sends the trimmed title and reports it', (tester) async {
await pump(tester);
await run(tester, '/rename Fresh title ');
final body = gateway.sent('chat_rename').single['chat_rename'] as JsonMap;
expect(body, {'session_id': 's', 'title': 'Fresh title'});
expect(renamed, 'Fresh title');
expect(find.text('Renamed this session to “Fresh title”.'), findsOneWidget);
});
testWidgets('/rename without a title reports usage and sends nothing', (
tester,
) async {
await pump(tester);
await run(tester, '/rename');
expect(gateway.sent('chat_rename'), isEmpty);
expect(
find.text(
'Usage: /rename <title> — renames this session on this device.',
),
findsOneWidget,
);
});
testWidgets('/close confirms, closes and a refusal keeps the reason', (
tester,
) async {
await pump(tester);
await run(tester, '/close');
await tester.tap(find.text('Close chat'));
await tester.pumpAndSettle();
expect(gateway.sent('chat_close').single['chat_query'], {
'session_id': 's',
});
expect(closed, 1);
gateway.refuse = 'chat is locked';
await run(tester, '/close');
await tester.tap(find.text('Close chat'));
await tester.pumpAndSettle();
expect(closed, 1);
expect(find.text('chat is locked'), findsOneWidget);
});
}
@@ -0,0 +1,340 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
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_composer_bar.dart';
import 'package:pipelines_app/features/chat/conversation/chat_composer_controller.dart';
import 'package:pipelines_app/features/chat/conversation/chat_draft_store.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_draft.dart';
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
import 'chat_composer_bar_fixtures.dart';
/// A host paste source driven by the test. [paste] mirrors the web listener:
/// it asks [canAccept] before it hands files to the composer.
class _FakePaste {
ChatPasteFiles? onFiles;
ChatPasteCanAccept? canAccept;
ChatPasteFailed? onError;
int listens = 0;
int stops = 0;
ChatClipboardPaste get source => ChatClipboardPaste(
subscribe: (onFiles, canAccept, onError) {
listens += 1;
this.onFiles = onFiles;
this.canAccept = canAccept;
this.onError = onError;
return () {
stops += 1;
this.onFiles = null;
this.canAccept = null;
this.onError = null;
};
},
);
void paste(List<ChatPickedFile> files) {
final handler = onFiles;
if (handler == null) return;
if (!(canAccept?.call() ?? false)) return;
handler(files);
}
}
ChatPickedFile _file(
String name,
List<int> bytes, {
String mime = 'text/plain',
}) => ChatPickedFile(name: name, mime: mime, bytes: Uint8List.fromList(bytes));
Finder get _field => find.byKey(const Key('chat-composer-field'));
Finder get _chips => find.byKey(const Key('chat-composer-attachments'));
void main() {
late ComposerBarGateway gateway;
late ChatComposerController composer;
late List<ChatComposerController> controllers;
setUp(() {
gateway = ComposerBarGateway();
controllers = [];
});
tearDown(() {
for (final controller in controllers) {
controller.dispose();
}
gateway.close();
});
/// Rebuilds the same composer with a different paste source, so a test can
/// tell a replaced listener from a disposed one.
Future<void> pumpWith(
WidgetTester tester, {
ChatClipboardPaste? paste,
}) async {
await tester.pumpWidget(
materialHost(
ChatComposerBar(composer: composer, running: false, paste: paste),
),
);
await tester.pump();
}
Future<void> pumpBar(
WidgetTester tester, {
ChatClipboardPaste? paste,
ChatDraftStore? drafts,
}) async {
composer = ChatComposerController(
gateway,
's',
drafts: drafts ?? MemoryChatDraftStore(),
newOperationId: () => 'op/1',
);
controllers.add(composer);
await pumpWith(tester, paste: paste);
}
Future<void> focusField(WidgetTester tester) async {
await tester.showKeyboard(_field);
await tester.pump();
}
testWidgets('a pasted file makes the same chip as a picked file', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
fake.paste([
_file('shot.png', [1, 2, 3], mime: 'image/png'),
]);
await tester.pump();
expect(_chips, findsOneWidget);
expect(find.text('shot.png'), findsOneWidget);
expect(fake.listens, 1);
expect(fake.stops, 0);
});
testWidgets('pasting several files keeps clipboard order', (tester) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
fake.paste([
_file('first.png', [1], mime: 'image/png'),
_file('second.txt', [2]),
]);
await tester.pump();
expect(find.text('first.png'), findsOneWidget);
expect(find.text('second.txt'), findsOneWidget);
expect(
tester.getTopLeft(find.text('first.png')).dx,
lessThan(tester.getTopLeft(find.text('second.txt')).dx),
);
});
testWidgets('an over-limit paste shows the draft refusal and adds nothing', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
fake.paste([
_file(
'huge.bin',
List<int>.filled(ChatAttachmentDraft.maxTotalBytes + 1, 0),
mime: 'application/octet-stream',
),
]);
await tester.pump();
expect(
find.text(ChatAttachmentRefusal.totalTooLarge.message),
findsOneWidget,
);
expect(_chips, findsNothing);
expect(find.text('huge.bin'), findsNothing);
});
testWidgets('a refused paste keeps the files already in the draft', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
fake.paste([
_file('kept.txt', [1]),
_file(
'huge.bin',
List<int>.filled(ChatAttachmentDraft.maxTotalBytes, 0),
mime: 'application/octet-stream',
),
]);
await tester.pump();
expect(find.text('kept.txt'), findsOneWidget);
expect(find.text('huge.bin'), findsNothing);
expect(
find.text(ChatAttachmentRefusal.totalTooLarge.message),
findsOneWidget,
);
});
testWidgets('a pasted attachment sends its exact name, type and bytes', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
fake.paste([
_file('shot.png', [9, 8, 7], mime: 'image/png'),
]);
await tester.pump();
await tester.enterText(_field, 'look');
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
expect(gateway.promptBodies.single['attachments'], [
{
'name': 'shot.png',
'mime': 'image/png',
'data': base64Encode([9, 8, 7]),
},
]);
expect(_chips, findsNothing);
});
testWidgets('pasted text keeps its caret, selection and draft', (
tester,
) async {
final drafts = MemoryChatDraftStore();
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source, drafts: drafts);
await focusField(tester);
await tester.enterText(_field, 'hello world');
await tester.pumpAndSettle();
final editable = tester.widget<EditableText>(
find.descendant(of: _field, matching: find.byType(EditableText)),
);
editable.controller.selection = const TextSelection.collapsed(offset: 5);
// The platform's text update for a paste between "hello" and " world".
tester.testTextInput.updateEditingValue(
const TextEditingValue(
text: 'hello there world',
selection: TextSelection.collapsed(offset: 11),
),
);
await tester.pumpAndSettle();
expect(editable.controller.text, 'hello there world');
expect(
editable.controller.selection,
const TextSelection.collapsed(offset: 11),
);
expect(composer.draft, 'hello there world');
expect(drafts.entries, {'s': 'hello there world'});
});
testWidgets('paste is only taken while this composer owns the field', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
expect(fake.canAccept!(), isFalse);
await focusField(tester);
expect(fake.canAccept!(), isTrue);
tester.binding.focusManager.primaryFocus?.unfocus();
await tester.pump();
expect(fake.canAccept!(), isFalse);
});
testWidgets('a paste during an in-flight prompt adds nothing', (
tester,
) async {
final reply = Completer<JsonMap>();
gateway.prompt = (_, _) => reply.future;
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
await tester.enterText(_field, 'hello');
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pump();
expect(fake.canAccept!(), isFalse);
fake.paste([
_file('late.png', [1], mime: 'image/png'),
]);
await tester.pump();
expect(_chips, findsNothing);
reply.complete(const {});
await tester.pumpAndSettle();
});
testWidgets('an unreadable clipboard file is reported', (tester) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
fake.onError!(StateError('clipboard read failed'));
await tester.pump();
expect(find.textContaining('clipboard read failed'), findsOneWidget);
});
testWidgets('a partial clipboard read shows its files and the failure', (
tester,
) async {
final fake = _FakePaste();
await pumpBar(tester, paste: fake.source);
await focusField(tester);
// The web source delivers readable files before a partial read's failure.
fake.paste([
_file('readable.png', [1], mime: 'image/png'),
]);
fake.onError!(StateError('second file failed'));
await tester.pump();
expect(find.text('readable.png'), findsOneWidget);
expect(find.textContaining('second file failed'), findsOneWidget);
});
testWidgets(
'a replaced source stops listening and dispose stops the new one',
(tester) async {
final first = _FakePaste();
await pumpBar(tester, paste: first.source);
expect(first.listens, 1);
final second = _FakePaste();
await pumpWith(tester, paste: second.source);
expect(first.stops, 1);
expect(second.listens, 1);
await tester.pumpWidget(const SizedBox());
expect(second.stops, 1);
},
);
}
@@ -380,7 +380,7 @@ void main() {
await _openThread(
tester,
gateway,
session: _withSession({'backend': 'claude'}),
session: _withSession({'backend': 'other'}),
);
expect(find.byKey(const Key('chat-model')), findsNothing);
@@ -57,6 +57,7 @@ void main() {
'Dismiss connection notice',
'Dismiss live updates notice',
'Dismiss compaction notice',
'Dismiss command notice',
],
);
// A later compaction has its own recorded condition.
@@ -110,7 +110,7 @@ void main() {
gateway: gateway,
sessionId: 's',
title: _title,
session: {..._session, 'backend': 'claude'},
session: {..._session, 'backend': 'other'},
onBack: () {},
),
cupertino: false,
@@ -0,0 +1,47 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/domain/chat_clipboard_paste.dart';
void main() {
test('a nameless clipboard file takes the web client name for its type', () {
expect(chatPastedAttachmentName('image/png', 1), 'pasted-1.png');
expect(chatPastedAttachmentName('image/jpeg', 2), 'pasted-2.jpg');
expect(
chatPastedAttachmentName('application/octet-stream', 3),
'pasted-3.octetstream',
);
expect(chatPastedAttachmentName('', 4), 'pasted-4.bin');
});
test('the unsupported host keeps text paste and reports the gap', () {
const paste = ChatClipboardPaste.unsupported('no files here');
expect(paste.isSupported, isFalse);
expect(
() =>
paste.listen(onFiles: (_) {}, canAccept: () => true, onError: (_) {}),
throwsUnsupportedError,
);
});
test('a supported host subscribes once and returns its unsubscribe', () {
var listens = 0;
var stops = 0;
final paste = ChatClipboardPaste(
subscribe: (onFiles, canAccept, onError) {
listens += 1;
return () => stops += 1;
},
);
final stop = paste.listen(
onFiles: (_) {},
canAccept: () => true,
onError: (_) {},
);
expect(paste.isSupported, isTrue);
expect(listens, 1);
stop();
expect(stops, 1);
});
}
@@ -0,0 +1,87 @@
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/ui/sessions/chat_sessions_screen.dart';
import 'chat_test_harness.dart';
import 'fake_chat_gateway.dart';
Future<void> _pump(
WidgetTester tester,
FakeChatGateway gateway, {
String? active,
}) async {
await tester.pumpWidget(
MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: ChatSessionsScreen(
gateway: gateway,
onOpen: (_) {},
localStore: _store,
activeSessionId: active,
),
),
);
await tester.pumpAndSettle();
}
late MemoryChatSessionLocalStore _store;
JsonMap _event(String id, {String kind = 'text'}) => {
'type': 'chat_event',
'chat_event': {'session_id': id, 'kind': kind},
};
void main() {
setUp(() => _store = MemoryChatSessionLocalStore());
FakeChatGateway gateway() => FakeChatGateway()
..sessionsReply = {
'chat_sessions': [
{'id': 's1', 'title': 'First', 'directory': '/w', 'updated_at': 1000},
],
};
testWidgets('a pushed event marks a background chat New without a refresh', (
tester,
) async {
final fake = gateway();
await _pump(tester, fake);
expect(find.text('New'), findsNothing);
fake.pushed.add(_event('s1'));
await tester.pumpAndSettle();
expect(find.text('New'), findsOneWidget);
expect(fake.count('chat_sessions'), 1);
await tester.tap(find.byKey(const Key('chat-row-s1')));
await tester.pumpAndSettle();
expect(find.text('New'), findsNothing);
});
testWidgets('usage frames and the open chat never raise New', (tester) async {
final fake = gateway();
await _pump(tester, fake, active: 's1');
fake.pushed.add(_event('s1'));
fake.pushed.add(_event('s1', kind: 'usage'));
await tester.pumpAndSettle();
expect(find.text('New'), findsNothing);
});
testWidgets('leaving the conversation lets later activity mark it New', (
tester,
) async {
final fake = gateway();
await _pump(tester, fake);
await tester.tap(find.byKey(const Key('chat-row-s1')));
await _pump(tester, fake, active: 's1');
await _pump(tester, fake);
fake.pushed.add({
'type': 'chat_permission',
'chat_permission': {'session_id': 's1'},
});
await tester.pumpAndSettle();
expect(find.text('New'), findsOneWidget);
});
}
@@ -19,8 +19,11 @@ class FakeChatGateway implements ApiGateway {
Future<JsonMap> Function()? directoriesHandler;
Future<JsonMap> Function()? backendsHandler;
/// Server-pushed frames; tests add `chat_event` or `chat_permission` here.
final pushed = StreamController<JsonMap>.broadcast();
@override
Stream<JsonMap> get frames => const Stream.empty();
Stream<JsonMap> get frames => pushed.stream;
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) {