// // ProfileSurfaceTests.swift // MuseionTests // // #134 (Profiles S1) — the per-surface topology over the fake-binary // harness shared with ProfileProvisionerTests.swift. // // Scenario: Surface→profile registry — Given the app's three LLM surfaces // (Pass 1, Pass 2, Chat; Sessions rides Chat), when code asks which Hermes // profile a surface runs on, then it answers museion-pass1 / museion-pass2 / // museion (adopted in place), with the #133 per-surface skill allocation and // the #132 sonnet defaults + 24k budget as constants. // // Scenario: First-run provisions all three idempotently — Given a machine // with hermes + keys that passes the binary-only gate, when the onPass // provisioner runs, then museion is adopted (never re-created, config // untouched), museion-pass1/2 are created fresh with the sonnet model block // and 24k budget written via `hermes -p config set`, the skills // master is seeded once and symlinked into each profile per allocation, // museion-tools registers per profile — and a re-pass changes nothing. // // Scenario: A failing provision names its profile (#48 pattern) — the // failure outcome and card carry the failing profile's name. // import Foundation @testable import Museion import Testing /// The full 12-skill shipped set as fixtures (names + minimal bodies). let shippedSkillNames = [ "serif-writing", "deslop", "demonstrator", "rhetoric", "prose-craft", "micro-drill", "owned-insight", "pass-persona", "session-persona", "fiction-craft", "reader-psychology", "titles-and-hooks", ] func makeShippedPayload(at dir: String) -> String { makePayload(at: dir, skills: shippedSkillNames.map { SeedSkill(name: $0, version: "1.0", body: "\($0) body") }) } /// Reads a fake-hermes log file as trimmed lines. func fakeHermesLog(_ binDir: String, _ file: String) -> [String] { ((try? String(contentsOfFile: (binDir as NSString).appendingPathComponent(file), encoding: .utf8)) ?? "") .split(separator: "\n").map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } } @Suite("ProfileSurfaceTests") struct ProfileSurfaceTests { @Test func `registry maps surfaces to profiles with allocation and defaults`() { // Chat first — the resident surface provisions before any // pass-profile failure can stop the loop. #expect(ProfileSurface.allCases.map(\.profileName) == ["museion", "museion-pass1", "museion-pass2"]) #expect(ProfileSurface(passKind: .pass1) == .pass1) #expect(ProfileSurface(passKind: .pass2) == .pass2) // #133 allocation table: P1 10 · P2 9 · Chat 11 (#58 adds owned-insight // to all three surfaces). #expect(ProfileSurface.pass1.allocatedSkills.count == 10) #expect(ProfileSurface.pass2.allocatedSkills.count == 9) #expect(ProfileSurface.chat.allocatedSkills.count == 11) // The persona pair never co-occurs (the manner co-trigger conflict). #expect(!ProfileSurface.chat.allocatedSkills.contains("pass-persona")) #expect(!ProfileSurface.pass1.allocatedSkills.contains("session-persona")) #expect(!ProfileSurface.pass2.allocatedSkills.contains("session-persona")) // #132 defaults: sonnet on all three, effort unset, 24k budget. #expect(ProfileSurface.defaultModelProvider == "anthropic") #expect(ProfileSurface.defaultModelID == "claude-sonnet-5-5") #expect(ProfileSurface.outputBudgetTokens == 24000) } @Test func `every allocated skill ships in the bundled payload`() { let payload = ProfileProvisioner.defaultPayloadRoot() guard FileManager.default.fileExists(atPath: payload) else { Issue.record("bundled writing-skills payload missing from app resources") return } let manifest = (try? String( contentsOfFile: (payload as NSString).appendingPathComponent("seed-manifest.txt"), encoding: .utf8, )) ?? "" let listed = Set(manifest.split(separator: "\n").map { $0.trimmingCharacters(in: .whitespaces) }) for surface in ProfileSurface.allCases { for skill in surface.allocatedSkills where !listed.contains(skill) { Issue.record("\(surface.profileName) allocates '\(skill)' — not in the bundled manifest") } } } @Test func `fresh pass provisions all three profiles and config-sets defaults on each`() { let binDir = provTempDir("x3") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: makeShippedPayload(at: provTempDir("payload")), masterRoot: master, serverBinaryPath: "/fake/Museion", ) // 12 master copies + 30 allocation links (11 + 10 + 9). #expect(outcome == .seeded(copied: 12 + 11 + 10 + 9, present: 0)) for name in ["museion", "museion-pass1", "museion-pass2"] { #expect(FileManager.default.fileExists(atPath: (profileRoot as NSString).appendingPathComponent(name))) } // Exact config-set command lines per profile (#132 defaults via the // #128 CLI rail — provider first, model, then the budget --force). for profile in ["museion", "museion-pass1", "museion-pass2"] { #expect(fakeHermesLog(binDir, "config-set-\(profile)") == [ "-p \(profile) config set model.provider anthropic", "-p \(profile) config set model.default claude-sonnet-5-5", "-p \(profile) config set model.extra_body.max_tokens 24000 --force", ]) } } @Test func `master holds the shipped set once and each profile symlinks its allocation`() { let binDir = provTempDir("alloc") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") _ = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: makeShippedPayload(at: provTempDir("payload")), masterRoot: master, serverBinaryPath: "/fake/Museion", ) let fileManager = FileManager.default // ONE master copy: every shipped skill as a real dir, nothing else. let masterEntries = (try? fileManager.contentsOfDirectory(atPath: master)) ?? [] #expect(Set(masterEntries) == Set(shippedSkillNames)) // Each profile's skills dir is exactly its allocation, every entry a // symlink resolving into the master. for surface in ProfileSurface.allCases { let skillsDir = (profileRoot as NSString) .appendingPathComponent("\(surface.profileName)/skills") let entries = Set((try? fileManager.contentsOfDirectory(atPath: skillsDir)) ?? []) #expect(entries == Set(surface.allocatedSkills), "\(surface.profileName) allocation") for name in surface.allocatedSkills { let link = (skillsDir as NSString).appendingPathComponent(name) let target = try? fileManager.destinationOfSymbolicLink(atPath: link) #expect(target == (master as NSString).appendingPathComponent(name), "\(surface.profileName)/\(name)") } } } @Test func `re-pass is a no-op`() { let binDir = provTempDir("noop") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") let payload = makeShippedPayload(at: provTempDir("payload")) let binary = makeFakeHermes(at: binDir, mode: "ok") func runPass() -> ProfileProvisioner.Outcome { ProfileProvisioner.perform( hermesBinary: binary, profileRoot: profileRoot, payloadRoot: payload, masterRoot: master, serverBinaryPath: "/fake/Museion", ) } _ = runPass() let configSetsBefore = fakeHermesLog(binDir, "config-set-museion-pass1") let outcome = runPass() // Everything present, nothing created — a user's later Settings // change (config set through the same rail) is never stomped. #expect(outcome == .seeded(copied: 0, present: 12 + 11 + 10 + 9)) #expect(fakeHermesLog(binDir, "config-set-museion-pass1") == configSetsBefore) #expect(fakeHermesLog(binDir, "profile-create-log") == ["museion", "museion-pass1", "museion-pass2"]) } @Test func `adopted museion is neither created nor configured`() { let binDir = provTempDir("adopt") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") let museion = (profileRoot as NSString).appendingPathComponent("museion") try? FileManager.default.createDirectory(atPath: museion, withIntermediateDirectories: true) // Engine-owned state the adoption must leave byte-identical. try? "sessions survive".write( toFile: (museion as NSString).appendingPathComponent("state.db"), atomically: true, encoding: .utf8, ) _ = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: makeShippedPayload(at: provTempDir("payload")), masterRoot: master, serverBinaryPath: "/fake/Museion", ) // Only the pass profiles were created; the live museion's model // block (glm-5.3-max until Settings switches it — #132) is never // touched: no create spawn, no config set. #expect(fakeHermesLog(binDir, "profile-create-log") == ["museion-pass1", "museion-pass2"]) #expect(!FileManager.default.fileExists(atPath: (binDir as NSString).appendingPathComponent("config-set-museion"))) let sentinel = ((try? String( contentsOfFile: (museion as NSString).appendingPathComponent("state.db"), encoding: .utf8, )) ?? "") #expect(sentinel == "sessions survive") } @Test func `unconfigured pass profile is healed and configured one is never stomped`() { let binDir = provTempDir("heal") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") // museion adopted; museion-pass1 exists WITHOUT its marker (a pass // interrupted between create and configure); museion-pass2 exists // WITH the marker (configured — e.g. the user changed the model in // Settings through the same rail). for profile in ["museion", "museion-pass1", "museion-pass2"] { try? FileManager.default.createDirectory( atPath: (profileRoot as NSString).appendingPathComponent(profile), withIntermediateDirectories: true, ) } try? Data().write( to: URL(fileURLWithPath: (profileRoot as NSString).appendingPathComponent("museion-pass2/.museion-provisioned")), ) _ = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: makeShippedPayload(at: provTempDir("payload")), masterRoot: master, serverBinaryPath: "/fake/Museion", ) #expect(fakeHermesLog(binDir, "config-set-museion-pass1").count == 3) // healed #expect(fakeHermesLog(binDir, "config-set-museion-pass2").isEmpty) // never stomped #expect(fakeHermesLog(binDir, "config-set-museion").isEmpty) // adopted } @Test func `failing provision surfaces the profile by name`() { let binDir = provTempDir("name") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") // museion adopted — the next profile in the loop is the one that fails. try? FileManager.default.createDirectory( atPath: (profileRoot as NSString).appendingPathComponent("museion"), withIntermediateDirectories: true, ) let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "fail"), profileRoot: profileRoot, payloadRoot: makeShippedPayload(at: provTempDir("payload")), masterRoot: provTempDir("master"), ) #expect(outcome == .failed(profile: "museion-pass1", phase: .create, excerpt: "Error: create exploded")) } @Test func `config set failure fails the configure phase`() { let binDir = provTempDir("cfgfail") try? "".write( toFile: (binDir as NSString).appendingPathComponent("config-set-fails"), atomically: true, encoding: .utf8, ) let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: makeShippedPayload(at: provTempDir("payload")), masterRoot: provTempDir("master"), ) // Fresh chat created first → its configure failed; no marker landed, // so the Retry from the #48 card configures it. #expect(outcome == .failed(profile: "museion", phase: .configure, excerpt: "Error: config set exploded")) #expect(!FileManager.default.fileExists( atPath: (profileRoot as NSString).appendingPathComponent("museion/.museion-provisioned"), )) } @Test func `marker write failure fails the configure phase`() { // Finish-audit: a marker that couldn't be recorded must surface — // swallowing it left the profile unmarked, and every later pass // re-ran config set, stomping a user's Settings change. let binDir = provTempDir("markerfail") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let fileManager = FileManager.default // museion adopted; museion-pass1 unconfigured AND read-only (r-x) — // the CLI rail succeeds (the fake writes nothing into the profile), // then the marker write is the first write the dir refuses. for profile in ["museion", "museion-pass1"] { try? fileManager.createDirectory( atPath: (profileRoot as NSString).appendingPathComponent(profile), withIntermediateDirectories: true, ) } let pass1 = (profileRoot as NSString).appendingPathComponent("museion-pass1") defer { try? fileManager.setAttributes([.posixPermissions: 0o755], ofItemAtPath: pass1) } try? fileManager.setAttributes([.posixPermissions: 0o555], ofItemAtPath: pass1) let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: makeShippedPayload(at: provTempDir("payload")), masterRoot: provTempDir("master"), ) guard case let .failed(profile, phase, excerpt) = outcome else { Issue.record("expected configure failure, got \(outcome)") return } #expect(profile == "museion-pass1") #expect(phase == .configure) #expect(excerpt.contains("marker")) // No marker landed → Retry from the #48 card heals it. #expect(!fileManager.fileExists(atPath: (pass1 as NSString).appendingPathComponent(".museion-provisioned"))) } @Test func `fresh pass registers museion-tools per profile with the exact command line`() { let binDir = provTempDir("mcp-add") let serverPath = (provTempDir("server") as NSString).appendingPathComponent("Museion") let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: (binDir as NSString).appendingPathComponent("profiles"), payloadRoot: makePayload(at: provTempDir("payload"), skills: defaultSkills), masterRoot: provTempDir("master"), serverBinaryPath: serverPath, ) #expect(outcome == .seeded(copied: 8, present: 0)) // #134: registration per profile — chat and one pass pinned exactly. for profile in ["museion", "museion-pass1"] { let invocation = ((try? String( contentsOfFile: (binDir as NSString).appendingPathComponent("mcp-add-invocation-\(profile)"), encoding: .utf8, )) ?? "").trimmingCharacters(in: .whitespacesAndNewlines) #expect(invocation == "-p \(profile) mcp add museion-tools --command \(serverPath) --args --serve-mcp") } } @Test func `already-registered profile skips the add`() { let binDir = provTempDir("mcp-skip") // Pre-existing registration → `mcp list` reports it → no add: a re-add // would prompt "Overwrite? [y/N]" — the skip avoids that branch. for profile in ProfileSurface.allCases { try? "".write( toFile: (binDir as NSString).appendingPathComponent("mcp-registered-\(profile.profileName)"), atomically: true, encoding: .utf8, ) } let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: (binDir as NSString).appendingPathComponent("profiles"), payloadRoot: makePayload(at: provTempDir("payload"), skills: defaultSkills), masterRoot: provTempDir("master"), serverBinaryPath: "/fake/Museion", ) #expect(outcome == .seeded(copied: 8, present: 0)) for profile in ProfileSurface.allCases { let invocation = (binDir as NSString).appendingPathComponent("mcp-add-invocation-\(profile.profileName)") #expect(!FileManager.default.fileExists(atPath: invocation)) } } @Test func `rejected add fails the register phase with the cli excerpt`() { let binDir = provTempDir("mcp-reject") try? "".write( toFile: (binDir as NSString).appendingPathComponent("mcp-add-fails"), atomically: true, encoding: .utf8, ) let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: (binDir as NSString).appendingPathComponent("profiles"), payloadRoot: makePayload(at: provTempDir("payload"), skills: defaultSkills), masterRoot: provTempDir("master"), serverBinaryPath: "/fake/Museion", ) #expect(outcome == .failed(profile: "museion", phase: .register, excerpt: "Error: mcp add exploded")) } }