feat(settings): 扩展 SettingsData 加 compact 字段,后端解析与校验
This commit is contained in:
@@ -36,8 +36,8 @@ describe("设置 API 路由", () => {
|
||||
const req = new Request("http://localhost/api/settings");
|
||||
const res = await getSettingsViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { theme: string };
|
||||
expect(body).toEqual({ theme: "system" });
|
||||
const body = (await res.json()) as { compact: boolean; theme: string };
|
||||
expect(body).toEqual({ compact: false, theme: "system" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -50,14 +50,14 @@ describe("设置 API 路由", () => {
|
||||
});
|
||||
const putRes = await updateSettingsViaHandler(putReq, db);
|
||||
expect(putRes.status).toBe(200);
|
||||
const putBody = (await putRes.json()) as { theme: string };
|
||||
expect(putBody).toEqual({ theme: "dark" });
|
||||
const putBody = (await putRes.json()) as { compact: boolean; theme: string };
|
||||
expect(putBody).toEqual({ compact: false, theme: "dark" });
|
||||
|
||||
const getReq = new Request("http://localhost/api/settings");
|
||||
const getRes = await getSettingsViaHandler(getReq, db);
|
||||
expect(getRes.status).toBe(200);
|
||||
const getBody = (await getRes.json()) as { theme: string };
|
||||
expect(getBody).toEqual({ theme: "dark" });
|
||||
const getBody = (await getRes.json()) as { compact: boolean; theme: string };
|
||||
expect(getBody).toEqual({ compact: false, theme: "dark" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -77,8 +77,8 @@ describe("设置 API 路由", () => {
|
||||
});
|
||||
const res2 = await updateSettingsViaHandler(req2, db);
|
||||
expect(res2.status).toBe(200);
|
||||
const body = (await res2.json()) as { theme: string };
|
||||
expect(body).toEqual({ theme: "light" });
|
||||
const body = (await res2.json()) as { compact: boolean; theme: string };
|
||||
expect(body).toEqual({ compact: false, theme: "light" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -139,8 +139,56 @@ describe("设置 API 路由", () => {
|
||||
});
|
||||
const res = await updateSettingsViaHandler(req, db);
|
||||
expect(res.status).toBe(200);
|
||||
const body = (await res.json()) as { theme: string };
|
||||
expect(body).toEqual({ theme: "system" });
|
||||
const body = (await res.json()) as { compact: boolean; theme: string };
|
||||
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