mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
Expand test coverage across all layers via 14 parallel agents: Unit tests (3,378 total, +534): - First-ever AI sidecar tests (157 tests covering bridge lifecycle, all 12 tool modules) - API route infrastructure (auth, pipeline, batch, settings, teams, roles, audit, api-keys, files, docs) - Lib coverage improvements (audit 7%->95%, worker-pool 33%->100%) - Web store/lib gap fills (features-store, tool-registry) Integration tests (4,403 total, +903): - Expanded 19 tool test files with parameter variations, format edge cases, boundary values - Cross-format matrix: 290 tests covering 14 tools x 17 formats - Adversarial/edge cases: 63 tests for extreme inputs, concurrent requests, corrupted files E2E-Docker (125 new tests): - Expanded 8 spec files + 1 new file covering all 49 tools - Added HEIC/format handling, auth failures, download verification GUI E2E (expanded 28 spec files): - Navigation, responsive layout, keyboard shortcuts - All 51 tool UIs with settings, processing, display modes - Batch/pipeline workflows, settings/RBAC, visual regression - Resilience, accessibility (ARIA, contrast, focus), performance budgets
164 lines
4.7 KiB
TypeScript
164 lines
4.7 KiB
TypeScript
/**
|
|
* Unit tests for the audit.ts library module.
|
|
*
|
|
* Tests the auditLog function's dual-write behavior (logger + DB insert),
|
|
* actor extraction, target type derivation, and DB failure resilience.
|
|
*/
|
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
// ── Mocks ───────────────────────────────────────────────────────────────
|
|
|
|
const mockInsertRun = vi.fn();
|
|
|
|
vi.mock("../../../apps/api/src/db/index.js", () => ({
|
|
db: {
|
|
insert: () => ({
|
|
values: () => ({ run: mockInsertRun }),
|
|
}),
|
|
},
|
|
schema: {
|
|
auditLog: {},
|
|
},
|
|
}));
|
|
|
|
import { auditLog } from "../../../apps/api/src/lib/audit.js";
|
|
|
|
// ── Tests ───────────────────────────────────────────────────────────────
|
|
|
|
describe("auditLog", () => {
|
|
const mockLogger = {
|
|
info: vi.fn(),
|
|
warn: vi.fn(),
|
|
error: vi.fn(),
|
|
debug: vi.fn(),
|
|
fatal: vi.fn(),
|
|
trace: vi.fn(),
|
|
child: vi.fn(),
|
|
level: "info",
|
|
silent: vi.fn(),
|
|
};
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it("logs to the structured logger with audit flag", () => {
|
|
auditLog(mockLogger as never, "LOGIN_SUCCESS", { userId: "u1", username: "alice" });
|
|
|
|
expect(mockLogger.info).toHaveBeenCalledTimes(1);
|
|
const [logData, logMessage] = mockLogger.info.mock.calls[0];
|
|
expect(logData.audit).toBe(true);
|
|
expect(logData.event).toBe("LOGIN_SUCCESS");
|
|
expect(logData.userId).toBe("u1");
|
|
expect(logMessage).toBe("[AUDIT] LOGIN_SUCCESS");
|
|
});
|
|
|
|
it("inserts a record into the database", () => {
|
|
auditLog(mockLogger as never, "USER_CREATED", {
|
|
adminId: "admin-1",
|
|
targetUserId: "new-user-1",
|
|
});
|
|
|
|
expect(mockInsertRun).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("extracts actorId from userId field first", () => {
|
|
auditLog(mockLogger as never, "FILE_UPLOADED", {
|
|
userId: "u1",
|
|
adminId: "a1",
|
|
});
|
|
|
|
// The insert was called; check logger received both fields
|
|
expect(mockLogger.info).toHaveBeenCalledTimes(1);
|
|
const logData = mockLogger.info.mock.calls[0][0];
|
|
expect(logData.userId).toBe("u1");
|
|
expect(logData.adminId).toBe("a1");
|
|
});
|
|
|
|
it("falls back to adminId when userId is absent", () => {
|
|
auditLog(mockLogger as never, "ROLE_CREATED", { adminId: "admin-1", roleName: "editor" });
|
|
|
|
expect(mockLogger.info).toHaveBeenCalledTimes(1);
|
|
const logData = mockLogger.info.mock.calls[0][0];
|
|
expect(logData.adminId).toBe("admin-1");
|
|
});
|
|
|
|
it("extracts username from details", () => {
|
|
auditLog(mockLogger as never, "LOGIN_SUCCESS", {
|
|
userId: "u1",
|
|
username: "alice",
|
|
});
|
|
|
|
const logData = mockLogger.info.mock.calls[0][0];
|
|
expect(logData.username).toBe("alice");
|
|
});
|
|
|
|
it("handles empty details object", () => {
|
|
auditLog(mockLogger as never, "LOGOUT");
|
|
|
|
expect(mockLogger.info).toHaveBeenCalledTimes(1);
|
|
expect(mockInsertRun).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("survives DB insert failure", () => {
|
|
mockInsertRun.mockImplementationOnce(() => {
|
|
throw new Error("DB write failed");
|
|
});
|
|
|
|
// Should not throw
|
|
auditLog(mockLogger as never, "SETTINGS_UPDATED", { userId: "u1" });
|
|
|
|
expect(mockLogger.info).toHaveBeenCalledTimes(1);
|
|
expect(mockLogger.warn).toHaveBeenCalledTimes(1);
|
|
const [warnData] = mockLogger.warn.mock.calls[0];
|
|
expect(warnData.event).toBe("SETTINGS_UPDATED");
|
|
});
|
|
|
|
it("logs different event types correctly", () => {
|
|
const events = [
|
|
"LOGIN_SUCCESS",
|
|
"LOGIN_FAILED",
|
|
"LOGOUT",
|
|
"PASSWORD_CHANGED",
|
|
"USER_CREATED",
|
|
"FILE_UPLOADED",
|
|
"API_KEY_CREATED",
|
|
"ROLE_CREATED",
|
|
"SETTINGS_UPDATED",
|
|
] as const;
|
|
|
|
for (const event of events) {
|
|
vi.clearAllMocks();
|
|
auditLog(mockLogger as never, event, { userId: "u1" });
|
|
|
|
expect(mockLogger.info).toHaveBeenCalledTimes(1);
|
|
const logMessage = mockLogger.info.mock.calls[0][1];
|
|
expect(logMessage).toBe(`[AUDIT] ${event}`);
|
|
}
|
|
});
|
|
|
|
it("serializes details as JSON for DB storage", () => {
|
|
auditLog(mockLogger as never, "USER_UPDATED", {
|
|
adminId: "admin-1",
|
|
targetUserId: "u2",
|
|
changes: { role: "editor" },
|
|
});
|
|
|
|
expect(mockInsertRun).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("includes all detail fields in the log output", () => {
|
|
const details = {
|
|
userId: "u1",
|
|
keyId: "key-123",
|
|
keyName: "Production Key",
|
|
};
|
|
|
|
auditLog(mockLogger as never, "API_KEY_CREATED", details);
|
|
|
|
const logData = mockLogger.info.mock.calls[0][0];
|
|
expect(logData.keyId).toBe("key-123");
|
|
expect(logData.keyName).toBe("Production Key");
|
|
});
|
|
});
|