// ChainRouterTests.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") struct ChainRouterTests { // MARK: - Helpers /// Audio bus pool size for test engines. private let numAudioBuses: Int = 128 /// Render duration in seconds. private let renderDuration: Double = 0.2 // MARK: - Non-silent output (full chain) @Test func routerWithOnePedalPerBoardRendersNonSilent() throws { // Build a router with .testLoop, add one pedal to each board via addToA/addToB, // finalize, start the engine, and confirm the hardware output is non-silent. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) try router.addToA(OverdrivePedal()) try router.addToB(DelayPedal()) try router.finalize() // The BoardOutputMaster writes hardware buses 0/1 directly; no extra master needed. try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "ChainRouter produced no samples") #expect(hasNonSilentOutput(samples), "ChainRouter output was silent") } // MARK: - Mode: branch A only @Test func modeARendersNonSilent() throws { // Set mode = .a before finalize so only board A contributes. // Output must still be non-silent (board A passes through the master). let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) try router.addToA(OverdrivePedal()) try router.addToB(DelayPedal()) router.mode = .a try router.finalize() try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "mode .a produced no samples") #expect(hasNonSilentOutput(samples), "mode .a: board A chain should produce non-silent output") } // MARK: - Mode: branch B only @Test func modeBRendersNonSilent() throws { // Set mode = .b before finalize so only board B contributes. // Output must still be non-silent (board B passes through the master). let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) try router.addToA(OverdrivePedal()) try router.addToB(DelayPedal()) router.mode = .b try router.finalize() try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "mode .b produced no samples") #expect(hasNonSilentOutput(samples), "mode .b: board B chain should produce non-silent output") } // MARK: - Both branches muted → silence @Test func bothBranchesMutedProducesSilence() throws { // Disable both splitter branches before finalize. // After the 20 ms lag settles the output should be silent. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) try router.addToA(OverdrivePedal()) try router.addToB(DelayPedal()) // Mute both branches before the engine starts. router.splitter.branchAEnabled = 0.0 router.splitter.branchBEnabled = 0.0 try router.finalize() try engine.start() let samples: [Float] = readStereoCAF(url: url) // Skip the initial lag-settle period (approx 20 ms = ~882 stereo frames at 44.1 kHz). let lagSettleFrames: Int = 2048 guard samples.count > lagSettleFrames else { return } let settled: [Float] = Array(samples[lagSettleFrames...]) #expect( !hasNonSilentOutput(settled, threshold: 1e-4), "Both branches muted: output should be silent after lag settles" ) } // MARK: - Empty boards @Test func routerWithEmptyBoardsRendersNonSilent() throws { // No pedals added — signal passes straight through head→output buses, // which are both the board head buses (pass-through). The merger sums them. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) // No pedals added to either board. try router.finalize() try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "Empty-board router produced no samples") #expect(hasNonSilentOutput(samples), "Empty-board router produced silence") } // MARK: - makeChainRouter registers and teardown does not crash @Test func makeChainRouterRegistersAndTeardownDoesNotCrash() throws { // Confirm ShoeGaze.makeChainRouter() tracks the router and teardown cleans up. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: 0.05, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let router: ChainRouter = try shoeGaze.makeChainRouter() try router.addToA(OverdrivePedal()) try router.addToB(ReverbPedal()) try router.finalize() // teardown must not crash and must remove all router nodes cleanly. shoeGaze.teardown() // Boards owned by ShoeGaze are unrelated to the router boards. #expect(shoeGaze.boards.isEmpty, "ShoeGaze.boards should be empty after teardown") } // MARK: - makeChainRouter renders non-silent via ShoeGaze @Test func makeChainRouterRendersNonSilentViaShoeGaze() throws { // End-to-end: ShoeGaze.makeChainRouter → add pedals → finalize → start → sound. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let shoeGaze: ShoeGaze = ShoeGaze(engine: engine) let router: ChainRouter = try shoeGaze.makeChainRouter() try router.addToA(FuzzPedal()) try router.addToB(ReverbPedal()) router.mode = .both try router.finalize() try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "makeChainRouter produced no samples") #expect(hasNonSilentOutput(samples), "makeChainRouter output was silent") } // MARK: - masterGainNode exposed after finalize @Test func masterGainNodeIsNilBeforeFinalizeAndExposedAfter() throws { // masterGainNode must be nil until finalize() is called. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: 0.01, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) #expect(router.masterGainNode == nil, "masterGainNode should be nil before finalize()") try router.finalize() #expect(router.masterGainNode != nil, "masterGainNode should be non-nil after finalize()") } // MARK: - routerAlreadyFinalized guard @Test func addToAAfterFinalizeThrows() throws { // addToA called after finalize() must throw routerAlreadyFinalized. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: 0.01, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) try router.finalize() #expect(throws: ShoeGazeError.routerAlreadyFinalized) { try router.addToA(OverdrivePedal()) } } @Test func addToBAfterFinalizeThrows() throws { // addToB called after finalize() must throw routerAlreadyFinalized. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: 0.01, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) try router.finalize() #expect(throws: ShoeGazeError.routerAlreadyFinalized) { try router.addToB(DelayPedal()) } } // MARK: - Populate order independence (B then A) @Test func populateBThenARendersNonSilentInModeBoth() throws { // Populate board B first, then board A. With mode .both the output must be // non-silent, demonstrating that lastInsertedNode tracking makes the merger // placement independent of populate order. let (engine, url): (SynthesisEngine, URL) = makeNRTEngine( duration: renderDuration, numAudioBuses: numAudioBuses ) defer { try? FileManager.default.removeItem(at: url) } let router: ChainRouter = try ChainRouter(engine: engine, inputSource: .testLoop) // B populated before A — the ordering bug would place the merger before A's pedal. try router.addToB(ReverbPedal()) try router.addToA(OverdrivePedal()) router.mode = .both try router.finalize() try engine.start() let samples: [Float] = readStereoCAF(url: url) #expect(!samples.isEmpty, "B-then-A populate produced no samples") #expect( hasNonSilentOutput(samples), "B-then-A populate with mode .both produced silence; merger ordering may be wrong" ) } }