b6fdee8bfc
Consolidated release: the cross-platform Linux port (engine, generators, CLI, REPL, NRT, portable DSP), live ALSA audio + ALSA MIDI + POSIX OSC + portable WAV codec, the GTK4 GUI (LyrebirdUI on the SwiftOpenUI backend), and the Linux live-coding scratchpad (LiveCompile). Apple platforms unchanged; all additive. See RELEASES.md and LINUX_README.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
143 lines
5.1 KiB
Swift
143 lines
5.1 KiB
Swift
// LiveCompilerTests.swift
|
||
// Lyrebird
|
||
//
|
||
// Copyright (c) 2026 Joshua Parmenter / realizedsound
|
||
// SPDX-License-Identifier: MIT
|
||
|
||
import Testing
|
||
import Foundation
|
||
@testable import LyrebirdLiveCompile
|
||
|
||
#if os(macOS) || os(Linux)
|
||
|
||
@Suite("LiveCompiler")
|
||
struct LiveCompilerTests {
|
||
|
||
/// Marker buffer the snippet writes to via the `ctx` pointer. One element so
|
||
/// we can confirm the entry symbol was actually invoked end-to-end. Held by
|
||
/// the test through a withUnsafeMutablePointer scope so the snippet write
|
||
/// lands in this process's address space.
|
||
@Test
|
||
func compileLoadAndInvokeEntry() throws {
|
||
// Snippet writes a sentinel `Int32` into the context pointer it gets handed.
|
||
let source: String = """
|
||
@_cdecl("lyrebird_snippet_entry")
|
||
public func lyrebird_snippet_entry(_ ctx: UnsafeMutableRawPointer?) {
|
||
guard let ctx else { return }
|
||
ctx.assumingMemoryBound(to: Int32.self).pointee = 0x4C425342 // 'LBSB'
|
||
}
|
||
"""
|
||
|
||
let snippet: CompiledSnippet = try LiveCompiler.compile(source: source)
|
||
#expect(snippet.moduleName.hasPrefix("LBSnippet_"))
|
||
#if os(macOS)
|
||
#expect(snippet.dylibPath.hasSuffix(".dylib"))
|
||
#else
|
||
#expect(snippet.dylibPath.hasSuffix(".so"))
|
||
#endif
|
||
|
||
var marker: Int32 = 0
|
||
withUnsafeMutablePointer(to: &marker) { ptr in
|
||
snippet.entry(UnsafeMutableRawPointer(ptr))
|
||
}
|
||
#expect(marker == 0x4C425342)
|
||
}
|
||
|
||
@Test
|
||
func compileFailureCarriesStderr() {
|
||
let source: String = """
|
||
@_cdecl("lyrebird_snippet_entry")
|
||
public func lyrebird_snippet_entry(_ ctx: UnsafeMutableRawPointer?) {
|
||
let _: Int = "this is a string, not an Int"
|
||
}
|
||
"""
|
||
|
||
do {
|
||
_ = try LiveCompiler.compile(source: source)
|
||
Issue.record("expected compileFailed, got success")
|
||
} catch let LiveCompileError.compileFailed(exitCode, stderr) {
|
||
#expect(exitCode != 0)
|
||
#expect(!stderr.isEmpty)
|
||
// Don't pin exact wording — Swift compiler diagnostics rephrase between
|
||
// toolchain versions. Just confirm the error is present in stderr.
|
||
#expect(stderr.lowercased().contains("error"))
|
||
} catch {
|
||
Issue.record("expected LiveCompileError.compileFailed, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test
|
||
func missingEntrySymbolIsDetected() {
|
||
// Snippet compiles cleanly but exports no `lyrebird_snippet_entry`.
|
||
let source: String = """
|
||
public func someUnrelatedFunction() -> Int { 42 }
|
||
"""
|
||
|
||
do {
|
||
_ = try LiveCompiler.compile(source: source)
|
||
Issue.record("expected entrySymbolMissing, got success")
|
||
} catch let LiveCompileError.entrySymbolMissing(symbol, _) {
|
||
#expect(symbol == lyrebirdSnippetEntrySymbol)
|
||
} catch {
|
||
Issue.record("expected LiveCompileError.entrySymbolMissing, got \(error)")
|
||
}
|
||
}
|
||
|
||
@Test
|
||
func swiftcResolvesToExecutablePath() throws {
|
||
let path: String = try LiveCompiler.findSwiftc()
|
||
#expect(!path.isEmpty)
|
||
// The resolved path must point at an actual executable on disk.
|
||
#expect(FileManager.default.isExecutableFile(atPath: path))
|
||
}
|
||
|
||
// MARK: - Prewarm
|
||
|
||
/// Module-resolution args pointing at the built Lyrebird modules, mirroring
|
||
/// `LiveSession.makeModuleResolutionArgs()`.
|
||
private func moduleResolutionArgs() -> [String] {
|
||
let importPaths: [URL] = LiveSession.defaultImportPaths()
|
||
var args: [String] = []
|
||
for path: URL in importPaths {
|
||
args += ["-I", path.path]
|
||
}
|
||
for url: URL in LiveSession.discoverClangModuleMaps(in: importPaths) {
|
||
args += ["-Xcc", "-fmodule-map-file=\(url.path)"]
|
||
}
|
||
for url: URL in LiveSession.discoverSPMModuleMaps(in: importPaths) {
|
||
args += ["-Xcc", "-fmodule-map-file=\(url.path)"]
|
||
}
|
||
#if os(Linux)
|
||
for url: URL in LiveSession.discoverSourceSystemModuleMaps(in: importPaths) {
|
||
args += ["-Xcc", "-fmodule-map-file=\(url.path)"]
|
||
}
|
||
#endif
|
||
return args
|
||
}
|
||
|
||
/// The prewarm type-checks `import LyrebirdSynth` / `import LyrebirdLiveCompile`;
|
||
/// with real module-resolution args it must succeed (cold ~2–3 s the first
|
||
/// time it builds the `.pcm`). This exercises the mechanism directly, not
|
||
/// behind `LiveSession.prewarm()`'s error-swallowing `try?`.
|
||
@Test
|
||
func prewarmSucceedsWithModuleResolutionArgs() throws {
|
||
try LiveCompiler.prewarm(extraSwiftcArgs: moduleResolutionArgs())
|
||
}
|
||
|
||
@Test
|
||
func prewarmThrowsWhenModulesUnresolvable() {
|
||
// No `-I`: `import LyrebirdSynth` cannot be found → type-check fails.
|
||
do {
|
||
try LiveCompiler.prewarm(extraSwiftcArgs: [])
|
||
Issue.record("expected prewarm to throw with no module paths")
|
||
} catch let LiveCompileError.compileFailed(exitCode, stderr) {
|
||
#expect(exitCode != 0)
|
||
#expect(!stderr.isEmpty)
|
||
} catch {
|
||
Issue.record("expected LiveCompileError.compileFailed, got \(error)")
|
||
}
|
||
}
|
||
}
|
||
|
||
#endif
|