Files
pipelines/app/test/features/chat/speech/fake_speech_engine.dart
T
alan 3164b7c8df Prefer natural voices for read aloud
Rank platform voice quality and select the most natural automatic voice while keeping explicit voice choices intact. Surface natural labels, hide novelty voices in the picker, and guide Apple users to download enhanced voices when needed.
2026-11-13 15:59:35 +00:00

93 lines
2.4 KiB
Dart

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<SpeechVoice> available;
final bool voiceDownloadHint;
final bool voiceFailure;
final calls = <String>[];
final spoken = <String>[];
double? rate;
SpeechVoice? voice;
@override
bool get supportsVoiceDownloadHint => voiceDownloadHint;
@override
Future<void> speak(String text) async {
calls.add('speak');
spoken.add(text);
}
@override
Future<void> pause() async => calls.add('pause');
@override
Future<void> stop() async {
calls.add('stop');
onCancel?.call();
}
@override
Future<void> setRate(double rate) async => this.rate = rate;
@override
Future<void> setVoice(SpeechVoice? voice) async => this.voice = voice;
@override
Future<List<SpeechVoice>> 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<SpeechPreferences?> load() async => saved;
@override
Future<bool> 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;
}