|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { afterEach, describe, test } from "node:test"; |
| 6 | +import { ActivityLogStore } from "../src/main/activity-log-store.js"; |
| 7 | + |
| 8 | +const tempDirs: string[] = []; |
| 9 | + |
| 10 | +afterEach(() => { |
| 11 | + for (const dir of tempDirs.splice(0)) { |
| 12 | + fs.rmSync(dir, { recursive: true, force: true }); |
| 13 | + } |
| 14 | +}); |
| 15 | + |
| 16 | +function makeTempDir(): string { |
| 17 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), "activity-log-test-")); |
| 18 | + tempDirs.push(dir); |
| 19 | + return dir; |
| 20 | +} |
| 21 | + |
| 22 | +function writeStoreFile(dir: string, name: string, events: unknown[]): void { |
| 23 | + fs.writeFileSync( |
| 24 | + path.join(dir, `${name}.json`), |
| 25 | + JSON.stringify({ events }) |
| 26 | + ); |
| 27 | +} |
| 28 | + |
| 29 | +function readStoreFile(dir: string, name: string): { events: unknown[] } { |
| 30 | + return JSON.parse( |
| 31 | + fs.readFileSync(path.join(dir, `${name}.json`), "utf-8") |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function makeEvent(overrides: Record<string, unknown> = {}) { |
| 36 | + return { |
| 37 | + id: `test-${Math.random().toString(36).slice(2)}`, |
| 38 | + timestamp: new Date().toISOString(), |
| 39 | + method: "GET", |
| 40 | + path: "/api/test", |
| 41 | + statusCode: 200, |
| 42 | + durationMs: 10, |
| 43 | + ...overrides, |
| 44 | + }; |
| 45 | +} |
| 46 | + |
| 47 | +function createStore(dir: string, name: string, maxEntries = 200) { |
| 48 | + return new ActivityLogStore({ maxEntries, cwd: dir, name }); |
| 49 | +} |
| 50 | + |
| 51 | +// --- truncateBody via add() --- |
| 52 | + |
| 53 | +describe("ActivityLogStore body truncation", () => { |
| 54 | + test("preserves short bodies unchanged", () => { |
| 55 | + const dir = makeTempDir(); |
| 56 | + const store = createStore(dir, "truncate-short", 10); |
| 57 | + const event = store.add({ |
| 58 | + timestamp: new Date().toISOString(), |
| 59 | + method: "POST", |
| 60 | + path: "/api/test", |
| 61 | + statusCode: 200, |
| 62 | + durationMs: 5, |
| 63 | + requestBody: "short body", |
| 64 | + responseBody: '{"ok": true}', |
| 65 | + }); |
| 66 | + |
| 67 | + assert.equal(event.requestBody, "short body"); |
| 68 | + assert.equal(event.responseBody, '{"ok": true}'); |
| 69 | + }); |
| 70 | + |
| 71 | + test("truncates bodies exceeding 8192 bytes", () => { |
| 72 | + const dir = makeTempDir(); |
| 73 | + const store = createStore(dir, "truncate-large", 10); |
| 74 | + const largeBody = "x".repeat(20_000); |
| 75 | + const event = store.add({ |
| 76 | + timestamp: new Date().toISOString(), |
| 77 | + method: "POST", |
| 78 | + path: "/api/test", |
| 79 | + statusCode: 200, |
| 80 | + durationMs: 5, |
| 81 | + requestBody: largeBody, |
| 82 | + responseBody: largeBody, |
| 83 | + }); |
| 84 | + |
| 85 | + assert.ok(event.requestBody!.length < largeBody.length); |
| 86 | + assert.ok(event.requestBody!.startsWith("x".repeat(100))); |
| 87 | + assert.ok(event.requestBody!.includes("truncated")); |
| 88 | + assert.ok(event.requestBody!.includes("20000 bytes total")); |
| 89 | + assert.ok(event.responseBody!.includes("truncated")); |
| 90 | + }); |
| 91 | + |
| 92 | + test("preserves undefined bodies", () => { |
| 93 | + const dir = makeTempDir(); |
| 94 | + const store = createStore(dir, "truncate-undef", 10); |
| 95 | + const event = store.add({ |
| 96 | + timestamp: new Date().toISOString(), |
| 97 | + method: "GET", |
| 98 | + path: "/api/test", |
| 99 | + statusCode: 200, |
| 100 | + durationMs: 5, |
| 101 | + }); |
| 102 | + |
| 103 | + assert.equal(event.requestBody, undefined); |
| 104 | + assert.equal(event.responseBody, undefined); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +// --- Constructor migration --- |
| 109 | + |
| 110 | +describe("ActivityLogStore startup migration", () => { |
| 111 | + test("truncates oversized bodies from persisted events on startup", () => { |
| 112 | + const dir = makeTempDir(); |
| 113 | + const name = "migrate-bodies"; |
| 114 | + const largeBody = "y".repeat(20_000); |
| 115 | + writeStoreFile(dir, name, [ |
| 116 | + makeEvent({ requestBody: largeBody, responseBody: largeBody }), |
| 117 | + makeEvent({ requestBody: "small", responseBody: "small" }), |
| 118 | + ]); |
| 119 | + |
| 120 | + const store = createStore(dir, name); |
| 121 | + const events = store.list(); |
| 122 | + |
| 123 | + assert.equal(events.length, 2); |
| 124 | + assert.ok(events[0].requestBody!.includes("truncated")); |
| 125 | + assert.equal(events[1].requestBody, "small"); |
| 126 | + assert.equal(events[1].responseBody, "small"); |
| 127 | + |
| 128 | + // Verify the truncated data was persisted to disk |
| 129 | + const persisted = readStoreFile(dir, name); |
| 130 | + const persistedEvents = persisted.events as Array<Record<string, unknown>>; |
| 131 | + assert.ok((persistedEvents[0].requestBody as string).includes("truncated")); |
| 132 | + assert.equal(persistedEvents[1].requestBody, "small"); |
| 133 | + }); |
| 134 | + |
| 135 | + test("trims event count to maxEntries on startup", () => { |
| 136 | + const dir = makeTempDir(); |
| 137 | + const name = "migrate-count"; |
| 138 | + const events = Array.from({ length: 50 }, (_, i) => |
| 139 | + makeEvent({ id: `evt-${i}`, requestBody: "ok" }) |
| 140 | + ); |
| 141 | + writeStoreFile(dir, name, events); |
| 142 | + |
| 143 | + const store = createStore(dir, name, 10); |
| 144 | + assert.equal(store.list().length, 10); |
| 145 | + |
| 146 | + // Verify persisted file was trimmed too |
| 147 | + const persisted = readStoreFile(dir, name); |
| 148 | + assert.equal((persisted.events as unknown[]).length, 10); |
| 149 | + }); |
| 150 | + |
| 151 | + test("does not rewrite file when nothing needs migration", () => { |
| 152 | + const dir = makeTempDir(); |
| 153 | + const name = "migrate-noop"; |
| 154 | + writeStoreFile(dir, name, [makeEvent({ requestBody: "small" })]); |
| 155 | + const statBefore = fs.statSync(path.join(dir, `${name}.json`)); |
| 156 | + |
| 157 | + createStore(dir, name); |
| 158 | + |
| 159 | + const statAfter = fs.statSync(path.join(dir, `${name}.json`)); |
| 160 | + assert.equal(statAfter.mtimeMs, statBefore.mtimeMs); |
| 161 | + }); |
| 162 | +}); |
| 163 | + |
| 164 | +// --- maxEntries enforcement --- |
| 165 | + |
| 166 | +describe("ActivityLogStore maxEntries", () => { |
| 167 | + test("caps events at maxEntries when adding", () => { |
| 168 | + const dir = makeTempDir(); |
| 169 | + const store = createStore(dir, "max-entries", 3); |
| 170 | + for (let i = 0; i < 5; i++) { |
| 171 | + store.add({ |
| 172 | + timestamp: new Date().toISOString(), |
| 173 | + method: "GET", |
| 174 | + path: `/api/test/${i}`, |
| 175 | + statusCode: 200, |
| 176 | + durationMs: 1, |
| 177 | + }); |
| 178 | + } |
| 179 | + |
| 180 | + assert.equal(store.list().length, 3); |
| 181 | + assert.equal(store.list()[0].path, "/api/test/4"); |
| 182 | + }); |
| 183 | +}); |
0 commit comments