Spaces:
Runtime error
Runtime error
File size: 3,257 Bytes
cd8bd0a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import { describe, it, expect, vi, beforeEach } from "vitest";
import { PATCH } from "../route";
// Mock the localDb functions used in the route
vi.mock("../../../../lib/localDb", () => {
const original = vi.importActual("../../../../lib/localDb");
return {
...original,
getSettings: vi.fn(),
updateSettings: vi.fn(),
};
});
import { getSettings, updateSettings } from "../../../../lib/localDb";
// Helper to create a Request with JSON body
function createPatchRequest(body: unknown) {
return new Request("http://localhost/api/settings", {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
}
describe("PATCH /api/settings", () => {
beforeEach(() => {
vi.resetAllMocks();
// Default settings before each test
(getSettings as any).mockResolvedValue({
debugMode: false,
hiddenSidebarItems: [],
hiddenSidebarGroupLabels: [],
comboConfigMode: "guided",
});
// Mock updateSettings to merge updates into the original
(updateSettings as any).mockImplementation(async (updates: Record<string, unknown>) => {
const current = await (getSettings as any)();
return { ...current, ...updates };
});
});
it("toggles debugMode via PATCH", async () => {
const req = createPatchRequest({ debugMode: true });
const res = await PATCH(req as any);
expect(res.status).toBe(200);
const json = await res.json();
expect(json.debugMode).toBe(true);
// Ensure password is not leaked
expect(json).not.toHaveProperty("password");
// Verify DB update called with correct payload
expect(updateSettings).toHaveBeenCalledOnce();
const calledWith = (updateSettings as any).mock.calls[0][0];
expect(calledWith.debugMode).toBe(true);
});
it("updates hiddenSidebarItems via PATCH", async () => {
const req = createPatchRequest({ hiddenSidebarItems: [] });
const res = await PATCH(req as any);
expect(res.status).toBe(200);
const json = await res.json();
expect(json.hiddenSidebarItems).toEqual([]);
expect(updateSettings).toHaveBeenCalledOnce();
const calledWith = (updateSettings as any).mock.calls[0][0];
expect(calledWith.hiddenSidebarItems).toEqual([]);
});
it("updates hiddenSidebarGroupLabels via PATCH", async () => {
const req = createPatchRequest({ hiddenSidebarGroupLabels: ["logs", "audit"] });
const res = await PATCH(req as any);
expect(res.status).toBe(200);
const json = await res.json();
expect(json.hiddenSidebarGroupLabels).toEqual(["logs", "audit"]);
expect(updateSettings).toHaveBeenCalledOnce();
const calledWith = (updateSettings as any).mock.calls[0][0];
expect(calledWith.hiddenSidebarGroupLabels).toEqual(["logs", "audit"]);
});
it("updates comboConfigMode via PATCH", async () => {
const req = createPatchRequest({ comboConfigMode: "expert" });
const res = await PATCH(req as any);
expect(res.status).toBe(200);
const json = await res.json();
expect(json.comboConfigMode).toBe("expert");
expect(updateSettings).toHaveBeenCalledOnce();
const calledWith = (updateSettings as any).mock.calls[0][0];
expect(calledWith.comboConfigMode).toBe("expert");
});
});
|