// LiveSessionTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation import LyrebirdSynth @testable import LyrebirdLiveCompile #if os(macOS) @Suite("LiveSession") struct LiveSessionTests { /// NRT engine — `start()` is not called. We never pull audio. The session /// holds a reference; the snippet reads `engine.sampleRate` via `context`. private func makeEngine() -> SynthesisEngine { SynthesisEngine( blockSize: 64, controlBlockSize: 16, sampleRate: 48_000, numOutputChannels: 2, numInputChannels: 0, numAudioBuses: 16, numControlBuses: 16, eventQueueCapacity: lyrebirdDefaultEventQueueCapacity, driver: OfflineDriver() ) } @Test func buildDirDiscoveryFindsLyrebirdDylib() throws { let buildDir: URL = try #require(LiveSession.currentBuildDir()) let dylib: URL = buildDir.appendingPathComponent("libLyrebirdDynamic.dylib") #expect(FileManager.default.fileExists(atPath: dylib.path)) } @Test func snippetReadsEngineSampleRateViaContext() throws { let engine: SynthesisEngine = makeEngine() let session: LiveSession = LiveSession(engine: engine) try session.compileAndRun("context.reportSampleRate(context.engine.sampleRate)") #expect(session.reportedSampleRate == 48_000) } @Test func snippetReportMessageRoundTripsThroughSession() throws { let engine: SynthesisEngine = makeEngine() let session: LiveSession = LiveSession(engine: engine) try session.compileAndRun("context.reportMessage(\"hello from snippet\")") #expect(session.lastSnippetMessage == "hello from snippet") } @Test func reEvaluationClearsLastMessageStateThenReruns() throws { let engine: SynthesisEngine = makeEngine() let session: LiveSession = LiveSession(engine: engine) try session.compileAndRun("context.reportMessage(\"first\")") #expect(session.lastSnippetMessage == "first") try session.compileAndRun("context.reportMessage(\"second\")") #expect(session.lastSnippetMessage == "second") } @Test func asyncCompileAndRunRoundTripsMessage() async throws { let engine: SynthesisEngine = makeEngine() let session: LiveSession = LiveSession(engine: engine) _ = try await session.compileAndRunAsync( "context.reportMessage(\"async hello\")" ) #expect(session.lastSnippetMessage == "async hello") } @Test func asyncCompileFailureSurfacesDiagnosticsThroughSnippetCompileFailed() async { let engine: SynthesisEngine = makeEngine() let session: LiveSession = LiveSession(engine: engine) do { _ = try await session.compileAndRunAsync("let x: Int = \"nope\"\n") Issue.record("expected snippetCompileFailed") } catch let LiveSessionError.snippetCompileFailed(diags, _) { #expect(diags.isEmpty == false) #expect(diags.contains(where: { $0.severity == .error })) } catch { Issue.record("unexpected error: \(error)") } } @Test func additiveReEvaluationDoesNotClearTrackedSynths() throws { let engine: SynthesisEngine = makeEngine() let session: LiveSession = LiveSession(engine: engine) // No synths added in either run; just verify the additive branch executes // without nuking session state. try session.compileAndRun("context.reportMessage(\"a\")", additive: false) try session.compileAndRun("context.reportMessage(\"b\")", additive: true) #expect(session.lastSnippetMessage == "b") #expect(session.trackedSynthCount == 0) } @Test func swiftcArgsIncludeImportLibrarySearchAndRpathFlags() { let buildDir: URL = URL(fileURLWithPath: "/tmp/fake-build") let session: LiveSession = LiveSession( engine: makeEngine(), importPaths: [buildDir], librarySearchPaths: [buildDir], libraries: ["Lyrebird"], rpaths: [buildDir] ) // Indirectly verify makeSwiftcArgs via attempting a compile that will // fail with the fake paths but expose the flags through the stderr. do { _ = try session.compileAndRun("// empty body") Issue.record("expected compile failure with fake build dir") } catch let LiveSessionError.snippetCompileFailed(_, underlying) { guard case .compileFailed(_, let stderr) = underlying else { Issue.record("expected compileFailed underlying, got \(underlying)") return } // swiftc rejects with "no such module 'Lyrebird'" — that's the proof // the -I /tmp/fake-build flag was passed and searched. #expect(stderr.contains("Lyrebird") || stderr.contains("module")) } catch { Issue.record("expected snippetCompileFailed, got \(error)") } } } #endif