merge(app): land chat-attach-send-ui micro-slice

This commit is contained in:
2026-10-22 12:30:43 +01:00
3 changed files with 416 additions and 23 deletions
@@ -0,0 +1,299 @@
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_picker.dart';
import 'chat_composer_bar_fixtures.dart';
import 'chat_composer_fixtures.dart';
/// A picker whose selections are supplied by the test, one call at a time.
class _FakePicker {
_FakePicker(this.results);
final List<List<ChatPickedFile>?> results;
ChatAttachmentPicker get picker => ChatAttachmentPicker(
select: () async => results.isEmpty ? null : results.removeAt(0),
);
}
ChatPickedFile _file(
String name,
List<int> bytes, {
String mime = 'text/plain',
}) => ChatPickedFile(name: name, mime: mime, bytes: Uint8List.fromList(bytes));
List<JsonMap> _wireParts(List<ChatPickedFile> files) => [
for (final file in files)
{'name': file.name, 'mime': file.mime, 'data': base64Encode(file.bytes)},
];
void main() {
group('ChatComposerController.send', () {
late FakeComposerGateway gateway;
late ChatComposerController controller;
var operationSeq = 0;
setUp(() {
operationSeq = 0;
gateway = FakeComposerGateway();
controller = ChatComposerController(
gateway,
's',
drafts: MemoryChatDraftStore(),
newOperationId: () => 'op/${operationSeq += 1}',
);
});
tearDown(() {
controller.dispose();
gateway.close();
});
test(
'carries the exact parts in draft order with the text and id',
() async {
final parts = _wireParts([
_file('screen.png', [0, 1, 2], mime: 'image/png'),
_file('notes.txt', utf8.encode('hello')),
]);
await controller.setDraft(' hello ');
final accepted = await controller.send(attachments: parts);
expect(accepted, isTrue);
expect(gateway.calls.single.fields, {
'chat_prompt': {
'session_id': 's',
'text': 'hello',
'operation_id': 'op/1',
'native_message_id': 'msg_op1',
'attachments': parts,
},
});
expect(controller.draft, '');
},
);
test('a refused prompt reuses its id only for matching parts', () async {
gateway.prompt = (_, _) async => throw StateError('response lost');
final first = _wireParts([
_file('notes.txt', [1]),
]);
final second = _wireParts([
_file('notes.txt', [2]),
]);
await controller.setDraft('hello');
await controller.send(attachments: first);
await controller.send(attachments: second);
await controller.send(
attachments: _wireParts([
_file('notes.txt', [2]),
]),
);
expect(gateway.promptBodies.map((body) => body['operation_id']), [
'op/1',
'op/2',
'op/2',
]);
expect(controller.draft, 'hello');
});
});
group('ChatComposerBar send', () {
late ComposerBarGateway gateway;
late ChatComposerController composer;
late List<ChatComposerController> controllers;
setUp(() {
gateway = ComposerBarGateway();
controllers = [];
});
tearDown(() {
for (final controller in controllers) {
controller.dispose();
}
gateway.close();
});
Future<void> pumpBar(
WidgetTester tester,
ChatAttachmentPicker picker,
) async {
composer = ChatComposerController(
gateway,
's',
drafts: MemoryChatDraftStore(),
newOperationId: () => 'op/1',
);
controllers.add(composer);
await tester.pumpWidget(
materialHost(
ChatComposerBar(composer: composer, running: false, picker: picker),
),
);
await tester.pump();
}
Future<void> pickFiles(WidgetTester tester) async {
await tester.tap(find.byKey(const Key('chat-composer-attach')));
await tester.pumpAndSettle();
}
Future<void> typeAndSend(WidgetTester tester, String text) async {
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
text,
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
}
testWidgets(
'Send carries the chips in order and clears them on acceptance',
(tester) async {
final files = [
_file('screen.png', [0, 1, 2], mime: 'image/png'),
_file('notes.txt', utf8.encode('hello')),
];
await pumpBar(tester, _FakePicker([files]).picker);
await pickFiles(tester);
expect(find.text('screen.png'), findsOneWidget);
expect(find.text('notes.txt'), findsOneWidget);
await typeAndSend(tester, ' hello ');
final body = gateway.promptBodies.single;
expect(body['text'], 'hello');
expect(body['operation_id'], 'op/1');
expect(body['attachments'], _wireParts(files));
expect(
find.byKey(const Key('chat-composer-attachments')),
findsNothing,
);
expect(find.text('screen.png'), findsNothing);
expect(fieldText(tester), '');
},
);
testWidgets('removing one duplicate name sends the other file', (
tester,
) async {
// Same name, type and size, so only position tells the two picks apart.
final files = [
_file('notes.txt', utf8.encode('aaaaa')),
_file('notes.txt', utf8.encode('bbbbb')),
];
await pumpBar(tester, _FakePicker([files]).picker);
await pickFiles(tester);
expect(find.text('notes.txt'), findsNWidgets(2));
await tester.tap(
find.descendant(
of: find.byKey(const ValueKey<String>('chat-composer-attachment-1')),
matching: find.byTooltip('Remove notes.txt'),
),
);
await tester.pump();
expect(find.text('notes.txt'), findsOneWidget);
await typeAndSend(tester, 'hello');
expect(
gateway.promptBodies.single['attachments'],
_wireParts([files[0]]),
);
expect(find.byKey(const Key('chat-composer-attachments')), findsNothing);
});
testWidgets('a refusal restores the exact draft and keeps the chips', (
tester,
) async {
gateway.prompt = (_, _) async => throw StateError('refused');
final file = _file('notes.txt', utf8.encode('keep'));
await pumpBar(
tester,
_FakePicker([
[file],
]).picker,
);
await pickFiles(tester);
await typeAndSend(tester, ' keep me ');
expect(gateway.promptBodies.single['attachments'], _wireParts([file]));
expect(fieldText(tester), ' keep me ');
expect(
find.byKey(const Key('chat-composer-attachments')),
findsOneWidget,
);
expect(find.text('notes.txt'), findsOneWidget);
});
testWidgets(
'Send is disabled while a prompt with attachments is in flight',
(tester) async {
final reply = Completer<JsonMap>();
gateway.prompt = (_, _) => reply.future;
final file = _file('notes.txt', [1, 2, 3]);
await pumpBar(
tester,
_FakePicker([
[file],
]).picker,
);
await pickFiles(tester);
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'hello',
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pump();
FilledButton primary() => tester.widget<FilledButton>(
find.byKey(const Key('chat-composer-primary')),
);
IconButton attach() => tester.widget<IconButton>(
find.byKey(const Key('chat-composer-attach')),
);
expect(primary().onPressed, isNull);
expect(attach().onPressed, isNull);
expect(gateway.promptBodies.single['attachments'], _wireParts([file]));
// The chips stay until the prompt is accepted.
expect(
find.byKey(const Key('chat-composer-attachments')),
findsOneWidget,
);
reply.complete(const {});
await tester.pumpAndSettle();
expect(
find.byKey(const Key('chat-composer-attachments')),
findsNothing,
);
expect(
primary().onPressed,
isNull,
reason: 'the accepted draft is empty',
);
},
);
});
}