Files
ShoeGaze/Tests/LyrebirdShoeGazeTests/ChainRouterPersistenceTests.swift
T
josh 2c970aa344 refactor(rigs): address external-review findings
- Recall router state in applyState: the lightweight (no-rebuild) recall path
  now positionally applies router branch pedal params and the splitter/merger
  blend, instead of silently dropping them for routed rigs. Test added.
- Simplify first-run seeding: RigLibraryStore.save persists asynchronously
  (best-effort, logged not thrown), so the previous per-entry rollback could
  never fire for a real disk failure. Replaced with an honest best-effort
  save + working-rig fallback, and a comment stating the semantics.
- Move FactoryRigBuild to its own file (one public type per file).
- Document that a router's output master gain is intentionally not persisted,
  matching BoardState's treatment of board master gain.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-09 13:32:58 -07:00

106 lines
4.7 KiB
Swift

// ChainRouterPersistenceTests.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("ChainRouter persistence")
struct ChainRouterPersistenceTests {
@Test func routerRigRoundTripsThroughJSONAndRenders() throws {
// Build a routed session: two-pedal board A, one-pedal board B, selecting board B.
let (engineA, urlA): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
defer { try? FileManager.default.removeItem(at: urlA) }
let sessionA: ShoeGaze = ShoeGaze(engine: engineA)
let router: ChainRouter = try sessionA.makeChainRouter()
try router.addToA(FuzzPedal())
try router.addToA(OverdrivePedal())
try router.addToB(ReverbPedal())
router.mode = .b
try router.finalize()
let state: ShoeGazeState = sessionA.captureState()
#expect(state.routers?.count == 1, "router was not captured")
#expect(state.boards.isEmpty, "router rig should have no plain boards")
// JSON round-trip.
let data: Data = try JSONEncoder().encode(state)
let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data)
#expect(decoded == state, "router state did not survive a JSON round-trip")
// Load into a fresh session and verify the router was rebuilt structurally.
let (engineB, urlB): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.2, numAudioBuses: 128)
defer { try? FileManager.default.removeItem(at: urlB) }
let sessionB: ShoeGaze = ShoeGaze(engine: engineB)
try sessionB.loadRig(from: decoded)
#expect(sessionB.routers.count == 1, "router was not rebuilt on load")
guard let rebuilt: ChainRouter = sessionB.routers.first else {
Issue.record("no rebuilt router")
return
}
#expect(rebuilt.boardA.pedals.count == 2, "board A pedal count not restored")
#expect(rebuilt.boardB.pedals.count == 1, "board B pedal count not restored")
#expect(rebuilt.boardA.pedals.map { $0.displayName } == ["Fuzz", "Overdrive"])
#expect(rebuilt.boardB.pedals.map { $0.displayName } == ["Reverb"])
// mode == .b → branch A muted, branch B active.
#expect(rebuilt.splitter.branchAEnabled == 0.0, "branch A level not restored")
#expect(rebuilt.splitter.branchBEnabled == 1.0, "branch B level not restored")
// The rebuilt router should render audio (board B is the active path).
try engineB.start()
let samples: [Float] = readStereoCAF(url: urlB)
#expect(hasNonSilentOutput(samples), "rebuilt router produced silence")
}
@Test func applyStateRecallsRouterParamsLive() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
defer { try? FileManager.default.removeItem(at: url) }
let session: ShoeGaze = ShoeGaze(engine: engine)
let router: ChainRouter = try session.makeChainRouter()
let fuzz: FuzzPedal = FuzzPedal()
fuzz.fuzz = 20.0
try router.addToA(fuzz)
try router.addToB(ReverbPedal())
router.mode = .a
try router.finalize()
let original: ShoeGazeState = session.captureState()
// Mutate live: change a router pedal param and flip the A/B selection.
fuzz.fuzz = 55.0
router.mode = .b
// Lightweight recall should restore the router pedal param and blend without a rebuild.
session.applyState(original)
#expect(fuzz.fuzz == 20.0, "router pedal param was not recalled")
#expect(router.splitter.branchAEnabled == 1.0, "branch A level not recalled")
#expect(router.splitter.branchBEnabled == 0.0, "branch B level not recalled")
}
@Test func routerlessRigStoresNoRoutersKey() throws {
// A plain single-board session must not gain a `routers` payload (byte-compat with rigs
// saved before router persistence existed).
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 64)
defer { try? FileManager.default.removeItem(at: url) }
let session: ShoeGaze = ShoeGaze(engine: engine)
let board: PedalBoard = try session.addBoard()
try board.add(FuzzPedal())
try session.finalizeBoard(board)
let state: ShoeGazeState = session.captureState()
#expect(state.routers == nil, "routerless rig should capture nil routers")
let json: String = String(data: try JSONEncoder().encode(state), encoding: .utf8) ?? ""
#expect(!json.contains("\"routers\""), "routerless JSON should omit the routers key")
}
}