Files
pipelines/app/test/features/chat/speech/speech_controller_test.dart
T

225 lines
7.1 KiB
Dart

import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'fake_speech_engine.dart';
void main() {
late FakeSpeechEngine engine;
late SpeechController speech;
setUp(() {
engine = FakeSpeechEngine();
});
SpeechController reader([SpeechPreferencesStore? store]) =>
speech = SpeechController(
engine: engine,
store: store ?? MemorySpeechPreferences(),
);
tearDown(() => speech.dispose());
testWidgets('read, pause, resume and stop move through the states', (
tester,
) async {
reader();
await speech.read('a', ['Hello **there**.']);
expect(speech.state, SpeechState.speaking);
expect(speech.messageId, 'a');
expect(engine.spoken.last, 'Hello there.');
engine.onStart!();
await speech.pause();
expect(speech.state, SpeechState.paused);
expect(engine.calls.last, 'pause');
engine.onPause!();
expect(speech.state, SpeechState.paused);
await speech.resume();
expect(speech.state, SpeechState.speaking);
// Every platform resumes when the paused text is spoken again.
expect(engine.spoken.last, 'Hello there.');
engine.onResume!();
await speech.stop();
expect(speech.state, SpeechState.idle);
expect(speech.messageId, isNull);
expect(speech.document, isNull);
});
testWidgets('starting another reply stops the first', (tester) async {
reader();
await speech.read('a', ['First reply']);
engine.onStart!();
engine.speakWord('reply');
await speech.read('b', ['Second reply']);
expect(engine.calls, ['speak', 'stop', 'speak']);
expect(speech.messageId, 'b');
expect(speech.word.value, isNull);
// A late cancel of the first reply does not end the second.
engine.onCancel!();
expect(speech.messageId, 'b');
expect(speech.state, SpeechState.speaking);
});
testWidgets('progress sets the word and maps back to the source', (
tester,
) async {
reader();
const first = 'Intro **bold** word';
const second = '- item word';
await speech.read('a', [first, second]);
engine.onStart!();
engine.speakWord('bold');
final document = speech.document!;
expect(speech.word.value, isNotNull);
expect(document.sourceRange(0, speech.word.value!), (
start: first.indexOf('bold'),
end: first.indexOf('bold') + 4,
));
expect(document.sourceRange(1, speech.word.value!), isNull);
final spoken = engine.spoken.last;
engine.speakWord('word', from: spoken.lastIndexOf('item'));
expect(document.sourceRange(0, speech.word.value!), isNull);
expect(document.sourceRange(1, speech.word.value!), (
start: second.indexOf('word'),
end: second.length,
));
});
testWidgets('progress after a resumed suffix keeps document offsets', (
tester,
) async {
reader();
await speech.read('a', ['one two three']);
engine.onStart!();
// Android speaks the remaining text after a resume.
engine.onProgress!('three', 0, 5);
expect(speech.word.value, (start: 8, end: 13));
});
testWidgets('completion and cancel clear the reply', (tester) async {
reader();
await speech.read('a', ['Done soon']);
engine.onStart!();
engine.speakWord('soon');
engine.onCompletion!();
expect(speech.state, SpeechState.idle);
expect(speech.messageId, isNull);
expect(speech.word.value, isNull);
await speech.read('b', ['Cancelled']);
engine.onStart!();
engine.onCancel!();
expect(speech.state, SpeechState.idle);
expect(speech.messageId, isNull);
});
testWidgets('going to the background stops reading', (tester) async {
reader();
await speech.read('a', ['Background']);
engine.onStart!();
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
await tester.pump();
expect(speech.state, SpeechState.speaking, reason: 'focus loss only');
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
await tester.pump();
expect(speech.state, SpeechState.idle);
expect(engine.calls.last, 'stop');
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
});
testWidgets('stopScope stops only replies of that conversation', (
tester,
) async {
reader();
await speech.read(speechMessageId('chat-1', 'm1'), ['Hello']);
engine.onStart!();
await speech.stopScope('chat-2');
expect(speech.state, SpeechState.speaking);
await speech.stopScope('chat-1');
expect(speech.state, SpeechState.idle);
});
testWidgets('reading names the conversation until the reply ends', (
tester,
) async {
reader();
await speech.read(speechMessageId('chat-1', 'm1'), ['Hi'], title: 'One');
expect(
speech.reading.value,
const SpeechReading(scope: 'chat-1', title: 'One'),
);
speech.retitle('chat-2', 'Other');
expect(speech.reading.value?.title, 'One');
speech.retitle('chat-1', 'Renamed');
expect(speech.reading.value?.title, 'Renamed');
// Another reply replaces it; finishing clears it.
await speech.read(speechMessageId('chat-2', 'm1'), ['Yo'], title: 'Two');
expect(speech.reading.value?.scope, 'chat-2');
engine.onStart!();
engine.onCompletion!();
expect(speech.reading.value, isNull);
expect(speech.state, SpeechState.idle);
});
testWidgets('voice and rate are applied and persist across a reload', (
tester,
) async {
final store = MemorySpeechPreferences();
reader(store);
const voice = SpeechVoice(name: 'Daniel', locale: 'en-GB');
await speech.setVoice(voice);
await speech.setRate(1.5);
await speech.read('a', ['Hello']);
expect(engine.voice, voice);
expect(engine.rate, 1.5);
speech.dispose();
reader(store);
await speech.restore();
expect(speech.voice, voice);
expect(speech.rate, 1.5);
await speech.reset();
expect(speech.voice, isNull);
expect(speech.rate, defaultSpeechRate);
expect(store.saved, (rate: defaultSpeechRate, voice: null));
});
testWidgets('an explicitly selected voice does not require voice inventory', (
tester,
) async {
engine = FakeSpeechEngine(voiceFailure: true);
reader();
const voice = SpeechVoice(name: 'Daniel', locale: 'en-GB');
// The saved selection is passed directly to the engine, even if querying
// the platform voice inventory is unavailable.
await speech.setVoice(voice);
await speech.read('a', ['Hello']);
expect(engine.voice, voice);
expect(engine.spoken, ['Hello']);
});
testWidgets(
'automatic voice selects the most natural device-language voice',
(tester) async {
engine = FakeSpeechEngine(
available: const [
SpeechVoice(name: 'Compact', locale: 'en-US', qualityRank: 1),
SpeechVoice(name: 'Enhanced', locale: 'en-US', qualityRank: 2),
SpeechVoice(name: 'French Natural', locale: 'fr-FR', qualityRank: 3),
],
);
reader();
await speech.read('a', ['Hello']);
expect(engine.voice?.name, 'Enhanced');
expect(speech.voice, isNull);
},
);
}