// EngineStopUnbindTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable @_spi(LyrebirdExtensions) import LyrebirdSynth /// `SynthesisEngine.stop()` teardown contract: every non-persistent node is /// unlinked AND every non-persistent synth's binding is released, so a synth /// that was hosted by a now-stopped engine can be `add`ed again — to the same /// engine or a different one — with no `EngineAddError.doubleAdd` and no /// "wait one processBlock" constraint (unlike `remove(_:)`). @MainActor @Suite("Engine stop unbind") struct EngineStopUnbindTests { /// A synth added, started, and stopped can be re-added to the same /// engine immediately — no intervening `processBlock` needed, because /// `stop()` already halted the RT thread before the unbind pass runs. @Test func reAddSameInstanceAfterStopDoesNotThrowDoubleAdd() throws { let engine: SynthesisEngine = makeTestEngine() try engine.start() let synth: ConstantSynth = ConstantSynth(value: 0.1, outputBus: 0) try engine.add(synth) engine.processBlock(inputSamples: []) #expect(engine.synthesizerCount == 1) engine.stop() // rootGroup / mainGroup persist across stop: rootGroup still holds // exactly the mainGroup PreparedNode, mainGroup is emptied. #expect(engine.rootGroup.count == 1) #expect(engine.mainGroup.count == 0) #expect(engine.synthesizerCount == 0) #expect(engine.groupCount == 0) #expect(throws: Never.self) { try engine.add(synth) } } /// Same synth instance, stopped on one engine, can be added to a /// *different* engine — `stop()`'s unbind pass releases the exclusive /// binding, so this does not throw `.boundToOtherEngine`. @Test func reAddSameInstanceToDifferentEngineAfterStopDoesNotThrowBoundToOther() throws { let engineA: SynthesisEngine = makeTestEngine() let engineB: SynthesisEngine = makeTestEngine() try engineA.start() let synth: ConstantSynth = ConstantSynth(value: 0.2, outputBus: 0) try engineA.add(synth) engineA.processBlock(inputSamples: []) engineA.stop() try engineB.start() #expect(throws: Never.self) { try engineB.add(synth) } engineB.stop() } /// Synths nested inside a `Group` subtree that `stop()` drains are also /// unbound — not just the top-level nodes directly under `mainGroup`. @Test func stopUnbindsSynthsNestedInsideDrainedGroups() throws { let engine: SynthesisEngine = makeTestEngine() try engine.start() let outer: Group = Group() let inner: Group = Group() let synth: ConstantSynth = ConstantSynth(value: 0.3, outputBus: 0) try engine.add(outer) try engine.add(inner, position: .tail(of: outer)) try engine.add(synth, position: .tail(of: inner)) engine.processBlock(inputSamples: []) #expect(engine.synthesizerCount == 1) #expect(engine.groupCount == 2) engine.stop() #expect(engine.synthesizerCount == 0) #expect(engine.groupCount == 0) // The nested synth's binding was released by stop()'s subtree // unbind pass — re-add to a fresh engine succeeds with no // `.doubleAdd` / `.boundToOtherEngine`. let engine2: SynthesisEngine = makeTestEngine() try engine2.start() #expect(throws: Never.self) { try engine2.add(synth) } engine2.stop() } /// Pre-main and post-main nodes (added `.before` / `.after` the persistent /// `mainGroup`, so they live directly under `rootGroup` and NOT inside /// `mainGroup`) are unbound by `stop()` too — the teardown drains the whole /// rootGroup subtree, not just `mainGroup`. @Test func stopUnbindsPreAndPostMainNodes() throws { let engine: SynthesisEngine = makeTestEngine() try engine.start() let pre: ConstantSynth = ConstantSynth(value: 0.5, outputBus: 0) let post: ConstantSynth = ConstantSynth(value: 0.6, outputBus: 0) try engine.add(pre, position: .before(engine.mainGroup)) try engine.add(post, position: .after(engine.mainGroup)) engine.processBlock(inputSamples: []) #expect(engine.synthesizerCount == 2) engine.stop() #expect(engine.synthesizerCount == 0) // rootGroup keeps only the persistent mainGroup; both flanking nodes gone. #expect(engine.mainGroup.count == 0) #expect(engine.rootGroup.count == 1) // Both bindings released — re-add to a fresh engine succeeds. let engine2: SynthesisEngine = makeTestEngine() try engine2.start() #expect(throws: Never.self) { try engine2.add(pre) try engine2.add(post) } engine2.stop() } /// A synth explicitly `remove`d (and drained via `processBlock`) before /// `stop()` is already unbound and no longer in the tree. `stop()`'s /// unbind pass must not trap or double-unbind when it later walks a /// tree that never contained the synth. @Test func removeBeforeStopDoesNotTrapDuringStopUnbindPass() throws { let engine: SynthesisEngine = makeTestEngine() try engine.start() let synth: ConstantSynth = ConstantSynth(value: 0.4, outputBus: 0) try engine.add(synth) engine.processBlock(inputSamples: []) _ = engine.remove(synth) engine.processBlock(inputSamples: []) #expect(engine.synthesizerCount == 0) // Must not trap / corrupt engine state. engine.stop() #expect(engine.synthesizerCount == 0) #expect(engine.mainGroup.count == 0) #expect(engine.rootGroup.count == 1) #expect(throws: Never.self) { try engine.add(synth) } } }