feat(settings): 扩展 SettingsData 加 compact 字段,后端解析与校验
This commit is contained in:
@@ -20,17 +20,17 @@ describe("设置数据访问层", () => {
|
||||
test("getSettings 无数据时返回默认值", () => {
|
||||
withSettingsDb((db) => {
|
||||
const result = getSettings(db);
|
||||
expect(result).toEqual({ theme: "system" });
|
||||
expect(result).toEqual({ compact: false, theme: "system" });
|
||||
});
|
||||
});
|
||||
|
||||
test("updateSettings 写入并读取", () => {
|
||||
withSettingsDb((db) => {
|
||||
const updated = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||
expect(updated).toEqual({ theme: "dark" });
|
||||
expect(updated).toEqual({ compact: false, theme: "dark" });
|
||||
|
||||
const read = getSettings(db);
|
||||
expect(read).toEqual({ theme: "dark" });
|
||||
expect(read).toEqual({ compact: false, theme: "dark" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -38,7 +38,7 @@ describe("设置数据访问层", () => {
|
||||
withSettingsDb((db) => {
|
||||
updateSettings(db, { theme: "dark" }, 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')",
|
||||
);
|
||||
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\"}')",
|
||||
);
|
||||
const result = getSettings(db);
|
||||
expect(result).toEqual({ theme: "system" });
|
||||
expect(result).toEqual({ compact: false, theme: "system" });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -66,8 +66,8 @@ describe("设置数据访问层", () => {
|
||||
withSettingsDb((db) => {
|
||||
const a = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||
const b = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||||
expect(a).toEqual({ theme: "dark" });
|
||||
expect(b).toEqual({ theme: "dark" });
|
||||
expect(a).toEqual({ compact: false, theme: "dark" });
|
||||
expect(b).toEqual({ compact: false, theme: "dark" });
|
||||
|
||||
const row = db
|
||||
.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);
|
||||
});
|
||||
});
|
||||
|
||||
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 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