Merge pull request 'test(canvas): add pure-function coverage for AuditTrailPanel + MemoryInspectorPanel' (#822) from design/remaining-canvas-coverage into main
Some checks are pending
Continuous synthetic E2E (staging) / Synthetic E2E against staging (push) Has started running
Sweep stale Cloudflare Tunnels / Sweep CF tunnels (push) Successful in 18s
main-red-watchdog / watchdog (push) Successful in 50s
ci-required-drift / drift (push) Successful in 1m29s
status-reaper / reap (push) Has started running
gitea-merge-queue / queue (push) Has started running
Sweep stale Cloudflare DNS records / Sweep CF orphans (push) Successful in 19s
Sweep stale e2e-* orgs (staging) / Sweep e2e orgs (push) Successful in 7s
Block internal-flavored paths / Block forbidden paths (push) Successful in 13s
Harness Replays / detect-changes (push) Successful in 18s
CI / Detect changes (push) Successful in 1m11s
E2E API Smoke Test / detect-changes (push) Successful in 1m21s
E2E Staging Canvas (Playwright) / detect-changes (push) Successful in 1m19s
Harness Replays / Harness Replays (push) Successful in 9s
Handlers Postgres Integration / detect-changes (push) Successful in 1m24s
Secret scan / Scan diff for credential-shaped strings (push) Successful in 20s
CI / Platform (Go) (push) Successful in 8s
Runtime PR-Built Compatibility / detect-changes (push) Successful in 1m9s
CI / Shellcheck (E2E scripts) (push) Successful in 7s
CI / Python Lint & Test (push) Successful in 8s
E2E API Smoke Test / E2E API Smoke Test (push) Successful in 9s
Handlers Postgres Integration / Handlers Postgres Integration (push) Successful in 9s
Runtime PR-Built Compatibility / PR-built wheel + import smoke (push) Successful in 8s
publish-canvas-image / Build & push canvas image (push) Successful in 6m15s
E2E Staging Canvas (Playwright) / Canvas tabs E2E (push) Successful in 8m41s
publish-workspace-server-image / build-and-push (push) Successful in 11m18s
CI / Canvas (Next.js) (push) Successful in 16m22s
CI / all-required (push) Successful in 3s
CI / Canvas Deploy Reminder (push) Successful in 3s
cascade-list-drift-gate / check (pull_request) Successful in 12s
Block internal-flavored paths / Block forbidden paths (pull_request) Successful in 29s
sop-tier-check / tier-check (pull_request) Successful in 12s
sop-checklist-gate / gate (pull_request) Successful in 16s
lint-mask-pr-atomicity / lint-mask-pr-atomicity (pull_request) Successful in 2m6s
Staging SaaS smoke (every 30 min) / Staging SaaS smoke (push) Successful in 7m39s
gate-check-v3 / gate-check (push) Compensated by status-reaper (workflow has no push: trigger; Gitea 1.22.6 hardcoded-suffix bug — see .gitea/scripts/status-reaper.py)

This commit is contained in:
devops-engineer 2026-05-13 09:47:27 +00:00
commit 9373b19a0e
2 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,63 @@
// @vitest-environment jsdom
/**
* Unit tests for formatAuditRelativeTime pure date formatter from AuditTrailPanel.
*/
import { describe, it, expect } from "vitest";
import { formatAuditRelativeTime } from "../AuditTrailPanel";
describe("formatAuditRelativeTime", () => {
it('returns "just now" for timestamps within the last minute', () => {
const now = 1_700_000_000_000;
const thirtySecAgo = new Date(now - 30_000).toISOString();
expect(formatAuditRelativeTime(thirtySecAgo, now)).toBe("just now");
});
it('returns "Xm ago" for timestamps within the last hour', () => {
const now = 1_700_000_000_000;
const fiveMinAgo = new Date(now - 5 * 60_000).toISOString();
expect(formatAuditRelativeTime(fiveMinAgo, now)).toBe("5m ago");
});
it('returns "Xh ago" for timestamps within the last day', () => {
const now = 1_700_000_000_000;
const threeHoursAgo = new Date(now - 3 * 3_600_000).toISOString();
expect(formatAuditRelativeTime(threeHoursAgo, now)).toBe("3h ago");
});
it("returns locale date string for timestamps older than 24h", () => {
const now = 1_700_000_000_000;
const twoDaysAgo = new Date(now - 2 * 86_400_000).toISOString();
const result = formatAuditRelativeTime(twoDaysAgo, now);
// Should be a date string (not "Xh ago" or "Xm ago")
expect(result).not.toMatch(/m ago|h ago|just now/);
expect(result).toBe(new Date(twoDaysAgo).toLocaleDateString());
});
it("handles the boundary between minute and hour correctly", () => {
const now = 1_700_000_000_000;
const exactlyOneHourAgo = new Date(now - 3_600_000).toISOString();
expect(formatAuditRelativeTime(exactlyOneHourAgo, now)).toBe("1h ago");
});
it("handles the boundary between hour and day correctly", () => {
const now = 1_700_000_000_000;
// 23h ago is < 24h so it shows "23h ago"; exactly 24h falls through to date string
const twentyThreeHoursAgo = new Date(now - 23 * 3_600_000).toISOString();
expect(formatAuditRelativeTime(twentyThreeHoursAgo, now)).toBe("23h ago");
});
it("returns locale date string for exactly 24h ago (boundary)", () => {
const now = 1_700_000_000_000;
const exactlyOneDayAgo = new Date(now - 86_400_000).toISOString();
const result = formatAuditRelativeTime(exactlyOneDayAgo, now);
// diff is exactly 86_400_000, which is NOT < 86_400_000, so it falls through
expect(result).toBe(new Date(exactlyOneDayAgo).toLocaleDateString());
});
it("future timestamps return 'just now' (negative diff < 60_000)", () => {
const now = 1_700_000_000_000;
const future = new Date(now + 60_000).toISOString();
// Negative diff passes diff < 60_000, returning "just now"
expect(formatAuditRelativeTime(future, now)).toBe("just now");
});
});

View File

@ -0,0 +1,90 @@
// @vitest-environment jsdom
/**
* Unit tests for pure helpers from MemoryInspectorPanel:
* isPluginUnavailableError, formatRelativeTime, formatTTL
*
* These are the three exported non-component functions. The component
* itself (MemoryInspectorPanel) requires full API + store mocking and
* is exercised by the existing MemoryTab.test.tsx.
*/
import { describe, it, expect } from "vitest";
import { isPluginUnavailableError, formatTTL } from "../MemoryInspectorPanel";
// formatRelativeTime is not exported — tested via the component in MemoryTab.test.tsx
describe("isPluginUnavailableError", () => {
it("returns true when Error message contains MEMORY_PLUGIN_URL", () => {
const err = new Error("memory: could not resolve MEMORY_PLUGIN_URL — plugin not configured");
expect(isPluginUnavailableError(err)).toBe(true);
});
it("returns true for Error containing MEMORY_PLUGIN_URL", () => {
expect(isPluginUnavailableError(new Error("MEMORY_PLUGIN_URL is not set"))).toBe(true);
});
it("returns false for unrelated error messages", () => {
expect(isPluginUnavailableError(new Error("workspace not found"))).toBe(false);
});
it("returns false for null", () => {
expect(isPluginUnavailableError(null)).toBe(false);
});
it("returns false for undefined", () => {
expect(isPluginUnavailableError(undefined)).toBe(false);
});
it("returns false for plain objects without message", () => {
expect(isPluginUnavailableError({ code: 503 })).toBe(false);
});
it("is case-sensitive (MEMORY_PLUGIN_URL must match exactly)", () => {
const lowerErr = new Error("memory_plugin_url missing");
const upperErr = new Error("MEMORY_PLUGIN_URL missing");
expect(isPluginUnavailableError(lowerErr)).toBe(false);
expect(isPluginUnavailableError(upperErr)).toBe(true);
});
});
describe("formatTTL", () => {
it("returns '' for null", () => {
expect(formatTTL(null)).toBe("");
});
it("returns '' for undefined", () => {
expect(formatTTL(undefined)).toBe("");
});
it('returns "expired" when expiresAt is in the past', () => {
const past = new Date(Date.now() - 60_000).toISOString();
expect(formatTTL(past)).toBe("expired");
});
it('returns "Xs" for less than a minute', () => {
const soon = new Date(Date.now() + 30_000).toISOString();
expect(formatTTL(soon)).toBe("30s");
});
it('returns "Xm" for less than an hour', () => {
const soon = new Date(Date.now() + 5 * 60_000).toISOString();
expect(formatTTL(soon)).toBe("5m");
});
it('returns "Xh" for less than a day', () => {
const soon = new Date(Date.now() + 3 * 3_600_000).toISOString();
expect(formatTTL(soon)).toBe("3h");
});
it('returns "Xd" for more than a day', () => {
const soon = new Date(Date.now() + 2 * 86_400_000).toISOString();
expect(formatTTL(soon)).toBe("2d");
});
it("returns '' for invalid date string", () => {
expect(formatTTL("not-a-date")).toBe("");
});
it("returns '' for empty string", () => {
expect(formatTTL("")).toBe("");
});
});