dc42db0c59
Replace the single hand-saved rig file with a managed library of named rigs persisted in app storage and synced across devices and both builder apps via a shared iCloud container (iCloud.net.realizedsound.ShoeGazeData), falling back to local storage when iCloud is unavailable. Core (LyrebirdShoeGaze/Presets): - RigEntry: named, timestamped ShoeGazeState (a whole multi-board rig). - RigArchive: portable multi-rig JSON bundle for backup/transfer; legacy single-rig files import as a one-rig archive. - RigLibraryStore (@MainActor @Observable): one JSON file per rig, with NSFileCoordinator writes and NSMetadataQuery live remote-change watching. - RigLibraryLocation: iCloud-or-local directory resolver (skips the slow ubiquity lookup when no iCloud account is signed in). - ShoeGaze.loadRig(from: ShoeGazeState) + captureEntry; shared filename sanitizer factored out of BoardVersionStore. UI (LyrebirdShoeGazeUI): - RigLibrarySidebarView: persistent left listing with new/save/save-as/ duplicate/rename/delete/erase-all and archive import/export. - RigNamePrompt; RigLibraryBootstrap one-call shell setup. - ShoeGazeBuilderView: sidebar wired in; selecting a rig replaces the session; File menu / sidebar do whole-library archive import/export. Shells/entitlements: both builder targets own+start the store and carry the iCloud Documents entitlement; NSUbiquitousContainers exposes the rigs in the Files app. (macOS pbxproj also carries a pre-existing 1.0->1.1.1 version bump.) Tests: RigEntry/RigArchive round-trip + RigLibraryStore CRUD/import/export (15 new; full suite 206 passing). Provisioning (manual, not done here): enable the iCloud container on both App IDs and select it in Signing & Capabilities for both targets. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
163 lines
6.2 KiB
Swift
163 lines
6.2 KiB
Swift
// RigLibraryStoreTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable import LyrebirdShoeGaze
|
|
|
|
@MainActor
|
|
@Suite("RigLibraryStore")
|
|
struct RigLibraryStoreTests {
|
|
|
|
/// A fresh, unique temp directory for an isolated, local (non-iCloud) store.
|
|
private func makeTempDirectory() -> URL {
|
|
let url: URL = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("RigLibraryTests-\(UUID().uuidString)", isDirectory: true)
|
|
return url
|
|
}
|
|
|
|
private func makeStore() -> (RigLibraryStore, URL) {
|
|
let dir: URL = makeTempDirectory()
|
|
let store: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
|
|
store.start()
|
|
return (store, dir)
|
|
}
|
|
|
|
private func makeEntry(_ name: String) -> RigEntry {
|
|
RigEntry(name: name, state: ShoeGazeState(boards: [BoardState(label: name, segments: [])]))
|
|
}
|
|
|
|
@Test func saveAddsEntryAndReloadsAcrossInstances() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
try store.save(makeEntry("Ambient"))
|
|
try store.save(makeEntry("Crunch"))
|
|
#expect(store.entries.count == 2)
|
|
// Sorted by name, case-insensitive.
|
|
#expect(store.entries.map(\.name) == ["Ambient", "Crunch"])
|
|
|
|
// A fresh store over the same directory sees the persisted files.
|
|
let reopened: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
|
|
reopened.start()
|
|
#expect(reopened.entries.count == 2)
|
|
#expect(reopened.entries.map(\.name) == ["Ambient", "Crunch"])
|
|
}
|
|
|
|
@Test func saveSameIdReplacesInPlace() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
let entry: RigEntry = makeEntry("Original")
|
|
try store.save(entry)
|
|
// Overwrite under the same id with a new name — no duplicate file should remain.
|
|
try store.save(entry.renamed(to: "Renamed"))
|
|
|
|
#expect(store.entries.count == 1)
|
|
#expect(store.entries.first?.name == "Renamed")
|
|
|
|
let files: [URL] = try FileManager.default.contentsOfDirectory(
|
|
at: dir, includingPropertiesForKeys: nil
|
|
).filter { $0.pathExtension == "json" }
|
|
#expect(files.count == 1, "rename must not leave the old-name file behind")
|
|
}
|
|
|
|
@Test func renameKeepsIdChangesName() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
let entry: RigEntry = makeEntry("Before")
|
|
try store.save(entry)
|
|
let renamed: RigEntry = try store.rename(entry, to: "After")
|
|
#expect(renamed.id == entry.id)
|
|
#expect(store.entries.map(\.name) == ["After"])
|
|
}
|
|
|
|
@Test func duplicateCreatesIndependentEntry() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
let entry: RigEntry = makeEntry("Base")
|
|
try store.save(entry)
|
|
let copy: RigEntry = try store.duplicate(entry, newName: "Base 2")
|
|
#expect(copy.id != entry.id)
|
|
#expect(store.entries.count == 2)
|
|
}
|
|
|
|
@Test func deleteRemovesFileAndEntry() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
let entry: RigEntry = makeEntry("Doomed")
|
|
try store.save(entry)
|
|
try store.delete(entry)
|
|
#expect(store.entries.isEmpty)
|
|
|
|
let files: [URL] = try FileManager.default.contentsOfDirectory(
|
|
at: dir, includingPropertiesForKeys: nil
|
|
).filter { $0.pathExtension == "json" }
|
|
#expect(files.isEmpty)
|
|
}
|
|
|
|
@Test func eraseAllClearsLibrary() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
try store.save(makeEntry("A"))
|
|
try store.save(makeEntry("B"))
|
|
try store.save(makeEntry("C"))
|
|
try store.eraseAll()
|
|
#expect(store.entries.isEmpty)
|
|
|
|
let reopened: RigLibraryStore = RigLibraryStore(directory: dir, isICloud: false)
|
|
reopened.start()
|
|
#expect(reopened.entries.isEmpty)
|
|
}
|
|
|
|
@Test func exportThenImportRoundTrips() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
try store.save(makeEntry("One"))
|
|
try store.save(makeEntry("Two"))
|
|
let archiveData: Data = try store.exportArchiveData()
|
|
|
|
// Import into a fresh, empty store.
|
|
let (other, otherDir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: otherDir) }
|
|
let added: Int = try other.importArchive(from: archiveData)
|
|
#expect(added == 2)
|
|
#expect(other.entries.map(\.name) == ["One", "Two"])
|
|
}
|
|
|
|
@Test func importColludingIdGetsFreshId() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
let entry: RigEntry = makeEntry("Shared")
|
|
try store.save(entry)
|
|
|
|
// An archive carrying the SAME id must not overwrite the existing rig on import.
|
|
let archive: RigArchive = RigArchive(rigs: [entry])
|
|
let data: Data = try JSONEncoder().encode(archive)
|
|
let added: Int = try store.importArchive(from: data)
|
|
#expect(added == 1)
|
|
#expect(store.entries.count == 2, "colliding id should be imported as a new entry")
|
|
}
|
|
|
|
@Test func importLegacySingleRigFile() throws {
|
|
let (store, dir): (RigLibraryStore, URL) = makeStore()
|
|
defer { try? FileManager.default.removeItem(at: dir) }
|
|
|
|
// A legacy file is a bare ShoeGazeState (no `rigs` array).
|
|
let legacyState: ShoeGazeState = ShoeGazeState(boards: [BoardState(label: "Legacy", segments: [])])
|
|
let data: Data = try JSONEncoder().encode(legacyState)
|
|
let added: Int = try store.importArchive(from: data, legacyRigName: "Recovered")
|
|
#expect(added == 1)
|
|
#expect(store.entries.first?.name == "Recovered")
|
|
}
|
|
}
|