// UIParamTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable import LyrebirdUI @testable @_spi(LyrebirdExtensions) import LyrebirdSynth // MARK: - @UIParam property wrapper @MainActor @Suite("@UIParam property wrapper") struct UIParamTests { @Test func wrappedValueReturnsInitialValue() { class S { @UIParam var freq: Double = 440.0 } let s: S = S() #expect(s.freq == 440.0) } @Test func wrappedValueSetterUpdatesValue() { class S { @UIParam var freq: Double = 440.0 } let s: S = S() s.freq = 880.0 #expect(s.freq == 880.0) } @Test func projectedValueIsUIParamNode() { class S { @UIParam var freq: Double = 440.0 } let s: S = S() #expect(s.$freq is UIParamNode) } @Test func projectedValueNodeValueMatchesWrappedValue() { class S { @UIParam var amp: Double = 0.5 } let s: S = S() #expect(s.$amp.value == s.amp) s.amp = 0.8 #expect(s.$amp.value == 0.8) } @Test func projectedValueIsStableIdentity() { class S { @UIParam var freq: Double = 440.0 } let s: S = S() let ref1: UIParamNode = s.$freq let ref2: UIParamNode = s.$freq #expect(ref1 === ref2) } @Test func projectedValueConformsToValidGeneratorInput() { class S { @UIParam var freq: Double = 440.0 } let s: S = S() let input: any ValidGeneratorInput = s.$freq let engine: SynthesisEngine = SynthesisEngine( blockSize: 8, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 8, numControlBuses: 8, driver: OfflineDriver() ) let graph: SynthGraph = SynthGraph(engine: engine, busOffset: 0) #expect(input.floatValue(graph: graph) == 440.0) } @Test func multipleParamsAreIndependent() { class S { @UIParam var freq: Double = 440.0 @UIParam var amp: Double = 0.5 } let s: S = S() s.freq = 880.0 #expect(s.amp == 0.5) // amp unchanged #expect(s.freq == 880.0) #expect(s.$freq !== s.$amp) } @Test func wrappedValueSetterAndNodeUpdateAreEquivalent() { class S { @UIParam var freq: Double = 440.0 } let s1: S = S() let s2: S = S() s1.freq = 660.0 // via wrappedValue setter s2.$freq.update(660.0) // via node directly #expect(s1.freq == s2.freq) #expect(s1.$freq.value == s2.$freq.value) } } // MARK: - @UIParam in a SynthesizerBase subclass @MainActor @Suite("@UIParam in synthesizer") struct UIParamInSynthTests { final class ToneWithParam: SynthesizerBase { @UIParam var frequency: Double = 440.0 @UIParam var amplitude: Double = 0.0 override func build() { // Direct typed wiring — `$amplitude` is a UIParamNode (ControlGenerator) // delivering a per-(sub-)block ramp. addOutputGenerator(Out.Audio(bus: 0, source: $amplitude)) } } @Test func buildWiresParamToOutput() throws { let engine: SynthesisEngine = SynthesisEngine( blockSize: 8, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 8, numControlBuses: 8, driver: OfflineDriver() ) try engine.start() let synth: UIParamInSynthTests.ToneWithParam = ToneWithParam() synth.amplitude = 1.0 try engine.add(synth) engine.processBlock(inputSamples: []) let anyNonZero: Bool = engine.audioBuses[0].contains { $0 != 0.0 } #expect(anyNonZero) } @Test func updatingParamAffectsNextBlock() throws { let engine: SynthesisEngine = SynthesisEngine( blockSize: 8, sampleRate: 44100, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 8, numControlBuses: 8, driver: OfflineDriver() ) try engine.start() let synth: UIParamInSynthTests.ToneWithParam = ToneWithParam() synth.amplitude = 0.0 // silent at first try engine.add(synth) engine.processBlock(inputSamples: []) let allZero: Bool = engine.audioBuses[0].allSatisfy { $0 == 0.0 } #expect(allZero) synth.amplitude = 1.0 engine.processBlock(inputSamples: []) let anyNonZero: Bool = engine.audioBuses[0].contains { $0 != 0.0 } #expect(anyNonZero) } }