Files
Lyrebird/LiveCompile/Tests/LyrebirdLiveCompileUITests/ScratchModelRunSelectionTests.swift
T
josh 8519cabe42 v1.4.2 release: live-coding editor — Run Selection, slot release, engine panel, console, manual completion
Additive conveniences to the macOS-only Live Scratch editor. No changes to
existing public API.

- Run Selection (⌘E): evaluate just the highlighted text, additively.
- Graceful slot release: context.release(slot:) / free(slot:).
- Engine panel: live oscilloscope (ScopeCapture) + CPU avg/peak + counts.
- Reset Session / Restart to Reclaim Memory.
- Always-open console panel (report messages + errors with line:col).
- Faster typing: debounced highlight, updateNSView fast-path, publish guards.
- Manual code completion (Esc / F5), no auto-trigger on `.`; completion
  replaces the partial word, not the receiver expression.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-21 11:09:30 -07:00

105 lines
4.1 KiB
Swift

// 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