|
| 1 | +import { Bash } from "just-bash"; |
| 2 | +import { describe, expect, test } from "vitest"; |
| 3 | +import { command, handlersToCustomCommands } from "./handlers"; |
| 4 | + |
| 5 | +const ctx = { stdin: "" }; |
| 6 | + |
| 7 | +describe("command()", () => { |
| 8 | + describe("string patterns", () => { |
| 9 | + test("matches the exact command and pattern tokens as a prefix", () => { |
| 10 | + const handler = command("npm install"); |
| 11 | + expect(handler.commandNames).toEqual(["npm"]); |
| 12 | + expect(handler.matches("npm", ["install"])).toBe(true); |
| 13 | + expect(handler.matches("npm", ["install", "--save"])).toBe(true); |
| 14 | + expect(handler.matches("npm", ["run", "build"])).toBe(false); |
| 15 | + expect(handler.matches("pnpm", ["install"])).toBe(false); |
| 16 | + }); |
| 17 | + |
| 18 | + test("a bare command name matches any args", () => { |
| 19 | + const handler = command("deploy"); |
| 20 | + expect(handler.matches("deploy", [])).toBe(true); |
| 21 | + expect(handler.matches("deploy", ["--prod"])).toBe(true); |
| 22 | + }); |
| 23 | + |
| 24 | + test("empty patterns throw", () => { |
| 25 | + expect(() => command(" ")).toThrow(/must not be empty/); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe("regex patterns", () => { |
| 30 | + test("matches the full command line", () => { |
| 31 | + const handler = command(/^git (pull|push)/); |
| 32 | + expect(handler.commandNames).toEqual(["git"]); |
| 33 | + expect(handler.matches("git", ["push", "origin"])).toBe(true); |
| 34 | + expect(handler.matches("git", ["status"])).toBe(false); |
| 35 | + }); |
| 36 | + |
| 37 | + test("stateful (global) regexes match consistently across calls", () => { |
| 38 | + const handler = command(/npm install/g); |
| 39 | + expect(handler.matches("npm", ["install"])).toBe(true); |
| 40 | + expect(handler.matches("npm", ["install"])).toBe(true); |
| 41 | + }); |
| 42 | + |
| 43 | + test("throws when the command name cannot be extracted", () => { |
| 44 | + expect(() => command(/(npm|pnpm) install/)).toThrow(/Cannot extract command name/); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("responses", () => { |
| 49 | + test("defaults to an empty successful response", async () => { |
| 50 | + expect(await command("noop").resolve("noop", [], ctx)).toEqual({}); |
| 51 | + }); |
| 52 | + |
| 53 | + test("static responses are returned as-is", async () => { |
| 54 | + const handler = command("fail", { stderr: "boom", exitCode: 2 }); |
| 55 | + expect(await handler.resolve("fail", [], ctx)).toEqual({ stderr: "boom", exitCode: 2 }); |
| 56 | + }); |
| 57 | + |
| 58 | + test("function responses receive args and context", async () => { |
| 59 | + const handler = command("echo-args", (args, { stdin }) => ({ |
| 60 | + stdout: JSON.stringify({ args, stdin }), |
| 61 | + })); |
| 62 | + const result = await handler.resolve("echo-args", ["a"], { stdin: "in" }); |
| 63 | + expect(JSON.parse(result.stdout!)).toEqual({ args: ["a"], stdin: "in" }); |
| 64 | + }); |
| 65 | + }); |
| 66 | +}); |
| 67 | + |
| 68 | +describe("handlersToCustomCommands", () => { |
| 69 | + test("groups handlers for the same command into one just-bash command", () => { |
| 70 | + const commands = handlersToCustomCommands([ |
| 71 | + command("npm install"), |
| 72 | + command("npm run"), |
| 73 | + command("deploy"), |
| 74 | + ]); |
| 75 | + expect(commands.map((c) => c.name).sort()).toEqual(["deploy", "npm"]); |
| 76 | + }); |
| 77 | + |
| 78 | + test("the first matching handler wins and defaults are filled in", async () => { |
| 79 | + const bash = new Bash({ |
| 80 | + customCommands: handlersToCustomCommands([ |
| 81 | + command("npm install", { stdout: "installed\n" }), |
| 82 | + command("npm", { stdout: "generic\n", exitCode: 1 }), |
| 83 | + ]), |
| 84 | + }); |
| 85 | + expect(await bash.exec("npm install")).toMatchObject({ |
| 86 | + stdout: "installed\n", |
| 87 | + stderr: "", |
| 88 | + exitCode: 0, |
| 89 | + }); |
| 90 | + expect(await bash.exec("npm audit")).toMatchObject({ stdout: "generic\n", exitCode: 1 }); |
| 91 | + }); |
| 92 | + |
| 93 | + test("no matching pattern yields exit code 127", async () => { |
| 94 | + const bash = new Bash({ |
| 95 | + customCommands: handlersToCustomCommands([command("npm install")]), |
| 96 | + }); |
| 97 | + const result = await bash.exec("npm run build"); |
| 98 | + expect(result.exitCode).toBe(127); |
| 99 | + expect(result.stderr).toContain("no pattern matched"); |
| 100 | + }); |
| 101 | + |
| 102 | + test("handlers receive piped stdin as a string", async () => { |
| 103 | + const bash = new Bash({ |
| 104 | + customCommands: handlersToCustomCommands([ |
| 105 | + command("consume", (_args, { stdin }) => ({ stdout: `got:${stdin}` })), |
| 106 | + ]), |
| 107 | + }); |
| 108 | + expect((await bash.exec("echo -n data | consume")).stdout).toBe("got:data"); |
| 109 | + }); |
| 110 | +}); |
0 commit comments