1.0.0 release
This commit is contained in:
@@ -0,0 +1,391 @@
|
||||
// MouseGeneratorTests.swift
|
||||
// Lyrebird
|
||||
//
|
||||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
import Testing
|
||||
import Synchronization
|
||||
@testable @_spi(LyrebirdExtensions) import LyrebirdSynth
|
||||
|
||||
// MARK: - Mouse Generator Tests
|
||||
//
|
||||
// Tests install a `SyntheticMouseInputBridge` that exposes its backing atomics so each
|
||||
// test can write deterministic values without needing a display connection or AppKit.
|
||||
//
|
||||
// Both Mouse-related suites are `.serialized` because `MouseInput` is a process-global
|
||||
// registry. Without serialization, parallel test instances would race on install /
|
||||
// uninstall and the default-bridge tests in `MouseInputDefaultsTests` would observe
|
||||
// stale state from other suites.
|
||||
|
||||
/// Test-only `MouseInputBridge` whose three values are settable via public atomics.
|
||||
final class SyntheticMouseInputBridge: MouseInputBridge, @unchecked Sendable {
|
||||
let _x: Atomic<Double> = Atomic<Double>(0.0)
|
||||
let _y: Atomic<Double> = Atomic<Double>(0.0)
|
||||
let _button: Atomic<Double> = Atomic<Double>(0.0)
|
||||
|
||||
override var x: Double { _x.load(ordering: .relaxed) }
|
||||
override var y: Double { _y.load(ordering: .relaxed) }
|
||||
override var button: Double { _button.load(ordering: .relaxed) }
|
||||
}
|
||||
|
||||
@Suite("Mouse Generators", .serialized)
|
||||
struct MouseGeneratorTests {
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
let bridge: SyntheticMouseInputBridge
|
||||
|
||||
init() {
|
||||
bridge = SyntheticMouseInputBridge()
|
||||
MouseInput.install(bridge)
|
||||
}
|
||||
|
||||
func makeEngine(blockSize: Int = 64) throws -> SynthesisEngine {
|
||||
let engine: SynthesisEngine = makeTestEngine(blockSize: blockSize)
|
||||
try engine.start()
|
||||
return engine
|
||||
}
|
||||
|
||||
// MARK: - Mouse.X
|
||||
|
||||
@Test func xRateIsControl() {
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func xOutputCountMatchesBlockSize() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._x.store(0.5, ordering: .relaxed)
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
#expect(Array(out[0].prefix(blockSize)).count == blockSize)
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func xReadsBridgeValue() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
let targetX: Double = 0.75
|
||||
|
||||
bridge._x.store(targetX, ordering: .relaxed)
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
|
||||
// First block: _rampTarget starts at 0, ramps toward targetX.
|
||||
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph1, count: blockSize)
|
||||
#expect(abs(out1[0][blockSize - 1] - targetX) < 1e-9,
|
||||
"last sample of block 1 should equal target \(targetX), got \(out1[0][blockSize - 1])")
|
||||
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func xFanOutCacheBothCallsReturnSameValues() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._x.store(0.3, ordering: .relaxed)
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
let samples1: [Double] = Array(out1[0].prefix(blockSize))
|
||||
let samples2: [Double] = Array(out2[0].prefix(blockSize))
|
||||
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(samples1[i] == samples2[i],
|
||||
"fan-out mismatch at sample \(i): \(samples1[i]) vs \(samples2[i])")
|
||||
}
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func xStateAdvancesOncePerBlock() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._x.store(0.4, ordering: .relaxed)
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
|
||||
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph1, count: blockSize)
|
||||
|
||||
// Second call in the same segment must be a cache hit — previousOutputValue should
|
||||
// not change (state has not double-advanced).
|
||||
let prevAfterFirst: Double = gen.previousOutputValue
|
||||
_ = gen.calculatedSamples(graph: graph1, count: blockSize)
|
||||
|
||||
#expect(gen.previousOutputValue == prevAfterFirst,
|
||||
"state advanced twice in the same segment (no cache hit)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func xRampReachesTargetByLastSample() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
let targetX: Double = 0.8
|
||||
|
||||
bridge._x.store(targetX, ordering: .relaxed)
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
// Pre-allocated buffer has 512 slots; use explicit index, not `.last`.
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - targetX) < 1e-9,
|
||||
"ramp should reach target at last sample; expected \(targetX), got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Mouse.Y
|
||||
|
||||
@Test func yRateIsControl() {
|
||||
let gen: Mouse.Y.Control = Mouse.Y.Control()
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func yReadsBridgeValue() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
let targetY: Double = 0.25
|
||||
|
||||
bridge._y.store(targetY, ordering: .relaxed)
|
||||
let gen: Mouse.Y.Control = Mouse.Y.Control()
|
||||
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
#expect(abs(out[0][blockSize - 1] - targetY) < 1e-9,
|
||||
"last sample should equal target \(targetY), got \(out[0][blockSize - 1])")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func yFanOutCacheBothCallsReturnSameValues() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._y.store(0.6, ordering: .relaxed)
|
||||
let gen: Mouse.Y.Control = Mouse.Y.Control()
|
||||
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
let samples1: [Double] = Array(out1[0].prefix(blockSize))
|
||||
let samples2: [Double] = Array(out2[0].prefix(blockSize))
|
||||
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(samples1[i] == samples2[i],
|
||||
"fan-out mismatch at sample \(i)")
|
||||
}
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func yRampReachesTargetByLastSample() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
let targetY: Double = 0.1
|
||||
|
||||
bridge._y.store(targetY, ordering: .relaxed)
|
||||
let gen: Mouse.Y.Control = Mouse.Y.Control()
|
||||
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - targetY) < 1e-9,
|
||||
"expected \(targetY) at last sample, got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Mouse.Click
|
||||
|
||||
@Test func clickRateIsControl() {
|
||||
let gen: Mouse.Click.Control = Mouse.Click.Control()
|
||||
#expect(gen.rate == .control)
|
||||
}
|
||||
|
||||
@Test func clickOutputsZeroWhenReleased() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._button.store(0.0, ordering: .relaxed)
|
||||
let gen: Mouse.Click.Control = Mouse.Click.Control()
|
||||
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 0.0) < 1e-9,
|
||||
"expected 0.0 when button released, got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func clickOutputsOneWhenPressed() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._button.store(1.0, ordering: .relaxed)
|
||||
let gen: Mouse.Click.Control = Mouse.Click.Control()
|
||||
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
let lastSample: Double = out[0][blockSize - 1]
|
||||
#expect(abs(lastSample - 1.0) < 1e-9,
|
||||
"expected 1.0 when button pressed, got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func clickFanOutCacheBothCallsReturnSameValues() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._button.store(1.0, ordering: .relaxed)
|
||||
let gen: Mouse.Click.Control = Mouse.Click.Control()
|
||||
|
||||
let out1: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
|
||||
let samples1: [Double] = Array(out1[0].prefix(blockSize))
|
||||
let samples2: [Double] = Array(out2[0].prefix(blockSize))
|
||||
|
||||
for i: Int in 0..<blockSize {
|
||||
#expect(samples1[i] == samples2[i],
|
||||
"fan-out mismatch at sample \(i)")
|
||||
}
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func clickStateAdvancesOncePerBlock() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._button.store(1.0, ordering: .relaxed)
|
||||
let gen: Mouse.Click.Control = Mouse.Click.Control()
|
||||
|
||||
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph1, count: blockSize)
|
||||
|
||||
let prevAfterFirst: Double = gen.previousOutputValue
|
||||
_ = gen.calculatedSamples(graph: graph1, count: blockSize)
|
||||
|
||||
#expect(gen.previousOutputValue == prevAfterFirst,
|
||||
"state advanced twice in the same segment (cache miss when hit expected)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - Ramp interpolation
|
||||
|
||||
@Test func xRampFromPreviousToNewValue() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
// First block: target = 0 → ramp 0→0
|
||||
bridge._x.store(0.0, ordering: .relaxed)
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
|
||||
let graph1: SynthGraph = makeSynthGraph(engine: engine)
|
||||
_ = gen.calculatedSamples(graph: graph1, count: blockSize)
|
||||
advanceBlock(engine)
|
||||
|
||||
// Second block: target changes to 1.0 → should ramp from 0 toward 1.
|
||||
bridge._x.store(1.0, ordering: .relaxed)
|
||||
|
||||
let graph2: SynthGraph = makeSynthGraph(engine: engine)
|
||||
let out2: [[Double]] = gen.calculatedSamples(graph: graph2, count: blockSize)
|
||||
|
||||
// First sample of ramp should be near 0 (ramp start), last sample near 1.
|
||||
let firstSample: Double = out2[0][0]
|
||||
let lastSample: Double = out2[0][blockSize - 1]
|
||||
#expect(firstSample < 0.1,
|
||||
"ramp should begin near 0, got \(firstSample)")
|
||||
#expect(abs(lastSample - 1.0) < 1e-9,
|
||||
"ramp should reach 1.0 at last sample, got \(lastSample)")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
@Test func xFloatValueReturnsPreviousOutputValue() throws {
|
||||
let engine: SynthesisEngine = try makeEngine()
|
||||
let blockSize: Int = engine.blockSize
|
||||
|
||||
bridge._x.store(0.9, ordering: .relaxed)
|
||||
let gen: Mouse.X.Control = Mouse.X.Control()
|
||||
let graph: SynthGraph = makeSynthGraph(engine: engine)
|
||||
|
||||
_ = gen.calculatedSamples(graph: graph, count: blockSize)
|
||||
#expect(gen.floatValue(graph: graph) == gen.previousOutputValue,
|
||||
"floatValue must return previousOutputValue for ControlGenerator")
|
||||
engine.stop()
|
||||
}
|
||||
|
||||
// MARK: - MouseInput registry default behaviour
|
||||
//
|
||||
// These tests live inside the same `.serialized` suite as the bridge-driven Mouse
|
||||
// generator tests so they can not race with `init()`'s `MouseInput.install(...)`.
|
||||
|
||||
@Test func defaultBridgeReturnsCenterAndReleased() {
|
||||
MouseInput.uninstall()
|
||||
defer { MouseInput.install(bridge) } // restore the suite-scoped bridge
|
||||
|
||||
#expect(MouseInput.currentX == 0.5)
|
||||
#expect(MouseInput.currentY == 0.5)
|
||||
#expect(MouseInput.currentButton == 0.0)
|
||||
}
|
||||
|
||||
@Test func currentBridgeIdentityRoundTrips() {
|
||||
defer { MouseInput.install(bridge) }
|
||||
let custom: SyntheticMouseInputBridge = SyntheticMouseInputBridge()
|
||||
MouseInput.install(custom)
|
||||
#expect(MouseInput.currentBridge === custom)
|
||||
}
|
||||
|
||||
@Test func repeatInstallOfSameBridgeIsNoOp() {
|
||||
// Identity-check fast path: re-installing the same bridge must keep that
|
||||
// identity registered and must keep the registry's view of its scalar values
|
||||
// intact across redundant installs. (CFGetRetainCount on Swift class objects
|
||||
// is documented unreliable across runtime versions, so we check observable
|
||||
// identity + scalar consistency rather than a raw refcount.)
|
||||
bridge._x.store(0.314, ordering: .relaxed)
|
||||
MouseInput.install(bridge)
|
||||
MouseInput.install(bridge)
|
||||
MouseInput.install(bridge)
|
||||
#expect(MouseInput.currentBridge === bridge,
|
||||
"install of same bridge must keep identity")
|
||||
#expect(MouseInput.currentX == 0.314,
|
||||
"install of same bridge must not swap to a stale snapshot")
|
||||
}
|
||||
|
||||
@Test func installReplacesPreviousBridge() {
|
||||
defer { MouseInput.install(bridge) } // restore suite-scoped bridge
|
||||
|
||||
let first: SyntheticMouseInputBridge = SyntheticMouseInputBridge()
|
||||
first._x.store(0.1, ordering: .relaxed)
|
||||
MouseInput.install(first)
|
||||
#expect(MouseInput.currentX == 0.1)
|
||||
|
||||
let second: SyntheticMouseInputBridge = SyntheticMouseInputBridge()
|
||||
second._x.store(0.9, ordering: .relaxed)
|
||||
MouseInput.install(second)
|
||||
#expect(MouseInput.currentX == 0.9)
|
||||
}
|
||||
|
||||
@Test func uninstallRestoresDefault() {
|
||||
defer { MouseInput.install(bridge) } // restore suite-scoped bridge
|
||||
|
||||
let custom: SyntheticMouseInputBridge = SyntheticMouseInputBridge()
|
||||
custom._y.store(0.42, ordering: .relaxed)
|
||||
MouseInput.install(custom)
|
||||
#expect(MouseInput.currentY == 0.42)
|
||||
|
||||
MouseInput.uninstall()
|
||||
#expect(MouseInput.currentY == 0.5)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user