158 lines
5.7 KiB
TypeScript
158 lines
5.7 KiB
TypeScript
import type Database from "bun:sqlite";
|
||
|
||
import { describe, expect, test } from "bun:test";
|
||
|
||
import { getSettings, updateSettings } from "../../../src/server/db/settings";
|
||
import { createNoopLogger } from "../../../src/server/logger";
|
||
import { createMigratedTestDatabase } from "../../helpers";
|
||
|
||
function withSettingsDb(callback: (db: Database) => void): void {
|
||
const handle = createMigratedTestDatabase("settings-test");
|
||
try {
|
||
callback(handle.db);
|
||
handle.close();
|
||
} finally {
|
||
handle.cleanup();
|
||
}
|
||
}
|
||
|
||
describe("设置数据访问层", () => {
|
||
test("getSettings 无数据时返回默认值", () => {
|
||
withSettingsDb((db) => {
|
||
const result = getSettings(db);
|
||
expect(result).toEqual({ compact: false, theme: "system" });
|
||
});
|
||
});
|
||
|
||
test("updateSettings 写入并读取", () => {
|
||
withSettingsDb((db) => {
|
||
const updated = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||
expect(updated).toEqual({ compact: false, theme: "dark" });
|
||
|
||
const read = getSettings(db);
|
||
expect(read).toEqual({ compact: false, theme: "dark" });
|
||
});
|
||
});
|
||
|
||
test("updateSettings 部分更新合并", () => {
|
||
withSettingsDb((db) => {
|
||
updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||
const result = updateSettings(db, { theme: "light" }, createNoopLogger());
|
||
expect(result).toEqual({ compact: false, theme: "light" });
|
||
});
|
||
});
|
||
|
||
test("getSettings 解析非法 JSON 返回默认值", () => {
|
||
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', 'not-json')",
|
||
);
|
||
const result = getSettings(db);
|
||
expect(result).toEqual({ compact: false, theme: "system" });
|
||
});
|
||
});
|
||
|
||
test("getSettings 未知 theme 值返回默认值", () => {
|
||
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\":\"unknown\"}')",
|
||
);
|
||
const result = getSettings(db);
|
||
expect(result).toEqual({ compact: false, theme: "system" });
|
||
});
|
||
});
|
||
|
||
test("updateSettings 幂等覆盖", () => {
|
||
withSettingsDb((db) => {
|
||
const a = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||
const b = updateSettings(db, { theme: "dark" }, createNoopLogger());
|
||
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")
|
||
.get() as { cnt: number };
|
||
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" });
|
||
});
|
||
});
|
||
|
||
test("updateSettings 写入 defaultModels 并读取", () => {
|
||
withSettingsDb((db) => {
|
||
const updated = updateSettings(
|
||
db,
|
||
{ defaultModels: { text: "model-1", imageRecognition: null } },
|
||
createNoopLogger(),
|
||
);
|
||
expect(updated.defaultModels).toEqual({ text: "model-1", imageRecognition: null });
|
||
expect(updated.compact).toBe(false);
|
||
expect(updated.theme).toBe("system");
|
||
|
||
const read = getSettings(db);
|
||
expect(read.defaultModels).toEqual({ text: "model-1", imageRecognition: null });
|
||
});
|
||
});
|
||
|
||
test("defaultModels 与 theme/compact 合并持久化", () => {
|
||
withSettingsDb((db) => {
|
||
updateSettings(db, { theme: "dark", compact: true }, createNoopLogger());
|
||
const result = updateSettings(db, { defaultModels: { imageGeneration: "model-2" } }, createNoopLogger());
|
||
expect(result).toEqual({
|
||
compact: true,
|
||
defaultModels: { imageGeneration: "model-2" },
|
||
theme: "dark",
|
||
});
|
||
});
|
||
});
|
||
|
||
test("defaultModels 全量替换——前端负责深度合并", () => {
|
||
withSettingsDb((db) => {
|
||
updateSettings(db, { defaultModels: { text: "model-1", imageRecognition: "model-2" } }, createNoopLogger());
|
||
// 前端在 onChange 中负责合并 old + newField,提交完整对象
|
||
const result = updateSettings(
|
||
db,
|
||
{ defaultModels: { text: "model-3", imageRecognition: "model-2" } },
|
||
createNoopLogger(),
|
||
);
|
||
expect(result.defaultModels).toEqual({ text: "model-3", imageRecognition: "model-2" });
|
||
});
|
||
});
|
||
});
|