Files
NimPulse/tests/test_config.nim
T

121 lines
3.9 KiB
Nim

## test_config.nim
## ================
## Tests d'intégration de config.nim : valeurs par défaut, résolution de profil,
## round-trip JSON (save/load), tolérance aux fichiers corrompus, et
## démarrage automatique via la clé de registre Run.
import std/[unittest, os]
import ../src/config
# Redirige APPDATA vers un répertoire temporaire pour isoler les fichiers.
let tmpApd = getTempDir() / "nimpulse_test_apd"
if dirExists(tmpApd):
removeDir(tmpApd)
putEnv("APPDATA", tmpApd)
suite "config":
test "defaultConfig has expected values":
let c = defaultConfig()
check c.workMin == 25
check c.shortBreakMin == 5
check c.longBreakMin == 15
check c.cyclesBeforeLongBreak == 4
check c.soundEnabled == true
check c.doNotDisturb == false
check c.autoStart == false
check c.activeProfile == "Classique"
check c.floatingWindow == false
check c.profiles.len == 3
check c.healthReminders.len == 5
test "getActiveProfile returns the matching profile":
let c = defaultConfig()
let p = c.getActiveProfile()
check p.name == "Classique"
check p.workMin == 25
check p.cycles == 4
test "getActiveProfile falls back to defaults for an unknown name":
var c = defaultConfig()
c.activeProfile = "N'existe pas"
let p = c.getActiveProfile()
check p.name == "Défaut"
check p.workMin == c.workMin
test "saveConfig then loadConfig round-trips (including accents)":
var c = defaultConfig()
c.workMin = 42
c.doNotDisturb = true
c.activeProfile = "Sprint"
saveConfig(c)
let l = loadConfig()
check l.workMin == 42
check l.doNotDisturb == true
check l.activeProfile == "Sprint"
check l.profiles.len == 3
check l.healthReminders.len == 5
# UTF-8 (accents) doit survivre à l'aller-retour JSON.
check l.healthReminders[0].text == "Lève-toi et marche 2 minutes"
test "loadConfig returns defaults on a corrupt file":
ensureConfigDir()
writeFile(configPath(), "{ pas du json valide")
let l = loadConfig()
check l.workMin == 25
check l.profiles.len == 3
test "new fields (lang, dailyGoal, hotkey, sound) round-trip":
var c = defaultConfig()
c.lang = "en"
c.dailyGoal = 8
c.hotkeyModifiers = 0x0003'i32
c.hotkeyVk = 0x50'i32
c.soundName = "SystemExclamation"
c.floatingWindow = true
saveConfig(c)
let l = loadConfig()
check l.lang == "en"
check l.dailyGoal == 8
check l.hotkeyModifiers == 0x0003'i32
check l.hotkeyVk == 0x50'i32
check l.soundName == "SystemExclamation"
check l.floatingWindow == true
test "English defaults localize profiles and reminders":
let c = defaultConfig(langEn)
check c.activeProfile == "Classic"
check c.profiles[0].name == "Classic"
check c.healthReminders[0].text == "Stand up and walk for 2 minutes"
test "loadConfig keeps defaults for missing new fields":
# Un ancien fichier sans les nouveaux champs doit rester lisible.
ensureConfigDir()
writeFile(configPath(), "{\"workMin\": 30}")
let l = loadConfig()
check l.workMin == 30
check l.lang == "auto"
check l.dailyGoal == 0
test "configDir returns exe dir in portable mode":
let exeDir = getAppFilename().parentDir
let marker = exeDir / "portable"
writeFile(marker, "")
check configDir() == exeDir
removeFile(marker)
check configDir() == (getEnv("APPDATA") / "NimPulse")
test "acquireSingleInstance detects a second lock":
check not acquireSingleInstance("NimPulse_Test_Mutex_0.2.0")
check acquireSingleInstance("NimPulse_Test_Mutex_0.2.0")
suite "autostart registry":
test "setAutostart toggles the Run key and cleans up":
let before = isAutostartEnabled()
check setAutostart(true)
check isAutostartEnabled()
check setAutostart(false)
check not isAutostartEnabled()
# Restaure l'état d'origine pour ne rien laisser traîner.
if before:
discard setAutostart(true)