feat(app): add chat foundation
This commit is contained in:
@@ -0,0 +1,259 @@
|
||||
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_sessions.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_transcript.dart';
|
||||
|
||||
class RecordingGateway implements ApiGateway {
|
||||
final calls = <({String type, JsonMap fields})>[];
|
||||
JsonMap reply = {};
|
||||
@override
|
||||
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
|
||||
calls.add((type: type, fields: fields));
|
||||
return reply;
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<JsonMap> get frames => const Stream.empty();
|
||||
}
|
||||
|
||||
void main() {
|
||||
group('chat domain', () {
|
||||
test(
|
||||
'filters/groups in server order and keeps server usage unknowns unavailable',
|
||||
() {
|
||||
final sessions = [
|
||||
{'id': 'a', 'directory': '/work', 'title': 'First'},
|
||||
{'id': 'b', 'directory': '/else', 'title': 'Second'},
|
||||
{'id': 'c', 'directory': '/work', 'title': 'Third'},
|
||||
];
|
||||
expect(filterChatSessions(sessions, 'ELSE').single['id'], 'b');
|
||||
expect(groupChatSessions(sessions).map((g) => g.directory), [
|
||||
'/work',
|
||||
'/else',
|
||||
]);
|
||||
expect(chatSessionActivity(running: true, unseen: true), 'streaming');
|
||||
expect(
|
||||
chatUsageSnapshot({
|
||||
'total_tokens': 3,
|
||||
'context_window': 'unknown',
|
||||
})!['context_window'],
|
||||
isNull,
|
||||
);
|
||||
expect(chatUsageSnapshot({'legacy_tokens': 9}), isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'merges pages by id, retains idless events, replaces authority, and folds parts',
|
||||
() {
|
||||
final first = mergeChatHistory({}, {
|
||||
'messages': [
|
||||
{
|
||||
'id': 'a',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'reasoning', 'text': 'think'},
|
||||
],
|
||||
},
|
||||
{
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'future_part', 'payload': 4},
|
||||
],
|
||||
},
|
||||
{'id': 'u', 'role': 'user', 'text': 'hello', 'operation_id': 'op'},
|
||||
],
|
||||
'nextCursor': 'opaque/1',
|
||||
'hasMore': true,
|
||||
});
|
||||
final older = mergeChatHistory(first, {
|
||||
'messages': [
|
||||
{
|
||||
'id': 'a',
|
||||
'role': 'assistant',
|
||||
'parts': [
|
||||
{'type': 'text', 'text': 'final'},
|
||||
],
|
||||
},
|
||||
],
|
||||
'nextCursor': '',
|
||||
'hasMore': false,
|
||||
}, older: true);
|
||||
expect(older['nextCursor'], '');
|
||||
final msgs = older['messages'] as List<JsonMap>;
|
||||
expect(msgs.map((m) => m['id']), ['a', null, 'u']);
|
||||
expect(
|
||||
(transcriptRows(msgs).first['parts'] as List).map((p) => p['type']),
|
||||
['text', 'future_part'],
|
||||
);
|
||||
expect(
|
||||
reconcileChatUser(msgs, {
|
||||
'id': 'remote',
|
||||
'role': 'user',
|
||||
'operation_id': 'op',
|
||||
}).where((m) => m['operation_id'] == 'op').length,
|
||||
1,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'send queue is session-scoped and operation IDs deduplicate retries',
|
||||
() {
|
||||
final queue = ChatSendQueue();
|
||||
final send = QueuedChatSend(
|
||||
operationId: 'stable',
|
||||
text: ' prompt ',
|
||||
files: ['/tmp/a'],
|
||||
);
|
||||
expect(queue.enqueue('s1', send), isTrue);
|
||||
expect(queue.enqueue('s1', send.retry()), isFalse);
|
||||
expect(queue.enqueue('s2', send.retry()), isTrue);
|
||||
expect(queue.takeNext('s1')!.operationId, 'stable');
|
||||
expect(queue.takeNext('s1'), isNull);
|
||||
expect(queue.forSession('s2').single.files, ['/tmp/a']);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'live text/reasoning append, tool snapshots merge, and steps settle only activity',
|
||||
() {
|
||||
var thread = <String, dynamic>{
|
||||
'sessionId': 's',
|
||||
'messages': <JsonMap>[],
|
||||
};
|
||||
thread = applyChatEvent(thread, {
|
||||
'kind': 'text',
|
||||
'session_id': 's',
|
||||
'message_id': 'a',
|
||||
'part': {'id': 'p'},
|
||||
'delta': 'one',
|
||||
});
|
||||
thread = applyChatEvent(thread, {
|
||||
'kind': 'text',
|
||||
'session_id': 's',
|
||||
'message_id': 'a',
|
||||
'part': {'id': 'p'},
|
||||
'delta': ' two',
|
||||
});
|
||||
thread = applyChatEvent(thread, {
|
||||
'kind': 'reasoning',
|
||||
'session_id': 's',
|
||||
'message_id': 'a',
|
||||
'part': {'id': 'r'},
|
||||
'delta': 'hidden',
|
||||
});
|
||||
thread = applyChatEvent(thread, {
|
||||
'kind': 'tool',
|
||||
'session_id': 's',
|
||||
'message_id': 'a',
|
||||
'part': {
|
||||
'id': 't',
|
||||
'type': 'tool',
|
||||
'state': {'input': 'x'},
|
||||
},
|
||||
});
|
||||
thread = applyChatEvent(thread, {
|
||||
'kind': 'tool',
|
||||
'session_id': 's',
|
||||
'message_id': 'a',
|
||||
'part': {
|
||||
'id': 't',
|
||||
'type': 'tool',
|
||||
'state': {'status': 'completed'},
|
||||
},
|
||||
});
|
||||
final parts =
|
||||
((thread['messages'] as List).single as JsonMap)['parts']
|
||||
as List<JsonMap>;
|
||||
expect(parts[0]['text'], 'one two');
|
||||
expect(parts[1]['text'], 'hidden');
|
||||
expect(parts[2]['state'], {'input': 'x', 'status': 'completed'});
|
||||
expect(thread['running'], true);
|
||||
thread = applyChatEvent(thread, {
|
||||
'kind': 'step',
|
||||
'session_id': 's',
|
||||
'step': {'status': 'ended', 'turn_id': 'turn'},
|
||||
});
|
||||
expect(thread['running'], false);
|
||||
expect(
|
||||
applyChatEvent(thread, {'kind': 'text', 'session_id': 'other'}),
|
||||
same(thread),
|
||||
);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('chat wire adapter', () {
|
||||
late RecordingGateway gateway;
|
||||
late ChatRepository repo;
|
||||
setUp(() {
|
||||
gateway = RecordingGateway();
|
||||
repo = ChatRepository(gateway);
|
||||
});
|
||||
test('uses web envelopes and opaque history cursor', () async {
|
||||
gateway.reply = {
|
||||
'chat_history': {
|
||||
'messages': [
|
||||
{'id': 'm', 'role': 'user'},
|
||||
],
|
||||
'next_cursor': 'opaque',
|
||||
'has_more': true,
|
||||
},
|
||||
};
|
||||
final page = await repo.history('s', cursor: 'before', limit: 17);
|
||||
expect(page['nextCursor'], 'opaque');
|
||||
expect(gateway.calls.last.type, 'chat_messages');
|
||||
expect(gateway.calls.last.fields, {
|
||||
'chat_query': {'session_id': 's', 'cursor': 'before', 'limit': 17},
|
||||
});
|
||||
await repo.subscribe('s');
|
||||
expect(gateway.calls.last.fields, {
|
||||
'chat_subscribe': {'session_id': 's'},
|
||||
});
|
||||
});
|
||||
test(
|
||||
'sends stable operation id, omits empty files, and builds action envelopes',
|
||||
() async {
|
||||
await repo.prompt('s', ' hi ', operationId: 'same');
|
||||
expect(gateway.calls.last.fields, {
|
||||
'chat_prompt': {
|
||||
'session_id': 's',
|
||||
'text': 'hi',
|
||||
'operation_id': 'same',
|
||||
},
|
||||
});
|
||||
await repo.answerForm('s', 'f', {'choice': 'yes'}, action: 'submit');
|
||||
expect(gateway.calls.last.fields, {
|
||||
'chat_answer': {
|
||||
'session_id': 's',
|
||||
'form_id': 'f',
|
||||
'answers': {'choice': 'yes'},
|
||||
'action': 'submit',
|
||||
},
|
||||
});
|
||||
await repo.replyPermission('s', 'r', 'once', message: ' approved ');
|
||||
expect(gateway.calls.last.fields, {
|
||||
'chat_reply': {
|
||||
'session_id': 's',
|
||||
'request_id': 'r',
|
||||
'decision': 'once',
|
||||
'client': 'web',
|
||||
'message': 'approved',
|
||||
},
|
||||
});
|
||||
await repo.create(title: 'New', directory: '~', operationId: 'create');
|
||||
expect(gateway.calls.last.fields, {
|
||||
'chat_create': {
|
||||
'title': 'New',
|
||||
'directory': '~',
|
||||
'operation_id': 'create',
|
||||
},
|
||||
});
|
||||
await repo.selectModel('s', 'provider', 'model');
|
||||
expect(gateway.calls.last.type, 'chat_model');
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user