One liveness-gated snapshot pass per launch, chained after provisioning: gateway.pid pid-liveness + ps scan (skip while the engine runs), 24 h age check, curated recursive copy (credentials and SQLite sidecars in; logs/caches/locks out), atomic temp+rename install, keep last 7. Restore stays manual copy-back in v1 — Help > Open Backup Folder and a RESTORE.md written beside the snapshots. [Generated by AI]
202 lines
7.9 KiB
Swift
202 lines
7.9 KiB
Swift
//
|
|
// ProfileBackupTests.swift
|
|
// MuseionTests
|
|
//
|
|
// Exercises the backup pass end-to-end on injected dirs (#50): liveness
|
|
// branches, exclusion list, credentials & sidecars, age check, retention,
|
|
// atomic install, failure path. `psScan` is injected (a dev machine may run
|
|
// the real engine); the clock is injected for the age check.
|
|
//
|
|
|
|
import Foundation
|
|
@testable import Museion
|
|
import Testing
|
|
|
|
/// Backup-test temp dir (distinct name from the other suites).
|
|
func backupTempDir(_ label: String) -> String {
|
|
let dir = NSTemporaryDirectory() + "museion-backup-\(label)-\(UUID().uuidString)"
|
|
try? FileManager.default.createDirectory(atPath: dir, withIntermediateDirectories: true)
|
|
return dir
|
|
}
|
|
|
|
/// Minimal profile fixture at `<root>/museion`: state that matters to a
|
|
/// restore (auth.json, .env, state.db + sidecars, a skill) plus excluded
|
|
/// entries (logs/, cache/, gateway.pid, .DS_Store, caches, locks).
|
|
@discardableResult
|
|
func makeProfileFixture(at root: String) -> String {
|
|
let fileManager = FileManager.default
|
|
let profile = (root as NSString).appendingPathComponent("museion")
|
|
for dir in ["skills/serif-writing", "logs", "cache", "sessions/xyz/cache"] {
|
|
try? fileManager.createDirectory(
|
|
atPath: (profile as NSString).appendingPathComponent(dir),
|
|
withIntermediateDirectories: true
|
|
)
|
|
}
|
|
func write(_ text: String, _ rel: String) {
|
|
try? text.write(
|
|
toFile: (profile as NSString).appendingPathComponent(rel),
|
|
atomically: true, encoding: .utf8
|
|
)
|
|
}
|
|
write("{\"token\": \"secret\"}", "auth.json")
|
|
write("KEY=1", ".env")
|
|
write("db", "state.db")
|
|
write("wal", "state.db-wal")
|
|
write("shm", "state.db-shm")
|
|
write("skill body", "skills/serif-writing/SKILL.md")
|
|
write("log", "logs/app.log")
|
|
write("{}", "gateway.pid")
|
|
write("junk", ".DS_Store")
|
|
write("{}", "models_dev_cache.json")
|
|
write("{}", "provider_models_cache.json")
|
|
write("{}", "context_length_cache.yaml")
|
|
write("lock", "auth.lock")
|
|
return profile
|
|
}
|
|
|
|
private func snapshotDirs(_ backupsRoot: String) -> [String] {
|
|
(try? FileManager.default.contentsOfDirectory(atPath: backupsRoot))?.sorted() ?? []
|
|
}
|
|
|
|
private func epoch(_ year: Int, _ month: Int, _ day: Int, _ hour: Int = 0, _ minute: Int = 0) -> Date {
|
|
var comps = DateComponents()
|
|
comps.year = year
|
|
comps.month = month
|
|
comps.day = day
|
|
comps.hour = hour
|
|
comps.minute = minute
|
|
comps.timeZone = .current
|
|
return Calendar(identifier: .gregorian).date(from: comps) ?? Date()
|
|
}
|
|
|
|
@Suite("ProfileBackupTests")
|
|
struct ProfileBackupTests {
|
|
private func perform(
|
|
profileRoot: String,
|
|
backupsRoot: String,
|
|
now: Date = epoch(2026, 9, 25, 10, 0),
|
|
psScan: @escaping () -> Bool = { false },
|
|
) -> ProfileBackup.Outcome {
|
|
ProfileBackup.perform(profileRoot: profileRoot, backupsRoot: backupsRoot, now: now, psScan: psScan)
|
|
}
|
|
|
|
@Test func `fresh pass snapshots with credentials and sidecars, exclusions applied`() {
|
|
let root = backupTempDir("fresh")
|
|
makeProfileFixture(at: root)
|
|
let backups = (root as NSString).appendingPathComponent("backups")
|
|
let outcome = perform(profileRoot: root, backupsRoot: backups)
|
|
guard case let .snapshotted(stamp) = outcome else {
|
|
Issue.record("expected snapshot, got \(outcome)")
|
|
return
|
|
}
|
|
let snap = ((backups as NSString).appendingPathComponent(stamp) as NSString).appendingPathComponent("museion")
|
|
let fileManager = FileManager.default
|
|
// Credentials + SQLite sidecars present (G4).
|
|
for rel in ["auth.json", ".env", "state.db", "state.db-wal", "state.db-shm", "skills/serif-writing/SKILL.md"] {
|
|
#expect(fileManager.fileExists(atPath: (snap as NSString).appendingPathComponent(rel)), "missing \(rel)")
|
|
}
|
|
// Exclusions: top-level and nested cache, locks, pid, .DS_Store, caches.
|
|
let excluded = ["logs", "cache", "sessions/xyz/cache", "gateway.pid", ".DS_Store",
|
|
"models_dev_cache.json", "provider_models_cache.json", "context_length_cache.yaml", "auth.lock"]
|
|
for rel in excluded {
|
|
#expect(!fileManager.fileExists(atPath: (snap as NSString).appendingPathComponent(rel)), "excluded \(rel) leaked")
|
|
}
|
|
}
|
|
|
|
@Test func `live gateway pid skips the pass`() {
|
|
let root = backupTempDir("pidlive")
|
|
let profile = makeProfileFixture(at: root)
|
|
// gateway.pid naming our own test process = a live pid.
|
|
try? "{\"pid\": \(ProcessInfo.processInfo.processIdentifier)}".write(
|
|
toFile: (profile as NSString).appendingPathComponent("gateway.pid"),
|
|
atomically: true, encoding: .utf8,
|
|
)
|
|
let outcome = perform(profileRoot: root, backupsRoot: (root as NSString).appendingPathComponent("backups"))
|
|
#expect(outcome == .skippedEngineRunning)
|
|
}
|
|
|
|
@Test func `stale pid falls through to ps scan and snapshots when idle`() {
|
|
let root = backupTempDir("pidstale")
|
|
let profile = makeProfileFixture(at: root)
|
|
try? "{\"pid\": 9999999}".write( // recorded but dead (verified pattern).
|
|
toFile: (profile as NSString).appendingPathComponent("gateway.pid"),
|
|
atomically: true, encoding: .utf8,
|
|
)
|
|
let outcome = perform(profileRoot: root, backupsRoot: (root as NSString).appendingPathComponent("backups"))
|
|
#expect(outcome != .skippedEngineRunning)
|
|
guard case .snapshotted = outcome else {
|
|
Issue.record("expected snapshot, got \(outcome)")
|
|
return
|
|
}
|
|
}
|
|
|
|
@Test func `ps match skips the pass`() {
|
|
let root = backupTempDir("pslive")
|
|
makeProfileFixture(at: root)
|
|
let outcome = perform(
|
|
profileRoot: root,
|
|
backupsRoot: (root as NSString).appendingPathComponent("backups"),
|
|
psScan: { true },
|
|
)
|
|
#expect(outcome == .skippedEngineRunning)
|
|
}
|
|
|
|
@Test func `fresh snapshot skips when newest is under 24h`() {
|
|
let root = backupTempDir("age")
|
|
makeProfileFixture(at: root)
|
|
let backups = (root as NSString).appendingPathComponent("backups")
|
|
let now = epoch(2026, 9, 25, 10, 0)
|
|
guard case .snapshotted = perform(profileRoot: root, backupsRoot: backups, now: now) else {
|
|
Issue.record("first snapshot failed")
|
|
return
|
|
}
|
|
#expect(perform(profileRoot: root, backupsRoot: backups, now: now.addingTimeInterval(23 * 3600)) == .skippedFresh)
|
|
#expect(perform(profileRoot: root, backupsRoot: backups, now: now.addingTimeInterval(25 * 3600)) != .skippedFresh)
|
|
}
|
|
|
|
@Test func `only the last 7 snapshots are kept`() {
|
|
let root = backupTempDir("retention")
|
|
makeProfileFixture(at: root)
|
|
let backups = (root as NSString).appendingPathComponent("backups")
|
|
for day in 0 ... 8 { // 9 daily passes
|
|
guard case .snapshotted = perform(
|
|
profileRoot: root, backupsRoot: backups,
|
|
now: epoch(2026, 9, 1 + day, 10, 0),
|
|
) else {
|
|
Issue.record("day \(day) snapshot failed")
|
|
return
|
|
}
|
|
}
|
|
let kept = snapshotDirs(backups).filter { $0 != "RESTORE.md" }
|
|
#expect(kept.count == 7)
|
|
#expect(!kept.contains("20260901-100000")) // oldest two pruned
|
|
#expect(!kept.contains("20260902-100000"))
|
|
#expect(kept.contains("20260909-100000")) // newest kept
|
|
}
|
|
|
|
@Test func `missing profile skips cleanly`() {
|
|
let root = backupTempDir("missing")
|
|
let outcome = perform(profileRoot: root, backupsRoot: (root as NSString).appendingPathComponent("backups"))
|
|
#expect(outcome == .skippedProfileMissing)
|
|
}
|
|
|
|
@Test func `copy failure returns failed and leaves no temp dir`() {
|
|
let root = backupTempDir("fail")
|
|
let profile = makeProfileFixture(at: root)
|
|
// Unreadable file mid-tree: copy throws; the pass must fail cleanly.
|
|
let unreadable = (profile as NSString).appendingPathComponent("skills/locked/SKILL.md")
|
|
try? FileManager.default.createDirectory(
|
|
atPath: (profile as NSString).appendingPathComponent("skills/locked"),
|
|
withIntermediateDirectories: true,
|
|
)
|
|
try? "x".write(toFile: unreadable, atomically: true, encoding: .utf8)
|
|
try? FileManager.default.setAttributes([.posixPermissions: 0o000], ofItemAtPath: unreadable)
|
|
let backups = (root as NSString).appendingPathComponent("backups")
|
|
let outcome = perform(profileRoot: root, backupsRoot: backups)
|
|
#expect(outcome == .failed)
|
|
#expect(snapshotDirs(backups).allSatisfy { !$0.hasPrefix(".") }) // temp swept
|
|
// Clean up the 000-mode file so the temp dir prunes.
|
|
try? FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: unreadable)
|
|
}
|
|
}
|