2a66be7b27
Native model control and chooser in the chat thread header, matching the web chat_models/chat_model exchange. Fixes the chat_models list parsing, keeps the accepted server echo across Back/reopen, and resolves the session row for /chat/:id deep links.
331 lines
10 KiB
Dart
331 lines
10 KiB
Dart
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',
|
|
'native_message_id': 'msg_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');
|
|
gateway.calls.clear();
|
|
await repo.selectModel(' s ', ' provider ', ' model ');
|
|
expect(gateway.calls.single.fields, {
|
|
'chat_model': {
|
|
'session_id': 's',
|
|
'provider': 'provider',
|
|
'model': 'model',
|
|
},
|
|
});
|
|
},
|
|
);
|
|
test('rejects invalid permissions and prompts before sending', () async {
|
|
expect(
|
|
() => repo.prompt(' ', 'hello', operationId: 'x'),
|
|
throwsArgumentError,
|
|
);
|
|
expect(
|
|
() => repo.prompt('s', ' ', operationId: 'x'),
|
|
throwsArgumentError,
|
|
);
|
|
expect(
|
|
() => repo.replyPermission('s', 'r', 'approve'),
|
|
throwsArgumentError,
|
|
);
|
|
expect(gateway.calls, isEmpty);
|
|
});
|
|
test(
|
|
'uses the server model catalog list and default history limit',
|
|
() async {
|
|
gateway.reply = {
|
|
'chat_models': [
|
|
{'id': 'm', 'provider': 'p', 'name': 'Model'},
|
|
],
|
|
};
|
|
final models = await repo.models('s');
|
|
expect(models.single['id'], 'm');
|
|
expect(models.single['provider'], 'p');
|
|
expect(gateway.calls.single.fields, {
|
|
'chat_query': {'session_id': 's'},
|
|
});
|
|
// A malformed catalog is empty, never invented rows, and a blank
|
|
// session id is refused before any request is sent.
|
|
gateway.reply = {'chat_models': 'not-a-list'};
|
|
expect(await repo.models('s'), isEmpty);
|
|
gateway.calls.clear();
|
|
await expectLater(repo.models(' '), throwsArgumentError);
|
|
expect(gateway.calls, isEmpty);
|
|
gateway.calls.clear();
|
|
await repo.history('s');
|
|
expect(gateway.calls.single.fields['chat_query']['limit'], 200);
|
|
},
|
|
);
|
|
test(
|
|
'reads exact directory/backend catalog envelopes without inventing fallback',
|
|
() async {
|
|
gateway.reply = {
|
|
'chat_directories': ['~', '/work'],
|
|
};
|
|
expect(await repo.directories(), ['~', '/work']);
|
|
expect(gateway.calls.last.type, 'chat_directories');
|
|
expect(gateway.calls.last.fields, isEmpty);
|
|
gateway.reply = {
|
|
'chat_backends': ['opencode', 'claude'],
|
|
};
|
|
expect(await repo.backends(), ['opencode', 'claude']);
|
|
expect(gateway.calls.last.type, 'chat_backends');
|
|
expect(gateway.calls.last.fields, isEmpty);
|
|
gateway.reply = {};
|
|
await expectLater(repo.directories(), throwsStateError);
|
|
await expectLater(repo.backends(), throwsStateError);
|
|
},
|
|
);
|
|
});
|
|
}
|