Files
museion/Museion/HomeResolution.swift
T
AlexKuceraandCipher ad021b5267
CI / ci (push) Canceled after 0s
feat(home): pin iCloud Home for fresh installs (#171)
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>
2026-11-05 13:10:44 +01:00

84 lines
3.4 KiB
Swift

//
// HomeResolution.swift
// Museion
//
// #171: fresh-install Home resolution — the ladder + probes AppGateState
// pins through at first launch. Own file so MuseionApp.swift stays under
// the lint line cap (WritingLockState extraction precedent).
//
import Foundation
extension AppGateState {
/// The legacy default Home — `~/Documents/Museion` (#171: still the
/// fallback when iCloud is absent; b3daac9: Documents, not the account
/// root; TCC note in ADR-0004).
nonisolated static let defaultMuseionHome = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent("Documents/Museion", isDirectory: true)
/// The stored override, if it decodes to a file URL (existing
/// semantics — the Home is always a folder).
nonisolated static func fileHomeURL(in defaults: UserDefaults) -> URL? {
defaults.string(forKey: "museionHome")
.flatMap(URL.init(string:))
.flatMap { $0.isFileURL ? $0 : nil }
}
/// #171: the fresh-install ladder — (2) a legacy `~/Documents/Museion`
/// (a real directory, per the default probe's `existingDirectory` gate —
/// the Home is always a folder) IS the Home (existing Homes migrate
/// manually, never silently),
/// (3) `<CloudDocs>/Museion` when iCloud Drive is present,
/// (4) else the silent Documents fallback. The stored override (1) is
/// checked by the caller.
nonisolated static func resolvedInitialHome(
legacyHomeExists: () -> Bool,
cloudDocsDir: () -> URL?,
) -> URL {
if legacyHomeExists() {
return defaultMuseionHome
}
if let cloudDocs = cloudDocsDir() {
return cloudDocs.appendingPathComponent("Museion", isDirectory: true)
}
return defaultMuseionHome
}
/// #171: CloudDocs resolved dynamically at the unprefixed path — the
/// `iCloud~`-prefixed spelling varies per account, never hard-code it.
/// Plain FileManager probing; the app has no ubiquity entitlements
/// (ADR-0009).
nonisolated static func liveCloudDocsDir() -> URL? {
existingDirectory(cloudDocsURL(under: FileManager.default.homeDirectoryForCurrentUser))
}
/// The CloudDocs path — unprefixed (the `iCloud~`-prefixed spelling
/// varies per account, never hard-code it); the seam the spelling pin
/// drives.
nonisolated static func cloudDocsURL(under home: URL) -> URL {
home.appendingPathComponent("Library/Mobile Documents/com~apple~CloudDocs", isDirectory: true)
}
/// `url` back only when it exists AND is a directory — the dir-ness
/// gate `liveCloudDocsDir` rides.
nonisolated static func existingDirectory(_ url: URL) -> URL? {
var isDirectory: ObjCBool = false
guard FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory), isDirectory.boolValue else { return nil }
return url
}
/// True when running as the hosted-test runner app (the panes' guard
/// pattern) — machine-state writes and probes are skipped there. Tests
/// constructing `AppGateState` with DEFAULT arguments hit this via the
/// init's early return: neither the real iCloud probes nor the pin run,
/// so the runner app's real state is never touched. An injected suite
/// is a test's own sandbox and always runs the full ladder + pin.
nonisolated static var hostedTestRun: Bool {
ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil
}
nonisolated static func storeHome(_ home: URL, in defaults: UserDefaults) {
defaults.set(home.absoluteString, forKey: "museionHome")
}
}