-
Notifications
You must be signed in to change notification settings - Fork 716
test(cli): add focused dispatch behavior tests #1457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { CLI_COMMANDS } from "../src/cli/registry"; | ||
| import { DISPATCH_ALIASES, DISPATCH_COMMANDS, dispatchCommand, resolveDispatchCommand } from "../src/cli/dispatch"; | ||
| import type { CliDispatchDeps } from "../src/cli/dispatch"; | ||
|
|
||
| /** Minimal fake deps. dispatchCommand only touches deps for real command | ||
| * runners, which these tests never invoke, so an empty object is enough. */ | ||
| const fakeDeps = {} as unknown as CliDispatchDeps; | ||
|
|
||
| describe("CLI dispatch command coverage", () => { | ||
| test("every non-hidden registry command is dispatchable", () => { | ||
| const aliasResolved = new Set([...DISPATCH_COMMANDS, ...DISPATCH_ALIASES.keys()]); | ||
| const missing = CLI_COMMANDS.filter(entry => { | ||
| if (entry.hidden) return false; | ||
| // A visible command counts as dispatchable when it is a direct runner | ||
| // key or an alias that resolves to one (setup/eject/remove/model). | ||
| return !aliasResolved.has(entry.name); | ||
| }).map(entry => entry.name); | ||
| expect(missing).toEqual([]); | ||
| }); | ||
|
|
||
| test("every dispatch alias resolves to a dispatchable command", () => { | ||
| for (const [alias, target] of DISPATCH_ALIASES) { | ||
| expect(DISPATCH_COMMANDS).toContain(target); | ||
| expect(alias).not.toBe(target); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| describe("CLI dispatch aliases", () => { | ||
| test("canonical alias pairs resolve to their command", () => { | ||
| expect(DISPATCH_ALIASES.get("setup")).toBe("init"); | ||
| expect(DISPATCH_ALIASES.get("eject")).toBe("restore"); | ||
| expect(DISPATCH_ALIASES.get("remove")).toBe("uninstall"); | ||
| expect(DISPATCH_ALIASES.get("model")).toBe("models"); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("resolveDispatchCommand maps each alias to its canonical runner key", () => { | ||
| // The same resolver dispatchCommand uses for runner selection, exercised | ||
| // at the resolution level so a regression in the lookup is caught. | ||
| expect(resolveDispatchCommand("setup")).toBe("init"); | ||
| expect(resolveDispatchCommand("eject")).toBe("restore"); | ||
| expect(resolveDispatchCommand("remove")).toBe("uninstall"); | ||
| expect(resolveDispatchCommand("model")).toBe("models"); | ||
| // Canonical names resolve to themselves; unknown names resolve undefined. | ||
| expect(resolveDispatchCommand("init")).toBe("init"); | ||
| expect(resolveDispatchCommand("definitely-not-a-command")).toBeUndefined(); | ||
| expect(resolveDispatchCommand(undefined)).toBeUndefined(); | ||
| }); | ||
|
|
||
| test("resolveDispatchCommand rejects inherited Object property names", () => { | ||
| // commandRunners is a normal object; inherited names (__proto__, | ||
| // constructor, toString) must not resolve as valid commands. | ||
| expect(resolveDispatchCommand("__proto__")).toBeUndefined(); | ||
| expect(resolveDispatchCommand("constructor")).toBeUndefined(); | ||
| expect(resolveDispatchCommand("toString")).toBeUndefined(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("dispatchCommand exit codes", () => { | ||
| test("returns 0 for help forms", async () => { | ||
| expect(await dispatchCommand({ kind: "help", command: "help", args: ["help"] }, fakeDeps)).toBe(0); | ||
| expect(await dispatchCommand({ kind: "help", command: "--help", args: ["--help"] }, fakeDeps)).toBe(0); | ||
| expect(await dispatchCommand({ kind: "help", command: "-h", args: ["-h"] }, fakeDeps)).toBe(0); | ||
| expect(await dispatchCommand({ kind: "command", command: undefined, args: [] }, fakeDeps)).toBe(0); | ||
| }); | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| test("returns 1 for an unknown command", async () => { | ||
| const head = { kind: "command" as const, command: "definitely-not-a-command", args: ["definitely-not-a-command"] }; | ||
| expect(await dispatchCommand(head, fakeDeps)).toBe(1); | ||
| }); | ||
|
|
||
| test("returns 1 for inherited Object property names", async () => { | ||
| for (const name of ["__proto__", "constructor", "toString"]) { | ||
| const head = { kind: "command" as const, command: name, args: [name] }; | ||
| expect(await dispatchCommand(head, fakeDeps), `${name} must be unknown`).toBe(1); | ||
| } | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.