import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/speech/speech.dart'; /// A scripted [SpeechEngine]: it records calls and a test fires the platform's /// events. [stop] reports a cancel like every real platform does. class FakeSpeechEngine extends SpeechEngine { FakeSpeechEngine({ this.supported = true, this.available = const [], this.voiceDownloadHint = false, this.voiceFailure = false, }); @override final bool supported; final List available; final bool voiceDownloadHint; final bool voiceFailure; final calls = []; final spoken = []; double? rate; SpeechVoice? voice; @override bool get supportsVoiceDownloadHint => voiceDownloadHint; @override Future speak(String text) async { calls.add('speak'); spoken.add(text); } @override Future pause() async => calls.add('pause'); @override Future stop() async { calls.add('stop'); onCancel?.call(); } @override Future setRate(double rate) async => this.rate = rate; @override Future setVoice(SpeechVoice? voice) async => this.voice = voice; @override Future> voices() async { if (voiceFailure) throw StateError('voice inventory failed'); return available; } /// Reports [word] (its first occurrence at or after [from]) being spoken. void speakWord(String word, {int from = 0}) { final text = spoken.last; final start = text.indexOf(word, from); expect(start, greaterThanOrEqualTo(0), reason: '"$word" is not spoken'); onProgress?.call(text, start, start + word.length); } } /// Saved read-aloud settings held in memory, surviving a "reload". class MemorySpeechPreferences implements SpeechPreferencesStore { SpeechPreferences? saved; @override Future load() async => saved; @override Future save(SpeechPreferences preferences) async { saved = preferences; return true; } } /// Installs a reader over [engine] as the app's own for one test. SpeechController installFakeSpeech( FakeSpeechEngine engine, { SpeechPreferencesStore? store, }) { final controller = SpeechController( engine: engine, store: store ?? MemorySpeechPreferences(), ); debugSpeechController = controller; addTearDown(() { debugSpeechController = null; controller.dispose(); }); return controller; }