import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/core/api_gateway.dart'; import 'package:pipelines_app/features/chat/chat_runtime.dart'; class _Gateway implements ApiGateway { @override Stream get frames => const Stream.empty(); @override Future call(String type, [JsonMap fields = const {}]) async => {}; } void main() { late ChatRuntime runtime; late int notified; setUp(() { runtime = ChatRuntime(gateway: _Gateway()); notified = 0; runtime.addListener(() => notified++); }); tearDown(() => runtime.dispose()); test('adopts listed rows for opened, selected and retained chats only', () { runtime.opened['a'] = {'id': 'a', 'title': 'Old', 'running': true}; runtime.adoptRows( [ {'id': 'a', 'title': 'New', 'running': false}, {'id': 'b', 'title': 'B'}, {'id': 'c', 'title': 'C'}, {'id': 'd', 'title': 'D'}, ], selected: 'b', retained: {'c'}, ); // The conversation keeps its own running flag. expect(runtime.opened['a'], {'id': 'a', 'title': 'New', 'running': true}); expect(runtime.opened.keys, ['a', 'b', 'c']); expect(notified, 1); // A chat that is only listed still resolves its row. expect(runtime.rowFor('d')?['title'], 'D'); expect(runtime.titleFor('zz'), 'zz'); }); test('an unchanged listing never reverts an accepted mode', () { final rows = [ {'id': 'a', 'mode': 'ask'}, ]; runtime.adoptRows(rows, selected: 'a', retained: const {}); expect(runtime.modeChanged('a', 'auto', 'Auto'), isTrue); runtime.adoptRows(rows, selected: 'a', retained: const {}); expect(runtime.opened['a']?['mode'], 'auto'); expect(notified, 2); expect(runtime.modeChanged('missing', 'auto', 'Auto'), isFalse); }); test('an accepted model wins over the row and follows /new', () { runtime.opened['a'] = {'id': 'a', 'model': 'old'}; runtime.modelChanged('a', ( provider: 'p', model: 'new', selection: 'p/new', )); expect(runtime.rowFor('a')?['model'], 'new'); runtime.movedToSession('a', {'id': 'b', 'running': true}, 'Started'); expect(runtime.rowFor('b'), { 'id': 'b', 'model': 'new', 'model_provider': 'p', 'model_selection': 'p/new', }); expect(runtime.startNotice, (id: 'b', notice: 'Started')); }); test('rename updates the opened row; a new gateway drops transcripts', () { runtime.opened['a'] = {'id': 'a', 'title': 'Old'}; runtime.renamed('a', 'New'); expect(runtime.titleFor('a'), 'New'); expect(notified, 1); final other = _Gateway(); runtime.gateway = other; expect(runtime.gateway, same(other)); expect(runtime.gatewayFor('a'), same(other)); expect(runtime.serverIdFor('a'), ''); }); }