Skip to content

Commit cc869d7

Browse files
committed
test(macos): cover root command dispatch
1 parent 5ce413a commit cc869d7

2 files changed

Lines changed: 83 additions & 17 deletions

File tree

apps/macos/Sources/OpenClawMacCLI/EntryPoint.swift

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,68 @@
11
import Foundation
22

3-
private struct RootCommand {
3+
struct RootCommand: Equatable {
44
var name: String
55
var args: [String]
66
}
77

8+
enum RootCommandAction: Equatable {
9+
case usage
10+
case connect([String])
11+
case configureRemote([String])
12+
case discover([String])
13+
case wizard([String])
14+
case unknown(exitCode: Int32)
15+
}
16+
817
@main
918
struct OpenClawMacCLI {
1019
static func main() async {
1120
let args = Array(CommandLine.arguments.dropFirst())
12-
let command = parseRootCommand(args)
13-
switch command?.name {
14-
case nil:
21+
switch resolveRootCommandAction(args) {
22+
case .usage:
1523
printUsage()
16-
case "-h", "--help", "help":
17-
printUsage()
18-
case "connect":
19-
await runConnect(command?.args ?? [])
20-
case "configure-remote":
21-
runConfigureRemote(command?.args ?? [])
22-
case "discover":
23-
await runDiscover(command?.args ?? [])
24-
case "wizard":
25-
await runWizardCommand(command?.args ?? [])
26-
default:
24+
case .connect(let commandArgs):
25+
await runConnect(commandArgs)
26+
case .configureRemote(let commandArgs):
27+
runConfigureRemote(commandArgs)
28+
case .discover(let commandArgs):
29+
await runDiscover(commandArgs)
30+
case .wizard(let commandArgs):
31+
await runWizardCommand(commandArgs)
32+
case .unknown(let exitCode):
2733
fputs("openclaw-mac: unknown command\n", stderr)
2834
printUsage()
29-
exit(1)
35+
exit(exitCode)
3036
}
3137
}
3238
}
3339

34-
private func parseRootCommand(_ args: [String]) -> RootCommand? {
40+
func parseRootCommand(_ args: [String]) -> RootCommand? {
3541
guard let first = args.first else { return nil }
3642
return RootCommand(name: first, args: Array(args.dropFirst()))
3743
}
3844

45+
func resolveRootCommandAction(_ args: [String]) -> RootCommandAction {
46+
guard let command = parseRootCommand(args) else {
47+
return .usage
48+
}
49+
50+
switch command.name {
51+
case "-h", "--help", "help":
52+
return .usage
53+
case "connect":
54+
return .connect(command.args)
55+
case "configure-remote":
56+
return .configureRemote(command.args)
57+
case "discover":
58+
return .discover(command.args)
59+
case "wizard":
60+
return .wizard(command.args)
61+
default:
62+
return .unknown(exitCode: 1)
63+
}
64+
}
65+
3966
private func printUsage() {
4067
print("""
4168
openclaw-mac
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import Testing
2+
@testable import OpenClawMacCLI
3+
4+
struct RootCommandParserTests {
5+
@Test func `parse root command returns nil for empty args`() {
6+
#expect(parseRootCommand([]) == nil)
7+
}
8+
9+
@Test func `parse root command splits command name and args`() throws {
10+
let command = try #require(parseRootCommand(["connect", "--json", "--timeout", "3000"]))
11+
12+
#expect(command.name == "connect")
13+
#expect(command.args == ["--json", "--timeout", "3000"])
14+
}
15+
16+
@Test func `help aliases resolve to usage`() {
17+
for args in [[], ["-h"], ["--help"], ["help"]] {
18+
#expect(resolveRootCommandAction(args) == .usage)
19+
}
20+
}
21+
22+
@Test func `known commands preserve trailing args`() {
23+
#expect(resolveRootCommandAction(["connect", "--json"]) == .connect(["--json"]))
24+
#expect(
25+
resolveRootCommandAction(["configure-remote", "--ssh-target", "[email protected]"])
26+
== .configureRemote(["--ssh-target", "[email protected]"]),
27+
)
28+
#expect(resolveRootCommandAction(["discover", "--include-local"]) == .discover(["--include-local"]))
29+
#expect(resolveRootCommandAction(["wizard", "--mode", "local"]) == .wizard(["--mode", "local"]))
30+
}
31+
32+
@Test func `unknown command resolves to nonzero exit action`() {
33+
#expect(resolveRootCommandAction(["nope"]) == .unknown(exitCode: 1))
34+
}
35+
36+
@Test func `command names remain case sensitive`() {
37+
#expect(resolveRootCommandAction(["Connect"]) == .unknown(exitCode: 1))
38+
}
39+
}

0 commit comments

Comments
 (0)