Files
pipelines/app/test/features/chat/speech/speech_document_test.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

57 lines
2.1 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech_document.dart';
void main() {
test('markdown is spoken as sentences and code is announced', () {
final result = markdownToSpeech(
'# Welcome\n\n- **First**\n- [Docs](https://example.com)\n\n```dart\nprint(1);\n```\n\nThat is all.',
);
expect(result.text, 'Welcome. First. Docs. Code block. That is all.');
expect(result.sources.length, result.text.length);
});
test('tilde fences and tables are summarised', () {
final result = markdownToSpeech(
'~~~\nsecret();\n~~~\n\n| A | B |\n|---|---|\n| 1 | 2 |',
);
expect(result.text, 'Code block. Table. A. B. 1. 2');
expect(result.text, isNot(contains('secret')));
});
test('repeated words map to their own occurrence', () {
const markdown = 'First repeat\n\nSecond repeat';
final result = markdownToSpeech(markdown);
final first = result.text.indexOf('repeat');
final second = result.text.lastIndexOf('repeat');
expect(result.sourceRange(0, (start: first, end: first + 6)), (
start: 6,
end: 12,
));
expect(result.sourceRange(0, (start: second, end: second + 6)), (
start: markdown.lastIndexOf('repeat'),
end: markdown.length,
));
});
test('text after a code block maps after the skipped code', () {
const markdown = 'Before repeat\n\n```dart\nrepeat();\n```\n\nAfter repeat';
final result = markdownToSpeech(markdown);
final spoken = result.text.lastIndexOf('repeat');
final range = result.sourceRange(0, (start: spoken, end: spoken + 6))!;
expect(range.start, markdown.lastIndexOf('repeat'));
});
test('parts are trimmed and keep their own sources', () {
final result = speechDocumentFor([' One part\n', '', 'Two part']);
expect(result.parts, ['One part', 'Two part']);
expect(result.text, 'One part. Two part');
expect(result.partOf('\nTwo part '), 1);
final two = result.text.indexOf('Two');
expect(result.sourceRange(1, (start: two, end: two + 3)), (
start: 0,
end: 3,
));
expect(result.sourceRange(0, (start: two, end: two + 3)), isNull);
});
}