// InteractiveShellTests.swift // Lyrebird // // Copyright (c) 2026 Joshua Parmenter / realizedsound // SPDX-License-Identifier: MIT import Testing import Foundation @testable import LyrebirdCLI @Suite("InteractiveShell") struct InteractiveShellTests { @Test func historyFilePathMatchesREPLLauncher() { // The shell and the REPL launcher share the same history file. #expect(InteractiveShell.historyFilePath == REPLLauncher.historyFilePath) } @Test func completionsIncludeAllSubcommands() { let completions: [String] = InteractiveShell.completions(for: "") #expect(completions.contains("new")) #expect(completions.contains("run")) #expect(completions.contains("export")) #expect(completions.contains("repl")) #expect(completions.contains("help")) #expect(completions.contains("version")) #expect(completions.contains("quit")) } @Test func completionsFilterByPrefix() { // `r` prefix → both `repl` and `run` let completions: [String] = InteractiveShell.completions(for: "r") #expect(completions.contains("repl")) #expect(completions.contains("run")) #expect(!completions.contains("help")) #expect(!completions.contains("version")) } @Test func completionsForUnknownPrefixAreEmpty() { let completions: [String] = InteractiveShell.completions(for: "xyz") #expect(completions.isEmpty) } @Test func shellCommandParsedFromInput() { #expect(InteractiveShell.command(from: "repl") == .repl) #expect(InteractiveShell.command(from: "help") == .help) #expect(InteractiveShell.command(from: "version") == .version) #expect(InteractiveShell.command(from: "quit") == .quit) #expect(InteractiveShell.command(from: "") == .none) #expect(InteractiveShell.command(from: " ") == .none) #expect(InteractiveShell.command(from: "unknown") == .unknown("unknown")) } @Test func shellCommandFromInputIsTrimmed() { // Leading/trailing whitespace must not affect parsing. #expect(InteractiveShell.command(from: " repl ") == .repl) } // MARK: - new @Test func shellNewCommandParsed() { #expect(InteractiveShell.command(from: "new MySynth") == .new(name: "MySynth", template: .basic, lyrebirdPath: nil)) } @Test func shellNewCommandCaseInsensitiveVerb() { // Verb is case-insensitive; name preserves original casing. #expect(InteractiveShell.command(from: "NEW MySynth") == .new(name: "MySynth", template: .basic, lyrebirdPath: nil)) } @Test func shellNewCommandWithTemplateDemo() { #expect(InteractiveShell.command(from: "new MySynth --template demo") == .new(name: "MySynth", template: .demo, lyrebirdPath: nil)) } @Test func shellNewCommandWithTemplateUIDemo() { #expect(InteractiveShell.command(from: "new MySynth --template ui-demo") == .new(name: "MySynth", template: .uiDemo, lyrebirdPath: nil)) } @Test func shellNewCommandWithTemplateNRT() { #expect(InteractiveShell.command(from: "new MySynth --template nrt") == .new(name: "MySynth", template: .nrt, lyrebirdPath: nil)) } @Test func shellNewCommandUnknownTemplateFallsBackToBasic() { #expect(InteractiveShell.command(from: "new MySynth --template bogus") == .new(name: "MySynth", template: .basic, lyrebirdPath: nil)) } @Test func shellNewCommandWithLyrebirdPathParsed() { #expect(InteractiveShell.command(from: "new MyPiece --lyrebird-path /Users/josh/Documents/LyrebirdII") == .new(name: "MyPiece", template: .basic, lyrebirdPath: "/Users/josh/Documents/LyrebirdII")) } @Test func shellNewCommandWithTemplateAndLyrebirdPathParsed() { #expect(InteractiveShell.command(from: "new MyApp --template macos-ui-xcode --lyrebird-path /abs/path") == .new(name: "MyApp", template: .macosUIXcode, lyrebirdPath: "/abs/path")) } @Test func shellNewCommandMissingNameReturnsUnknown() { // No argument → unknown rather than a crash or silent failure. if case .unknown(_) = InteractiveShell.command(from: "new") { // pass } else { Issue.record("Expected .unknown for 'new' without a name") } } @Test func shellNewCommandFlagInsteadOfNameReturnsUnknown() { // Flag token in name position — user omitted the project name. if case .unknown(_) = InteractiveShell.command(from: "new --flag") { // pass } else { Issue.record("Expected .unknown when new name slot is a flag") } } // MARK: - run @Test func shellRunCommandParsed() { #expect(InteractiveShell.command(from: "run") == .run(release: false)) } @Test func shellRunCommandWithReleaseFlag() { #expect(InteractiveShell.command(from: "run --release") == .run(release: true)) } @Test func shellRunCommandCaseInsensitiveVerb() { #expect(InteractiveShell.command(from: "RUN") == .run(release: false)) } // MARK: - export @Test func shellExportCommandParsed() { #expect(InteractiveShell.command(from: "export MySynth") == .export(name: "MySynth", to: nil, overwrite: false)) } @Test func shellExportCommandWithToPath() { #expect( InteractiveShell.command(from: "export MySynth --to /tmp/MySynth.swift") == .export(name: "MySynth", to: "/tmp/MySynth.swift", overwrite: false) ) } @Test func shellExportCommandWithOverwriteFlag() { #expect( InteractiveShell.command(from: "export MySynth --overwrite") == .export(name: "MySynth", to: nil, overwrite: true) ) } @Test func shellExportCommandWithToAndOverwrite() { #expect( InteractiveShell.command(from: "export MySynth --to /tmp/MySynth.swift --overwrite") == .export(name: "MySynth", to: "/tmp/MySynth.swift", overwrite: true) ) } @Test func shellExportCommandMissingNameReturnsUnknown() { if case .unknown(_) = InteractiveShell.command(from: "export") { // pass } else { Issue.record("Expected .unknown for 'export' without a name") } } @Test func shellExportCommandCaseInsensitiveVerb() { // Verb is case-insensitive; name preserves original casing. #expect(InteractiveShell.command(from: "EXPORT BassLine") == .export(name: "BassLine", to: nil, overwrite: false)) } @Test func shellExportFlagInsteadOfNameReturnsUnknown() { // `export --to foo.swift` — name slot is a flag token, not a valid identifier. if case .unknown(_) = InteractiveShell.command(from: "export --to foo.swift") { // pass } else { Issue.record("Expected .unknown when export name slot is a flag") } } @Test func shellExportToValueNotConsumedAsFlagToken() { // `export MySynth --to --overwrite` — "--overwrite" starts with "-", so toPath is nil. #expect( InteractiveShell.command(from: "export MySynth --to --overwrite") == .export(name: "MySynth", to: nil, overwrite: true) ) } }