Files
Lyrebird/Tests/LyrebirdCLITests/WorkflowIntegrationTests.swift
T

189 lines
8.6 KiB
Swift

// WorkflowIntegrationTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
import Testing
import Foundation
@testable import LyrebirdCLI
// MARK: - Workflow integration
/// End-to-end tests verifying the REPL → compile workflow:
///
/// 1. `ProjectScaffolder.scaffold` + `SynthExporter.export` produce the correct file tree.
/// 2. Every generated file is syntactically valid Swift (verified with `swiftc -parse`).
///
/// These tests verify that a piece authored in the interactive REPL can be exported
/// and placed in a compiled Lyrebird project without any manual fixups. `swiftc -parse`
/// catches template regressions (missing braces, malformed declarations, etc.) at
/// parse time without requiring Lyrebird to be resolved as a package dependency.
@Suite("Workflow integration")
struct WorkflowIntegrationTests {
// MARK: - Helpers
/// Creates a unique temporary directory path (not yet on disk).
private func temporaryDir() -> String {
FileManager.default.temporaryDirectory
.appendingPathComponent("LyrebirdWorkflowTest_\(UUID().uuidString)")
.path
}
/// Derives the path to `swiftc` from the toolchain that `ProjectRunner` locates.
///
/// Returns `nil` if no toolchain is found — callers should treat `nil` as "skip
/// parse verification" rather than a test failure.
private func swiftcPath() -> String? {
guard let swiftPath: String = try? ProjectRunner.findSwift() else { return nil }
let swiftcPath: String = URL(fileURLWithPath: swiftPath)
.deletingLastPathComponent()
.appendingPathComponent("swiftc")
.path
guard FileManager.default.isExecutableFile(atPath: swiftcPath) else { return nil }
return swiftcPath
}
/// Writes `source` to a temp file and runs `swiftc -parse` on it.
///
/// - Returns: `true` if the file parses successfully (exit code 0) **or** if
/// `swiftc` could not be located (graceful skip on toolchain-less environments).
private func parseCheck(source: String, fileName: String) throws -> Bool {
guard let swiftc: String = swiftcPath() else { return true }
let tmpDir: String = temporaryDir()
try FileManager.default.createDirectory(
atPath: tmpDir,
withIntermediateDirectories: true,
attributes: nil
)
defer { try? FileManager.default.removeItem(atPath: tmpDir) }
let filePath: String = URL(fileURLWithPath: tmpDir)
.appendingPathComponent(fileName)
.path
try source.write(toFile: filePath, atomically: true, encoding: .utf8)
let process: Process = Process()
process.executableURL = URL(fileURLWithPath: swiftc)
process.arguments = ["-parse", filePath]
// Suppress compiler output — we only care about the exit code.
process.standardOutput = Pipe()
process.standardError = Pipe()
try process.run()
process.waitUntilExit()
return process.terminationStatus == 0
}
// MARK: - Scaffold + export file tree
/// Scaffolding a project and exporting a synthesizer into it must produce the
/// expected directory tree, including both the core project files and the new
/// synthesizer source file.
@Test func scaffoldAndExportProduceCorrectFileTree() throws {
let parentDir: String = temporaryDir()
try FileManager.default.createDirectory(
atPath: parentDir,
withIntermediateDirectories: true,
attributes: nil
)
defer { try? FileManager.default.removeItem(atPath: parentDir) }
try ProjectScaffolder.scaffold(name: "MyPiece", in: parentDir)
let projectRoot: String = URL(fileURLWithPath: parentDir)
.appendingPathComponent("MyPiece")
.path
let sourcesDir: String = (projectRoot as NSString).appendingPathComponent("Sources/MyPiece")
let synthPath: String = (sourcesDir as NSString).appendingPathComponent("Tone.swift")
// Export the synth into the scaffolded project's Sources directory.
try SynthExporter.export(name: "Tone", to: synthPath)
let fm: FileManager = FileManager.default
#expect(fm.fileExists(atPath: (projectRoot as NSString).appendingPathComponent("Package.swift")))
#expect(fm.fileExists(atPath: (sourcesDir as NSString).appendingPathComponent("main.swift")))
#expect(fm.fileExists(atPath: (projectRoot as NSString)
.appendingPathComponent("Tests/MyPieceTests/MyPieceTests.swift")))
#expect(fm.fileExists(atPath: synthPath),
"Exported Tone.swift must exist in the project's Sources directory")
}
// MARK: - Syntax validity
/// The synthesizer template generated by `SynthExporter` must be valid Swift at the
/// syntax level. This catches missing braces, broken declarations, or doc-comment
/// formatting bugs that would prevent the file from compiling in a real project.
@Test func exportedSynthTemplateIsValidSwiftSyntax() throws {
let source: String = SynthExporter.synthTemplate(name: "BassLine")
#expect(try parseCheck(source: source, fileName: "BassLine.swift"),
"SynthExporter template must pass swiftc -parse")
}
/// The `main.swift` template scaffolded by `ProjectScaffolder` must be valid Swift.
@Test func scaffoldedMainSwiftIsValidSwiftSyntax() throws {
let source: String = ProjectScaffolder.mainSwift(name: "MyPiece")
#expect(try parseCheck(source: source, fileName: "main.swift"),
"Scaffolded main.swift must pass swiftc -parse")
}
/// The test-file template scaffolded by `ProjectScaffolder` must be valid Swift.
@Test func scaffoldedTestsFileIsValidSwiftSyntax() throws {
let source: String = ProjectScaffolder.testsSwift(name: "MyPiece")
#expect(try parseCheck(source: source, fileName: "MyPieceTests.swift"),
"Scaffolded tests file must pass swiftc -parse")
}
// MARK: - End-to-end workflow
/// All files produced by the full `lyrebird new` → `lyrebird export` workflow must
/// pass a Swift syntax check. This is the primary regression guard for the
/// REPL → compile transition: if any template drifts into invalid syntax the test
/// catches it before it reaches users.
@Test func fullWorkflowAllGeneratedFilesAreValidSwiftSyntax() throws {
let synthSource: String = SynthExporter.synthTemplate(name: "KickDrum")
let mainSource: String = ProjectScaffolder.mainSwift(name: "MyPiece")
let testsSource: String = ProjectScaffolder.testsSwift(name: "MyPiece")
#expect(try parseCheck(source: synthSource, fileName: "KickDrum.swift"),
"Exported synth template must pass swiftc -parse")
#expect(try parseCheck(source: mainSource, fileName: "main.swift"),
"Scaffolded main.swift must pass swiftc -parse")
#expect(try parseCheck(source: testsSource, fileName: "MyPieceTests.swift"),
"Scaffolded tests file must pass swiftc -parse")
}
// MARK: - Template syntax validity
@Test func demoTemplateMainSwiftIsValidSwiftSyntax() throws {
let source: String = ProjectScaffolder.mainSwift(name: "MyPiece", template: .demo)
#expect(try parseCheck(source: source, fileName: "main.swift"),
"demo template main.swift must pass swiftc -parse")
}
@Test func uiDemoTemplateMainSwiftIsValidSwiftSyntax() throws {
let source: String = ProjectScaffolder.mainSwift(name: "MyApp", template: .uiDemo)
#expect(try parseCheck(source: source, fileName: "main.swift"),
"ui-demo template main.swift must pass swiftc -parse")
}
@Test func uiDemoAppDelegateSwiftIsValidSwiftSyntax() throws {
let source: String = ProjectScaffolder.appDelegateSwift(name: "MyApp")
#expect(try parseCheck(source: source, fileName: "AppDelegate.swift"),
"ui-demo AppDelegate.swift must pass swiftc -parse")
}
@Test func uiDemoSynthSwiftIsValidSwiftSyntax() throws {
let source: String = ProjectScaffolder.synthSwift(name: "MyApp")
#expect(try parseCheck(source: source, fileName: "MyAppSynth.swift"),
"ui-demo Synth.swift must pass swiftc -parse")
}
@Test func nrtTemplateMainSwiftIsValidSwiftSyntax() throws {
let source: String = ProjectScaffolder.mainSwift(name: "MyRender", template: .nrt)
#expect(try parseCheck(source: source, fileName: "main.swift"),
"nrt template main.swift must pass swiftc -parse")
}
}