From 66e43830be803a04462d8d5e5253a698d74197df Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 1 Jun 2026 22:55:27 +0200 Subject: [PATCH] fix(io): idempotent MusicController.level; assert gain forwarding + shutdown Co-Authored-By: Claude Sonnet 4.6 --- .../jeanluc/adventure/io/MusicController.java | 3 +++ .../adventure/io/MusicControllerTest.java | 21 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/io/MusicController.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/io/MusicController.java index ee572b0..3a4b1f4 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/io/MusicController.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/io/MusicController.java @@ -37,6 +37,9 @@ public final class MusicController { /** Applies a new music level (volume / off). */ public void level(MusicLevel newLevel) { + if (newLevel == this.level) { + return; + } this.level = newLevel; if (!newLevel.isOn()) { backend.fadeOut(); diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/io/MusicControllerTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/io/MusicControllerTest.java index 0b931fc..9238b31 100644 --- a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/io/MusicControllerTest.java +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/io/MusicControllerTest.java @@ -12,7 +12,8 @@ class MusicControllerTest { /** Records backend calls for assertions. */ private static final class FakeBackend implements MusicBackend { final List calls = new ArrayList<>(); - public void fadeTo(String track, float gainDb) { calls.add("fadeTo:" + track); } + float lastGain = Float.NaN; + public void fadeTo(String track, float gainDb) { calls.add("fadeTo:" + track); lastGain = gainDb; } public void fadeOut() { calls.add("fadeOut"); } public void setGainDb(float gainDb) { calls.add("setGain:" + gainDb); } public void shutdown() { calls.add("shutdown"); } @@ -84,4 +85,22 @@ class MusicControllerTest { c.room("a"); // current was reset → plays again assertThat(b.calls).containsExactly("fadeTo:a"); } + + @Test + void forwardsTheCurrentLevelGain() { + FakeBackend b = new FakeBackend(); + MusicController c = new MusicController(b); + c.room("a"); + assertThat(b.lastGain).isEqualTo(MusicLevel.MEDIUM.gainDb()); + c.level(MusicLevel.HIGH); + c.room("b"); + assertThat(b.lastGain).isEqualTo(MusicLevel.HIGH.gainDb()); + } + + @Test + void shutdownDelegatesToBackend() { + FakeBackend b = new FakeBackend(); + new MusicController(b).shutdown(); + assertThat(b.calls).containsExactly("shutdown"); + } }