Files
museion/MuseionTests/HermesConfigRailTests.swift
AlexKucera a8b158ca26
CI / ci (push) Canceled after 0s
refactor(profiles): one layout + config-set rail
#182: ProfileLayout owns the profile root default and every per-profile
path (dir, .env, configured marker, gateway pid, skills dir) — the five
scattered profile-root literals are gone; the provisioner, the settings
writer, and backup/restore derive paths only through it, pinned once in
ProfileLayoutTests. RESTORE.md prose and the ps-scan grep keep their
literal text deliberately (doc text and argv matching, not paths).

HermesConfigRail owns the config set/unset contract once: argv shape
(+--force), provider-first hoisting (switching providers clears a stale
inherited base_url — a caller can no longer order it wrong), ✓-line
verification, unset-idempotence. The provisioner seeds provider/default/
max_tokens; the writer edits provider/default/effort/base_url — each
keeps its own key list, only the mechanism and ordering rule unify. The
rail's Failure carries key/profile/excerpt for both error surfaces.

Audit hardenings folded in: a leading-dash value refuses before any
spawn (free-text model/endpoint fields cannot reach the child CLI as
options), whitespace-only endpoints read as empty at both writer gates,
the deliberate 500-char excerpt cap is pinned not just commented, and
the failed-unset branch is pinned (the fake hermes grew a
config-unset-fails knob).

Full suite 684 passed / 0 failed; dash + whitespace pins flip-verified.

Closes #182
2026-11-10 11:32:47 +01:00

140 lines
5.2 KiB
Swift

//
// 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 <profile> 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)
}
}