fix: bootstrap user preferences without invalidating DWH cache

This commit is contained in:
User
2026-07-16 20:32:20 +02:00
parent ccb3cf4aa9
commit 5cacf70a0d
6 changed files with 242 additions and 3 deletions
+7 -1
View File
@@ -64,7 +64,13 @@ export function buildApp(config: AppConfig, deps?: BuildAppDeps): FastifyInstanc
// The real runner persists preferences through the harness repository. The file fallback
// only keeps older isolated route tests and externally injected runners compatible.
if (typeof runner.preferencesGet === "function") {
return effectiveSettings(config, await runner.preferencesGet());
const stored = await runner.preferencesGet() as Settings;
if (Object.keys(stored).length === 0) {
const seeded = effectiveSettings(config, loadSettings(config));
await runner.preferencesSet(seeded);
return seeded;
}
return effectiveSettings(config, stored);
}
return effectiveSettings(config, loadSettings(config));
};
+60 -1
View File
@@ -1,5 +1,5 @@
import { test, expect } from "vitest";
import { mkdtempSync, rmSync } from "node:fs";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { buildApp } from "../src/app.js";
@@ -77,6 +77,65 @@ test("settings are isolated by the authenticated repository principal", async ()
expect(bob.json()).not.toMatchObject({ workspace: "psd", thinking: "high" });
});
test("GET /settings seeds an empty private profile from complete legacy settings once", async () => {
let preferences: Record<string, unknown> = {};
const writes: Record<string, unknown>[] = [];
const runner = {
withPrincipal: () => ({
preferencesGet: async () => preferences,
preferencesSet: async (next: Record<string, unknown>) => {
writes.push(next);
preferences = next;
},
}),
};
const { app, dir } = appWithTmpSettings({}, { thtRunner: runner as any, listModels: async () => [] });
try {
writeFileSync(join(dir, "settings.json"), JSON.stringify({
workspace: "local", provider: "local-qwen", model: "qwen3.6-35b-a3b", thinking: "low",
}));
const first = await app.inject({ method: "GET", url: "/settings" });
const second = await app.inject({ method: "GET", url: "/settings" });
const expected = {
workspace: "local", provider: "local-qwen", model: "qwen3.6-35b-a3b", thinking: "low",
};
expect(first.statusCode).toBe(200);
expect(first.json()).toEqual(expected);
expect(second.json()).toEqual(expected);
expect(writes).toEqual([expected]);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
test("GET /settings does not overwrite an existing private profile with legacy settings", async () => {
const privateSettings = {
workspace: "private", provider: "zai", model: "glm-5.2", thinking: "high",
};
const preferencesSet = async () => { throw new Error("must not seed an existing profile"); };
const runner = {
withPrincipal: () => ({
preferencesGet: async () => privateSettings,
preferencesSet,
}),
};
const { app, dir } = appWithTmpSettings({}, { thtRunner: runner as any, listModels: async () => [] });
try {
writeFileSync(join(dir, "settings.json"), JSON.stringify({
workspace: "local", provider: "local-qwen", model: "qwen3.6-35b-a3b", thinking: "low",
}));
const response = await app.inject({ method: "GET", url: "/settings" });
expect(response.statusCode).toBe(200);
expect(response.json()).toEqual(privateSettings);
} 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 }],