import Foundation import HelderCore import HelderServices import Testing @Suite struct ConfigServiceTests { @Test func aFreshProjectGetsTheDefaultsFileAndTheDefaults() async throws { let dir = try TempDirectory() let config = await ConfigService(root: dir.root).resolve() #expect(config == .defaults) let written = try Data(contentsOf: dir.url(".helder/config.default.json")) #expect(try JSONDecoder().decode(HelderConfig.self, from: written) == .defaults) } @Test func aSparseConfigMergesOverTheDefaults() async throws { let dir = try TempDirectory() try dir.write(".helder/config.json", #"{"ai":{"command":"claude --model opus"},"editor":{"tabSize":2}}"#) let config = await ConfigService(root: dir.root).resolve() #expect(config.ai.command == "claude --model opus") #expect(config.ai.autoLaunch) #expect(config.editor.tabSize == 2) #expect(!config.editor.autoSave) #expect(config.git.confirmDiscard) } @Test func theDefaultsFileIsRewrittenWithTheBuiltInValues() async throws { let dir = try TempDirectory() try dir.write(".helder/config.json", #"{"ai":{"command":"x"}}"#) try dir.write(".helder/config.default.json", #"{"ai":{"command":"stale"}}"#) _ = await ConfigService(root: dir.root).resolve() let written = try JSONDecoder().decode(HelderConfig.self, from: Data(contentsOf: dir.url(".helder/config.default.json"))) #expect(written.ai.command == "claude") } @Test func currentReturnsTheLastResolvedConfig() async throws { let dir = try TempDirectory() let service = ConfigService(root: dir.root) #expect(await service.current() == .defaults) try dir.write(".helder/config.json", #"{"editor":{"tabSize":8}}"#) await service.resolve() #expect(await service.current().editor.tabSize == 8) } @Test func anUnwritableProjectStillYieldsTheDefaults() async throws { let dir = try TempDirectory() try dir.write(".helder", "a file where the folder should be") #expect(await ConfigService(root: dir.root).resolve() == .defaults) } }