// 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 = Atomic(0.0) let _y: Atomic = Atomic(0.0) let _button: Atomic = Atomic(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..