donkey / tests /core /t_command_dispatch.nim
manelmoura's picture
Sync from manelsen/donkey via hub-sync (part 4)
41d461d verified
Raw History Blame Contribute Delete
14.5 kB
## t_command_dispatch.nim β€” Test coverage for command dispatch.
##
## Covers:
## - Dispatch for each registered command (setup, ctl, service, tui, doctor, about, start)
## - Handler argument validation (minArgs enforcement)
## - Standardized exit codes (-1 = unknown, 0 = ok, 1 = error, 2 = help)
## - Context flags on each command entry
## - Command description quality gate
##
## All tests run without full daemon boot β€” no adapters, no network, no daemon.
import std/[options, sequtils, strutils, unittest]
import donkey/core/command_registry
import donkey/core/app_meta
# =============================================================================
# Suite 1 β€” Registry mirrors production command definitions
# =============================================================================
## Build a registry with mock handlers that match the production command
## names, minArgs, and context flags. This lets us test dispatch behaviour
## without needing DonkeyConfig, MdFileAdapter, or the daemon.
proc makeProductionLikeRegistry*(): CommandRegistry =
## Creates a registry with all commands that main() registers,
## but with mock handlers that just return their expected exit code.
result = newCommandRegistry()
# setup β€” pre-bootstrap, no config needed
result.register(CommandEntry(
name: "setup", minArgs: 1,
handler: proc(args: seq[string]): int = 0,
context: ccNone, description: "Interactive first-run configuration wizard"))
# ctl β€” post-bootstrap, needs config
result.register(CommandEntry(
name: "ctl", minArgs: 2,
handler: proc(args: seq[string]): int = 0,
context: ccConfigLoaded, description: "Admin commands (user, skill, tool, memory, status)"))
# service β€” pre-bootstrap
result.register(CommandEntry(
name: "service", minArgs: 2,
handler: proc(args: seq[string]): int = 0,
context: ccNone, description: "Systemd --user service management"))
# tui β€” post-bootstrap
result.register(CommandEntry(
name: "tui", minArgs: 1,
handler: proc(args: seq[string]): int = 0,
context: ccConfigLoaded, description: "Open live TUI dashboard"))
# doctor β€” post-bootstrap
result.register(CommandEntry(
name: "doctor", minArgs: 1,
handler: proc(args: seq[string]): int = 0,
context: ccConfigLoaded, description: "Run full runtime diagnostic"))
# about β€” post-bootstrap
result.register(CommandEntry(
name: "about", minArgs: 1,
handler: proc(args: seq[string]): int = 0,
context: ccConfigLoaded, description: "Show concise runtime overview"))
# start β€” post-bootstrap
result.register(CommandEntry(
name: "start", minArgs: 1,
handler: proc(args: seq[string]): int = 0,
context: ccConfigLoaded, description: "Start the daemon"))
# ── all command names are registered ──────────────────────────────────────────
const ExpectedCommands = ["setup", "ctl", "service", "tui", "doctor", "about", "start"]
proc runTests*() =
suite "command dispatch β€” all registered commands resolve":
test "setup command resolves":
let r = makeProductionLikeRegistry()
let e = r.resolve("setup")
check isSome(e)
check e.get().name == "setup"
test "ctl command resolves":
let r = makeProductionLikeRegistry()
check isSome(r.resolve("ctl"))
test "service command resolves":
let r = makeProductionLikeRegistry()
check isSome(r.resolve("service"))
test "tui command resolves":
let r = makeProductionLikeRegistry()
check isSome(r.resolve("tui"))
test "doctor command resolves":
let r = makeProductionLikeRegistry()
check isSome(r.resolve("doctor"))
test "about command resolves":
let r = makeProductionLikeRegistry()
check isSome(r.resolve("about"))
test "start command resolves":
let r = makeProductionLikeRegistry()
check isSome(r.resolve("start"))
test "all expected commands are present":
let r = makeProductionLikeRegistry()
let cmds = r.listCommands()
check cmds.len == ExpectedCommands.len
let names = toSeq(cmds.mapIt(it.name))
for cmd in ExpectedCommands:
check cmd in names
# =============================================================================
# Suite 2 β€” Dispatch and exit codes for each command
# =============================================================================
suite "command dispatch β€” happy path (exit code 0)":
test "dispatch setup returns 0":
let r = makeProductionLikeRegistry()
check r.dispatch(@["setup"]) == 0
test "dispatch ctl returns 0 with enough args":
let r = makeProductionLikeRegistry()
check r.dispatch(@["ctl", "user", "list"]) == 0
test "dispatch service returns 0 with enough args":
let r = makeProductionLikeRegistry()
check r.dispatch(@["service", "install"]) == 0
test "dispatch tui returns 0":
let r = makeProductionLikeRegistry()
check r.dispatch(@["tui"]) == 0
test "dispatch doctor returns 0":
let r = makeProductionLikeRegistry()
check r.dispatch(@["doctor"]) == 0
test "dispatch about returns 0":
let r = makeProductionLikeRegistry()
check r.dispatch(@["about"]) == 0
test "dispatch start returns 0":
let r = makeProductionLikeRegistry()
check r.dispatch(@["start"]) == 0
# =============================================================================
# Suite 3 β€” Argument validation (minArgs enforcement)
# =============================================================================
suite "command dispatch β€” argument validation (exit code 2)":
test "ctl with only command name (no subcommand) returns 2":
let r = makeProductionLikeRegistry()
check r.dispatch(@["ctl"]) == 2
test "service with only command name (no subcommand) returns 2":
let r = makeProductionLikeRegistry()
check r.dispatch(@["service"]) == 2
test "commands with minArgs=1 never trigger insufficient args":
let r = makeProductionLikeRegistry()
check r.dispatch(@["tui"]) == 0
check r.dispatch(@["doctor"]) == 0
check r.dispatch(@["about"]) == 0
check r.dispatch(@["start"]) == 0
test "setup with minArgs=1 always succeeds with at least the command name":
let r = makeProductionLikeRegistry()
check r.dispatch(@["setup"]) == 0
test "setup with extra args still succeeds":
let r = makeProductionLikeRegistry()
check r.dispatch(@["setup", "--force"]) == 0
# =============================================================================
# Suite 4 β€” Unknown command β†’ exit code -1
# =============================================================================
suite "command dispatch β€” unknown command (exit code -1)":
test "completely unknown command returns -1":
let r = makeProductionLikeRegistry()
check r.dispatch(@["foobar"]) == -1
test "typo'd command returns -1":
let r = makeProductionLikeRegistry()
check r.dispatch(@["ctll"]) == -1
check r.dispatch(@["servce"]) == -1
check r.dispatch(@["stat"]) == -1
test "empty args returns -1":
let r = makeProductionLikeRegistry()
check r.dispatch(@[]) == -1
# =============================================================================
# Suite 5 β€” Standardized exit codes
# =============================================================================
suite "command dispatch β€” standardized exit codes":
test "exit code 0 means success":
let r = makeProductionLikeRegistry()
for cmd in ExpectedCommands:
let entry = r.resolve(cmd).get()
let args = if entry.minArgs > 1:
@[cmd, "sub"]
else:
@[cmd]
let code = r.dispatch(args)
check code == 0
test "exit code -1 means command not found":
let r = makeProductionLikeRegistry()
check r.dispatch(@["nonexistent"]) == -1
test "exit code 2 means insufficient arguments":
let r = makeProductionLikeRegistry()
check r.dispatch(@["ctl"]) == 2 # minArgs=2
check r.dispatch(@["service"]) == 2 # minArgs=2
test "exit code 1 from handler indicates handler-level error":
let r = newCommandRegistry()
r.register(CommandEntry(
name: "failing", minArgs: 1,
handler: proc(args: seq[string]): int = 1,
context: ccNone, description: "always fails"))
check r.dispatch(@["failing"]) == 1
test "exit code 2 from handler indicates unrecognized subcommand":
# This is the pattern used by dispatchCtl and dispatchService
# when a subcommand is not recognized
let r = newCommandRegistry()
r.register(CommandEntry(
name: "subcmd", minArgs: 2,
handler: proc(args: seq[string]): int =
if args.len >= 2 and args[1] in ["install", "remove", "start"]: 0
else: 2,
context: ccNone, description: "subcommand router"))
check r.dispatch(@["subcmd", "install"]) == 0
check r.dispatch(@["subcmd", "unknown-sub"]) == 2
test "exit code contract: -1, 0, 1, 2 are the only codes":
let r = newCommandRegistry()
r.register(CommandEntry(
name: "ok", minArgs: 1,
handler: proc(args: seq[string]): int = 0,
context: ccNone, description: "ok"))
r.register(CommandEntry(
name: "err", minArgs: 1,
handler: proc(args: seq[string]): int = 1,
context: ccNone, description: "err"))
r.register(CommandEntry(
name: "help", minArgs: 1,
handler: proc(args: seq[string]): int = 2,
context: ccNone, description: "help"))
check r.dispatch(@["ok"]) == 0
check r.dispatch(@["err"]) == 1
check r.dispatch(@["help"]) == 2
check r.dispatch(@["unknown"]) == -1
# =============================================================================
# Suite 6 β€” Context flags on commands
# =============================================================================
suite "command dispatch β€” context flags":
test "setup requires no context (ccNone)":
let r = makeProductionLikeRegistry()
check r.resolve("setup").get().context == ccNone
test "service requires no context (ccNone)":
let r = makeProductionLikeRegistry()
check r.resolve("service").get().context == ccNone
test "ctl requires config loaded (ccConfigLoaded)":
let r = makeProductionLikeRegistry()
check r.resolve("ctl").get().context == ccConfigLoaded
test "tui requires config loaded (ccConfigLoaded)":
let r = makeProductionLikeRegistry()
check r.resolve("tui").get().context == ccConfigLoaded
test "doctor requires config loaded (ccConfigLoaded)":
let r = makeProductionLikeRegistry()
check r.resolve("doctor").get().context == ccConfigLoaded
test "about requires config loaded (ccConfigLoaded)":
let r = makeProductionLikeRegistry()
check r.resolve("about").get().context == ccConfigLoaded
test "start requires config loaded (ccConfigLoaded)":
let r = makeProductionLikeRegistry()
check r.resolve("start").get().context == ccConfigLoaded
test "pre-bootstrap commands (ccNone) can run before config":
let r = makeProductionLikeRegistry()
for cmd in ["setup", "service"]:
check r.resolve(cmd).get().context == ccNone
test "post-bootstrap commands need config":
let r = makeProductionLikeRegistry()
for cmd in ["ctl", "tui", "doctor", "about", "start"]:
check r.resolve(cmd).get().context == ccConfigLoaded
# =============================================================================
# Suite 7 β€” Dispatch minArgs matches production values
# =============================================================================
suite "command dispatch β€” minArgs matches production":
## Verifies the mock registry minArgs values match what main() actually uses.
## This is a regression guard: if someone changes minArgs in main() but not
## here, the tests below will catch it when the registry is used for
## dispatch (since the mock handlers always return 0).
test "ctl minArgs is 2 (needs subcommand)":
let r = makeProductionLikeRegistry()
let entry = r.resolve("ctl").get()
check entry.minArgs == 2
test "service minArgs is 2 (needs subcommand)":
let r = makeProductionLikeRegistry()
let entry = r.resolve("service").get()
check entry.minArgs == 2
test "tui minArgs is 1":
let r = makeProductionLikeRegistry()
check r.resolve("tui").get().minArgs == 1
test "doctor minArgs is 1":
let r = makeProductionLikeRegistry()
check r.resolve("doctor").get().minArgs == 1
test "about minArgs is 1":
let r = makeProductionLikeRegistry()
check r.resolve("about").get().minArgs == 1
test "start minArgs is 1":
let r = makeProductionLikeRegistry()
check r.resolve("start").get().minArgs == 1
test "setup minArgs is 1":
let r = makeProductionLikeRegistry()
check r.resolve("setup").get().minArgs == 1
# =============================================================================
# Suite 8 β€” Command descriptions are non-empty
# =============================================================================
suite "command dispatch β€” command descriptions":
## Ensures every registered command has a non-empty description
## (help text quality gate).
test "all commands have non-empty descriptions":
let r = makeProductionLikeRegistry()
for cmd in ExpectedCommands:
let entry = r.resolve(cmd).get()
check entry.description.len > 0
test "ctl description mentions admin":
let r = makeProductionLikeRegistry()
check r.resolve("ctl").get().description.contains("Admin")
test "service description mentions systemd":
let r = makeProductionLikeRegistry()
check r.resolve("service").get().description.contains("Systemd")
test "doctor description mentions diagnostic":
let r = makeProductionLikeRegistry()
check r.resolve("doctor").get().description.contains("diagnostic")
test "start description mentions daemon":
let r = makeProductionLikeRegistry()
check r.resolve("start").get().description.contains("daemon")
when isMainModule:
runTests()