|
| 1 | +/** |
| 2 | + * Tests for route-subcommands and interceptSubcommand. |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, expect, test } from "bun:test"; |
| 6 | +import { interceptSubcommand } from "../../src/lib/route-subcommands.js"; |
| 7 | +import { getRouteSubcommands } from "../../src/lib/route-subcommands.js"; |
| 8 | + |
| 9 | +describe("getRouteSubcommands", () => { |
| 10 | + test("returns a non-empty set", () => { |
| 11 | + const subs = getRouteSubcommands(); |
| 12 | + expect(subs.size).toBeGreaterThan(0); |
| 13 | + }); |
| 14 | + |
| 15 | + test("contains known subcommand names from routes", () => { |
| 16 | + const subs = getRouteSubcommands(); |
| 17 | + // These exist because issue route has list/view/explain/plan |
| 18 | + expect(subs.has("list")).toBe(true); |
| 19 | + expect(subs.has("view")).toBe(true); |
| 20 | + expect(subs.has("explain")).toBe(true); |
| 21 | + expect(subs.has("plan")).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + test("does not contain top-level command names", () => { |
| 25 | + const subs = getRouteSubcommands(); |
| 26 | + expect(subs.has("issue")).toBe(false); |
| 27 | + expect(subs.has("project")).toBe(false); |
| 28 | + expect(subs.has("api")).toBe(false); |
| 29 | + }); |
| 30 | + |
| 31 | + test("returns the same cached set on repeat calls", () => { |
| 32 | + const a = getRouteSubcommands(); |
| 33 | + const b = getRouteSubcommands(); |
| 34 | + expect(a).toBe(b); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("interceptSubcommand", () => { |
| 39 | + function makeStderr(): { write(s: string): void; output: string } { |
| 40 | + let output = ""; |
| 41 | + return { |
| 42 | + write(s: string) { |
| 43 | + output += s; |
| 44 | + }, |
| 45 | + get output() { |
| 46 | + return output; |
| 47 | + }, |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + test("returns undefined and writes hint for known subcommand", () => { |
| 52 | + const stderr = makeStderr(); |
| 53 | + const result = interceptSubcommand("list", stderr); |
| 54 | + expect(result).toBeUndefined(); |
| 55 | + expect(stderr.output).toContain("Tip:"); |
| 56 | + expect(stderr.output).toContain("singular form"); |
| 57 | + }); |
| 58 | + |
| 59 | + test("returns target unchanged for normal project names", () => { |
| 60 | + const stderr = makeStderr(); |
| 61 | + const result = interceptSubcommand("my-project", stderr); |
| 62 | + expect(result).toBe("my-project"); |
| 63 | + expect(stderr.output).toBe(""); |
| 64 | + }); |
| 65 | + |
| 66 | + test("returns target unchanged for org/project patterns", () => { |
| 67 | + const stderr = makeStderr(); |
| 68 | + const result = interceptSubcommand("sentry/cli", stderr); |
| 69 | + expect(result).toBe("sentry/cli"); |
| 70 | + expect(stderr.output).toBe(""); |
| 71 | + }); |
| 72 | + |
| 73 | + test("returns undefined/empty unchanged (no hint)", () => { |
| 74 | + const stderr = makeStderr(); |
| 75 | + expect(interceptSubcommand(undefined, stderr)).toBeUndefined(); |
| 76 | + expect(stderr.output).toBe(""); |
| 77 | + |
| 78 | + expect(interceptSubcommand("", stderr)).toBe(""); |
| 79 | + expect(stderr.output).toBe(""); |
| 80 | + }); |
| 81 | + |
| 82 | + test("includes the subcommand name in the hint", () => { |
| 83 | + const stderr = makeStderr(); |
| 84 | + interceptSubcommand("view", stderr); |
| 85 | + expect(stderr.output).toContain("view"); |
| 86 | + }); |
| 87 | + |
| 88 | + test("handles 'explain' and 'plan' subcommands", () => { |
| 89 | + const stderr1 = makeStderr(); |
| 90 | + expect(interceptSubcommand("explain", stderr1)).toBeUndefined(); |
| 91 | + expect(stderr1.output).toContain("Tip:"); |
| 92 | + |
| 93 | + const stderr2 = makeStderr(); |
| 94 | + expect(interceptSubcommand("plan", stderr2)).toBeUndefined(); |
| 95 | + expect(stderr2.output).toContain("Tip:"); |
| 96 | + }); |
| 97 | +}); |
0 commit comments