// // ProfileProvisionerFailureTests.swift // MuseionTests // // #48 failure path: alert state (defer/retry), config.yaml and state.json // invariants, error excerpt surfacing. Shares the fake-binary harness with // ProfileProvisionerTests.swift. // import Foundation @testable import Museion import Testing @Suite("ProfileProvisionerFailureTests") struct ProfileProvisionerFailureTests { private let skills = defaultSkills @MainActor @Test func `not-now defers and a later pass can fail again`() async { let binDir = provTempDir("defer") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let payload = makePayload(at: provTempDir("payload"), skills: skills) let failing = makeFakeHermes(at: binDir, name: "hermes-fail", mode: "fail") let model = ProvisioningModel() await model.runPass(hermesBinary: failing, profileRoot: profileRoot, payloadRoot: payload) #expect(model.hasFailure) model.deferFailure() // Not Now (#48) #expect(!model.hasFailure) #expect(model.lastFailure == nil) // Next launch's silent retry fails again — the card may show again. await model.runPass(hermesBinary: failing, profileRoot: profileRoot, payloadRoot: payload) #expect(model.hasFailure) } @Test func `provisioning never writes mcp_servers into the profile config`() { // #44 negative invariant: a pre-existing engine-owned config.yaml stays // byte-identical through a failed AND a successful pass — no museion-tools entry. let binDir = provTempDir("mcp") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let profileDir = (profileRoot as NSString).appendingPathComponent("museion") let configPath = (profileDir as NSString).appendingPathComponent("config.yaml") let config = "model: glm-5.3\nmcp_servers:\n weather: {}\n" let goodPayload = makePayload(at: provTempDir("payload"), skills: skills) // Payload whose manifest names a skill it doesn't carry → copy failure (#48 AC). let badPayload = makePayload(at: provTempDir("bad-payload"), skills: skills) try? "serif-writing\ndeslop\nabsent-skill\n".write( toFile: (badPayload as NSString).appendingPathComponent("seed-manifest.txt"), atomically: true, encoding: .utf8, ) try? FileManager.default.createDirectory(atPath: profileDir, withIntermediateDirectories: true) try? config.write(toFile: configPath, atomically: true, encoding: .utf8) let binary = makeFakeHermes(at: binDir, mode: "ok") let failed = ProfileProvisioner.perform(hermesBinary: binary, profileRoot: profileRoot, payloadRoot: badPayload) guard case .failed = failed else { Issue.record("expected copy failure, got \(failed)") return } #expect((try? String(contentsOfFile: configPath, encoding: .utf8)) == config) // The failed pass copied both real skills before hitting the missing entry // (partial progress is the point — retry resumes additively), so the clean // re-run finds them present. let seeded = ProfileProvisioner.perform(hermesBinary: binary, profileRoot: profileRoot, payloadRoot: goodPayload) #expect(seeded == .seeded(copied: 0, present: 2)) #expect((try? String(contentsOfFile: configPath, encoding: .utf8)) == config) // #44, fresh-profile case: a profile the fake hermes just created carries // no config.yaml at all — provisioning never writes one. let freshRoot = (provTempDir("fresh-mcp") as NSString).appendingPathComponent("profiles") _ = ProfileProvisioner.perform(hermesBinary: binary, profileRoot: freshRoot, payloadRoot: goodPayload) let freshConfig = ((freshRoot as NSString).appendingPathComponent("museion") as NSString).appendingPathComponent("config.yaml") #expect(!FileManager.default.fileExists(atPath: freshConfig)) } @Test func `provisioning failure leaves state json engine-gate-only`() { let appSupport = provTempDir("state") EngineProbe.writeState(.pass(version: "9.9", binaryPath: "/fake/hermes"), appSupportDir: appSupport) let binDir = provTempDir("statefail") let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "fail"), profileRoot: (binDir as NSString).appendingPathComponent("profiles"), payloadRoot: makePayload(at: provTempDir("payload"), skills: skills), ) guard case .failed = outcome else { Issue.record("expected create failure, got \(outcome)") return } // state.json stays schemaVersion 1, engine-gate-only (#48 AC). let record = EngineProbe.readStateRecord(appSupportDir: appSupport) #expect(record?.schemaVersion == 1) #expect(record?.status == "pass") #expect(record?.engineVersion == "9.9") } @Test func `copy failure surfaces the file-manager error`() { let binDir = provTempDir("catch") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let profileDir = (profileRoot as NSString).appendingPathComponent("museion") try? FileManager.default.createDirectory(atPath: profileDir, withIntermediateDirectories: true) // `skills` pre-exists as a FILE → directory creation throws → the copy // catch branch (the only copy-error path no other test reaches). try? "blocker".write( toFile: (profileDir as NSString).appendingPathComponent("skills"), atomically: true, encoding: .utf8, ) let outcome = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: makePayload(at: provTempDir("payload"), skills: skills), ) guard case let .failed(phase, excerpt) = outcome else { Issue.record("expected copy failure, got \(outcome)") return } #expect(phase == .copy) #expect(excerpt.hasPrefix("copy serif-writing:")) } }