feat: add safe optional audio playback

This commit is contained in:
goose
2026-10-03 23:55:34 -04:00
parent d3c417fb91
commit efcaef181b
11 changed files with 339 additions and 54 deletions
+50
View File
@@ -0,0 +1,50 @@
extends GutTest
const GameScene = preload("res://scenes/Game.tscn")
const AudioImportManifest = preload("res://scripts/infrastructure/audio_import_manifest.gd")
var game: Game
func before_each() -> void:
game = GameScene.instantiate()
add_child_autofree(game)
await get_tree().process_frame
func test_options_control_audio_buses_and_imported_manifest_playback_without_external_files() -> void:
var audio = game.get_node("AudioLibrary") as AudioLibrary
var manifest := AudioImportManifest.new()
assert_true(manifest.register_ambience(&"combat", _silent_stream()))
assert_true(manifest.register_sfx(&"attack_given", _silent_stream()))
audio.set_manifest(manifest)
await _dispatch_key(KEY_O)
var options_panel := game.get_node("HUD/OptionsPanel") as OptionsPanel
assert_true(options_panel.visible)
assert_string_contains((options_panel.get_node("Body") as RichTextLabel).get_parsed_text(), "Ambiance")
await _dispatch_key(KEY_A)
await _dispatch_key(KEY_S)
assert_eq(audio.get_ambience_volume_percent(), 90)
assert_eq(audio.get_sfx_volume_percent(), 90)
audio.set_ambience_context("combat")
audio.play_sfx("attack_given")
assert_true(audio.has_playable_ambience())
assert_true(audio.has_playable_sfx())
assert_eq(audio.get_ambience_player().bus, AudioLibrary.AMBIENCE_BUS)
assert_eq(audio.get_sfx_player().bus, AudioLibrary.SFX_BUS)
func _dispatch_key(keycode: Key) -> void:
var event := InputEventKey.new()
event.keycode = keycode
event.physical_keycode = keycode
event.pressed = true
game._unhandled_input(event)
await get_tree().process_frame
func _silent_stream() -> AudioStreamWAV:
var stream := AudioStreamWAV.new()
stream.format = AudioStreamWAV.FORMAT_16_BITS
stream.mix_rate = 44100
stream.stereo = false
stream.data = PackedByteArray([0, 0, 0, 0])
return stream
+1
View File
@@ -0,0 +1 @@
uid://b44wxq45xtuwp
+6 -11
View File
@@ -12,7 +12,7 @@ func before_each() -> void:
DirAccess.make_dir_recursive_absolute(empty_audio_home)
OS.set_environment("DCW_AUDIO_HOME", empty_audio_home)
game = GameScene.instantiate()
# Replace the scene library before ready to isolate both production search roots.
# Replace the scene library before ready to isolate the no-audio fallback.
var scene_audio := game.get_node("AudioLibrary")
var isolated_audio := AudioLibrary.new(empty_audio_home)
isolated_audio.name = "AudioLibrary"
@@ -43,18 +43,14 @@ func test_real_game_resolves_combat_with_empty_audio_home() -> void:
assert_false(audio.has_playable_ambience())
assert_false(audio.has_playable_sfx())
func test_external_nominal_and_invalid_wavs_do_not_affect_combat() -> void:
func test_external_wavs_do_not_affect_combat_or_become_playable() -> void:
var audio = game.get_node("AudioLibrary")
var nominal_path := empty_audio_home.path_join("audio/combat/silence.wav")
var nominal_path := empty_audio_home.path_join("audio/combat/external.wav")
var invalid_path := empty_audio_home.path_join("sfx/attack_given/truncated.wav")
DirAccess.make_dir_recursive_absolute(nominal_path.get_base_dir())
DirAccess.make_dir_recursive_absolute(invalid_path.get_base_dir())
var nominal_file := FileAccess.open(nominal_path, FileAccess.WRITE)
nominal_file.store_buffer(PackedByteArray([
82, 73, 70, 70, 36, 0, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32,
16, 0, 0, 0, 1, 0, 1, 0, 68, 172, 0, 0, 136, 88, 1, 0,
2, 0, 16, 0, 100, 97, 116, 97, 0, 0, 0, 0,
]))
nominal_file.store_string("untrusted external WAV")
nominal_file.close()
var invalid_file := FileAccess.open(invalid_path, FileAccess.WRITE)
invalid_file.store_string("truncated WAV")
@@ -69,10 +65,9 @@ func test_external_nominal_and_invalid_wavs_do_not_affect_combat() -> void:
assert_eq(first_melee[0].kind, &"melee")
assert_eq(lethal_melee[1].kind, &"enemy_defeated")
assert_false(game.get_game_state().enemies.has(&"goblin"))
# Discovery remains diagnostic-only after combat; no external stream is playable.
audio.set_ambience_context("combat")
audio.play_sfx("attack_given")
assert_eq(audio.get_last_ambience_path(), nominal_path)
assert_eq(audio.get_last_sfx_path(), invalid_path)
assert_eq(audio.get_last_ambience_path(), "")
assert_eq(audio.get_last_sfx_path(), "")
assert_false(audio.has_playable_ambience())
assert_false(audio.has_playable_sfx())