import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:integration_test/integration_test.dart'; import 'package:lumen/domain/game.dart'; import 'package:lumen/domain/library.dart'; import 'package:lumen/main.dart'; import 'package:lumen/services/ambience.dart'; import 'package:lumen/services/narrator.dart'; import 'package:lumen/services/progress.dart'; void main() { final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized(); testWidgets( 'two drama scenes play with atmosphere and live character labels', (tester) async { expect(const bool.fromEnvironment('QA_TEST'), true); final library = await StoryLibrary.load(); final book = await library.open('midnight-invitation'); final game = Game( book, Progress.memory({'story_access_level': 11, 'language': 'en'}), library: library, ); final voice = Narrator(book.tracks); final ambience = Ambience(); game.showStory( book.pages.indexWhere((p) => p.id == 'midnight.feather.memory'), ); await tester.pumpWidget( MaterialApp( debugShowCheckedModeBanner: false, home: Notebook( game: game, adsEnabled: false, narrator: voice, ambience: ambience, ), ), ); await tester.pumpAndSettle(); await voice.stop(); await ambience.settled; expect(ambience.error, isEmpty); await binding.convertFlutterSurfaceToImage(); Future tap(String label) async { await tester.ensureVisible(find.text(label)); await tester.pumpAndSettle(); await tester.tap(find.text(label)); await tester.pumpAndSettle(); } Future reach(int ms) async { final deadline = DateTime.now().add(const Duration(seconds: 65)); while (voice.position.inMilliseconds < ms && DateTime.now().isBefore(deadline)) { await Future.delayed(const Duration(milliseconds: 180)); await tester.pump(); expect(voice.error, isEmpty); } expect(voice.position.inMilliseconds, greaterThanOrEqualTo(ms)); } await tap('Play this reading'); debugPrint('DRAMA_MIX_READY: threshold atmosphere and cast reading'); final doorCues = book.tracks['midnight.feather.memory']['cues'] as List; final firstIris = doorCues.firstWhere((c) => c['speaker'] == 'Iris')['startMs'] as int; for (final role in ['Iris', 'Elias']) { final cue = (book.tracks['midnight.feather.memory']['cues'] as List) .firstWhere( (c) => c['speaker'] == role && c['startMs'] > (role == 'Elias' ? firstIris : 0), ); await reach(cue['startMs'] + 150); expect(voice.speaker, role); expect(find.text('$role · recorded story'), findsOneWidget); expect(ambience.error, isEmpty); } await tap('Pause recording'); final paused = voice.position; await Future.delayed(const Duration(milliseconds: 700)); expect((voice.position - paused).inMilliseconds.abs(), lessThan(150)); await tap('Resume recording'); await reach(paused.inMilliseconds + 500); await binding.takeScreenshot('drama-threshold-playing'); game.showStory( book.pages.indexWhere((p) => p.id == 'midnight.candle.entry'), ); await tester.pumpAndSettle(); await voice.stop(); await ambience.settled; expect(ambience.error, isEmpty); expect(voice.page, isEmpty); await tap('Play this reading'); final iris = (book.tracks['midnight.candle.entry']['cues'] as List) .firstWhere((c) => c['speaker'] == 'Iris'); await reach(iris['startMs'] + 150); expect(find.text('Iris · recorded story'), findsOneWidget); await tap('Pause recording'); await tap('Sound & atmosphere'); await tester.tap(find.byType(SwitchListTile)); await tester.pumpAndSettle(); await ambience.settled; expect(game.progress.flag('ambience_enabled'), false); await tap('Keep going'); await binding.takeScreenshot('drama-kitchen-paused'); game.home(); await tester.pumpAndSettle(); await ambience.settled; expect(voice.page, isEmpty); expect(ambience.error, isEmpty); expect(tester.takeException(), isNull); await tester.pumpWidget(const SizedBox()); await tester.pumpAndSettle(); }, ); }