// ScratchModelRunSelectionTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT #if os(macOS) import Testing import LyrebirdSynth @testable import LyrebirdLiveCompile @testable import LyrebirdLiveCompileUI @Suite("LyrebirdScratchModel run selection") @MainActor struct ScratchModelRunSelectionTests { private func makeModel(text: String) -> LyrebirdScratchModel { let engine: SynthesisEngine = SynthesisEngine( blockSize: 64, controlBlockSize: 16, sampleRate: 48_000, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 16, numControlBuses: 16, eventQueueCapacity: lyrebirdDefaultEventQueueCapacity, driver: OfflineDriver() ) return LyrebirdScratchModel(session: LiveSession(engine: engine), initialText: text) } @Test func hasSelectionReflectsNonWhitespaceSelection() { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") #expect(model.hasSelection == false) model.updateSelection("OscSin(freq: 220) * 0.1") #expect(model.hasSelection) model.updateSelection(" \n ") // whitespace only #expect(model.hasSelection == false) model.updateSelection("") #expect(model.hasSelection == false) } @Test func runSelectionWithNoSelectionIsNoOp() async { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) let ok: Bool = await model.runSelection() #expect(ok == false) #expect(model.trackedSynthCount == 0) } @Test func runSelectionAddsToRunningWithoutDisturbingBaseline() async { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) // Whole-buffer run establishes the baseline → clean, one synth. _ = await model.run(additive: false) #expect(model.reevalState == .clean) #expect(model.trackedSynthCount == 1) // Run a selected fragment: additive (adds a second synth) and leaves the // whole-buffer baseline intact (buffer still matches the last whole run). model.updateSelection("OscSin(freq: 220) * 0.1") let ok: Bool = await model.runSelection() #expect(ok) #expect(model.trackedSynthCount == 2) #expect(model.reevalState == .clean) // baseline untouched #expect(model.diagnostics.isEmpty) } @Test func selectionRunAddsEvenFromADirtyBuffer() async { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) _ = await model.run(additive: false) // tracked 1, .clean // Dirty the buffer (edited since the last whole run). model.text = "OscSin(freq: 440) * 0.1\n# edited" #expect(model.reevalState == .dirty) model.updateSelection("OscSin(freq: 330) * 0.1") _ = await model.runSelection() // additive → tracked 2 model.updateSelection("OscSin(freq: 110) * 0.1") _ = await model.runSelection() // additive → tracked 3 #expect(model.trackedSynthCount == 3) // The selection runs never replaced, and the buffer stays dirty. #expect(model.reevalState == .dirty) } @Test func failedSelectionRunLeavesWholeBufferBadgeUntouched() async { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) _ = await model.run(additive: false) // .clean #expect(model.reevalState == .clean) // A selection with an unknown generator fails to build. model.updateSelection("NotAGenerator(freq: 1)") let ok: Bool = await model.runSelection() #expect(ok == false) #expect(!model.diagnostics.isEmpty) // The whole-buffer badge must stay clean — the buffer itself is fine. #expect(model.reevalState == .clean) #expect(model.statusMessage.hasPrefix("Selection")) } } #endif