feat(backend): /settings GET+PUT, /models PiModel shape, app wiring
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { test, expect } from "vitest";
|
||||
import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { buildApp } from "../src/app.js";
|
||||
import { loadConfig } from "../src/config.js";
|
||||
|
||||
function appWithTmpSettings(extraEnv: Record<string, string> = {}, deps = {}) {
|
||||
const dir = mkdtempSync(join(tmpdir(), "tht-set-route-"));
|
||||
const app = buildApp(
|
||||
loadConfig({ THT_HARNESS_DIR: "../harness", SETTINGS_FILE: join(dir, "settings.json"), ...extraEnv }),
|
||||
{ thtRunner: {} as any, ...deps },
|
||||
);
|
||||
return { app, dir };
|
||||
}
|
||||
|
||||
test("GET /settings returns effective defaults (env provider/model/thinking, first workspace)", async () => {
|
||||
const { app, dir } = appWithTmpSettings({ PI_PROVIDER: "zai", PI_MODEL: "glm-5.2", PI_THINKING: "medium" }, {
|
||||
listModels: async () => [{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true }],
|
||||
});
|
||||
try {
|
||||
const res = await app.inject({ method: "GET", url: "/settings" });
|
||||
expect(res.statusCode).toBe(200);
|
||||
const body = res.json();
|
||||
expect(body.provider).toBe("zai");
|
||||
expect(body.model).toBe("glm-5.2");
|
||||
expect(body.thinking).toBe("medium");
|
||||
expect(typeof body.workspace).toBe("string"); // first workspace from ../harness/workspaces
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("PUT /settings persists and GET reads it back", async () => {
|
||||
const { app, dir } = appWithTmpSettings({}, {
|
||||
listModels: async () => [{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true }],
|
||||
});
|
||||
try {
|
||||
const put = await app.inject({
|
||||
method: "PUT", url: "/settings",
|
||||
payload: { workspace: "psd", provider: "zai", model: "glm-5.2", thinking: "high" },
|
||||
});
|
||||
expect(put.statusCode).toBe(200);
|
||||
const got = await app.inject({ method: "GET", url: "/settings" });
|
||||
expect(got.json()).toMatchObject({ workspace: "psd", provider: "zai", model: "glm-5.2", thinking: "high" });
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("PUT /settings rejects an unknown model when a model list is available", async () => {
|
||||
const { app, dir } = appWithTmpSettings({}, {
|
||||
listModels: async () => [{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true }],
|
||||
});
|
||||
try {
|
||||
const put = await app.inject({
|
||||
method: "PUT", url: "/settings",
|
||||
payload: { workspace: "psd", provider: "zai", model: "does-not-exist", thinking: "low" },
|
||||
});
|
||||
expect(put.statusCode).toBe(400);
|
||||
expect(put.json()).toMatchObject({ error: expect.stringMatching(/model/i) });
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("PUT /settings allows any model when model list is empty (Pi unavailable)", async () => {
|
||||
const { app, dir } = appWithTmpSettings({}, { listModels: async () => [] });
|
||||
try {
|
||||
const put = await app.inject({
|
||||
method: "PUT", url: "/settings",
|
||||
payload: { workspace: "psd", model: "whatever", thinking: "low" },
|
||||
});
|
||||
expect(put.statusCode).toBe(200);
|
||||
} finally {
|
||||
rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
@@ -123,13 +123,17 @@ test("GET /workspaces returns [] when harnessDir has no workspaces subdir", asyn
|
||||
test("GET /models returns {models:[...]} from injected listModels stub", async () => {
|
||||
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
||||
thtRunner: {} as any,
|
||||
listModels: async () => ["claude-opus-4", "claude-sonnet-4-5"],
|
||||
listModels: async () => [
|
||||
{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true },
|
||||
],
|
||||
});
|
||||
|
||||
const res = await app.inject({ method: "GET", url: "/models" });
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json()).toEqual({ models: ["claude-opus-4", "claude-sonnet-4-5"] });
|
||||
expect(res.json()).toEqual({
|
||||
models: [{ provider: "zai", id: "glm-5.2", name: "GLM 5.2", reasoning: true }],
|
||||
});
|
||||
});
|
||||
|
||||
test("GET /models returns {models:[]} when listModels throws (graceful fallback)", async () => {
|
||||
@@ -144,8 +148,8 @@ test("GET /models returns {models:[]} when listModels throws (graceful fallback)
|
||||
expect(res.json()).toEqual({ models: [] });
|
||||
});
|
||||
|
||||
test("GET /models with no listModels injected falls back to {models:[]}", async () => {
|
||||
// No listModels dep → defaultListModels → returns [] → {models:[]}
|
||||
test("GET /models with no listModels injected uses real lister (returns array)", async () => {
|
||||
// No listModels dep → createPiModelLister (real); returns PiModel[] (possibly empty if Pi not running)
|
||||
const app = buildApp(loadConfig({ THT_HARNESS_DIR: "../harness" }), {
|
||||
thtRunner: {} as any,
|
||||
// listModels intentionally omitted
|
||||
@@ -154,5 +158,5 @@ test("GET /models with no listModels injected falls back to {models:[]}", async
|
||||
const res = await app.inject({ method: "GET", url: "/models" });
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.json()).toEqual({ models: [] });
|
||||
expect(Array.isArray(res.json().models)).toBe(true);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user