merge(app): land chat-attach-send micro-slice
This commit is contained in:
@@ -0,0 +1,193 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_attachment_draft.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_attachment_wire.dart';
|
||||
|
||||
class _RecordingGateway implements ApiGateway {
|
||||
JsonMap? fields;
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
this.fields = fields;
|
||||
return const {};
|
||||
}
|
||||
}
|
||||
|
||||
ChatAttachment attachment(
|
||||
String name,
|
||||
int sizeBytes, {
|
||||
String mime = 'text/plain',
|
||||
}) => ChatAttachment(name: name, mime: mime, sizeBytes: sizeBytes);
|
||||
|
||||
void main() {
|
||||
final png = Uint8List.fromList([0, 1, 2, 255]);
|
||||
final notes = Uint8List.fromList(utf8.encode('hello'));
|
||||
|
||||
test('builds one exact {name, mime, data} part per draft file, in order', () {
|
||||
final draft = ChatAttachmentDraft()
|
||||
..add(attachment(' screen.png ', png.length, mime: ' image/png '))
|
||||
..add(attachment('notes.txt', notes.length, mime: ''));
|
||||
|
||||
final parts = chatAttachmentParts(
|
||||
draft,
|
||||
read: (entry) => entry.name.trim() == 'screen.png' ? png : notes,
|
||||
);
|
||||
|
||||
expect(parts, hasLength(2));
|
||||
expect(parts[0].keys.toList(), ['name', 'mime', 'data']);
|
||||
expect(parts[0], {
|
||||
'name': 'screen.png',
|
||||
'mime': 'image/png',
|
||||
'data': base64Encode(png),
|
||||
});
|
||||
expect(parts[1], {
|
||||
'name': 'notes.txt',
|
||||
'mime': 'application/octet-stream',
|
||||
'data': base64Encode(notes),
|
||||
});
|
||||
});
|
||||
|
||||
test('base64 data round-trips to the picked bytes', () {
|
||||
final draft = ChatAttachmentDraft()
|
||||
..add(attachment('screen.png', png.length, mime: 'image/png'));
|
||||
|
||||
final part = chatAttachmentParts(draft, read: (_) => png).single;
|
||||
final encoded = part['data'] as String;
|
||||
|
||||
expect(encoded, 'AAEC/w==');
|
||||
expect(base64Decode(encoded), png);
|
||||
});
|
||||
|
||||
test(
|
||||
'an empty draft yields no parts, so the prompt has no attachments key',
|
||||
() async {
|
||||
final gateway = _RecordingGateway();
|
||||
final parts = chatAttachmentParts(
|
||||
ChatAttachmentDraft(),
|
||||
read: (_) => throw StateError('an empty draft reads nothing'),
|
||||
);
|
||||
|
||||
expect(parts, isEmpty);
|
||||
|
||||
await ChatRepository(
|
||||
gateway,
|
||||
).prompt('ses_1', 'hello', operationId: 'op_1', attachments: parts);
|
||||
final body = gateway.fields!['chat_prompt'] as JsonMap;
|
||||
expect(body.containsKey('attachments'), isFalse);
|
||||
},
|
||||
);
|
||||
|
||||
test('refuses more than eight files before reading any of them', () {
|
||||
final draft = ChatAttachmentDraft();
|
||||
for (var index = 0; index < maxChatAttachmentCount + 1; index += 1) {
|
||||
draft.add(attachment('file-$index.bin', 0));
|
||||
}
|
||||
var reads = 0;
|
||||
|
||||
expect(
|
||||
() => chatAttachmentParts(
|
||||
draft,
|
||||
read: (_) {
|
||||
reads += 1;
|
||||
return Uint8List(0);
|
||||
},
|
||||
),
|
||||
throwsA(
|
||||
isA<ChatAttachmentWireException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'At most 8 attachments are allowed.',
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(reads, 0);
|
||||
});
|
||||
|
||||
test('refuses an actual total over 1 MiB before encoding a part', () {
|
||||
final draft = ChatAttachmentDraft()
|
||||
..add(attachment('first.bin', 1))
|
||||
..add(attachment('second.bin', 1));
|
||||
var reads = 0;
|
||||
|
||||
expect(
|
||||
() => chatAttachmentParts(
|
||||
draft,
|
||||
read: (_) {
|
||||
reads += 1;
|
||||
return Uint8List(ChatAttachmentDraft.maxTotalBytes + 1);
|
||||
},
|
||||
),
|
||||
throwsA(
|
||||
isA<ChatAttachmentWireException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'Attachments are too large for web chat (1 MiB total).',
|
||||
),
|
||||
),
|
||||
);
|
||||
// The bound is checked while reading: no part was built and the next file
|
||||
// was never touched.
|
||||
expect(reads, 1);
|
||||
});
|
||||
|
||||
test('names the file that could not be read', () {
|
||||
final draft = ChatAttachmentDraft()..add(attachment('missing.txt', 4));
|
||||
|
||||
expect(
|
||||
() => chatAttachmentParts(draft, read: (_) => null),
|
||||
throwsA(
|
||||
isA<ChatAttachmentWireException>().having(
|
||||
(error) => error.message,
|
||||
'message',
|
||||
'"missing.txt" could not be read.',
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('parses a recorded history file part without decoding it into UI', () {
|
||||
final parsed = chatAttachmentFromHistoryPart({
|
||||
'type': 'file',
|
||||
'name': 'report.csv',
|
||||
'mime': 'text/csv',
|
||||
'uri': 'file:///tmp/report.csv',
|
||||
});
|
||||
|
||||
expect(
|
||||
parsed,
|
||||
const ChatAttachment(name: 'report.csv', mime: 'text/csv', sizeBytes: 0),
|
||||
);
|
||||
});
|
||||
|
||||
test('falls back to the URI basename and keeps a recorded size', () {
|
||||
expect(
|
||||
chatAttachmentFromHistoryPart({
|
||||
'type': 'file',
|
||||
'uri': 'file:///tmp/plan.md',
|
||||
'size': 42,
|
||||
}),
|
||||
const ChatAttachment(name: 'plan.md', mime: '', sizeBytes: 42),
|
||||
);
|
||||
});
|
||||
|
||||
test('ignores parts that are not a recorded attachment', () {
|
||||
expect(chatAttachmentFromHistoryPart(null), isNull);
|
||||
expect(chatAttachmentFromHistoryPart('file'), isNull);
|
||||
expect(
|
||||
chatAttachmentFromHistoryPart({'type': 'text', 'text': 'hello'}),
|
||||
isNull,
|
||||
);
|
||||
expect(chatAttachmentFromHistoryPart({'type': 'file'}), isNull);
|
||||
expect(
|
||||
chatAttachmentFromHistoryPart({'type': 'file', 'name': ' '}),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user