Files
pipelines/app/test/features/chat/speech/fake_speech_engine.dart
T
alan 6367cce68a Read chat replies aloud with word highlighting
An assistant reply gets Read aloud beside Copy; while it reads it becomes
Pause/Resume and Stop, one reply at a time app-wide. The spoken word is
highlighted in the rendered reply (across multi-part replies) and the
transcript scrolls only when that word leaves the viewport. Reading stops
when its chat closes or is swapped, another reply starts, or the app goes
to the background. Markdown is spoken as sentences; code blocks are
announced as "Code block.".

Speech goes through a SpeechEngine seam with a flutter_tts implementation
(Android, macOS, web; hidden on Linux) and a fake for tests. Settings has
a Read aloud section: a voice picker grouped by language (device language
first, a sample per voice), a rate slider and reset, saved per device.

flutter_tts has no Swift package, so macOS/iOS gain the CocoaPods
integration (Podfiles with the Runner deployment targets); Android
declares the TTS_SERVICE query. The capture fixture gains ?speech=fake.
2026-11-13 01:29:04 +00:00

80 lines
2.1 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 []});
@override
final bool supported;
final List<SpeechVoice> available;
final calls = <String>[];
final spoken = <String>[];
double? rate;
SpeechVoice? voice;
@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 => 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;
}