// UIParamNodeTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - Init @MainActor @Suite("UIParamNode init") struct UIParamNodeInitTests { @Test func initialValueStoredAsObservable() { let node: UIParamNode = UIParamNode(440.0) #expect(node.value == 440.0) } @Test func initialValueReadableViaFloatValue() { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let node: UIParamNode = UIParamNode(0.5) #expect(node.floatValue(graph: graph) == 0.5) } @Test func initialValueReadableViaIntValue() { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let node: UIParamNode = UIParamNode(3.7) #expect(node.intValue(graph: graph) == 3) } @Test func intValueAfterUpdate() { // intValue must reflect the updated value (direct atomic read, no ramp). let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let node: UIParamNode = UIParamNode(1.0) node.update(5.7) #expect(node.intValue(graph: graph) == 5) } @Test func intValueDoesNotPrimeFanOutCache() { // Calling intValue then calculatedSamples in the same segment must produce a correct // ramp — intValue must not consume the fan-out budget (no calculatedSamples side-effect). let engine: SynthesisEngine = makeUITestEngine() let blockCount: Int = 4 let node: UIParamNode = UIParamNode(0.0) node.update(4.0) let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) // intValue call — must be a direct atomic read, no _computeSamples invocation. _ = node.intValue(graph: graph) // calculatedSamples in the same segment must still compute the correct ramp. let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount) // Ramp 0→4 in 4 steps: [1.0, 2.0, 3.0, 4.0] #expect(result[0][blockCount - 1] == 4.0, "calculatedSamples after intValue must still produce the correct ramp target") } @Test func needsInterpolationIsTrue() { let node: UIParamNode = UIParamNode(1.0) #expect(node.needsInterpolation() == true) } } // MARK: - Atomic update round-trip @MainActor @Suite("UIParamNode atomic update") struct UIParamNodeAtomicTests { @Test func updateSetsObservableValue() { let node: UIParamNode = UIParamNode(100.0) node.update(200.0) #expect(node.value == 200.0) } @Test func updateReflectsInFloatValue() { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let node: UIParamNode = UIParamNode(100.0) node.update(880.0) #expect(node.floatValue(graph: graph) == 880.0) } @Test func multipleUpdatesRetainLatestValue() { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let node: UIParamNode = UIParamNode(0.0) node.update(1.0) node.update(2.0) node.update(3.0) #expect(node.floatValue(graph: graph) == 3.0) #expect(node.value == 3.0) } @Test func zeroRoundTrip() { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let node: UIParamNode = UIParamNode(1.0) node.update(0.0) #expect(node.floatValue(graph: graph) == 0.0) } @Test func negativeValueRoundTrip() { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let node: UIParamNode = UIParamNode(0.0) node.update(-1.5) #expect(node.floatValue(graph: graph) == -1.5) } } // MARK: - calculatedSamples interpolation @MainActor @Suite("UIParamNode calculatedSamples") struct UIParamNodeCalcTests { @Test func flatBlockWhenValueUnchanged() throws { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let blockCount: Int = 4 let node: UIParamNode = UIParamNode(1.0) let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount) #expect(result.count == 1) // Buffer is pre-allocated to lyrebirdDefaultResultBufferSize — never resized. // Only the first blockCount elements are meaningful. #expect(result[0].count >= blockCount) // No change — step = 0 — the first blockCount samples should all equal 1.0 for sample: Double in result[0].prefix(blockCount) { #expect(sample == 1.0) } } @Test func rampReachesTargetAtLastSample() throws { let engine: SynthesisEngine = makeUITestEngine() let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) let blockCount: Int = 4 let node: UIParamNode = UIParamNode(0.0) node.update(4.0) let result: [[Double]] = node.calculatedSamples(graph: graph, count: blockCount) // The meaningful range is [0.. Double { _value } func _computeSamples(graph: SynthGraph, count: Int) -> [[Double]] { return _buf } } // MARK: - Test helpers /// Advances the fan-out cache serial by one, simulating a block boundary. /// Mirror of `advanceBlock` in `LyrebirdTests/TestHelpers.swift` for use in UI tests. private func advanceUIBlock(_ engine: SynthesisEngine) { engine.advanceSegmentSerial() } private func makeUITestEngine() -> SynthesisEngine { SynthesisEngine( blockSize: 8, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 8, numControlBuses: 8, driver: OfflineDriver() ) }