// // HermesConfigRailTests.swift // MuseionTests // // #182 — the config-set CLI contract's ONE pinning suite, over the shared // fake-hermes double (makeFakeHermes, ProfileProvisionerTests.swift). The // callers' suites pin only their own key lists. // // Scenario: The config-set wire is owned once — From user story #182 // Given the provisioner and the settings writer both write config keys // through `hermes -p config set` // When either hands the rail its key list // Then every write rides the exact argv (with --force where asked), is // verified against the `✓ Set` line (not exit code alone), executes // provider-first (switching providers clears stale inherited values), // and a failed write surfaces key + profile + CLI excerpt. // import Foundation @testable import Museion import Testing @Suite("HermesConfigRailTests") struct HermesConfigRailTests { @Test func `set argv rides the exact wire with force`() throws { let binDir = provTempDir("rail") let binary = makeFakeHermes(at: binDir, mode: "ok") try HermesConfigRail.apply( [ .set(key: "model.provider", value: "anthropic", force: false), .set(key: "model.extra_body.max_tokens", value: "24000", force: true), ], profile: "museion-pass1", binary: binary, timeout: 5, ) #expect(fakeHermesLog(binDir, "config-set-museion-pass1") == [ "-p museion-pass1 config set model.provider anthropic", "-p museion-pass1 config set model.extra_body.max_tokens 24000 --force", ]) } @Test func `provider goes first whatever the caller s list order`() throws { // model.provider clears a stale inherited base_url — the rail owns // the rule; a caller can't order it wrong. Relative order holds. let binDir = provTempDir("rail-order") let binary = makeFakeHermes(at: binDir, mode: "ok") try HermesConfigRail.apply( [ .set(key: "model.base_url", value: "https://x", force: false), .unset(key: "model.reasoning_effort"), .set(key: "model.default", value: "m", force: false), .set(key: "model.provider", value: "zai", force: false), ], profile: "museion", binary: binary, timeout: 5, ) #expect(fakeHermesLog(binDir, "config-set-museion") == [ "-p museion config set model.provider zai", "-p museion config set model.base_url https://x", "-p museion config set model.default m", ]) #expect(fakeHermesLog(binDir, "config-unset-museion") == [ "-p museion config unset model.reasoning_effort", ]) } @Test func `unset of an absent key is idempotent success`() throws { let binDir = provTempDir("rail-unset") let binary = makeFakeHermes(at: binDir, mode: "ok") try HermesConfigRail.apply([.unset(key: "model.reasoning_effort")], profile: "museion", binary: binary, timeout: 5) } @Test func `a failed set throws naming key and profile with the cli excerpt`() { let binDir = provTempDir("rail-fail") try? "".write(toFile: binDir + "/config-set-fails", atomically: true, encoding: .utf8) let binary = makeFakeHermes(at: binDir, mode: "ok") do { try HermesConfigRail.apply( [.set(key: "model.default", value: "m", force: false)], profile: "museion-pass2", binary: binary, timeout: 5, ) Issue.record("expected a throw") } catch { let failure = error as? HermesConfigRail.Failure #expect(failure?.key == "model.default") #expect(failure?.profile == "museion-pass2") #expect(failure?.excerpt == "Error: config set exploded") #expect(failure?.errorDescription == "couldn't set model.default on museion-pass2: Error: config set exploded") } } @Test func `a failed unset throws the structured failure`() { // The fourth branch: neither ✓ Unset nor the not-set line. let binDir = provTempDir("rail-unset-fail") try? "".write(toFile: binDir + "/config-unset-fails", atomically: true, encoding: .utf8) let binary = makeFakeHermes(at: binDir, mode: "ok") do { try HermesConfigRail.apply([.unset(key: "model.reasoning_effort")], profile: "museion", binary: binary, timeout: 5) Issue.record("expected a throw") } catch { let failure = error as? HermesConfigRail.Failure #expect(failure?.verb == "unset") #expect(failure?.key == "model.reasoning_effort") #expect(failure?.excerpt == "Error: config unset exploded") } } @Test func `a value that looks like an option is refused before any spawn`() { // Free-text fields (model IDs, endpoints) feed the rail; a leading // `-` is an option to the child CLI, never a value. let binDir = provTempDir("rail-dash") let binary = makeFakeHermes(at: binDir, mode: "ok") do { try HermesConfigRail.apply( [.set(key: "model.default", value: "--force", force: false)], profile: "museion", binary: binary, timeout: 5, ) Issue.record("expected a throw") } catch { let failure = error as? HermesConfigRail.Failure #expect(failure?.key == "model.default") #expect(failure?.excerpt == "value starts with '-' — not written") } #expect(fakeHermesLog(binDir, "config-set-museion").isEmpty) // nothing reached the CLI } @Test func `excerpt caps at 500 chars`() { // The #180 deliberate divergence (writer's old 300 is gone): pinned, // not just commented. let output = EngineProbe.ProcessOutput(exitCode: 1, stdout: String(repeating: "x", count: 900)) #expect(HermesConfigRail.excerpt(output).count == 500) } }