// ScratchModelConsoleTests.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 console") @MainActor struct ScratchModelConsoleTests { 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 successfulRunLogsInfo() async { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) _ = await model.run(additive: false) #expect(model.consoleEntries.contains { $0.kind == .info }) } @Test func failedRunLogsErrorEntries() async { let model: LyrebirdScratchModel = makeModel(text: "NotAGenerator(freq: 1)") model.switchMode(to: .graphDSL) _ = await model.run(additive: false) #expect(model.consoleEntries.contains { $0.kind == .error }) #expect(model.consoleEntries.contains { $0.text.contains("NotAGenerator") }) } @Test func reportMessagesAppearAsMessages() async { // A DSL run pushes no messages; drive the log directly to prove the // message → console wiring (drainMessages → .message entries). let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) model.session.appendMessage("hello from snippet") _ = await model.run(additive: false) #expect(model.consoleEntries.contains { $0.kind == .message && $0.text == "hello from snippet" }) } @Test func clearConsoleEmpties() async { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) _ = await model.run(additive: false) #expect(!model.consoleEntries.isEmpty) model.clearConsole() #expect(model.consoleEntries.isEmpty) } @Test func resetSessionClearsConsole() async { let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1") model.switchMode(to: .graphDSL) _ = await model.run(additive: false) #expect(!model.consoleEntries.isEmpty) model.resetSession() #expect(model.consoleEntries.isEmpty) } } #endif