Files
jlinc-server/backend/modules/dashboard/api.test.js
2026-08-20 15:08:32 +00:00

61 lines
3.0 KiB
JavaScript

import { jest } from "@jest/globals";
import { makeDashboardApi } from "./api.js";
const fakeService = () => ({
summary: jest.fn(async () => ({ apiUsage: { total: 3 } })),
transactions: jest.fn(async (_u, opts) => ({ rows: [], total: 0, limit: opts.limit, offset: opts.offset })),
series: jest.fn(async () => ({ usage: [], storage: [] })),
verify: jest.fn(async (_u, targets) => ({ results: targets })),
event: jest.fn(async () => ({ eventId: "x", data: { a: 1 } })),
});
describe("dashboard API adapter", () => {
it("summary wraps the service result with a message + window", async () => {
const service = fakeService();
const out = await makeDashboardApi(service).summary({ from: "2026-01-01", to: "2026-01-31" }, 7);
expect(out).toEqual({ message: "dashboard summary", status: 200, data: { apiUsage: { total: 3 } } });
expect(service.summary).toHaveBeenCalledWith(7, { from: "2026-01-01", to: "2026-01-31" });
});
it("transactions passes validated options through", async () => {
const service = fakeService();
const out = await makeDashboardApi(service).transactions({ type: "audit", limit: "5" }, 7);
expect(out.message).toBe("dashboard transactions");
expect(out.status).toBe(200);
expect(service.transactions).toHaveBeenCalledWith(7, expect.objectContaining({ type: "audit", limit: 5 }));
});
it("series wraps the combined usage+storage result with the window", async () => {
const service = fakeService();
const out = await makeDashboardApi(service).series({ from: "2026-01-01", to: "2026-01-31" }, 7);
expect(out).toEqual({ message: "dashboard series", status: 200, data: { usage: [], storage: [] } });
expect(service.series).toHaveBeenCalledWith(7, { from: "2026-01-01", to: "2026-01-31" });
});
it("transactions returns a 400 error payload (not a throw) on invalid input", async () => {
const service = fakeService();
const out = await makeDashboardApi(service).transactions({ type: "evil" }, 7);
expect(out.status).toBe(400);
expect(out.data.error).toBeTruthy();
expect(service.transactions).not.toHaveBeenCalled();
});
it("verify forwards valid event uuids and audit ids, dropping junk", async () => {
const service = fakeService();
const good = "3fa85f64-5717-4562-b3fc-2c963f66afa6";
const out = await makeDashboardApi(service).verify({ ids: `${good},junk`, auditIds: "9,nope" }, 7);
expect(out.data.results).toEqual({ eventUuids: [good], auditIds: [9] });
});
it("event rejects a non-uuid with 400 and 404s a missing record", async () => {
const service = fakeService();
const api = makeDashboardApi(service);
const bad = await api.event({ eventUuid: "nope" }, 7);
expect(bad).toMatchObject({ status: 400, data: { error: "invalid event id" } });
service.event = jest.fn(async () => null);
const missing = await api.event({ eventUuid: "3fa85f64-5717-4562-b3fc-2c963f66afa6" }, 7);
expect(missing).toMatchObject({ status: 404, data: { error: "event not found" } });
});
});