Live end-to-end surfaced a 500 on every tht call: ThtRunner prepended
'-c <config>' before the subcommand, but tht has no global -c option
('No such option: -c'). --config/-c is a per-command option, so it must be
appended AFTER the subcommand. Extracted buildArgv() and fixed the unit test
that had codified the wrong (prepended) order.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
76 lines
3.7 KiB
TypeScript
76 lines
3.7 KiB
TypeScript
import { test, expect, vi } from "vitest";
|
|
import { EventEmitter } from "node:events";
|
|
import { ThtRunner } from "../src/tht/tht-runner.js";
|
|
|
|
// Spy on child_process.spawn so we can capture the resolved argv (incl. -c config)
|
|
// that ThtRunner.run() builds, without launching a real process.
|
|
vi.mock("node:child_process", () => ({
|
|
spawn: vi.fn(() => {
|
|
const ch: any = new EventEmitter();
|
|
ch.stdout = new EventEmitter();
|
|
ch.stderr = new EventEmitter();
|
|
queueMicrotask(() => {
|
|
ch.stdout.emit("data", Buffer.from('{"id":"x"}'));
|
|
ch.emit("close", 0);
|
|
});
|
|
return ch;
|
|
}),
|
|
}));
|
|
import { spawn } from "node:child_process";
|
|
|
|
test("sessionNew parses id from JSON", async () => {
|
|
const r = new ThtRunner({ thtBin: "tht", harnessDir: "/h", configPath: "config/tht.yaml" });
|
|
r.run = async () => ({ code: 0, stdout: '{"id":"2026-06-27-100000-x"}', stderr: "" });
|
|
expect(await r.sessionNew({ question: "q" })).toEqual({ id: "2026-06-27-100000-x" });
|
|
});
|
|
|
|
test("run with exit != 0 propagates error with stderr", async () => {
|
|
const r = new ThtRunner({ thtBin: "tht", harnessDir: "/h", configPath: "config/tht.yaml" });
|
|
r.run = async () => ({ code: 1, stdout: "", stderr: "ERRORE: boom" });
|
|
await expect(r.sessionList()).rejects.toThrow(/boom/);
|
|
});
|
|
|
|
test("sessionNew with missing workspace file falls back to default configPath argv", async () => {
|
|
// harnessDir "/nope" has no workspaces/foo.yaml -> configArg falls back to default.
|
|
(spawn as any).mockClear();
|
|
const r = new ThtRunner({ thtBin: "tht", harnessDir: "/nope", configPath: "config/tht.yaml" });
|
|
await r.sessionNew({ question: "q", workspace: "foo" });
|
|
const [bin, argv] = (spawn as any).mock.calls[0];
|
|
expect(bin).toBe("tht");
|
|
// `--config`/`-c` is a PER-COMMAND option in tht (no global -c): it MUST follow
|
|
// the subcommand, never precede it. (Prepending it caused a live 500 "No such option: -c".)
|
|
expect(argv).toEqual(["session", "new", "q", "--json", "-c", "config/tht.yaml"]);
|
|
});
|
|
|
|
test("buildArgv appends -c AFTER the subcommand (never a global -c)", () => {
|
|
const r = new ThtRunner({ thtBin: "tht", harnessDir: "/nope", configPath: "config/tht.yaml" });
|
|
expect(r.buildArgv(["session", "list", "--json"])).toEqual([
|
|
"session", "list", "--json", "-c", "config/tht.yaml",
|
|
]);
|
|
});
|
|
|
|
test("sqlPreview argv has no positional file — uses --session to resolve path", async () => {
|
|
// The harness preview_cmd now resolves sql_final.sql from the session workspace;
|
|
// the backend must NOT pass a sessions/<id>/sql_final.sql positional arg.
|
|
(spawn as any).mockClear();
|
|
const r = new ThtRunner({ thtBin: "tht", harnessDir: "/nope", configPath: "config/tht.yaml" });
|
|
// stub json() via run() — just need spawn call captured
|
|
r.run = async () => ({ code: 0, stdout: '{"columns":[],"rows":[],"execution_ms":1,"truncated":false}', stderr: "" });
|
|
await r.sqlPreview("ses1", { limit: 10, offset: 5 });
|
|
// Verify via the patched run — we stub run() so spawn isn't called again.
|
|
// Instead confirm directly that sqlPreview builds the right args by inspecting run calls.
|
|
// We swap back to a spy on run itself.
|
|
const runSpy = vi.fn().mockResolvedValue({
|
|
code: 0,
|
|
stdout: '{"columns":["c"],"rows":[[1]],"execution_ms":2,"truncated":false}',
|
|
stderr: "",
|
|
});
|
|
const r2 = new ThtRunner({ thtBin: "tht", harnessDir: "/nope", configPath: "config/tht.yaml" });
|
|
r2.run = runSpy;
|
|
await r2.sqlPreview("ses2", { limit: 20, offset: 0 });
|
|
const [calledArgs] = runSpy.mock.calls[0];
|
|
// Must NOT include any positional file path before --session
|
|
expect(calledArgs).toEqual(["sql", "preview", "--session", "ses2", "--json", "--limit", "20", "--offset", "0"]);
|
|
expect(calledArgs).not.toContain("sessions/ses2/sql_final.sql");
|
|
});
|