255 lines
10 KiB
Swift
255 lines
10 KiB
Swift
// NodeTreeMirrorTests.swift
|
|
// Lyrebird
|
|
//
|
|
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
import Testing
|
|
import Foundation
|
|
@testable import LyrebirdSynth
|
|
|
|
@Suite("NodeTreeMirror")
|
|
struct NodeTreeMirrorTests {
|
|
|
|
// MARK: - Fixtures
|
|
|
|
/// Silent synth that writes nothing — minimal subclass for tree tests.
|
|
private final class SilentSynth: SynthesizerBase {
|
|
override func build() {
|
|
addOutputGenerator(Out.Audio(bus: 0, source: 0.0))
|
|
}
|
|
}
|
|
|
|
private func makeEngine() -> SynthesisEngine {
|
|
SynthesisEngine(
|
|
blockSize: 64,
|
|
sampleRate: 44100,
|
|
numOutputChannels: 2,
|
|
numInputChannels: 0,
|
|
driver: OfflineDriver()
|
|
)
|
|
}
|
|
|
|
// MARK: - Tests
|
|
|
|
@Test("Initial snapshot has rootGroup containing only mainGroup")
|
|
func testInitialSnapshot() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let snap: NodeTreeSnapshot = engine.nodeTreeMirror.snapshot
|
|
#expect(snap.root.kind == .group)
|
|
#expect(snap.root.displayName == "rootGroup")
|
|
#expect(snap.root.children.count == 1)
|
|
let main: NodeTreeSnapshot.Node = snap.root.children[0]
|
|
#expect(main.kind == .group)
|
|
#expect(main.displayName == "mainGroup")
|
|
#expect(main.children.isEmpty)
|
|
}
|
|
|
|
@Test("engine.add lands the synth at mainGroup head")
|
|
func testAddLandsAtMainGroupHead() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let a: SilentSynth = SilentSynth()
|
|
let b: SilentSynth = SilentSynth()
|
|
|
|
try engine.add(a)
|
|
try engine.add(b)
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 2)
|
|
// Newest-first: b was added last, so b sits at index 0.
|
|
#expect(mainNode.children[0].id == ObjectIdentifier(b))
|
|
#expect(mainNode.children[1].id == ObjectIdentifier(a))
|
|
}
|
|
|
|
@Test("try engine.add(_:position: .tail(of: mainGroup)) appends")
|
|
func testAddTailOfMainGroup() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let a: SilentSynth = SilentSynth()
|
|
let b: SilentSynth = SilentSynth()
|
|
|
|
try engine.add(a, position: .tail(of: engine.mainGroup))
|
|
try engine.add(b, position: .tail(of: engine.mainGroup))
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 2)
|
|
#expect(mainNode.children[0].id == ObjectIdentifier(a))
|
|
#expect(mainNode.children[1].id == ObjectIdentifier(b))
|
|
}
|
|
|
|
@Test("engine.remove drops the synth from the mirror")
|
|
func testRemoveDropsSynth() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let a: SilentSynth = SilentSynth()
|
|
try engine.add(a)
|
|
#expect(engine.nodeTreeMirror.snapshot.root.children[0].children.count == 1)
|
|
|
|
_ = engine.remove(a)
|
|
#expect(engine.nodeTreeMirror.snapshot.root.children[0].children.isEmpty)
|
|
}
|
|
|
|
@Test("try engine.add(_:position: .before(_:)) inserts sibling correctly")
|
|
func testInsertBefore() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let anchor: SilentSynth = SilentSynth()
|
|
let newcomer: SilentSynth = SilentSynth()
|
|
|
|
try engine.add(anchor, position: .tail(of: engine.mainGroup))
|
|
try engine.add(newcomer, position: .before(anchor))
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 2)
|
|
#expect(mainNode.children[0].id == ObjectIdentifier(newcomer))
|
|
#expect(mainNode.children[1].id == ObjectIdentifier(anchor))
|
|
}
|
|
|
|
@Test("try engine.add(_:position: .after(_:)) inserts sibling correctly")
|
|
func testInsertAfter() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let anchor: SilentSynth = SilentSynth()
|
|
let newcomer: SilentSynth = SilentSynth()
|
|
|
|
try engine.add(anchor, position: .tail(of: engine.mainGroup))
|
|
try engine.add(newcomer, position: .after(anchor))
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 2)
|
|
#expect(mainNode.children[0].id == ObjectIdentifier(anchor))
|
|
#expect(mainNode.children[1].id == ObjectIdentifier(newcomer))
|
|
}
|
|
|
|
@Test("Nested group with children — add group, add synth into group")
|
|
func testNestedGroup() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let group: Group = Group()
|
|
let child: SilentSynth = SilentSynth()
|
|
|
|
try engine.add(group, position: .tail(of: engine.mainGroup))
|
|
try engine.add(child, position: .head(of: group))
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 1)
|
|
let groupNode: NodeTreeSnapshot.Node = mainNode.children[0]
|
|
#expect(groupNode.id == ObjectIdentifier(group))
|
|
#expect(groupNode.kind == .group)
|
|
#expect(groupNode.children.count == 1)
|
|
#expect(groupNode.children[0].id == ObjectIdentifier(child))
|
|
}
|
|
|
|
@Test("engine.remove(child) leaves the parent group intact in the mirror")
|
|
func testRemoveChildLeavesGroupIntact() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let group: Group = Group()
|
|
let child: SilentSynth = SilentSynth()
|
|
|
|
try engine.add(group, position: .tail(of: engine.mainGroup))
|
|
try engine.add(child, position: .head(of: group))
|
|
#expect(engine.nodeTreeMirror.snapshot.root.children[0].children[0].children.count == 1)
|
|
|
|
_ = engine.remove(child)
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 1, "Group should remain after removing its only child")
|
|
let groupNode: NodeTreeSnapshot.Node = mainNode.children[0]
|
|
#expect(groupNode.id == ObjectIdentifier(group))
|
|
#expect(groupNode.children.isEmpty, "Group should have no children after child remove")
|
|
}
|
|
|
|
@Test("Group.free drops the group + all descendants from the mirror")
|
|
func testGroupFreeDropsSubtree() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let group: Group = Group()
|
|
let child: SilentSynth = SilentSynth()
|
|
|
|
try engine.add(group, position: .tail(of: engine.mainGroup))
|
|
try engine.add(child, position: .head(of: group))
|
|
|
|
group.free(engine: engine)
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.isEmpty, "Group should be removed from mainGroup")
|
|
}
|
|
|
|
@Test("ParallelGroup add — children walk reflects head→tail order")
|
|
func testParallelGroupChildren() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let pg: ParallelGroup = ParallelGroup(engine: engine)
|
|
let a: SilentSynth = SilentSynth()
|
|
let b: SilentSynth = SilentSynth()
|
|
pg.add(a)
|
|
pg.add(b)
|
|
|
|
try engine.add(pg, position: .tail(of: engine.mainGroup))
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 1)
|
|
let pgNode: NodeTreeSnapshot.Node = mainNode.children[0]
|
|
#expect(pgNode.kind == .parallelGroup)
|
|
#expect(pgNode.children.count == 2)
|
|
#expect(pgNode.children[0].id == ObjectIdentifier(a))
|
|
#expect(pgNode.children[1].id == ObjectIdentifier(b))
|
|
}
|
|
|
|
@Test("Synth node displayName is its runtime class name")
|
|
func testDisplayNameMatchesClass() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let s: SilentSynth = SilentSynth()
|
|
try engine.add(s)
|
|
let main: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(main.children.count == 1)
|
|
let synthNode: NodeTreeSnapshot.Node = main.children[0]
|
|
switch synthNode.kind {
|
|
case .synth(let className):
|
|
#expect(className.contains("SilentSynth"))
|
|
default:
|
|
Issue.record("Expected .synth kind, got \(synthNode.kind)")
|
|
}
|
|
}
|
|
|
|
@Test("Group without label renders displayName as plain \"Group\"")
|
|
func testUnlabeledGroupDisplayName() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let g: Group = Group()
|
|
try engine.add(g)
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
let groupNode: NodeTreeSnapshot.Node = mainNode.children[0]
|
|
#expect(groupNode.displayName == "Group")
|
|
}
|
|
|
|
@Test("Group with label renders displayName as Group (\"Label\")")
|
|
func testLabeledGroupDisplayName() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let g: Group = Group(label: "drums")
|
|
try engine.add(g)
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
let groupNode: NodeTreeSnapshot.Node = mainNode.children[0]
|
|
#expect(groupNode.displayName == "Group (\"drums\")")
|
|
#expect(g.label == "drums")
|
|
}
|
|
|
|
@Test("ParallelGroup with label renders displayName as ParallelGroup (\"Label\")")
|
|
func testLabeledParallelGroupDisplayName() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let pg: ParallelGroup = ParallelGroup(engine: engine, label: "voices")
|
|
pg.add(SilentSynth())
|
|
try engine.add(pg)
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
let pgNode: NodeTreeSnapshot.Node = mainNode.children[0]
|
|
#expect(pgNode.displayName == "ParallelGroup (\"voices\")")
|
|
#expect(pg.label == "voices")
|
|
}
|
|
|
|
@Test(".before(_:) with an unknown anchor falls back to mainGroup head")
|
|
func testBeforeFallback() throws {
|
|
let engine: SynthesisEngine = makeEngine()
|
|
let stranger: SilentSynth = SilentSynth() // never added to engine
|
|
let newcomer: SilentSynth = SilentSynth()
|
|
|
|
// Anchor not in mirror — engine docs say fall back to mainGroup head.
|
|
try engine.add(newcomer, position: .before(stranger))
|
|
|
|
let mainNode: NodeTreeSnapshot.Node = engine.nodeTreeMirror.snapshot.root.children[0]
|
|
#expect(mainNode.children.count == 1)
|
|
#expect(mainNode.children[0].id == ObjectIdentifier(newcomer))
|
|
}
|
|
}
|