diigo-cache: the caches take their clock as a constructor argument

Ocap discipline: no ambient time in logic. PagedCache reads time and
waits only through a Clock { now, sleep }, a default constructor
argument (the live clock; the runtime constructs DOs with ctx and env
only). The fill job's timestamps and alarms, the etch time, and the
diigo-call pacing all go through it.

Tests interpose through the constructor: tests/test-worker.ts (now the
tests' main module) adds FixedClockCache, bound as CLOCKED_DO, and a
test pins startedAt, updatedAt, and the alarm time to the fixed clock
(an ambient new Date() fails it).

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-10-07 04:34:48 -05:00
1 parent 7d2100bf77
commit b6823867ff
4 files changed
+76 -17

No files matched your search

+14
View File
@@ -5,6 +5,7 @@ import worker from "../src/index.js";
import { newHttpBatchRpcSession } from "capnweb";
import type { CacheApi } from "../src/api.js";
import { toWebkey } from "../src/webkeys.js";
import { FIXED_NOW } from "./test-worker.js";
// goal 1e (capsite checklist): a job that grabs every page. Fake
// accounts: tests/fake-webapp.js ("fake-webapp-id": 30 items = 2 pages
@@ -12,6 +13,7 @@ import { toWebkey } from "../src/webkeys.js";
type TestEnv = {
CACHE_DO: DurableObjectNamespace<import("../src/cache.js").DiigoCache>;
CLOCKED_DO: DurableObjectNamespace<import("./test-worker.js").FixedClockCache>;
PUBLIC_DO: DurableObjectNamespace<import("../src/cache.js").PublicCache>;
WEBAPP: { calls(): Promise<number> };
};
@@ -107,6 +109,18 @@ describe("fill job (goal 1e)", () => {
}
});
it("reads time only through its clock: a fixed clock pins the timestamps and the alarm", async () => {
const stub = testEnv.CLOCKED_DO.get(testEnv.CLOCKED_DO.newUniqueId());
await stub.setup({ webappId: "fake-webapp-id" });
const at = new Date(FIXED_NOW).toISOString();
expect(await stub.startFill()).toMatchObject({ startedAt: at, updatedAt: at });
await runInDurableObject(stub, async (_obj, state) => {
expect(await state.storage.getAlarm()).toBe(FIXED_NOW + 1000);
});
await drain(stub);
expect(await stub.fillStatus()).toMatchObject({ state: "done", updatedAt: at });
});
it("a public cache's fill stores no private rows", async () => {
const stub = testEnv.PUBLIC_DO.get(testEnv.PUBLIC_DO.newUniqueId());
await stub.setup({ webappId: "fake-webapp-id" });
+19
View File
@@ -0,0 +1,19 @@
/**
* @file the tests' main module: the real worker, plus a DiigoCache on a
* fixed clock (bound as CLOCKED_DO in wrangler.tests.jsonc), so tests can
* interpose time through the constructor.
*/
import { DiigoCache } from "../src/cache.js";
import type { CacheEnv } from "../src/cache.js";
export * from "../src/index.js";
export { default } from "../src/index.js";
/** 2026-10-07T12:00:00Z */
export const FIXED_NOW = Date.UTC(2026, 9, 7, 12, 0, 0);
export class FixedClockCache extends DiigoCache {
constructor(ctx: DurableObjectState, env: CacheEnv) {
super(ctx, env, { now: () => FIXED_NOW, sleep: async () => {} });
}
}