// CLICommandTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing @testable import LyrebirdCLI @Suite("CLICommand parsing") struct CLICommandTests { @Test func replSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "repl"]) == .repl) } @Test func noArgsParsesToInteractive() { // `lyrebird` with no subcommand enters the LineNoise interactive shell. #expect(CLICommand.parse(["lyrebird"]) == .interactive) } @Test func helpFlagParsed() { #expect(CLICommand.parse(["lyrebird", "--help"]) == .help) } @Test func helpSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "help"]) == .help) } @Test func versionFlagParsed() { #expect(CLICommand.parse(["lyrebird", "--version"]) == .version) } @Test func versionSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "version"]) == .version) } @Test func unknownSubcommandParsesToHelp() { #expect(CLICommand.parse(["lyrebird", "unknowncommand"]) == .help) } @Test func emptyArgsParsesToInteractive() { // Edge case: called with empty args array (should not crash). #expect(CLICommand.parse([]) == .interactive) } // MARK: - new @Test func newSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "new", "MySynth"]) == .new(name: "MySynth", template: .basic, lyrebirdPath: nil, shoegazePath: nil)) } @Test func newWithTemplateDemoParsed() { #expect(CLICommand.parse(["lyrebird", "new", "MySynth", "--template", "demo"]) == .new(name: "MySynth", template: .demo, lyrebirdPath: nil, shoegazePath: nil)) } @Test func newWithTemplateUIDemoParsed() { #expect(CLICommand.parse(["lyrebird", "new", "MySynth", "--template", "ui-demo"]) == .new(name: "MySynth", template: .uiDemo, lyrebirdPath: nil, shoegazePath: nil)) } @Test func newWithTemplateNRTParsed() { #expect(CLICommand.parse(["lyrebird", "new", "MySynth", "--template", "nrt"]) == .new(name: "MySynth", template: .nrt, lyrebirdPath: nil, shoegazePath: nil)) } @Test func newWithUnknownTemplateFallsBackToBasic() { #expect(CLICommand.parse(["lyrebird", "new", "MySynth", "--template", "bogus"]) == .new(name: "MySynth", template: .basic, lyrebirdPath: nil, shoegazePath: nil)) } @Test func newWithoutNameParsesToHelp() { #expect(CLICommand.parse(["lyrebird", "new"]) == .help) } @Test func newWithEmptyNameParsesToHelp() { #expect(CLICommand.parse(["lyrebird", "new", ""]) == .help) } @Test func newWithFlagInsteadOfNameParsesToHelp() { // Flag token in name position — user omitted the project name. #expect(CLICommand.parse(["lyrebird", "new", "--flag"]) == .help) } @Test func newWithLyrebirdPathParsed() { #expect(CLICommand.parse([ "lyrebird", "new", "MyPiece", "--lyrebird-path", "/Users/josh/Documents/LyrebirdII" ]) == .new(name: "MyPiece", template: .basic, lyrebirdPath: "/Users/josh/Documents/LyrebirdII", shoegazePath: nil)) } @Test func newWithTemplateAndLyrebirdPathParsed() { #expect(CLICommand.parse([ "lyrebird", "new", "MyApp", "--template", "macos-ui-xcode", "--lyrebird-path", "/abs/path" ]) == .new(name: "MyApp", template: .macosUIXcode, lyrebirdPath: "/abs/path", shoegazePath: nil)) } @Test func newWithoutLyrebirdPathReportsNil() { #expect(CLICommand.parse([ "lyrebird", "new", "MyPiece" ]) == .new(name: "MyPiece", template: .basic, lyrebirdPath: nil, shoegazePath: nil)) } // MARK: - shoegaze template @Test func newWithTemplateShoegaze() { let cmd: CLICommand = CLICommand.parse(["lyrebird", "new", "MyRig", "--template", "shoegaze"]) #expect(cmd == .new(name: "MyRig", template: .shoegaze, lyrebirdPath: nil, shoegazePath: nil)) } @Test func newWithShoegazePathParsed() { let cmd: CLICommand = CLICommand.parse([ "lyrebird", "new", "MyRig", "--template", "shoegaze", "--lyrebird-path", "/abs/Lyrebird", "--shoegaze-path", "/abs/ShoeGaze", ]) #expect(cmd == .new( name: "MyRig", template: .shoegaze, lyrebirdPath: "/abs/Lyrebird", shoegazePath: "/abs/ShoeGaze" )) } @Test func newShoegazeWithoutShoegazePathHasNilShoegazePath() { let cmd: CLICommand = CLICommand.parse([ "lyrebird", "new", "MyRig", "--template", "shoegaze", "--lyrebird-path", "/abs/Lyrebird", ]) #expect(cmd == .new( name: "MyRig", template: .shoegaze, lyrebirdPath: "/abs/Lyrebird", shoegazePath: nil )) } @Test func shoegazePathValueNotConsumedWhenFlagToken() { // `--shoegaze-path --lyrebird-path /abs/Lyrebird` — the value slot for // `--shoegaze-path` is `--lyrebird-path` (starts with "-") → shoegazePath = nil. // `firstIndex(of: "--lyrebird-path")` finds the first occurrence (index 6) // whose next token is also "--lyrebird-path" (starts with "-") → lyrebirdPath = nil too. let args: [String] = [ "lyrebird", "new", "MyRig", "--template", "shoegaze", "--shoegaze-path", "--lyrebird-path", "--lyrebird-path", "/abs/Lyrebird", ] let cmd: CLICommand = CLICommand.parse(args) #expect(cmd == .new( name: "MyRig", template: .shoegaze, lyrebirdPath: nil, shoegazePath: nil )) } // MARK: - run @Test func runSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "run"]) == .run(release: false)) } @Test func runWithReleaseFlagParsed() { #expect(CLICommand.parse(["lyrebird", "run", "--release"]) == .run(release: true)) } // MARK: - export @Test func exportSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "export", "MySynth"]) == .export(name: "MySynth", to: nil, overwrite: false)) } @Test func exportWithToPathParsed() { let cmd: CLICommand = CLICommand.parse(["lyrebird", "export", "MySynth", "--to", "/tmp/MySynth.swift"]) #expect(cmd == .export(name: "MySynth", to: "/tmp/MySynth.swift", overwrite: false)) } @Test func exportWithOverwriteFlagParsed() { let cmd: CLICommand = CLICommand.parse(["lyrebird", "export", "MySynth", "--overwrite"]) #expect(cmd == .export(name: "MySynth", to: nil, overwrite: true)) } @Test func exportWithToAndOverwriteParsed() { let cmd: CLICommand = CLICommand.parse(["lyrebird", "export", "MySynth", "--to", "/tmp/x.swift", "--overwrite"]) #expect(cmd == .export(name: "MySynth", to: "/tmp/x.swift", overwrite: true)) } @Test func exportWithoutNameParsesToHelp() { #expect(CLICommand.parse(["lyrebird", "export"]) == .help) } @Test func exportWithFlagInsteadOfNameParsesToHelp() { // `lyrebird export --to foo.swift` — name slot is a flag, should fall back to help. #expect(CLICommand.parse(["lyrebird", "export", "--to", "foo.swift"]) == .help) } @Test func exportToValueIsNotConsumedAsFlagWhenFlagToken() { // `lyrebird export MySynth --to --overwrite` — "--overwrite" starts with "-", // so --to has no valid path value and toPath must be nil (not "--overwrite"). let cmd: CLICommand = CLICommand.parse(["lyrebird", "export", "MySynth", "--to", "--overwrite"]) #expect(cmd == .export(name: "MySynth", to: nil, overwrite: true)) } // MARK: - install-templates / uninstall-templates @Test func installTemplatesSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "install-templates"]) == .installTemplates(force: false)) } @Test func installTemplatesWithForceFlag() { #expect(CLICommand.parse(["lyrebird", "install-templates", "--force"]) == .installTemplates(force: true)) } @Test func uninstallTemplatesSubcommandParsed() { #expect(CLICommand.parse(["lyrebird", "uninstall-templates"]) == .uninstallTemplates) } }