// // ProfileSkillMigrationTests.swift // MuseionTests // // Scenario: Copy-era dirs migrate to master symlinks on byte-match (#134/ // #133) — Given today's museion profile whose skills landed as real // directory copies, when the provisioner passes, then a dir still // byte-matching the payload swaps to a symlink into the master (content // identical through the swap); a diverged dir stays a copy; untracked user // skills are never touched. Own file — ProfileSurfaceTests crossed its // type-body ceiling (MCPToolsPromoteTests precedent); shares the harness. // import Foundation @testable import Museion import Testing @Suite("ProfileSkillMigrationTests") struct ProfileSkillMigrationTests { @Test func `copy-era dir byte-matching the shipped set migrates to a master symlink`() { let binDir = provTempDir("migrate") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") let payload = makeShippedPayload(at: provTempDir("payload")) let fileManager = FileManager.default // Today's museion: copy-era REAL dirs from the old seeding — one // pristine, one carrying Finder's .DS_Store noise (not a user edit). let museionSkills = (profileRoot as NSString).appendingPathComponent("museion/skills") for name in ["serif-writing", "deslop"] { try? fileManager.createDirectory(atPath: museionSkills, withIntermediateDirectories: true) try? fileManager.copyItem( atPath: (payload as NSString).appendingPathComponent(name), toPath: (museionSkills as NSString).appendingPathComponent(name), ) } try? "junk".write( toFile: ((museionSkills as NSString).appendingPathComponent("deslop") as NSString).appendingPathComponent(".DS_Store"), atomically: true, encoding: .utf8, ) _ = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: payload, masterRoot: master, serverBinaryPath: "/fake/Museion", ) for name in ["serif-writing", "deslop"] { let link = (museionSkills as NSString).appendingPathComponent(name) let target = try? fileManager.destinationOfSymbolicLink(atPath: link) #expect(target == (master as NSString).appendingPathComponent(name), "\(name) migrated") // Content identical through the swap. #expect(readSkillBody(link).contains("\(name) body")) } } @Test func `diverged dir stays a copy and untracked skills stay untouched`() { let binDir = provTempDir("diverged") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") let payload = makeShippedPayload(at: provTempDir("payload")) let fileManager = FileManager.default // A writer-modified copy-era dir and a user skill the manifest never // tracked — neither may be touched (G3a). let museionSkills = (profileRoot as NSString).appendingPathComponent("museion/skills") try? fileManager.createDirectory(atPath: museionSkills, withIntermediateDirectories: true) try? fileManager.copyItem( atPath: (payload as NSString).appendingPathComponent("serif-writing"), toPath: (museionSkills as NSString).appendingPathComponent("serif-writing"), ) try? "the writer's own version".write( toFile: ((museionSkills as NSString).appendingPathComponent("serif-writing") as NSString).appendingPathComponent("SKILL.md"), atomically: true, encoding: .utf8, ) let userSkill = (museionSkills as NSString).appendingPathComponent("my-own-skill") try? fileManager.createDirectory(atPath: userSkill, withIntermediateDirectories: true) try? "precious".write( toFile: (userSkill as NSString).appendingPathComponent("SKILL.md"), atomically: true, encoding: .utf8, ) _ = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: payload, masterRoot: master, serverBinaryPath: "/fake/Museion", ) // Diverged: still a REAL dir, edits intact. let diverged = (museionSkills as NSString).appendingPathComponent("serif-writing") #expect((try? fileManager.destinationOfSymbolicLink(atPath: diverged)) == nil) #expect(readSkillBody(diverged) == "the writer's own version") // Untracked: never linked, never altered. #expect((try? fileManager.destinationOfSymbolicLink(atPath: userSkill)) == nil) #expect(readSkillBody(userSkill) == "precious") } @Test func `symlinked entry inside a copy-era dir refuses the migration`() { // Finish-audit: the copy-era tree is untrusted — a symlinked SKILL.md // must fail the byte-match (never be followed) and the dir stays a // copy, the planted link untouched. let binDir = provTempDir("symlinkrefuse") let profileRoot = (binDir as NSString).appendingPathComponent("profiles") let master = provTempDir("master") let payload = makeShippedPayload(at: provTempDir("payload")) let fileManager = FileManager.default let museionSkills = (profileRoot as NSString).appendingPathComponent("museion/skills") try? fileManager.createDirectory(atPath: museionSkills, withIntermediateDirectories: true) try? fileManager.copyItem( atPath: (payload as NSString).appendingPathComponent("serif-writing"), toPath: (museionSkills as NSString).appendingPathComponent("serif-writing"), ) let skillDir = (museionSkills as NSString).appendingPathComponent("serif-writing") let elsewhere = provTempDir("elsewhere") try? "elsewhere".write( toFile: (elsewhere as NSString).appendingPathComponent("SKILL.md"), atomically: true, encoding: .utf8, ) try? fileManager.removeItem(atPath: (skillDir as NSString).appendingPathComponent("SKILL.md")) try? fileManager.createSymbolicLink( atPath: (skillDir as NSString).appendingPathComponent("SKILL.md"), withDestinationPath: (elsewhere as NSString).appendingPathComponent("SKILL.md"), ) _ = ProfileProvisioner.perform( hermesBinary: makeFakeHermes(at: binDir, mode: "ok"), profileRoot: profileRoot, payloadRoot: payload, masterRoot: master, serverBinaryPath: "/fake/Museion", ) // The dir is still a REAL dir (no migration), the planted link intact. #expect((try? fileManager.destinationOfSymbolicLink(atPath: skillDir)) == nil) #expect(readSkillBody(skillDir) == "elsewhere") } }