import 'dart:async'; import 'package:audio_session/audio_session.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; import 'package:just_audio/just_audio.dart'; import 'package:lumen/domain/book.dart'; import 'package:lumen/domain/game.dart'; import 'package:lumen/main.dart'; import 'package:lumen/services/progress.dart'; void main() { final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets('extended readings decode and mix with bridge atmosphere', ( tester, ) async { expect(const bool.fromEnvironment('QA_TEST'), true); final book = await Book.load(); final session = await AudioSession.instance; await session.configure(const AudioSessionConfiguration.speech()); final probe = AudioPlayer(); // Exercise the newly added audio, beyond the old recording's endpoint. for (final track in book.tracks.values.where( (t) => t['continuation'] != null, )) { await probe.setAsset('assets/audio/${track['file']}'); expect(probe.duration!.inMilliseconds, closeTo(track['durationMs'], 5)); final start = Duration(milliseconds: track['cues'].last['startMs']); await probe.seek(start); await probe.setVolume(.3); unawaited(probe.play()); await Future.delayed(const Duration(milliseconds: 350)); expect(probe.position, greaterThanOrEqualTo(start)); expect(probe.processingState, ProcessingState.ready); await probe.stop(); } await probe.dispose(); final g = Game(book, Progress.memory({'story_access_level': 11})); g.briefing(6); await tester.pumpWidget(LumenApp(game: g, adsEnabled: false)); await tester.pumpAndSettle(); await tester.ensureVisible(find.text('Play this reading')); await tester.pumpAndSettle(); await tester.tap(find.text('Play this reading')); await Future.delayed(const Duration(seconds: 2)); await tester.pump(); expect(find.text('Pause recording'), findsOneWidget); // The host captures AudioFlinger during this interval to verify two real tracks. debugPrint('MIXED_AUDIO_READY: bridge water and narration'); await Future.delayed(const Duration(seconds: 12)); await tester.tap(find.text('Pause recording')); await tester.pumpAndSettle(); expect(find.text('Resume recording'), findsOneWidget); await tester.tap(find.text('Sound & atmosphere')); await tester.pumpAndSettle(); await tester.tap(find.byType(SwitchListTile)); await tester.pumpAndSettle(); expect(g.progress.flag('ambience_enabled'), false); await tester.tap(find.text('Keep going')); await tester.pumpAndSettle(); await tester.tap(find.text('Stop')); await tester.pumpAndSettle(); // A single Play control carries Mara's unchanged diary into the narrator's // new continuation and updates the speaker label at the voice boundary. g.showStory(1); await tester.pumpAndSettle(); await tester.ensureVisible(find.text('Play this reading')); await tester.pumpAndSettle(); await tester.tap(find.text('Play this reading')); final originalMs = (book.tracks['cottage.window']['originalFrames'] as int) * 1000 ~/ 24000; await Future.delayed(Duration(milliseconds: originalMs + 1800)); await tester.pump(); expect(find.text('Narrator ยท recorded story'), findsOneWidget); await tester.tap(find.text('Pause recording')); await tester.pumpAndSettle(); expect(tester.takeException(), isNull); await binding.convertFlutterSurfaceToImage(); await tester.pump(); await binding.takeScreenshot('expanded-narration-continuation'); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); }); }