refactor(settings): guard all Settings fields vs null; tidy import + test

Guard colorMode and glyphMode against null in the Settings compact
constructor (alongside the existing musicLevel guard), so a hand-edited
settings.json with a missing field no longer NPEs in SettingsMenu.
Remove the unused MusicLevel import from SettingsMenu. Assert that the
defaulted musicLevel survives a round-trip in SettingsStoreTest.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-01 23:17:26 +02:00
parent c6ed199f7e
commit 7380dcae41
3 changed files with 7 additions and 1 deletions

View File

@@ -2,7 +2,6 @@ package thb.jeanluc.adventure.menu;
import thb.jeanluc.adventure.io.ConsoleIO;
import thb.jeanluc.adventure.io.GameIO;
import thb.jeanluc.adventure.io.MusicLevel;
import thb.jeanluc.adventure.save.Settings;
import thb.jeanluc.adventure.save.SettingsStore;

View File

@@ -7,6 +7,12 @@ import thb.jeanluc.adventure.io.MusicLevel;
public record Settings(ConsoleIO.ColorMode colorMode, ConsoleIO.GlyphMode glyphMode, MusicLevel musicLevel) {
public Settings {
if (colorMode == null) {
colorMode = ConsoleIO.ColorMode.AUTO;
}
if (glyphMode == null) {
glyphMode = ConsoleIO.GlyphMode.UNICODE;
}
if (musicLevel == null) {
musicLevel = MusicLevel.MEDIUM; // backward-compat: old saves lack this field
}

View File

@@ -20,6 +20,7 @@ class SettingsStoreTest {
Settings loaded = store.load();
assertThat(loaded.colorMode()).isEqualTo(ConsoleIO.ColorMode.ON);
assertThat(loaded.glyphMode()).isEqualTo(ConsoleIO.GlyphMode.ASCII);
assertThat(loaded.musicLevel()).isEqualTo(MusicLevel.MEDIUM);
}
@Test