// LiveCompilerTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable import LyrebirdLiveCompile #if os(macOS) @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_")) #expect(snippet.dylibPath.hasSuffix(".dylib")) 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)"] } 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