fix(backend): forward workspace to tht config selection (MVP backend-side)

POST /sessions no longer silently drops `workspace`. ThtRunner.run/json
take an optional workspace; configArg() selects workspaces/<ws>.yaml when
it exists under harnessDir, else falls back to default configPath.
sessionNew threads workspace through. Route forwards b.workspace with an
MVP note (gate/Pi side still single-workspace via symlinked config/tht.yaml).

19/19 tests pass, tsc clean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 21:26:09 +02:00
co-authored by Claude Sonnet 4.6
parent b4b26e313b
commit 061002f85c
4 changed files with 61 additions and 9 deletions
+8 -2
View File
@@ -7,13 +7,19 @@ import { loadConfig } from "../src/config.js";
const FAKE = path.resolve("../harness/tests/fake_pi/fake_pi_rpc.mjs");
const SCRIPT = path.resolve("../harness/tests/fake_pi/scripts/f1_disambiguation.json");
test("POST /sessions crea e avvia, GET /sessions lista", async () => {
test("POST /sessions crea e avvia, GET /sessions lista; inoltra workspace a sessionNew", async () => {
let sessionNewArg: any;
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
thtRunner: { sessionNew: async () => ({ id: "s1" }), sessionList: async () => [{ id: "s1" }] } as any,
thtRunner: {
sessionNew: async (o: any) => { sessionNewArg = o; return { id: "s1" }; },
sessionList: async () => [{ id: "s1" }],
} as any,
spawnFn: () => spawn("node", [FAKE, SCRIPT]) as any,
});
const created = await app.inject({ method: "POST", url: "/sessions", payload: { workspace: "w", question: "q" } });
expect(created.json()).toEqual({ id: "s1" });
expect(sessionNewArg.workspace).toBe("w");
expect(sessionNewArg.question).toBe("q");
const list = await app.inject({ method: "GET", url: "/sessions" });
expect(list.json()).toEqual([{ id: "s1" }]);
});
+28 -1
View File
@@ -1,6 +1,23 @@
import { test, expect } from "vitest";
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: "" });
@@ -12,3 +29,13 @@ test("run with exit != 0 propagates error with stderr", async () => {
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");
expect(argv).toEqual(["-c", "config/tht.yaml", "session", "new", "q", "--json"]);
});