WIP read aloud keeps playing across chats

This commit is contained in:
2026-12-03 22:18:38 +00:00
parent 81c73275c0
commit f4bd8ad44c
24 changed files with 778 additions and 78 deletions
@@ -2,21 +2,34 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'package:pipelines_app/features/chat/ui/speech/chat_speaking_control.dart';
import 'package:pipelines_app/features/chat/ui/transcript/chat_read_aloud_button.dart';
import '../speech/fake_speech_engine.dart';
import 'chat_thread_fixtures.dart';
Widget _thread(FakeChatGateway gateway, String sessionId) => MaterialApp(
Widget _thread(
FakeChatGateway gateway,
String sessionId, {
String title = '',
bool pasteTarget = true,
}) => MaterialApp(
theme: ThemeData(platform: TargetPlatform.android),
home: Scaffold(
body: ChatThreadView(gateway: gateway, sessionId: sessionId, onBack: () {}),
body: ChatThreadView(
gateway: gateway,
sessionId: sessionId,
title: title,
pasteTarget: pasteTarget,
onBack: () {},
),
),
);
Future<(FakeSpeechEngine, SpeechController, FakeChatGateway)> _reading(
WidgetTester tester,
) async {
WidgetTester tester, {
String title = 'Alpha',
}) async {
tester.view.physicalSize = const Size(800, 1600);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
@@ -33,42 +46,78 @@ Future<(FakeSpeechEngine, SpeechController, FakeChatGateway)> _reading(
],
),
]);
await tester.pumpWidget(_thread(gateway, 's1'));
await tester.pumpWidget(_thread(gateway, 's1', title: title));
await tester.pumpAndSettle();
await tester.tap(find.byTooltip(chatReadAloudLabel));
await tester.pump();
engine.onStart!();
await tester.pump();
expect(engine.spoken, ['Hello from the agent.']);
expect(speech.messageId, startsWith(speechScopePrefix('s1')));
expect(speech.reading.value, SpeechReading(scope: 's1', title: title));
return (engine, speech, gateway);
}
void main() {
testWidgets('switching the pane to another chat stops reading', (
testWidgets('switching the pane to another chat keeps reading', (
tester,
) async {
final (engine, speech, gateway) = await _reading(tester);
await tester.pumpWidget(_thread(gateway, 's2'));
await tester.pumpWidget(_thread(gateway, 's2', title: 'Beta'));
await tester.pumpAndSettle();
expect(speech.state, SpeechState.idle);
expect(engine.calls, contains('stop'));
expect(speech.state, SpeechState.speaking);
expect(engine.calls, isNot(contains('stop')));
expect(speechReading.value?.scope, 's1');
// Back in the chat, its reply shows the reading's Pause and Stop again.
await tester.pumpWidget(_thread(gateway, 's1', title: 'Alpha'));
await tester.pumpAndSettle();
expect(find.byTooltip(chatStopReadingLabel), findsOneWidget);
expect(find.byTooltip(chatPauseReadingLabel), findsOneWidget);
});
testWidgets('closing the chat stops reading', (tester) async {
testWidgets('leaving the conversation keeps its reply reading', (
tester,
) async {
final (engine, speech, _) = await _reading(tester);
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
await tester.pumpAndSettle();
expect(speech.state, SpeechState.idle);
expect(engine.calls.last, 'stop');
});
testWidgets('closing a chat leaves another chat reading', (tester) async {
final (engine, speech, _) = await _reading(tester);
await speech.read(speechMessageId('elsewhere', 'b1'), ['Other reply']);
engine.onStart!();
await tester.pumpWidget(const MaterialApp(home: SizedBox()));
await tester.pumpAndSettle();
expect(speech.state, SpeechState.speaking);
expect(speech.messageId, speechMessageId('elsewhere', 'b1'));
expect(engine.calls, isNot(contains('stop')));
});
testWidgets('another chat names the reading and its Stop ends it', (
tester,
) async {
final (engine, speech, gateway) = await _reading(tester);
expect(find.text('Speaking · Alpha'), findsOneWidget);
await tester.pumpWidget(_thread(gateway, 's2', title: 'Beta'));
await tester.pumpAndSettle();
expect(find.text('Speaking · Alpha'), findsOneWidget);
await tester.tap(find.byTooltip(chatSpeakingStopLabel));
await tester.pumpAndSettle();
expect(speech.state, SpeechState.idle);
expect(engine.calls.last, 'stop');
expect(find.byKey(const Key('chat-speaking-control')), findsNothing);
});
testWidgets('the control follows a pause and a rename', (tester) async {
final (_, speech, gateway) = await _reading(tester);
await speech.pause();
await tester.pump();
expect(find.text('Paused · Alpha'), findsOneWidget);
await tester.pumpWidget(_thread(gateway, 's1', title: 'Renamed'));
await tester.pumpAndSettle();
expect(find.text('Paused · Renamed'), findsOneWidget);
});
testWidgets('only the active pane shows the global control', (tester) async {
final (_, _, gateway) = await _reading(tester);
await tester.pumpWidget(_thread(gateway, 's2', pasteTarget: false));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-speaking-control')), findsNothing);
});
}
@@ -143,6 +143,29 @@ void main() {
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 {
@@ -0,0 +1,78 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
import '../../speech/fake_speech_engine.dart';
import 'chat_test_harness.dart';
import 'fake_chat_gateway.dart';
JsonMap _row(String id, {bool closed = false}) => {
'id': id,
'title': id,
'directory': '/w',
if (closed) 'closed_at': '2026-12-03T10:00:00Z',
};
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
late FakeChatGateway gateway;
late ChatSessionsController controller;
late SpeechController speech;
setUp(() async {
speech = installFakeSpeech(FakeSpeechEngine());
gateway = FakeChatGateway()
..sessionsReply = {
'chat_sessions': [_row('s1'), _row('s2')],
}
..actionHandler = (_, _) async => const {};
controller = ChatSessionsController(
ChatRepository(gateway),
localStore: MemoryChatSessionLocalStore(),
);
addTearDown(controller.dispose);
await controller.refresh();
await speech.read(speechMessageId('s1', 'r1'), ['Hello.']);
expect(speech.state, SpeechState.speaking);
});
test('closing the reading chat stops it', () async {
await controller.close('s1');
await pumpEventQueue();
expect(speech.state, SpeechState.idle);
});
test('closing another chat keeps reading', () async {
await controller.close('s2');
await pumpEventQueue();
expect(speech.state, SpeechState.speaking);
});
test('a chat closed on another device stops it on the next read', () async {
gateway.sessionsReply = {
'chat_sessions': [_row('s1', closed: true), _row('s2')],
};
await controller.refresh();
await pumpEventQueue();
expect(speech.state, SpeechState.idle);
});
test('a deleted chat stops it; a failed read does not', () async {
gateway.sessionsHandler = () async => throw StateError('offline');
await controller.refresh();
await pumpEventQueue();
expect(speech.state, SpeechState.speaking);
gateway
..sessionsHandler = null
..sessionsReply = {
'chat_sessions': [_row('s2')],
};
await controller.refresh();
await pumpEventQueue();
expect(speech.state, SpeechState.idle);
});
}
@@ -0,0 +1,104 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/features/chat/speech/speech.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_awaiting.dart';
import 'package:pipelines_app/features/chat/ui/sessions/chat_compact_avatar.dart';
import 'package:pipelines_app/features/chat/ui/sessions/session_row_content.dart';
import '../../speech/fake_speech_engine.dart';
const _speaking = Key('chat-row-speaking');
Widget _avatars({bool reduceMotion = false}) => MaterialApp(
home: MediaQuery(
data: MediaQueryData(disableAnimations: reduceMotion),
child: const Scaffold(
body: Column(
children: [
ChatSessionAvatar(
key: Key('one'),
session: {'id': 's1', 'title': 'One'},
running: true,
awaitingOperator: 2,
),
ChatSessionAvatar(
key: Key('two'),
session: {'id': 's2', 'title': 'Two'},
),
ChatCompactAvatar(
key: Key('two-compact'),
sessionId: 's2',
title: 'Two',
color: Colors.blue,
active: false,
visible: false,
running: false,
backgroundTasks: 0,
unread: false,
),
],
),
),
),
);
Finder _badgeOf(String avatar) => find.descendant(
of: find.byKey(Key(avatar)),
matching: find.byKey(_speaking),
);
Future<void> _read(WidgetTester tester, SpeechController speech, String chat) =>
tester.runAsync(() => speech.read(speechMessageId(chat, 'r1'), ['Hi.']));
void main() {
testWidgets('the reading chat wears the speaking badge, and it moves', (
tester,
) async {
final speech = installFakeSpeech(FakeSpeechEngine());
await tester.pumpWidget(_avatars());
expect(find.byKey(_speaking), findsNothing);
await _read(tester, speech, 's1');
await tester.pump();
expect(_badgeOf('one'), findsOneWidget);
expect(_badgeOf('two'), findsNothing);
expect(_badgeOf('two-compact'), findsNothing);
// Speaking outranks the operator wait and the running spinner.
expect(find.byKey(const Key('chat-row-working')), findsNothing);
expect(find.byType(ChatAwaitingBadge), findsNothing);
await _read(tester, speech, 's2');
await tester.pump();
expect(_badgeOf('one'), findsNothing);
expect(_badgeOf('two'), findsOneWidget);
expect(_badgeOf('two-compact'), findsOneWidget);
expect(find.byKey(const Key('chat-speaking-wave')), findsNWidgets(2));
await tester.runAsync(speech.stop);
await tester.pump();
expect(find.byKey(_speaking), findsNothing);
});
testWidgets('a Settings voice sample marks no chat', (tester) async {
final speech = installFakeSpeech(FakeSpeechEngine());
await tester.pumpWidget(_avatars());
await tester.runAsync(speech.sample);
await tester.pump();
expect(speech.state, SpeechState.speaking);
expect(find.byKey(_speaking), findsNothing);
});
testWidgets('reduced motion shows a still speaker', (tester) async {
final speech = installFakeSpeech(FakeSpeechEngine());
await tester.pumpWidget(_avatars(reduceMotion: true));
// The first chat's spinner gives way to the speaking badge.
await _read(tester, speech, 's1');
await tester.pump();
expect(_badgeOf('one'), findsOneWidget);
expect(find.byKey(const Key('chat-speaking-still')), findsOneWidget);
expect(find.byKey(const Key('chat-speaking-wave')), findsNothing);
// Nothing ticks: settling finishes with the badge still shown.
await tester.pumpAndSettle();
expect(_badgeOf('one'), findsOneWidget);
});
}