import 'dart:async'; import 'package:flutter_test/flutter_test.dart'; import 'package:lumen/services/ambience.dart'; class _Output implements AmbienceOutput { Soundscape? scene; double gain = 0; bool playing = false, disposed = false, fail = false; Completer? loading; @override Future load(Soundscape value) async { if (fail) throw StateError('No decoder'); await loading?.future; scene = value; } @override Future volume(double value) async => gain = value; @override Future play() async => playing = true; @override Future stop() async => playing = false; @override Future dispose() async => disposed = true; } void main() { test('bridge, harbour and tower have their own atmosphere', () { expect(soundscapeFor(6), Soundscape.bridge); for (final i in [4, 5, 7]) { expect(soundscapeFor(i), Soundscape.harbour); } for (final i in [8, 9, 10]) { expect(soundscapeFor(i), Soundscape.tower); } expect(soundscapeFor(0), Soundscape.cottage); expect(soundscapeFor(11), Soundscape.cottage); }); test( 'voice ducks atmosphere, pause restores it, mute and background stop it', () async { final output = _Output(); final ambience = Ambience(output: output); void update({ bool voice = false, bool enabled = true, bool foreground = true, }) => ambience.update( scene: Soundscape.bridge, enabled: enabled, foreground: foreground, narrating: voice, volume: .7, ); update(); await ambience.settled; expect(output.playing, true); expect(output.gain, closeTo(.28, .0001)); update(voice: true); await ambience.settled; expect(output.gain, closeTo(.07, .0001)); update(); await ambience.settled; expect(output.gain, closeTo(.28, .0001)); update(enabled: false); await ambience.settled; expect(output.playing, false); update(); await ambience.settled; update(foreground: false); await ambience.settled; expect(output.playing, false); ambience.dispose(); await Future.delayed(Duration.zero); expect(output.disposed, true); }, ); test('a mute while loading cannot start a stale sound', () async { final output = _Output()..loading = Completer(); final ambience = Ambience(output: output); ambience.update( scene: Soundscape.bridge, enabled: true, foreground: true, narrating: false, volume: 1, ); await Future.delayed(Duration.zero); ambience.update( scene: Soundscape.bridge, enabled: false, foreground: true, narrating: false, volume: 1, ); output.loading!.complete(); await ambience.settled; expect(output.playing, false); expect(output.gain, 0); ambience.dispose(); }); test( 'failed atmosphere stays isolated and can recover after toggling', () async { final output = _Output()..fail = true; final ambience = Ambience(output: output); void update(bool enabled) => ambience.update( scene: Soundscape.bridge, enabled: enabled, foreground: true, narrating: false, volume: .5, ); update(true); await ambience.settled; expect(ambience.error, isNotEmpty); expect(output.playing, false); update(false); await ambience.settled; output.fail = false; update(true); await ambience.settled; expect(ambience.error, isEmpty); expect(output.playing, true); ambience.dispose(); }, ); }