// ProjectScaffolderTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable import LyrebirdCLI @Suite("ProjectScaffolder") struct ProjectScaffolderTests { // MARK: - Name validation @Test func validNamePasses() throws { // Should not throw #expect(throws: Never.self) { try ProjectScaffolder.validateName("MySynth") } } @Test func validNameWithUnderscorePasses() throws { #expect(throws: Never.self) { try ProjectScaffolder.validateName("My_Synth_01") } } @Test func emptyNameThrows() { #expect(throws: ProjectScaffolder.Error.invalidName(name: "")) { try ProjectScaffolder.validateName("") } } @Test func nameStartingWithDigitThrows() { #expect(throws: ProjectScaffolder.Error.invalidName(name: "1Bad")) { try ProjectScaffolder.validateName("1Bad") } } @Test func nameWithSpaceThrows() { #expect(throws: ProjectScaffolder.Error.invalidName(name: "My Synth")) { try ProjectScaffolder.validateName("My Synth") } } @Test func nameWithHyphenThrows() { #expect(throws: ProjectScaffolder.Error.invalidName(name: "my-synth")) { try ProjectScaffolder.validateName("my-synth") } } @Test func nonASCIINameThrows() { // Unicode letters are not valid SPM module names. #expect(throws: ProjectScaffolder.Error.invalidName(name: "Синт")) { try ProjectScaffolder.validateName("Синт") } } // MARK: - packageManifest template @Test func packageManifestContainsName() { let manifest: String = ProjectScaffolder.packageManifest(name: "MyProject") #expect(manifest.contains("MyProject")) } @Test func packageManifestHasSwiftToolsVersion() { let manifest: String = ProjectScaffolder.packageManifest(name: "Foo") #expect(manifest.contains("swift-tools-version: 6.0")) } @Test func packageManifestTargetsMacOS15() { let manifest: String = ProjectScaffolder.packageManifest(name: "Foo") #expect(manifest.contains(".macOS(.v15)")) } @Test func packageManifestHasLyrebirdDependency() { let manifest: String = ProjectScaffolder.packageManifest(name: "Foo") #expect(manifest.contains("Lyrebird")) } // MARK: - mainSwift template @Test func mainSwiftContainsName() { let source: String = ProjectScaffolder.mainSwift(name: "Demo") #expect(source.contains("Demo")) } @Test func mainSwiftImportsLyrebird() { let source: String = ProjectScaffolder.mainSwift(name: "Demo") #expect(source.contains("import LyrebirdSynth")) } @Test func mainSwiftDefersDriverChoiceToTheLibrary() { // A scaffolded CLI project has to build wherever it was created, and // which drivers exist is not something consumer code can test for: the // Linux driver is absent from a build without libasound, and from a // Linux binary cross-compiled on a Mac. Naming a concrete driver — or // writing the `#if` in the emitted source — produces a project that // fails to compile on those configurations. for template: ProjectTemplate in [.quickstart, .basic, .demo] { let source: String = ProjectScaffolder.mainSwift(name: "Demo", template: template) #expect(source.contains("makeDefaultAudioDriver()"), "\(template)") #expect(source.contains("driver: driver"), "\(template)") #expect(!source.contains("CoreAudioDriver()"), "\(template)") #expect(!source.contains("ALSAAudioDriver()"), "\(template)") } } @Test func appKitTemplateStaysAppleOnly() { // The GUI template imports AppKit, so it is macOS-only by construction; // a platform switch there would imply a portability it does not have. let source: String = ProjectScaffolder.mainSwift(name: "Demo", template: .uiDemo) #expect(source.contains("import AppKit")) #expect(source.contains("CoreAudioDriver()")) #expect(!source.contains("ALSAAudioDriver")) } // MARK: - testsSwift template @Test func testsSwiftContainsName() { let source: String = ProjectScaffolder.testsSwift(name: "Demo") #expect(source.contains("Demo")) } @Test func testsSwiftImportsTestingAndLyrebird() { let source: String = ProjectScaffolder.testsSwift(name: "Demo") #expect(source.contains("import Testing")) #expect(source.contains("import LyrebirdSynth")) } @Test func testsSwiftReferencesOfflineDriver() { let source: String = ProjectScaffolder.testsSwift(name: "Demo") #expect(source.contains("OfflineDriver")) } // MARK: - scaffold creates files @Test func scaffoldCreatesDirectoryTree() throws { let tmp: String = FileManager.default.temporaryDirectory .appendingPathComponent("LyrebirdScaffolderTest_\(UUID().uuidString)") .path try FileManager.default.createDirectory(atPath: tmp, withIntermediateDirectories: true, attributes: nil) defer { try? FileManager.default.removeItem(atPath: tmp) } try ProjectScaffolder.scaffold(name: "TestProject", in: tmp) let fm: FileManager = FileManager.default let root: String = (tmp as NSString).appendingPathComponent("TestProject") #expect(fm.fileExists(atPath: root)) #expect(fm.fileExists(atPath: (root as NSString).appendingPathComponent("Package.swift"))) #expect(fm.fileExists(atPath: (root as NSString).appendingPathComponent("Sources/TestProject/main.swift"))) #expect(fm.fileExists(atPath: (root as NSString).appendingPathComponent("Tests/TestProjectTests/TestProjectTests.swift"))) #expect(fm.fileExists(atPath: (root as NSString).appendingPathComponent("TestProject.xcworkspace/contents.xcworkspacedata"))) } // MARK: - workspace template @Test func workspaceContentsReferencesPackage() { let contents: String = ProjectScaffolder.workspaceContents() // Must ref the package *directory* (`group:.`), not the Package.swift // file — the file form loads a loose document with no schemes. #expect(contents.contains("group:.")) #expect(!contents.contains("group:Package.swift")) #expect(contents.contains("/Sources/LyrebirdCLI/X.swift → up 3 == . let synthetic: String = (tmp as NSString) .appendingPathComponent("Sources/LyrebirdCLI/X.swift") #expect(ProjectScaffolder.lyrebirdCheckoutRoot(forSourceFile: synthetic) == tmp) } @Test func checkoutRootNilWhenNoManifest() { let tmp: String = FileManager.default.temporaryDirectory .appendingPathComponent("LyrebirdCheckoutNil_\(UUID().uuidString)") .path try? FileManager.default.createDirectory(atPath: tmp, withIntermediateDirectories: true, attributes: nil) defer { try? FileManager.default.removeItem(atPath: tmp) } let synthetic: String = (tmp as NSString).appendingPathComponent("a/b/c.swift") #expect(ProjectScaffolder.lyrebirdCheckoutRoot(forSourceFile: synthetic) == nil) } @Test func checkoutRootNilWhenManifestNotLyrebird() { let tmp: String = FileManager.default.temporaryDirectory .appendingPathComponent("LyrebirdCheckoutForeign_\(UUID().uuidString)") .path try? FileManager.default.createDirectory(atPath: tmp, withIntermediateDirectories: true, attributes: nil) defer { try? FileManager.default.removeItem(atPath: tmp) } let manifest: String = (tmp as NSString).appendingPathComponent("Package.swift") try? "// some unrelated SwiftPM package".write(toFile: manifest, atomically: true, encoding: .utf8) let synthetic: String = (tmp as NSString).appendingPathComponent("a/b/c.swift") #expect(ProjectScaffolder.lyrebirdCheckoutRoot(forSourceFile: synthetic) == nil) } @Test func macOSAppPBXProjUsesLocalPackageRefWhenPathSet() { let pbx: String = ProjectScaffolder.macOSAppPBXProj( name: "Foo", lyrebirdPath: "/abs/path/Lyrebird" ) #expect(pbx.contains("XCLocalSwiftPackageReference")) #expect(pbx.contains(#"relativePath = "/abs/path/Lyrebird""#)) #expect(!pbx.contains("XCRemoteSwiftPackageReference")) #expect(!pbx.contains(ProjectScaffolder.lyrebirdPackageURL)) } @Test func iOSAppPBXProjUsesLocalPackageRefWhenPathSet() { let pbx: String = ProjectScaffolder.iOSAppPBXProj( name: "Foo", lyrebirdPath: "/abs/path/Lyrebird" ) #expect(pbx.contains("XCLocalSwiftPackageReference")) #expect(pbx.contains(#"relativePath = "/abs/path/Lyrebird""#)) #expect(!pbx.contains("XCRemoteSwiftPackageReference")) } @Test func auv3PBXProjUsesLocalPackageRefWhenPathSet() { let pbx: String = ProjectScaffolder.auv3PBXProj( name: "Foo", lyrebirdPath: "/abs/path/Lyrebird" ) #expect(pbx.contains("XCLocalSwiftPackageReference")) #expect(pbx.contains(#"relativePath = "/abs/path/Lyrebird""#)) } @Test func pbxQuoteEscapeHandlesQuotesAndBackslashes() { #expect(ProjectScaffolder.pbxQuoteEscape("/plain/path") == "/plain/path") #expect(ProjectScaffolder.pbxQuoteEscape(#"/has"quote"#) == #"/has\"quote"#) #expect(ProjectScaffolder.pbxQuoteEscape(#"/has\backslash"#) == #"/has\\backslash"#) // Backslash before quote: escape backslash first, then quote. #expect(ProjectScaffolder.pbxQuoteEscape(#"/a\"b"#) == #"/a\\\"b"#) } @Test func macOSAppPBXProjEscapesPathInLocalRef() { let pbx: String = ProjectScaffolder.macOSAppPBXProj( name: "Foo", lyrebirdPath: #"/has"quote/Lyrebird"# ) #expect(pbx.contains(#"relativePath = "/has\"quote/Lyrebird";"#)) } @Test func packageManifestEscapesPathInLocalRef() { let manifest: String = ProjectScaffolder.packageManifest( name: "Foo", template: .basic, lyrebirdPath: #"/has"quote/Lyrebird"# ) #expect(manifest.contains(#".package(name: "Lyrebird", path: "/has\"quote/Lyrebird")"#)) } @Test func scaffoldWithLyrebirdPathEmitsLocalRefs() throws { let fm: FileManager = FileManager.default let tmp: String = fm.temporaryDirectory .appendingPathComponent("LyrebirdScaffolderTest_\(UUID().uuidString)").path try fm.createDirectory(atPath: tmp, withIntermediateDirectories: true, attributes: nil) defer { try? fm.removeItem(atPath: tmp) } try ProjectScaffolder.scaffold( name: "WithPath", template: .macosUIXcode, in: tmp, lyrebirdPath: "/local/Lyrebird" ) let pbxPath: String = (tmp as NSString) .appendingPathComponent("WithPath/WithPath.xcodeproj/project.pbxproj") let pbx: String = try String(contentsOfFile: pbxPath, encoding: .utf8) #expect(pbx.contains("XCLocalSwiftPackageReference")) #expect(pbx.contains("/local/Lyrebird")) } @Test func macOSAppPBXProjWiresEntitlementsAndHardenedRuntime() { let pbx: String = ProjectScaffolder.macOSAppPBXProj(name: "Perf") // Hardened Runtime + entitlements are both required for a notarized, // distributable performance app that opens the AUHAL input bus. #expect(pbx.contains("ENABLE_HARDENED_RUNTIME = YES")) #expect(pbx.contains("CODE_SIGN_ENTITLEMENTS = Perf/Perf.entitlements;")) #expect(pbx.contains("Perf.entitlements */ = {isa = PBXFileReference")) } @Test func macOSAppEntitlementsGrantAudioInputAndAreNotSandboxed() { let ent: String = ProjectScaffolder.macOSAppEntitlements() #expect(ent.contains("com.apple.security.device.audio-input")) // A live-performance app must not be sandboxed — it needs free file / // device / MIDI / OSC access the sandbox would gate. #expect(!ent.contains("com.apple.security.app-sandbox")) } @Test func macOSAppInfoPlistHasMicrophoneUsageDescription() { let plist: String = ProjectScaffolder.macOSAppInfoPlist(name: "Perf") // Absent, a Hardened-Runtime process crashes on audio-input access. #expect(plist.contains("NSMicrophoneUsageDescription")) } @Test func iOSAppPBXProjReferencesIPhoneOS() { let pbx: String = ProjectScaffolder.iOSAppPBXProj(name: "Foo") #expect(pbx.contains("SDKROOT = iphoneos")) #expect(pbx.contains("IPHONEOS_DEPLOYMENT_TARGET = 18.0")) #expect(pbx.contains("TARGETED_DEVICE_FAMILY")) } @Test func auv3PBXProjHasTwoTargets() { let pbx: String = ProjectScaffolder.auv3PBXProj(name: "Foo") // Two PBXNativeTarget entries let nativeTargetCount: Int = pbx.components(separatedBy: "isa = PBXNativeTarget").count - 1 #expect(nativeTargetCount == 2) #expect(pbx.contains("FooExtension")) #expect(pbx.contains("PBXCopyFilesBuildPhase")) // embed extension phase #expect(pbx.contains("dstSubfolderSpec = 13")) // Plug-ins folder #expect(pbx.contains("app-extension")) } // MARK: - demo template @Test func demoMainSwiftImportsLiveParam() { let source: String = ProjectScaffolder.mainSwift(name: "Demo", template: .demo) #expect(source.contains("@LiveParam")) #expect(source.contains("SineTone")) // Driver choice belongs to the library — see // `mainSwiftDefersDriverChoiceToTheLibrary`. #expect(source.contains("makeDefaultAudioDriver()")) } @Test func demoMainSwiftContainsName() { let source: String = ProjectScaffolder.mainSwift(name: "MyPiece", template: .demo) #expect(source.contains("MyPiece")) } // MARK: - ui-demo template @Test func uiDemoPackageManifestIncludesLyrebirdUI() { let manifest: String = ProjectScaffolder.packageManifest(name: "MyApp", template: .uiDemo) #expect(manifest.contains("LyrebirdUI")) } @Test func uiDemoMainSwiftUsesNSApplication() { let source: String = ProjectScaffolder.mainSwift(name: "MyApp", template: .uiDemo) #expect(source.contains("NSApplication")) #expect(source.contains("AppDelegate")) } @Test func appDelegateSwiftContainsName() { let source: String = ProjectScaffolder.appDelegateSwift(name: "MyApp") #expect(source.contains("MyAppSynth")) #expect(source.contains("LyrebirdWindow")) #expect(source.contains("ParameterSlider")) } @Test func synthSwiftContainsUIParam() { let source: String = ProjectScaffolder.synthSwift(name: "MyApp") #expect(source.contains("@UIParam")) #expect(source.contains("MyAppSynth")) } @Test func uiDemoScaffoldCreatesAllFiles() throws { let tmp: String = FileManager.default.temporaryDirectory .appendingPathComponent("LyrebirdScaffolderTest_\(UUID().uuidString)") .path try FileManager.default.createDirectory(atPath: tmp, withIntermediateDirectories: true, attributes: nil) defer { try? FileManager.default.removeItem(atPath: tmp) } try ProjectScaffolder.scaffold(name: "MyApp", template: .uiDemo, in: tmp) let fm: FileManager = FileManager.default let root: String = (tmp as NSString).appendingPathComponent("MyApp") let sources: String = (root as NSString).appendingPathComponent("Sources/MyApp") #expect(fm.fileExists(atPath: (sources as NSString).appendingPathComponent("main.swift"))) #expect(fm.fileExists(atPath: (sources as NSString).appendingPathComponent("AppDelegate.swift"))) #expect(fm.fileExists(atPath: (sources as NSString).appendingPathComponent("MyAppSynth.swift"))) } // MARK: - quickstart template @Test func quickstartMainSwiftPlaysThenExits() { let source: String = ProjectScaffolder.mainSwift(name: "MyFirstSound", template: .quickstart) // The pedagogical shape: define a synth, start, add, sleep, stop. #expect(source.contains("SineTone")) #expect(source.contains("OscSin.Audio(freq: 440.0)")) #expect(source.contains("engine.start()")) #expect(source.contains("engine.add(SineTone())")) #expect(source.contains("Thread.sleep(forTimeInterval:")) #expect(source.contains("engine.stop()")) // Plays for a fixed duration and exits — no RunLoop. #expect(!source.contains("RunLoop.main.run()")) } @Test func quickstartPackageManifestDoesNotIncludeLyrebirdUI() { let manifest: String = ProjectScaffolder.packageManifest(name: "MyFirstSound", template: .quickstart) #expect(!manifest.contains("LyrebirdUI")) } // MARK: - nrt template @Test func nrtMainSwiftUsesNRTDriver() { let source: String = ProjectScaffolder.mainSwift(name: "MyRender", template: .nrt) #expect(source.contains("NRTDriver")) #expect(source.contains("renderDuration")) // WAV, not CAF: CAF is Apple-only, so a CAF render fails at run time // on Linux even though the project compiles there. #expect(source.contains("output.wav")) #expect(source.contains(".wavFloat32")) } @Test func nrtMainSwiftContainsName() { let source: String = ProjectScaffolder.mainSwift(name: "MyRender", template: .nrt) #expect(source.contains("MyRenderSynth")) } @Test func nrtPackageManifestDoesNotIncludeLyrebirdUI() { let manifest: String = ProjectScaffolder.packageManifest(name: "MyRender", template: .nrt) #expect(!manifest.contains("LyrebirdUI")) } // MARK: - score template @Test func scoreMainSwiftBuildsAndExportsScore() { let source: String = ProjectScaffolder.mainSwift(name: "MyPiece", template: .score) #expect(source.contains("Score(bpm:")) #expect(source.contains("toMusicXMLString")) #expect(source.contains("fromMusicXML")) #expect(source.contains("score.musicxml")) } @Test func scoreMainSwiftRendersOfflineWithNRTDriver() { let source: String = ProjectScaffolder.mainSwift(name: "MyPiece", template: .score) #expect(source.contains("NRTDriver")) #expect(source.contains("score.wav")) #expect(source.contains(".wavFloat32")) #expect(source.contains("engine.schedule")) // Must not embed the Lyrebird copyright in emitted user source. #expect(!source.contains("Joshua Parmenter")) } @Test func scorePackageManifestDoesNotIncludeLyrebirdUI() { let manifest: String = ProjectScaffolder.packageManifest(name: "MyPiece", template: .score) #expect(!manifest.contains("LyrebirdUI")) } // MARK: - basic template unchanged by default @Test func defaultTemplateIsBasic() { let defaultSource: String = ProjectScaffolder.mainSwift(name: "Foo") let basicSource: String = ProjectScaffolder.mainSwift(name: "Foo", template: .basic) #expect(defaultSource == basicSource) } }