feat(home): pin iCloud Home for fresh installs (#171)
CI / ci (push) Canceled after 0s

With no stored Home override, launch resolves once and PINS the
winner, so the Home never silently moves: a legacy
~/Documents/Museion directory wins (existing Homes migrate manually,
never silently), else <CloudDocs>/Museion when iCloud Drive is
present (dynamically resolved unprefixed path, plain FileManager —
no ubiquity entitlements, ADR-0009), else the silent Documents
fallback. Repoint-only stands (ADR-0004), zero mover code.

The ladder + probes live in HomeResolution.swift (own file —
MuseionApp.swift stays under the lint line cap); init takes an
injectable probe/defaults seam, and default constructions under
hosted tests take an early return so the runner app's real iCloud
state and defaults are never touched. 9 seam-driven tests replace
the old default-Home pin; implement + finish-audit reviews landed
the legacy-probe dir gate, the non-destructive hosted no-write
guard, and the flip-verified CloudDocs spelling pin.

Suite 628/628 (xcresult executed count). DOX: Museion/AGENTS.md,
MuseionTests/AGENTS.md, GLOSSARY.md; devlog + INDEX + CHANGELOG.

Closes #171

Co-Authored-By: Cipher <cipher@hermes.local>
This commit is contained in:
AlexKuceraandCipher committed 2026-11-05 13:10:44 +01:00
1 parent c485cfac60
commit ad021b5267
10 files changed
+282 -34

No files matched your search

+90 -4
View File
@@ -194,11 +194,97 @@ private func writePost(_ home: URL, _ id: String, _ body: String) throws {
#expect(post.title == "After Blank")
}
@Test func `home default is documents museion`() {
// MARK: #171 fresh-install Home resolution
// Scenario: a fresh install with no stored override resolves its Home once —
// legacy ~/Documents/Museion wins if it exists (existing Homes migrate
// manually, never silently), else <CloudDocs>/Museion when iCloud Drive is
// present, else the silent Documents fallback; the winner is PINNED so the
// Home never silently moves after.
/// A throwaway defaults suite per test — the pin must land in the test's
/// own sandbox, never the runner app's real defaults.
private func homeDefaults(_ name: String) -> UserDefaults {
let suite = "home171-\(name)"
let defaults = UserDefaults(suiteName: suite)!
defaults.removePersistentDomain(forName: suite)
return defaults
}
@Test func `home resolution guards the legacy documents home first`() {
// iCloud available AND a legacy Home exists → the legacy Home wins.
let resolved = AppGateState.resolvedInitialHome(legacyHomeExists: { true }, cloudDocsDir: {
URL(fileURLWithPath: "/tmp/irrelevant-clouddocs")
})
#expect(resolved == AppGateState.defaultMuseionHome)
}
@Test func `home resolution pins clouddocs museion without a legacy home`() {
let cloudDocs = URL(fileURLWithPath: "/tmp/clouddocs")
let resolved = AppGateState.resolvedInitialHome(legacyHomeExists: { false }, cloudDocsDir: { cloudDocs })
#expect(resolved == cloudDocs.appendingPathComponent("Museion", isDirectory: true))
}
@Test func `home resolution silently falls back to documents museion`() {
let resolved = AppGateState.resolvedInitialHome(legacyHomeExists: { false }, cloudDocsDir: { nil })
#expect(resolved == AppGateState.defaultMuseionHome)
}
@Test func `clouddocs probe requires a real directory`() throws {
// The live probe's dir-ness gate, driven on temp paths — hosted tests
// never touch the machine's real iCloud state.
let dir = try makeHome { _ in }
#expect(AppGateState.existingDirectory(dir) == dir)
let file = dir.appendingPathComponent("plain-file")
try "not a dir".write(to: file, atomically: true, encoding: .utf8)
#expect(AppGateState.existingDirectory(file) == nil)
#expect(AppGateState.existingDirectory(dir.appendingPathComponent("missing")) == nil)
}
@Test @MainActor func `stored home override wins over the resolution ladder`() {
let defaults = homeDefaults("stored-wins")
let custom = URL(fileURLWithPath: "/tmp/museion-custom-home")
defaults.set(custom.absoluteString, forKey: "museionHome")
let gate = AppGateState(defaults: defaults, legacyHomeExists: { true }, cloudDocsDir: { nil })
#expect(gate.museionHome == custom)
}
@Test @MainActor func `non-file stored home falls through to the ladder`() {
let defaults = homeDefaults("non-file-stored")
defaults.set("https://not.a/home", forKey: "museionHome")
let gate = AppGateState(defaults: defaults, legacyHomeExists: { false }, cloudDocsDir: { nil })
#expect(gate.museionHome == AppGateState.defaultMuseionHome)
#expect(defaults.string(forKey: "museionHome") == AppGateState.defaultMuseionHome.absoluteString)
}
@Test @MainActor func `fresh install pins once and never silently moves`() throws {
let defaults = homeDefaults("pin-once")
let cloudDocs = try makeHome { _ in }
let first = AppGateState(defaults: defaults, legacyHomeExists: { false }, cloudDocsDir: { cloudDocs })
#expect(first.museionHome == cloudDocs.appendingPathComponent("Museion", isDirectory: true))
#expect(defaults.string(forKey: "museionHome") == first.museionHome.absoluteString)
// A legacy Home "appears" after the pin — the stored winner stands.
let second = AppGateState(defaults: defaults, legacyHomeExists: { true }, cloudDocsDir: { nil })
#expect(second.museionHome == first.museionHome)
}
@Test @MainActor func `default construction never writes the real defaults under hosted tests`() {
// The init guard protecting the runner app's real state — pinned
// without touching it: whatever .standard holds before a default
// construction must hold after (a deleted guard resolves + PINS,
// changing the stored value wherever the resolved Home differs).
let real = UserDefaults.standard
let before = real.string(forKey: "museionHome")
_ = AppGateState() // default construction — no probes, no pin
#expect(real.string(forKey: "museionHome") == before)
}
@Test func `clouddocs path is the unprefixed mobile documents form`() {
// #171: never a hard-coded `iCloud~` form — the prefixed spelling
// varies per account and would silently disable iCloud detection.
#expect(
AppGateState.defaultMuseionHome
== FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent("Documents/Museion", isDirectory: true),
AppGateState.cloudDocsURL(under: URL(fileURLWithPath: "/h"))
== URL(fileURLWithPath: "/h/Library/Mobile Documents/com~apple~CloudDocs", isDirectory: true),
)
}