/** * RULE (owner, 22 Nov 2026: his database file stood at 6.5 GB for 1.45 GB of * records): the act log keeps the change log's window — nothing older than * its days, at most its rows of the latest — trimmed a bounded step at a * time, the oldest first, as the change log is. It was never trimmed before. * Invented lines only. */ import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { SCHEMA, Store } from "../src/store.ts"; const laid = (sql: SqlStorage) => { sql.exec(SCHEMA); }; const NOW = new Date("2026-11-22T00:00:00.000Z"); describe("the act log's window", () => { it("lets lines older than the window go, then keeps the newest rows, at most a step a call", async () => { await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName("acts-trim")), (_i, state) => { const sql = state.storage.sql; laid(sql); const store = new Store(sql, () => NOW); const line = (i: number, at: string) => sql.exec("INSERT INTO acts (id, at, person_id, act, args, outcome) VALUES (?, ?, NULL, 'create', '{}', 'done')", `act-${String(i).padStart(5, "0")}`, at); for (let i = 0; i < 250; i++) line(i, "2026-06-01T00:00:00.000Z"); // older than 90 days for (let i = 250; i < 400; i++) line(i, "2026-11-01T00:00:00.000Z"); // inside them const n = () => Number(sql.exec<{ n: number }>("SELECT COUNT(*) AS n FROM acts").one().n); const before = "2026-08-24T00:00:00.000Z"; expect(store.trimActs(before, 100_000, 100)).toEqual({ trimmed: 100, more: true }); expect(store.trimActs(before, 100_000, 100)).toEqual({ trimmed: 100, more: true }); expect(store.trimActs(before, 100_000, 100)).toEqual({ trimmed: 50, more: false }); expect(n()).toBe(150); expect(sql.exec<{ at: string }>("SELECT MIN(at) AS at FROM acts").one().at, "every line left is inside the window").toBe("2026-11-01T00:00:00.000Z"); // By number: the newest 120 stay. expect(store.trimActs(before, 120, 100)).toEqual({ trimmed: 30, more: false }); expect(sql.exec<{ id: string }>("SELECT id FROM acts ORDER BY rowid LIMIT 1").one().id).toBe("act-00280"); expect(store.trimActs(before, 120, 100)).toEqual({ trimmed: 0, more: false }); }); }); it("says nothing to do on an empty log", async () => { await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName("acts-trim-empty")), (_i, state) => { laid(state.storage.sql); const store = new Store(state.storage.sql, () => NOW); expect(store.trimActs("2026-08-24T00:00:00.000Z", 100_000)).toEqual({ trimmed: 0, more: false }); }); }); });