// LiveSessionGraphDSLTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) import Testing import LyrebirdSynth import LyrebirdGraphDSLEmbedded @testable import LyrebirdLiveCompile @MainActor @Suite("LiveSession graph-DSL") struct LiveSessionGraphDSLTests { /// NRT engine — `start()` is never called. The DSL graphs use only /// oscillators (no `LyrebirdBuffer`), so no engine run cycle is needed and /// the buffer-teardown race is avoided. private func makeSession() -> LiveSession { let engine: SynthesisEngine = SynthesisEngine( blockSize: 64, controlBlockSize: 16, sampleRate: 48_000, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 16, numControlBuses: 16, eventQueueCapacity: lyrebirdDefaultEventQueueCapacity, driver: OfflineDriver() ) return LiveSession(engine: engine) } @Test func anonymousStatementIsTracked() throws { let session: LiveSession = makeSession() try session.runGraphDSL("OscSin(freq: 440) * 0.1") #expect(session.trackedSynthCount == 1) #expect(session.slotCount == 0) } @Test func multipleAnonymousStatementsEachTrack() throws { let session: LiveSession = makeSession() try session.runGraphDSL("OscSin(freq: 440) * 0.1\nOscSawtooth(freq: 55) * 0.2") #expect(session.trackedSynthCount == 2) } @Test func namedStatementGoesToASlotNotTracked() throws { let session: LiveSession = makeSession() try session.runGraphDSL("bass = OscSawtooth(freq: 55) * 0.2") #expect(session.trackedSynthCount == 0) #expect(session.slotCount == 1) #expect(session.slotNames == ["bass"]) } @Test func nonAdditiveReRunReplacesAnonymousSynths() throws { let session: LiveSession = makeSession() try session.runGraphDSL("OscSin(freq: 440) * 0.1") try session.runGraphDSL("OscSin(freq: 220) * 0.1") #expect(session.trackedSynthCount == 1) } @Test func additiveReRunKeepsPriorSynths() throws { let session: LiveSession = makeSession() try session.runGraphDSL("OscSin(freq: 440) * 0.1") try session.runGraphDSL("OscSin(freq: 220) * 0.1", additive: true) #expect(session.trackedSynthCount == 2) } @Test func namedSlotSurvivesNonAdditiveReRun() throws { let session: LiveSession = makeSession() try session.runGraphDSL("bass = OscSawtooth(freq: 55) * 0.2") // A plain anonymous re-run should not disturb the slot. try session.runGraphDSL("OscSin(freq: 440) * 0.1") #expect(session.slotCount == 1) #expect(session.trackedSynthCount == 1) } @Test func parseErrorThrowsGraphDSLFailedWithLine() { let session: LiveSession = makeSession() // `%` starts no token — a lex error on the second line. #expect { try session.runGraphDSL("OscSin(freq: 1)\n%%%") } throws: { error in guard case LiveSessionError.graphDSLFailed(let diags, _) = error else { return false } return diags.count == 1 && diags[0].line == 2 } } @Test func buildErrorThrowsGraphDSLFailedWithoutLine() { let session: LiveSession = makeSession() #expect { try session.runGraphDSL("NotAGenerator(freq: 1)") } throws: { error in guard case LiveSessionError.graphDSLFailed(let diags, _) = error else { return false } return diags.count == 1 && diags[0].line == nil } } @Test func failedEvaluationLeavesPriorGraphUntouched() throws { let session: LiveSession = makeSession() try session.runGraphDSL("OscSin(freq: 440) * 0.1") #expect(session.trackedSynthCount == 1) // A broken non-additive re-run must NOT clear the playing graph — the // program is built before any engine mutation. #expect(throws: LiveSessionError.self) { try session.runGraphDSL("Nope(freq: 1)") } #expect(session.trackedSynthCount == 1) } /// Engine whose `pendingQueue` holds only `capacity` (power-of-two) adds /// before it fills — and it is never processed, so it never drains. Lets a /// test drive a deterministic mid-run `.pendingQueueFull`. private func makeSmallQueueSession(capacity: Int) -> LiveSession { let engine: SynthesisEngine = SynthesisEngine( blockSize: 64, controlBlockSize: 16, sampleRate: 48_000, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 16, numControlBuses: 16, eventQueueCapacity: capacity, driver: OfflineDriver() ) return LiveSession(engine: engine) } @Test func engineRejectionIsPerStatementNotAbortAll() { // A small pending queue fills partway through eight statements. Continue- // on-error attempts EVERY statement, so `tracked + failed == 8` and more // than one failed. An abort-on-first-error loop would stop at the first // rejection, leaving `tracked + failed < 8` and exactly one diagnostic. let session: LiveSession = makeSmallQueueSession(capacity: 4) let count: Int = 8 let source: String = (0..= 2) #expect(session.trackedSynthCount >= 1) } @Test func resetAllClearsDSLTrackedAndSlots() throws { let session: LiveSession = makeSession() try session.runGraphDSL("bass = OscSawtooth(freq: 55) * 0.2\nOscSin(freq: 440) * 0.1") #expect(session.trackedSynthCount == 1) #expect(session.slotCount == 1) session.resetAll() #expect(session.trackedSynthCount == 0) #expect(session.slotCount == 0) } } #endif