import 'dart:async'; import 'package:flutter_test/flutter_test.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:shared_preferences/shared_preferences.dart'; import 'chat_composer_fixtures.dart'; /// A store whose read resolves only when the test says so. class _DelayedDraftStore implements ChatDraftStore { _DelayedDraftStore(this.saved); final String saved; final reads = >[]; final writes = []; @override Future read(String sessionId) { final completer = Completer(); reads.add(completer); return completer.future; } @override Future write(String sessionId, String text) async { writes.add(text); return true; } } void main() { TestWidgetsFlutterBinding.ensureInitialized(); test( 'SharedPreferences stores the exact per-session draft and clears it', () async { SharedPreferences.setMockInitialValues({}); const store = SharedPreferencesChatDraftStore(); expect(await store.write('a', ' line one\nline two '), isTrue); expect(await store.read('a'), ' line one\nline two '); expect(await store.read('b'), ''); final preferences = await SharedPreferences.getInstance(); expect( preferences.getString('${chatDraftKeyPrefix}a'), ' line one\nline two ', ); expect(await store.write('a', ''), isTrue); expect(await store.read('a'), ''); expect(preferences.getString('${chatDraftKeyPrefix}a'), isNull); }, ); test('drafts stay isolated per conversation and load exactly', () async { final drafts = MemoryChatDraftStore(); final firstGateway = FakeComposerGateway(); final secondGateway = FakeComposerGateway(); final first = ChatComposerController(firstGateway, 'a', drafts: drafts); final second = ChatComposerController(secondGateway, 'b', drafts: drafts); addTearDown(first.dispose); addTearDown(second.dispose); addTearDown(firstGateway.close); addTearDown(secondGateway.close); await first.setDraft('draft a\n'); await second.setDraft('draft b'); expect(drafts.entries, {'a': 'draft a\n', 'b': 'draft b'}); final reopened = ChatComposerController( FakeComposerGateway(), 'a', drafts: drafts, ); addTearDown(reopened.dispose); await reopened.loadDraft(); expect(reopened.draft, 'draft a\n'); }); test('a draft typed while the saved draft loads is not replaced', () async { final store = _DelayedDraftStore('saved'); final controller = ChatComposerController( FakeComposerGateway(), 's', drafts: store, ); addTearDown(controller.dispose); final loading = controller.loadDraft(); await pumpEventQueue(); await controller.setDraft('typed'); store.reads.single.complete('saved'); await loading; expect(controller.draft, 'typed'); expect(store.writes.last, 'typed'); }); test( 'an unreadable store leaves the draft empty and can load later', () async { final drafts = MemoryChatDraftStore(drafts: {'s': 'saved'}) ..failReads = true; final controller = ChatComposerController( FakeComposerGateway(), 's', drafts: drafts, ); addTearDown(controller.dispose); await controller.loadDraft(); expect(controller.draft, ''); drafts.failReads = false; await controller.loadDraft(); expect(controller.draft, 'saved'); }, ); test( 'a failed draft save keeps the text and reports it, then clears', () async { final drafts = MemoryChatDraftStore()..failWrites = true; final controller = ChatComposerController( FakeComposerGateway(), 's', drafts: drafts, ); addTearDown(controller.dispose); await controller.setDraft('still here'); expect(controller.draft, 'still here'); expect(controller.draftError, ChatComposerController.draftSaveFailed); expect(drafts.entries, isEmpty); drafts.failWrites = false; await controller.setDraft('saved now'); expect(controller.draftError, isNull); expect(drafts.entries, {'s': 'saved now'}); }, ); test('rapid edits persist in order and the newest draft wins', () async { final drafts = MemoryChatDraftStore(); final controller = ChatComposerController( FakeComposerGateway(), 's', drafts: drafts, ); addTearDown(controller.dispose); final first = controller.setDraft('a'); final second = controller.setDraft('ab'); final third = controller.setDraft('abc'); await Future.wait([first, second, third]); expect(controller.draft, 'abc'); expect(drafts.entries, {'s': 'abc'}); }); test('a failed draft clear still sends and stays visibly unsaved', () async { final drafts = MemoryChatDraftStore()..failWrites = true; final gateway = FakeComposerGateway(); gateway.prompt = (_, _) async => const {}; final controller = ChatComposerController(gateway, 's', drafts: drafts); addTearDown(controller.dispose); addTearDown(gateway.close); await controller.setDraft('send me'); await controller.send(); expect(gateway.promptBodies.single['text'], 'send me'); expect(controller.draft, ''); expect(controller.error, isNull); // The in-memory draft cleared; the device store still reports the failure. expect(controller.draftError, ChatComposerController.draftSaveFailed); }); }