|
| 1 | +import { describe, expect, it, beforeEach, vi, afterEach } from "vitest"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { resolve } from "node:path"; |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// Unit tests for brain-section-timestamps utility |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | + |
| 9 | +describe("brain-section-timestamps utility", () => { |
| 10 | + let mod: typeof import("../../apps/desktop/src/features/brain/lib/brain-section-timestamps"); |
| 11 | + |
| 12 | + const STORAGE_KEY = "opengoat:brain-section-timestamps"; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + // Mock localStorage |
| 16 | + const store: Record<string, string> = {}; |
| 17 | + vi.stubGlobal("localStorage", { |
| 18 | + getItem: vi.fn((key: string) => store[key] ?? null), |
| 19 | + setItem: vi.fn((key: string, value: string) => { store[key] = value; }), |
| 20 | + removeItem: vi.fn((key: string) => { delete store[key]; }), |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + vi.unstubAllGlobals(); |
| 26 | + vi.resetModules(); |
| 27 | + }); |
| 28 | + |
| 29 | + async function load() { |
| 30 | + mod = await import("../../apps/desktop/src/features/brain/lib/brain-section-timestamps"); |
| 31 | + return mod; |
| 32 | + } |
| 33 | + |
| 34 | + it("returns null when no timestamp has been stored", async () => { |
| 35 | + const { getSectionTimestamp } = await load(); |
| 36 | + expect(getSectionTimestamp("agent-1", "PRODUCT.md")).toBeNull(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("stores and retrieves a timestamp", async () => { |
| 40 | + const { getSectionTimestamp, setSectionTimestamp } = await load(); |
| 41 | + const before = Date.now(); |
| 42 | + setSectionTimestamp("agent-1", "PRODUCT.md"); |
| 43 | + const ts = getSectionTimestamp("agent-1", "PRODUCT.md"); |
| 44 | + expect(ts).not.toBeNull(); |
| 45 | + const parsed = new Date(ts!).getTime(); |
| 46 | + expect(parsed).toBeGreaterThanOrEqual(before); |
| 47 | + expect(parsed).toBeLessThanOrEqual(Date.now()); |
| 48 | + }); |
| 49 | + |
| 50 | + it("stores timestamps independently per agent and filename", async () => { |
| 51 | + const { getSectionTimestamp, setSectionTimestamp } = await load(); |
| 52 | + setSectionTimestamp("agent-1", "PRODUCT.md"); |
| 53 | + setSectionTimestamp("agent-2", "MARKET.md"); |
| 54 | + expect(getSectionTimestamp("agent-1", "PRODUCT.md")).not.toBeNull(); |
| 55 | + expect(getSectionTimestamp("agent-2", "MARKET.md")).not.toBeNull(); |
| 56 | + expect(getSectionTimestamp("agent-1", "MARKET.md")).toBeNull(); |
| 57 | + }); |
| 58 | + |
| 59 | + it("detects content changes via hash comparison", async () => { |
| 60 | + const { hasContentChanged, setContentHash } = await load(); |
| 61 | + setContentHash("agent-1", "PRODUCT.md", "hello world"); |
| 62 | + expect(hasContentChanged("agent-1", "PRODUCT.md", "hello world")).toBe(false); |
| 63 | + expect(hasContentChanged("agent-1", "PRODUCT.md", "updated content")).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns true for hasContentChanged when no hash stored", async () => { |
| 67 | + const { hasContentChanged } = await load(); |
| 68 | + // No prior hash — treat as changed (new file) |
| 69 | + expect(hasContentChanged("agent-1", "PRODUCT.md", "some content")).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + it("persists data to localStorage under the correct key", async () => { |
| 73 | + const { setSectionTimestamp } = await load(); |
| 74 | + setSectionTimestamp("agent-1", "PRODUCT.md"); |
| 75 | + expect(localStorage.setItem).toHaveBeenCalledWith( |
| 76 | + STORAGE_KEY, |
| 77 | + expect.any(String), |
| 78 | + ); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +// --------------------------------------------------------------------------- |
| 83 | +// UI integration tests — SectionHeader shows timestamp |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | + |
| 86 | +const brainSrc = readFileSync( |
| 87 | + resolve(__dirname, "../../apps/desktop/src/features/brain/components/BrainWorkspace.tsx"), |
| 88 | + "utf-8", |
| 89 | +); |
| 90 | + |
| 91 | +describe("Brain section freshness indicator — SectionHeader", () => { |
| 92 | + // AC1: SectionHeader accepts and renders a lastUpdated prop |
| 93 | + it("SectionHeader accepts a lastUpdated prop", () => { |
| 94 | + // The SectionHeader function signature should include lastUpdated |
| 95 | + expect(brainSrc).toMatch(/lastUpdated\??:\s*string\s*\|\s*null/); |
| 96 | + }); |
| 97 | + |
| 98 | + // AC2: Timestamp uses 11px mono muted styling consistent with chat timestamps |
| 99 | + it("uses the metadata timestamp styling class", () => { |
| 100 | + // Should have the exact metadata styling for the timestamp |
| 101 | + expect(brainSrc).toMatch(/text-\[11px\].*font-mono.*text-muted-foreground\/60/); |
| 102 | + expect(brainSrc).toMatch(/tabular-nums/); |
| 103 | + }); |
| 104 | + |
| 105 | + // AC3: Uses formatRelativeTime from the shared utility |
| 106 | + it("imports formatRelativeTime from the shared output-labels utility", () => { |
| 107 | + expect(brainSrc).toMatch(/import.*formatRelativeTime.*from.*output-labels/); |
| 108 | + }); |
| 109 | + |
| 110 | + // AC4: Timestamp only renders when lastUpdated is truthy |
| 111 | + it("conditionally renders timestamp only when lastUpdated is present", () => { |
| 112 | + expect(brainSrc).toMatch(/lastUpdated\s*[?&]/); |
| 113 | + }); |
| 114 | + |
| 115 | + // AC5: BrainEditor tracks timestamps via the utility |
| 116 | + it("imports brain-section-timestamps utility", () => { |
| 117 | + expect(brainSrc).toMatch(/from.*brain-section-timestamps/); |
| 118 | + }); |
| 119 | + |
| 120 | + // AC6: Timestamp is updated on successful save |
| 121 | + it("calls setSectionTimestamp on successful file write", () => { |
| 122 | + expect(brainSrc).toMatch(/setSectionTimestamp/); |
| 123 | + }); |
| 124 | + |
| 125 | + // AC7: Content hash is tracked for detecting external changes (Refine) |
| 126 | + it("tracks content hash for detecting external changes", () => { |
| 127 | + expect(brainSrc).toMatch(/setContentHash|hasContentChanged/); |
| 128 | + }); |
| 129 | +}); |
0 commit comments