Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/RigStoreTests.swift
T
josh cc3a986cde Persist I/O + input via the rig, not UserDefaults
The rig snapshot already stores inputSource + input/output channel counts and
syncs them via the autosave envelope and the iCloud rig library. Make that the
single source of truth instead of the parallel UserDefaults path.

- RigStore.restore applies the saved channel layout (applyChannelLayout) before
  loadRig, while stopped at launch, so a saved live-input channel validates
  against the restored count instead of falling back to the test loop. The
  general loadRig policy (don't force layout mid-session) is unchanged.
- applyInputSource and the engine-config onConfigApplied now autosave, so input
  and I/O changes are captured into the rig immediately.
- Remove the UserDefaults keys and read/write sites: ShoeGaze.inputSelection
  (builder view + iOS model) and the ShoeGaze.inputChannels/outputChannels keys
  added earlier (EngineConfigView + both app targets build at the default layout
  now; RigStore.restore supplies the rig's layout).
- Fresh-launch default: the seed paths set inputSource = liveInput(0) when the
  engine has an input channel, so a first launch listens to the instrument. A
  restored rig keeps its own saved input; the view's on-appear hook only requests
  mic access for a restored live selection (test-loop fallback on denial).

Test: RigStore.restoreAppliesSavedChannelLayoutAndInput. 461 tests pass; both
app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-11 15:34:23 -07:00

134 lines
5.8 KiB
Swift

// RigStoreTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
import LyrebirdSynth
import LyrebirdUI
@testable import LyrebirdShoeGaze
@MainActor
@Suite("RigStore")
struct RigStoreTests {
/// Redirects `RigStore` to a unique temp file so the suite never touches — let alone
/// overwrites or deletes — the running app's real autosave in Application Support. Returns
/// the temp file URL (its parent directory is created).
private func isolateAutosave() throws -> URL {
let dir: URL = FileManager.default.temporaryDirectory
.appendingPathComponent("RigStoreTests-" + UUID().uuidString, isDirectory: true)
try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
let file: URL = dir.appendingPathComponent("autosave-rig.json")
RigStore.autosaveURLOverride = file
return file
}
@Test func savesAndRestoresAcrossInstances() throws {
let autosave: URL = try isolateAutosave()
defer {
RigStore.autosaveURLOverride = nil
try? FileManager.default.removeItem(at: autosave.deletingLastPathComponent())
}
RigStore.clear()
// Build a rig and autosave it.
let (engineA, urlA): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: urlA) }
let a: ShoeGaze = ShoeGaze(engine: engineA)
let lead: PedalBoard = try a.addBoard()
lead.label = "Lead"
try a.appendPedal(.overdrive, to: lead)
try a.finalizeBoard(lead)
let clean: PedalBoard = try a.addBoard()
clean.label = "Clean"
try a.appendPedal(.graphicEQ, to: clean)
try a.finalizeBoard(clean)
#expect(RigStore.save(a))
// A fresh, boardless instance restores the autosaved rig.
let (engineB, urlB): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: urlB) }
let b: ShoeGaze = ShoeGaze(engine: engineB)
#expect(RigStore.restore(into: b))
#expect(b.boards.count == 2)
#expect(b.boards.first?.label == "Lead")
#expect(b.boards.last?.label == "Clean")
}
@Test func restorePreservesCurrentRigID() throws {
let autosave: URL = try isolateAutosave()
defer {
RigStore.autosaveURLOverride = nil
try? FileManager.default.removeItem(at: autosave.deletingLastPathComponent())
}
RigStore.clear()
let (engineA, urlA): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: urlA) }
let a: ShoeGaze = ShoeGaze(engine: engineA)
let board: PedalBoard = try a.addBoard()
try a.appendPedal(.overdrive, to: board)
try a.finalizeBoard(board)
let rigID: UUID = UUID()
a.currentRigID = rigID
#expect(RigStore.save(a))
let (engineB, urlB): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: urlB) }
let b: ShoeGaze = ShoeGaze(engine: engineB)
#expect(RigStore.restore(into: b))
#expect(b.currentRigID == rigID, "restore must carry the source-rig id so the UI highlights it")
}
/// A launch restore reinstates the rig's saved I/O layout so a multi-channel setup survives
/// relaunch, and a saved live-input channel that only fits the wider layout stays selected
/// (rather than falling back to the test loop against the fresh engine's default count).
@Test func restoreAppliesSavedChannelLayoutAndInput() throws {
let autosave: URL = try isolateAutosave()
defer {
RigStore.autosaveURLOverride = nil
try? FileManager.default.removeItem(at: autosave.deletingLastPathComponent())
}
RigStore.clear()
// Save a rig on a 2-in / 2-out engine with live input on channel 1.
let (engineA, urlA): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.05, numOutputChannels: 2, numInputChannels: 2, numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: urlA) }
let a: ShoeGaze = ShoeGaze(engine: engineA, inputSource: .liveInput(channel: 1))
let board: PedalBoard = try a.addBoard()
try a.appendPedal(.overdrive, to: board)
try a.finalizeBoard(board)
#expect(RigStore.save(a))
// Restore into a fresh engine built with the default single input channel.
let (engineB, urlB): (SynthesisEngine, URL) = makeNRTEngine(
duration: 0.05, numOutputChannels: 2, numInputChannels: 1, numAudioBuses: 64
)
defer { try? FileManager.default.removeItem(at: urlB) }
let b: ShoeGaze = ShoeGaze(engine: engineB)
#expect(RigStore.restore(into: b))
#expect(b.engine.numInputChannels == 2, "restore must reapply the rig's saved input count")
#expect(b.inputSource == .liveInput(channel: 1),
"the saved live-input channel must survive because the layout was restored first")
}
@Test func restoreReturnsFalseWhenNoAutosave() throws {
let autosave: URL = try isolateAutosave()
defer {
RigStore.autosaveURLOverride = nil
try? FileManager.default.removeItem(at: autosave.deletingLastPathComponent())
}
RigStore.clear()
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.05, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: url) }
let sg: ShoeGaze = ShoeGaze(engine: engine)
#expect(!RigStore.restore(into: sg), "restore should report nothing to load after clear()")
}
}