// GeneratedRegistryTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import LyrebirdSynth import LyrebirdGraphDSL @MainActor @Suite("GeneratedDSLRegistry") struct GeneratedRegistryTests { /// Floor on the generated table size — a regression that halved the registry /// (e.g. a scanner change) would trip this rather than pass silently. private static let minimumBuiltinCount: Int = 150 @Test func registryIsPopulatedAndUnique() { #expect(DSLBuiltinRegistry.all.count >= GeneratedRegistryTests.minimumBuiltinCount) // Each (name, rate) is unique — no entry collapsed in the lookup table. let totalEntries: Int = DSLBuiltinRegistry.byName.values.reduce(0) { $0 + $1.count } #expect(totalEntries == DSLBuiltinRegistry.all.count) } @Test func generatorsAvailableAtBothRates() throws { // OscSin exists at audio and control; lookup defaults to audio. #expect(DSLBuiltinRegistry.rates(of: "OscSin") == Set([.audio, .control])) let audio: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin") #expect(audio.rate == .audio) let control: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin", rate: .control) #expect(control.rate == .control) } /// A doubly-nested control-only type (`Mouse.X.Control`) is registered under /// its joined path name (`MouseX`), not the bare inner enum (`X`), and /// constructs via the full `Mouse.X.Control` path. @Test func doublyNestedTypeUsesJoinedNameAndFullConstructor() throws { #expect(DSLBuiltinRegistry.rates(of: "MouseX") == Set([.control])) // The bare inner name must NOT leak into the flat DSL namespace. #expect(DSLBuiltinRegistry.rates(of: "X").isEmpty) let mouseX: DSLBuiltin = try DSLBuiltinRegistry.lookup("MouseX", rate: .control) #expect(mouseX.rate == .control) // Constructs without throwing (no args; reads the mouse input registry). _ = try mouseX.make([:]) } @Test func unavailableRateThrows() throws { // Pan2 is audio-only (no ControlGenerator variant). #expect(DSLBuiltinRegistry.rates(of: "Pan2") == Set([.audio])) #expect(throws: DSLBuildError.rateUnavailable(builtin: "Pan2", rate: "control")) { _ = try DSLBuiltinRegistry.lookup("Pan2", rate: .control) } } @Test func controlRateBuiltinRenders() throws { // A control-rate OscSin drives an audio OscSin's frequency — the kind of // modulation nesting the DSL will produce. let lfoControl: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin", rate: .control) let audio: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin") let lfo: any ValidGeneratorInput = try lfoControl.make(["freq": sig(4.0), "mul": sig(50.0), "add": sig(440.0)]) let graph: any ValidGeneratorInput = try audio.make(["freq": sig(lfo), "mul": sig(0.1)]) let engine: SynthesisEngine = makeDSLTestEngine() try engine.start() try engine.add(ClosureSynthesizer { graph }) engine.processBlock(inputSamples: []) #expect(busHasSignal(engine, bus: 0)) } @Test func wellKnownGeneratorsArePresent() throws { for name: String in ["OscSin", "OscSawtooth", "OscSquare", "WhiteNoise", "Pan2"] { let builtin: DSLBuiltin = try DSLBuiltinRegistry.lookup(name) #expect(builtin.name == name) } } @Test func lookupOfUnknownNameThrows() { #expect(throws: DSLBuildError.unknownBuiltin("NotAGenerator")) { _ = try DSLBuiltinRegistry.lookup("NotAGenerator") } } @Test func requiredParameterIsReportedAndEnforced() throws { // OscSin's `freq` has no default, so it is required. let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin") let freq: DSLParameter? = osc.parameters.first { $0.label == "freq" } #expect(freq?.hasDefault == false) #expect(throws: DSLBuildError.missingArgument(builtin: "OscSin", label: "freq")) { _ = try osc.make([:]) } } /// The generated closure builds a real generator that renders audio. @Test func generatedBuiltinRendersAudio() throws { let osc: DSLBuiltin = try DSLBuiltinRegistry.lookup("OscSin") let graph: any ValidGeneratorInput = try osc.make(["freq": sig(440.0), "mul": sig(0.1)]) let engine: SynthesisEngine = makeDSLTestEngine() try engine.start() try engine.add(ClosureSynthesizer { graph }) engine.processBlock(inputSamples: []) let anyNonZero: Bool = (0..