// PostBoardChainTests.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("Post-board chain") struct PostBoardChainTests { private func makeRig( boardCount: Int, duration: Double = 0.1 ) throws -> (ShoeGaze, SynthesisEngine, URL) { let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: duration, numAudioBuses: 256) let sg: ShoeGaze = ShoeGaze(engine: engine) for _ in 0 ..< boardCount { let board: PedalBoard = try sg.addBoard() try board.add(OverdrivePedal()) try sg.finalizeBoard(board) } return (sg, engine, url) } private func render(_ engine: SynthesisEngine, _ url: URL) throws -> [Float] { try engine.start() return readStereoCAF(url: url) } @Test func postBoardChainProcessesTheMixAndRenders() throws { let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2) defer { try? FileManager.default.removeItem(at: url) } try sg.appendPostPedal(.reverb) #expect(sg.postBoardStates.count == 1) #expect(sg.postBoardPedals.count == 1, "the live post-chain holds the added pedal") #expect(sg.boards.count == 2, "the boards are left in place") let samples: [Float] = try render(engine, url) #expect(hasNonSilentOutput(samples), "a post-board pedal keeps the summed mix flowing to output") } @Test func aClosedPostBoardVolumeSilencesTheOutput() throws { let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2) defer { try? FileManager.default.removeItem(at: url) } try sg.appendPostPedal(.volume) // Close the post-board volume (position 0 = silent) before start → the final output is silent, // proving the post-chain sits in the output path after the board mix. let volume: Pedal = try #require(sg.postBoardPedals.first) let state: PedalState = volume.captureState() try #require(!state.knobValues.isEmpty) volume.applyState( PedalState(id: state.id, type: state.type, knobValues: [0.0], switchValues: state.switchValues, bypass: state.bypass) ) let samples: [Float] = try render(engine, url) #expect(!hasNonSilentOutput(samples), "a closed post-board volume silences the whole rig output") } @Test func removingTheLastPostPedalRestoresDirectOutput() throws { let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1) defer { try? FileManager.default.removeItem(at: url) } try sg.appendPostPedal(.reverb) #expect(sg.postBoardPedals.count == 1) try sg.removePostPedal(at: 0) #expect(sg.postBoardStates.isEmpty) #expect(sg.postBoardPedals.isEmpty, "the post-chain is gone") let samples: [Float] = try render(engine, url) #expect(hasNonSilentOutput(samples), "the rig renders straight to hardware again") } @Test func addingABoardWhileAPostChainExistsKeepsOutputFlowing() throws { let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1) defer { try? FileManager.default.removeItem(at: url) } try sg.appendPostPedal(.reverb) // Add a second board AFTER the post-chain exists. The post-chain sits at the tail of the main // group, so the new master (spliced nearer the head) evaluates before it with no rebuild. let second: PedalBoard = try sg.addBoard() try second.add(DelayPedal()) try sg.finalizeBoard(second) #expect(sg.boards.count == 2) #expect(sg.postBoardPedals.count == 1, "the post-chain survives the board add") let samples: [Float] = try render(engine, url) #expect(hasNonSilentOutput(samples), "both boards reach output through the re-ordered post-chain") } @Test func postChainEvaluatesAfterEveryBoardMasterNotJustTheLast() throws { // The naive anchor was `outputMasters.last` — the *last-finalized* board. But board taps are // spliced `.after(feedNode)`, so a later board evaluates EARLIER; `outputMasters.last` is the // first-evaluated master, and a post-chain placed after it reads the mix before the other // boards write it. Here we silence the last-finalized board and keep the first one audible: // if the post-chain ran before board 0's master, the mix would be empty and the output silent. let (sg, engine, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 2) defer { try? FileManager.default.removeItem(at: url) } try sg.appendPostPedal(.reverb) // Close the LAST-finalized board's master; only the first board feeds the mix now. let lastBoardGain: UIParamNode = try #require(sg.boards[1].masterGainNode) lastBoardGain.update(0.0) let samples: [Float] = try render(engine, url) #expect(hasNonSilentOutput(samples), "the first board must still reach the output through a tail-ordered post-chain") } @Test func insertMoveAndRemoveOrderThePostChain() throws { let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1) defer { try? FileManager.default.removeItem(at: url) } try sg.appendPostPedal(.reverb) try sg.insertPostPedal(.volume, at: 0) #expect(sg.postBoardStates.map { $0.type } == ["Volume", "Reverb"], "insert lands at the index") // SwiftUI `toOffset` semantics — move the Reverb (index 1) to the front. try sg.movePostPedal(from: 1, to: 0) #expect(sg.postBoardStates.map { $0.type } == ["Reverb", "Volume"], "move reorders the chain") try sg.removePostPedal(at: 0) #expect(sg.postBoardStates.map { $0.type } == ["Volume"], "remove drops the indexed pedal") #expect(sg.postBoardPedals.map { $0.id } == sg.postBoardStates.map { $0.id }, "live pedals track the states in order") } @Test func postBoardChainRoundTripsThroughSaveLoad() throws { let (sg, _, url): (ShoeGaze, SynthesisEngine, URL) = try makeRig(boardCount: 1) defer { try? FileManager.default.removeItem(at: url) } try sg.appendPostPedal(.reverb) let knob: PedalKnob = try #require(sg.postBoardPedals.first?.knobs.first) let sentinel: Double = knob.range.lowerBound + (knob.range.upperBound - knob.range.lowerBound) * 0.44 knob.node.update(sentinel) let preset: URL = FileManager.default.temporaryDirectory .appendingPathComponent(UUID().uuidString + ".json") defer { try? FileManager.default.removeItem(at: preset) } try sg.saveRig(to: preset) let (engine2, url2): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 256) defer { try? FileManager.default.removeItem(at: url2) } let sg2: ShoeGaze = ShoeGaze(engine: engine2) try sg2.loadRig(from: preset) #expect(sg2.postBoardStates.map { $0.type } == ["Reverb"], "post-chain round-trips") #expect(sg2.postBoardPedals.count == 1, "the reloaded post-chain is live") let reloadedKnob: Double = try #require(sg2.postBoardPedals.first?.captureState().knobValues.first) #expect(abs(reloadedKnob - sentinel) < 1e-9, "a post-pedal knob tweak survives save/load") try engine2.start() #expect(hasNonSilentOutput(readStereoCAF(url: url2)), "reloaded post-board rig renders") } @Test func oldRigWithoutPostChainDecodesToEmpty() throws { let state: ShoeGazeState = ShoeGazeState(boards: []) #expect(state.postChain == nil) let data: Data = try JSONEncoder().encode(state) let decoded: ShoeGazeState = try JSONDecoder().decode(ShoeGazeState.self, from: data) #expect(decoded.postChain == nil, "absent/nil postChain stays nil") } }