204 lines
6.8 KiB
TypeScript
204 lines
6.8 KiB
TypeScript
import { test, expect } from "vitest";
|
|
import Fastify from "fastify";
|
|
import { buildApp } from "../src/app.js";
|
|
import { loadConfig } from "../src/config.js";
|
|
import { sqlRoutes } from "../src/routes/sql.js";
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// SQL routes
|
|
// ---------------------------------------------------------------------------
|
|
|
|
test("POST /sessions/:id/sql/preview returns rows from injected thtRunner stub", async () => {
|
|
const previewResult = {
|
|
columns: ["a"],
|
|
rows: [[1]],
|
|
execution_ms: 2,
|
|
truncated: false,
|
|
};
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
thtRunner: {
|
|
sqlPreview: async () => previewResult,
|
|
} as any,
|
|
});
|
|
|
|
const res = await app.inject({
|
|
method: "POST",
|
|
url: "/sessions/s1/sql/preview",
|
|
payload: { limit: 10, offset: 0 },
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.json()).toEqual(previewResult);
|
|
});
|
|
|
|
test("SQL lookup and execution derive harness admin compatibility from session.read_all", async () => {
|
|
const seen: boolean[] = [];
|
|
const app = Fastify();
|
|
app.addHook("preHandler", async (request) => {
|
|
request.principal = {
|
|
issuer: "portal", subject: "inconsistent", roles: ["user"], permissions: ["session.use"], isAdmin: true,
|
|
};
|
|
});
|
|
const runner = {
|
|
sessionShow: async () => ({ id: "s1" }),
|
|
sqlPreview: async () => ({ columns: [], rows: [], execution_ms: 0, truncated: false }),
|
|
};
|
|
sqlRoutes(app, {
|
|
tht: {
|
|
withPrincipal: (principal: { isAdmin: boolean }) => {
|
|
seen.push(principal.isAdmin);
|
|
return runner;
|
|
},
|
|
} as any,
|
|
getSettings: async () => ({ workspace: "legacy" }),
|
|
workspaceRegistry: { list: async () => [] } as any,
|
|
});
|
|
|
|
const response = await app.inject({ method: "POST", url: "/sessions/s1/sql/preview", payload: {} });
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(seen).toEqual([false, false]);
|
|
});
|
|
|
|
test("POST /sessions/:id/sql/preview passes limit and offset to thtRunner", async () => {
|
|
let captured: any;
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
thtRunner: {
|
|
sqlPreview: async (id: string, p: any) => {
|
|
captured = { id, ...p };
|
|
return { columns: [], rows: [], execution_ms: 0, truncated: false };
|
|
},
|
|
} as any,
|
|
});
|
|
|
|
await app.inject({
|
|
method: "POST",
|
|
url: "/sessions/abc/sql/preview",
|
|
payload: { limit: 25, offset: 50 },
|
|
});
|
|
|
|
expect(captured.id).toBe("abc");
|
|
expect(captured.limit).toBe(25);
|
|
expect(captured.offset).toBe(50);
|
|
});
|
|
|
|
test("POST /sessions/:id/sql/export returns {path} from injected thtRunner stub", async () => {
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
thtRunner: {
|
|
sqlExport: async (_id: string) => ({ path: "/tmp/export.csv" }),
|
|
} as any,
|
|
});
|
|
|
|
const res = await app.inject({
|
|
method: "POST",
|
|
url: "/sessions/s1/sql/export",
|
|
payload: {},
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.json()).toEqual({ path: "/tmp/export.csv" });
|
|
});
|
|
|
|
test("POST /sessions/:id/sql/preview returns 500 when thtRunner throws", async () => {
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
thtRunner: {
|
|
sqlPreview: async () => { throw new Error("tht boom"); },
|
|
} as any,
|
|
});
|
|
|
|
const res = await app.inject({
|
|
method: "POST",
|
|
url: "/sessions/s1/sql/preview",
|
|
payload: {},
|
|
});
|
|
|
|
expect(res.statusCode).toBe(500);
|
|
expect(res.json()).toMatchObject({ error: /boom/ });
|
|
});
|
|
|
|
test("registry-backed SQL preview resolves and uses the session's pinned runtime revision", async () => {
|
|
const activePath = `/registry/snapshots/${"a".repeat(40)}/psd-clinical.yaml`;
|
|
const pinnedPath = `/registry/snapshots/${"b".repeat(40)}/psd-clinical.yaml`;
|
|
const calls: string[] = [];
|
|
const runner = {
|
|
sessionShow: async (_id: string, workspace: string) => {
|
|
calls.push(`show:${workspace}`);
|
|
if (workspace === activePath) return {
|
|
id: "s1", workspace_id: "psd-clinical", workspace_revision: "b".repeat(40),
|
|
};
|
|
throw new Error("session not found");
|
|
},
|
|
sqlPreview: async (_id: string, _page: unknown, workspace: string) => {
|
|
calls.push(`preview:${workspace}`);
|
|
return { columns: [], rows: [], execution_ms: 0, truncated: false };
|
|
},
|
|
};
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
thtRunner: { ...runner, withPrincipal: () => runner } as any,
|
|
getSettings: () => ({ workspace: "legacy-default" }) as any,
|
|
workspaceRegistry: {
|
|
list: async () => [{
|
|
id: "psd-clinical", commit: "a".repeat(40), blob: "c".repeat(40),
|
|
snapshotPath: activePath,
|
|
}],
|
|
readPinned: async () => ({ workspace: {}, workspaceConfigPath: pinnedPath }),
|
|
} as any,
|
|
});
|
|
|
|
const response = await app.inject({
|
|
method: "POST", url: "/sessions/s1/sql/preview", payload: { limit: 10 },
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(calls).toEqual([`show:${activePath}`, `preview:${pinnedPath}`]);
|
|
});
|
|
|
|
// Workspace registry route coverage lives in routes-workspaces.test.ts. `/workspaces` no longer
|
|
// reads legacy harness files: the Git registry is the single shared source of truth.
|
|
|
|
test("GET /models returns session choices from the installation model catalog", async () => {
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
thtRunner: {} as any,
|
|
runtimeModelCatalog: {
|
|
defaultSession: "zai/glm-5.2",
|
|
defaultMetadataGeneration: null,
|
|
embedding: { id: "ollama/qwen3-embedding:0.6b", dimensions: 1024 },
|
|
sessionModels: () => [{
|
|
id: "zai/glm-5.2", provider: "zai", model: "glm-5.2", label: "GLM 5.2",
|
|
upstreamModel: "glm-5.2", authentication: { mode: "pi_auth" },
|
|
sessionAdapter: { mode: "pi_builtin" }, session: { reasoning: true },
|
|
}],
|
|
metadataModels: () => [],
|
|
hasSession: (id: string) => id === "zai/glm-5.2",
|
|
},
|
|
// Runtime introspection is a health gate for starting a session, not a second catalog.
|
|
listModels: async () => { throw new Error("Pi is unavailable"); },
|
|
});
|
|
|
|
const res = await app.inject({ method: "GET", url: "/models" });
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.json()).toEqual({
|
|
models: [{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true }],
|
|
});
|
|
});
|
|
|
|
test("GET /models returns an empty list when the catalog has no session models", async () => {
|
|
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
|
thtRunner: {} as any,
|
|
runtimeModelCatalog: {
|
|
defaultSession: null,
|
|
defaultMetadataGeneration: null,
|
|
embedding: null,
|
|
sessionModels: () => [],
|
|
metadataModels: () => [],
|
|
hasSession: () => false,
|
|
},
|
|
});
|
|
|
|
const res = await app.inject({ method: "GET", url: "/models" });
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
expect(res.json()).toEqual({ models: [] });
|
|
});
|