Files
united-workforce/packages/cli/src/__tests__/thread-step-count.test.ts
T
xiaoju 18170a4313
CI / check (pull_request) Successful in 2m24s
refactor: extract validateCount, replace CLI spawn with direct import
- Extract validateCount() from cmdThreadExec (throw instead of process.exit)
- 5 validation tests now import validateCount directly (no subprocess)
- Only --help tests still spawn CLI (need Commander output)
- Test time: 1.7s → 475ms

Fixes #61
2026-06-04 12:31:17 +00:00

71 lines
2.1 KiB
TypeScript

import { execFileSync } from "node:child_process";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { describe, expect, test } from "vitest";
import { validateCount } from "../commands/thread.js";
const CLI_PATH = join(dirname(fileURLToPath(import.meta.url)), "..", "..", "dist", "cli.js");
function runCli(args: string[]): {
stdout: string;
stderr: string;
exitCode: number;
} {
try {
const stdout = execFileSync("node", [CLI_PATH, ...args], {
encoding: "utf8",
env: { ...process.env, UWF_HOME: "/tmp/uwf-test-nonexistent" },
stdio: ["ignore", "pipe", "pipe"],
});
return { stdout, stderr: "", exitCode: 0 };
} catch (e: unknown) {
const err = e as NodeJS.ErrnoException & {
stdout?: string;
stderr?: string;
status?: number;
};
return {
stdout: err.stdout ?? "",
stderr: err.stderr ?? "",
exitCode: err.status ?? 1,
};
}
}
describe("thread exec --count CLI parsing", { timeout: 30_000 }, () => {
test("--help shows -c/--count option", () => {
const result = runCli(["thread", "exec", "--help"]);
const combined = result.stdout + result.stderr;
expect(combined).toContain("--count");
expect(combined).toContain("-c");
});
test("description says 'one or more steps'", () => {
const result = runCli(["thread", "exec", "--help"]);
const combined = result.stdout + result.stderr;
expect(combined).toContain("one or more steps");
});
});
describe("validateCount", () => {
test("count=0 throws validation error", () => {
expect(() => validateCount(0)).toThrow("positive integer");
});
test("negative count throws validation error", () => {
expect(() => validateCount(-1)).toThrow("positive integer");
});
test("non-integer count throws validation error", () => {
expect(() => validateCount(1.5)).toThrow("positive integer");
});
test("count=1 passes validation", () => {
expect(() => validateCount(1)).not.toThrow();
});
test("count=3 passes validation", () => {
expect(() => validateCount(3)).not.toThrow();
});
});