feat(settings): 扩展 SettingsData 加 compact 字段,后端解析与校验
This commit is contained in:
@@ -19,16 +19,17 @@ export function getSettings(raw: Database): SettingsData {
|
|||||||
.get();
|
.get();
|
||||||
|
|
||||||
if (!row) {
|
if (!row) {
|
||||||
return { theme: "system" };
|
return { compact: false, theme: "system" };
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const parsed = JSON.parse(row.data) as Partial<SettingsData>;
|
const parsed = JSON.parse(row.data) as Partial<SettingsData>;
|
||||||
return {
|
return {
|
||||||
|
compact: typeof parsed.compact === "boolean" ? parsed.compact : false,
|
||||||
theme: parsed.theme === "dark" || parsed.theme === "light" || parsed.theme === "system" ? parsed.theme : "system",
|
theme: parsed.theme === "dark" || parsed.theme === "light" || parsed.theme === "system" ? parsed.theme : "system",
|
||||||
};
|
};
|
||||||
} catch {
|
} catch {
|
||||||
return { theme: "system" };
|
return { compact: false, theme: "system" };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +41,7 @@ export function updateSettings(raw: Database, data: Partial<SettingsData>, _logg
|
|||||||
.where(and(eq(settings.id, SETTINGS_ID), notDeleted(settings)))
|
.where(and(eq(settings.id, SETTINGS_ID), notDeleted(settings)))
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
let currentData: SettingsData = { theme: "system" };
|
let currentData: SettingsData = { compact: false, theme: "system" };
|
||||||
if (existing) {
|
if (existing) {
|
||||||
try {
|
try {
|
||||||
currentData = JSON.parse(existing.data) as SettingsData;
|
currentData = JSON.parse(existing.data) as SettingsData;
|
||||||
|
|||||||
@@ -32,6 +32,10 @@ export async function handleUpdateSettings(
|
|||||||
return jsonResponse(createApiError("theme 仅支持 dark、light、system", 400), { mode, status: 400 });
|
return jsonResponse(createApiError("theme 仅支持 dark、light、system", 400), { mode, status: 400 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (body.compact !== undefined && typeof body.compact !== "boolean") {
|
||||||
|
return jsonResponse(createApiError("compact 必须为布尔值", 400), { mode, status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
const result = updateSettings(db, body, logger);
|
const result = updateSettings(db, body, logger);
|
||||||
logger.info({ data: result }, "设置已更新");
|
logger.info({ data: result }, "设置已更新");
|
||||||
return jsonResponse(result, { mode });
|
return jsonResponse(result, { mode });
|
||||||
|
|||||||
@@ -238,6 +238,7 @@ export type ProviderType = "anthropic" | "openai" | "openai-compatible";
|
|||||||
export type RuntimeMode = "development" | "production" | "test";
|
export type RuntimeMode = "development" | "production" | "test";
|
||||||
|
|
||||||
export interface SettingsData {
|
export interface SettingsData {
|
||||||
|
compact?: boolean;
|
||||||
theme: ThemePreference;
|
theme: ThemePreference;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,17 +20,17 @@ describe("设置数据访问层", () => {
|
|||||||
test("getSettings 无数据时返回默认值", () => {
|
test("getSettings 无数据时返回默认值", () => {
|
||||||
withSettingsDb((db) => {
|
withSettingsDb((db) => {
|
||||||
const result = getSettings(db);
|
const result = getSettings(db);
|
||||||
expect(result).toEqual({ theme: "system" });
|
expect(result).toEqual({ compact: false, theme: "system" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test("updateSettings 写入并读取", () => {
|
test("updateSettings 写入并读取", () => {
|
||||||
withSettingsDb((db) => {
|
withSettingsDb((db) => {
|
||||||
const updated = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
const updated = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||||
expect(updated).toEqual({ theme: "dark" });
|
expect(updated).toEqual({ compact: false, theme: "dark" });
|
||||||
|
|
||||||
const read = getSettings(db);
|
const read = getSettings(db);
|
||||||
expect(read).toEqual({ theme: "dark" });
|
expect(read).toEqual({ compact: false, theme: "dark" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ describe("设置数据访问层", () => {
|
|||||||
withSettingsDb((db) => {
|
withSettingsDb((db) => {
|
||||||
updateSettings(db, { theme: "dark" }, createNoopLogger());
|
updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||||
const result = updateSettings(db, { theme: "light" }, createNoopLogger());
|
const result = updateSettings(db, { theme: "light" }, createNoopLogger());
|
||||||
expect(result).toEqual({ theme: "light" });
|
expect(result).toEqual({ compact: false, theme: "light" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ describe("设置数据访问层", () => {
|
|||||||
"INSERT INTO settings (id, created_at, updated_at, data) VALUES ('default', '2024-01-01T00:00:00.000Z', '2024-01-01T00:00:00.000Z', 'not-json')",
|
"INSERT INTO settings (id, created_at, updated_at, data) VALUES ('default', '2024-01-01T00:00:00.000Z', '2024-01-01T00:00:00.000Z', 'not-json')",
|
||||||
);
|
);
|
||||||
const result = getSettings(db);
|
const result = getSettings(db);
|
||||||
expect(result).toEqual({ theme: "system" });
|
expect(result).toEqual({ compact: false, theme: "system" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ describe("设置数据访问层", () => {
|
|||||||
"INSERT INTO settings (id, created_at, updated_at, data) VALUES ('default', '2024-01-01T00:00:00.000Z', '2024-01-01T00:00:00.000Z', '{\"theme\":\"unknown\"}')",
|
"INSERT INTO settings (id, created_at, updated_at, data) VALUES ('default', '2024-01-01T00:00:00.000Z', '2024-01-01T00:00:00.000Z', '{\"theme\":\"unknown\"}')",
|
||||||
);
|
);
|
||||||
const result = getSettings(db);
|
const result = getSettings(db);
|
||||||
expect(result).toEqual({ theme: "system" });
|
expect(result).toEqual({ compact: false, theme: "system" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -66,8 +66,8 @@ describe("设置数据访问层", () => {
|
|||||||
withSettingsDb((db) => {
|
withSettingsDb((db) => {
|
||||||
const a = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
const a = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||||
const b = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
const b = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||||
expect(a).toEqual({ theme: "dark" });
|
expect(a).toEqual({ compact: false, theme: "dark" });
|
||||||
expect(b).toEqual({ theme: "dark" });
|
expect(b).toEqual({ compact: false, theme: "dark" });
|
||||||
|
|
||||||
const row = db
|
const row = db
|
||||||
.query("SELECT COUNT(*) as cnt FROM settings WHERE id = 'default' AND deleted_at IS NULL")
|
.query("SELECT COUNT(*) as cnt FROM settings WHERE id = 'default' AND deleted_at IS NULL")
|
||||||
@@ -75,4 +75,42 @@ describe("设置数据访问层", () => {
|
|||||||
expect(row.cnt).toBe(1);
|
expect(row.cnt).toBe(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("getSettings 无 compact 字段时默认 false", () => {
|
||||||
|
withSettingsDb((db) => {
|
||||||
|
db.run(
|
||||||
|
"INSERT INTO settings (id, created_at, updated_at, data) VALUES ('default', '2024-01-01T00:00:00.000Z', '2024-01-01T00:00:00.000Z', '{\"theme\":\"dark\"}')",
|
||||||
|
);
|
||||||
|
const result = getSettings(db);
|
||||||
|
expect(result).toEqual({ compact: false, theme: "dark" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("updateSettings 写入 compact 并读取", () => {
|
||||||
|
withSettingsDb((db) => {
|
||||||
|
const updated = updateSettings(db, { compact: true }, createNoopLogger());
|
||||||
|
expect(updated).toEqual({ compact: true, theme: "system" });
|
||||||
|
|
||||||
|
const read = getSettings(db);
|
||||||
|
expect(read).toEqual({ compact: true, theme: "system" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("updateSettings compact 与 theme 合并", () => {
|
||||||
|
withSettingsDb((db) => {
|
||||||
|
updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||||
|
const result = updateSettings(db, { compact: true }, createNoopLogger());
|
||||||
|
expect(result).toEqual({ compact: true, theme: "dark" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("getSettings compact 为非布尔值时回退 false", () => {
|
||||||
|
withSettingsDb((db) => {
|
||||||
|
db.run(
|
||||||
|
"INSERT INTO settings (id, created_at, updated_at, data) VALUES ('default', '2024-01-01T00:00:00.000Z', '2024-01-01T00:00:00.000Z', '{\"theme\":\"dark\",\"compact\":\"yes\"}')",
|
||||||
|
);
|
||||||
|
const result = getSettings(db);
|
||||||
|
expect(result).toEqual({ compact: false, theme: "dark" });
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,8 +36,8 @@ describe("设置 API 路由", () => {
|
|||||||
const req = new Request("http://localhost/api/settings");
|
const req = new Request("http://localhost/api/settings");
|
||||||
const res = await getSettingsViaHandler(req, db);
|
const res = await getSettingsViaHandler(req, db);
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
const body = (await res.json()) as { theme: string };
|
const body = (await res.json()) as { compact: boolean; theme: string };
|
||||||
expect(body).toEqual({ theme: "system" });
|
expect(body).toEqual({ compact: false, theme: "system" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -50,14 +50,14 @@ describe("设置 API 路由", () => {
|
|||||||
});
|
});
|
||||||
const putRes = await updateSettingsViaHandler(putReq, db);
|
const putRes = await updateSettingsViaHandler(putReq, db);
|
||||||
expect(putRes.status).toBe(200);
|
expect(putRes.status).toBe(200);
|
||||||
const putBody = (await putRes.json()) as { theme: string };
|
const putBody = (await putRes.json()) as { compact: boolean; theme: string };
|
||||||
expect(putBody).toEqual({ theme: "dark" });
|
expect(putBody).toEqual({ compact: false, theme: "dark" });
|
||||||
|
|
||||||
const getReq = new Request("http://localhost/api/settings");
|
const getReq = new Request("http://localhost/api/settings");
|
||||||
const getRes = await getSettingsViaHandler(getReq, db);
|
const getRes = await getSettingsViaHandler(getReq, db);
|
||||||
expect(getRes.status).toBe(200);
|
expect(getRes.status).toBe(200);
|
||||||
const getBody = (await getRes.json()) as { theme: string };
|
const getBody = (await getRes.json()) as { compact: boolean; theme: string };
|
||||||
expect(getBody).toEqual({ theme: "dark" });
|
expect(getBody).toEqual({ compact: false, theme: "dark" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -77,8 +77,8 @@ describe("设置 API 路由", () => {
|
|||||||
});
|
});
|
||||||
const res2 = await updateSettingsViaHandler(req2, db);
|
const res2 = await updateSettingsViaHandler(req2, db);
|
||||||
expect(res2.status).toBe(200);
|
expect(res2.status).toBe(200);
|
||||||
const body = (await res2.json()) as { theme: string };
|
const body = (await res2.json()) as { compact: boolean; theme: string };
|
||||||
expect(body).toEqual({ theme: "light" });
|
expect(body).toEqual({ compact: false, theme: "light" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -139,8 +139,56 @@ describe("设置 API 路由", () => {
|
|||||||
});
|
});
|
||||||
const res = await updateSettingsViaHandler(req, db);
|
const res = await updateSettingsViaHandler(req, db);
|
||||||
expect(res.status).toBe(200);
|
expect(res.status).toBe(200);
|
||||||
const body = (await res.json()) as { theme: string };
|
const body = (await res.json()) as { compact: boolean; theme: string };
|
||||||
expect(body).toEqual({ theme: "system" });
|
expect(body).toEqual({ compact: false, theme: "system" });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("PUT /api/settings compact 为非布尔值返回 400", async () => {
|
||||||
|
await withRouteDb(async (db) => {
|
||||||
|
const req = new Request("http://localhost/api/settings", {
|
||||||
|
body: JSON.stringify({ compact: "true" }),
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
method: "PUT",
|
||||||
|
});
|
||||||
|
const res = await updateSettingsViaHandler(req, db);
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("PUT /api/settings compact=true 合法", async () => {
|
||||||
|
await withRouteDb(async (db) => {
|
||||||
|
const req = new Request("http://localhost/api/settings", {
|
||||||
|
body: JSON.stringify({ compact: true }),
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
method: "PUT",
|
||||||
|
});
|
||||||
|
const res = await updateSettingsViaHandler(req, db);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
const body = (await res.json()) as { compact: boolean; theme: string };
|
||||||
|
expect(body.compact).toBe(true);
|
||||||
|
expect(body.theme).toBe("system");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test("PUT /api/settings compact 与 theme 合并持久化", async () => {
|
||||||
|
await withRouteDb(async (db) => {
|
||||||
|
const req1 = new Request("http://localhost/api/settings", {
|
||||||
|
body: JSON.stringify({ theme: "dark" }),
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
method: "PUT",
|
||||||
|
});
|
||||||
|
await updateSettingsViaHandler(req1, db);
|
||||||
|
|
||||||
|
const req2 = new Request("http://localhost/api/settings", {
|
||||||
|
body: JSON.stringify({ compact: true }),
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
method: "PUT",
|
||||||
|
});
|
||||||
|
const res2 = await updateSettingsViaHandler(req2, db);
|
||||||
|
expect(res2.status).toBe(200);
|
||||||
|
const body = (await res2.json()) as { compact: boolean; theme: string };
|
||||||
|
expect(body).toEqual({ compact: true, theme: "dark" });
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user