Simpl-Resultat/src/services/logService.test.ts
maximus 4f4ab87bea feat: feedback hub widget in Settings Logs card (#67)
Closes #67

Add opt-in Feedback Hub widget integrated into the Settings Logs card. Routes through a Rust command to bypass CORS and centralize privacy audit. First submission triggers a one-time consent dialog; three opt-in checkboxes (context, logs, identify with Maximus account) all unchecked by default. Wording and payload follow the cross-app conventions in la-compagnie-maximus/docs/feedback-hub-ops.md.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 14:36:26 +00:00

56 lines
1.8 KiB
TypeScript

import { describe, it, expect, beforeEach, beforeAll, vi } from "vitest";
import { getRecentErrorLogs, clearLogs, initLogCapture } from "./logService";
beforeAll(() => {
// Patch console.* so addEntry runs. Idempotent.
initLogCapture();
});
describe("getRecentErrorLogs", () => {
beforeEach(() => {
// Reset the in-memory buffer. clearLogs also clears sessionStorage
// which jsdom provides in vitest.
clearLogs();
});
it("returns an empty string when the log buffer is empty", () => {
expect(getRecentErrorLogs(5)).toBe("");
});
it("returns an empty string when n <= 0", () => {
console.error("boom");
expect(getRecentErrorLogs(0)).toBe("");
expect(getRecentErrorLogs(-3)).toBe("");
});
it("filters out info-level entries", () => {
// Freeze time so the ISO prefix is predictable
vi.setSystemTime(new Date("2026-04-17T15:00:00.000Z"));
console.log("just chatter");
console.warn("low fuel");
console.error("engine out");
const out = getRecentErrorLogs(10);
expect(out).not.toContain("chatter");
expect(out).toContain("WARN: low fuel");
expect(out).toContain("ERROR: engine out");
vi.useRealTimers();
});
it("keeps only the last N non-info entries in order", () => {
for (let i = 0; i < 5; i++) console.warn(`w${i}`);
const out = getRecentErrorLogs(2);
const lines = out.split("\n");
expect(lines).toHaveLength(2);
expect(lines[0]).toContain("w3");
expect(lines[1]).toContain("w4");
});
it("formats each line as `[ISO] LEVEL: message`", () => {
vi.setSystemTime(new Date("2026-04-17T15:23:45.000Z"));
console.error("export failed");
const out = getRecentErrorLogs(1);
expect(out).toMatch(/^\[2026-04-17T15:23:45\.000Z\] ERROR: export failed$/);
vi.useRealTimers();
});
});