From d680ecf51353694eb90dcc959d583c5a7b634666 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Mon, 16 Nov 2026 10:13:57 -0600 Subject: [PATCH 1/2] test(pkarr-worker): exercise RETENTION_SECONDS guard through the real DO alarm path (#10818) The retention regression tests only drove worker.fetch with a mock PKARR_STORE whose putPacket re-implements the production alarm expression, so a wiring regression in the real PkarrStore would pass the suite. Add test/worker.retention.test.ts, run once per vitest project with RETENTION_SECONDS set to 0, 7d, out-of-range, and 3600: each PUT goes through SELF to the real Durable Object and asserts the scheduled alarm from real DO storage against a literal constant. The default-env matrix in worker.test.ts is untouched. --- .../test/worker.retention.test.ts | 63 +++++++++++++++++++ pkgs/pkarr-worker/vitest.config.ts | 38 +++++++++-- 2 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 pkgs/pkarr-worker/test/worker.retention.test.ts diff --git a/pkgs/pkarr-worker/test/worker.retention.test.ts b/pkgs/pkarr-worker/test/worker.retention.test.ts new file mode 100644 index 0000000000..2e4f5f94ad --- /dev/null +++ b/pkgs/pkarr-worker/test/worker.retention.test.ts @@ -0,0 +1,63 @@ +import { env as testBindings, runInDurableObject, SELF } from "cloudflare:test"; +import { describe, expect, it } from "vitest"; +import type { Env } from "../src/index"; +import { buildRelayPayload, dnsPacketWithTxt, keypairFromSeed } from "./helpers"; +import { z32Encode } from "../src/pkarr"; + +// Real-DO retention coverage for #10818. Runs once per vitest project; each +// project boots the worker with a different RETENTION_SECONDS (see +// vitest.config.ts). The PUT goes through SELF to the real PkarrStore +// Durable Object and the scheduled alarm is read back from real DO storage — +// no mock store, and the expected alarm is a literal per-setting constant, +// never retentionMilliseconds itself, so a wiring regression in the real DO +// fails this test instead of following a re-implemented expression. + +const BASE = "https://pkarr.test"; +const TS = 1_762_000_000_000_000n; // fixed microsecond timestamp base +const SEVEN_DAYS_MS = 604_800_000; +const CLOCK_SKEW_TOLERANCE_MS = 60_000; + +// Every project value maps to a literal: invalid settings fall back to the +// seven-day default, the valid non-default uses its own retention. +const EXPECTED_RETENTION_MS: Record = { + "0": SEVEN_DAYS_MS, + "7d": SEVEN_DAYS_MS, + "8640000000000": SEVEN_DAYS_MS, + "3600": 3_600_000, +}; + +const SEED_TAGS: Record = { + "0": 61, + "7d": 62, + "8640000000000": 63, + "3600": 64, +}; + +const setting = (testBindings as unknown as Env).RETENTION_SECONDS; +const expectedRetentionMs = EXPECTED_RETENTION_MS[setting]; +const seedTag = SEED_TAGS[setting] ?? 60; + +describe("RETENTION_SECONDS through the real DO", () => { + it(`schedules the alarm from the real PkarrStore for setting ${setting}`, async () => { + expect(expectedRetentionMs).toBeDefined(); + const seed = new Uint8Array(32).fill(seedTag); + const kp = keypairFromSeed(seed); + const key = z32Encode(kp.publicKey); + const payload = buildRelayPayload(kp, TS, dnsPacketWithTxt(`_iroh.${key}`, "retention-real-do")); + + const putResponse = await SELF.fetch(`${BASE}/pkarr/${key}`, { + method: "PUT", + headers: { "content-type": "application/x-pkarr-signed-packet" }, + body: payload, + }); + expect(putResponse.status).toBe(204); + + const bindings = testBindings as unknown as Env; + const stub = bindings.PKARR_STORE.get(bindings.PKARR_STORE.idFromName(key)); + const alarmAt = await runInDurableObject(stub, (_instance, state) => state.storage.getAlarm()); + expect(alarmAt).not.toBeNull(); + const skew = alarmAt! - Date.now(); + expect(skew).toBeGreaterThan(expectedRetentionMs - CLOCK_SKEW_TOLERANCE_MS); + expect(skew).toBeLessThanOrEqual(expectedRetentionMs); + }); +}); diff --git a/pkgs/pkarr-worker/vitest.config.ts b/pkgs/pkarr-worker/vitest.config.ts index 3ae94c7059..0b01a36d6b 100644 --- a/pkgs/pkarr-worker/vitest.config.ts +++ b/pkgs/pkarr-worker/vitest.config.ts @@ -1,10 +1,40 @@ import { cloudflareTest } from "@cloudflare/vitest-plugin"; import { defineConfig } from "vitest/config"; +// The real-DO retention suite (test/worker.retention.test.ts) must run once +// per RETENTION_SECONDS value, because a Durable Object instance always sees +// the worker's configured vars — the value cannot be varied per test through +// SELF. Each project below boots the same worker with a different +// RETENTION_SECONDS; the test file itself stays value-agnostic (it reads the +// setting from the worker env and asserts a literal alarm constant). +const retentionSettings = ["0", "7d", "8640000000000", "3600"]; + export default defineConfig({ - plugins: [ - cloudflareTest({ - wrangler: { configPath: "./wrangler.jsonc" }, - }), + projects: [ + { + plugins: [ + cloudflareTest({ + wrangler: { configPath: "./wrangler.jsonc" }, + }), + ], + test: { + include: ["test/worker.test.ts"], + name: "default", + }, + }, + ...retentionSettings.map((setting) => ({ + plugins: [ + cloudflareTest({ + wrangler: { configPath: "./wrangler.jsonc" }, + miniflare: { + vars: { RETENTION_SECONDS: setting }, + }, + }), + ], + test: { + include: ["test/worker.retention.test.ts"], + name: `retention-${setting}`, + }, + })), ], }); From e47a0d032f983d0e994cd3f7f3ac5b6fb7035e7b Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Mon, 16 Nov 2026 10:49:15 -0600 Subject: [PATCH 2/2] test(pkarr-worker): nest vitest projects under test.* and override RETENTION_SECONDS via miniflare bindings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Top-level projects:[] is ignored by vitest 4 (must be test.projects); without it the cloudflareTest plugin never applied. miniflare.vars is silently dropped by the plugin's V4 schema parse — the V4 shape carries plain-text vars as bindings, which merge over wrangler-derived values per key. Also keep deploy-*.test.ts in the default project so the suite still runs every file main runs. --- pkgs/pkarr-worker/vitest.config.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pkgs/pkarr-worker/vitest.config.ts b/pkgs/pkarr-worker/vitest.config.ts index 0b01a36d6b..50e08ca4ae 100644 --- a/pkgs/pkarr-worker/vitest.config.ts +++ b/pkgs/pkarr-worker/vitest.config.ts @@ -10,7 +10,8 @@ import { defineConfig } from "vitest/config"; const retentionSettings = ["0", "7d", "8640000000000", "3600"]; export default defineConfig({ - projects: [ + test: { + projects: [ { plugins: [ cloudflareTest({ @@ -18,7 +19,7 @@ export default defineConfig({ }), ], test: { - include: ["test/worker.test.ts"], + include: ["test/worker.test.ts", "test/deploy-*.test.ts"], name: "default", }, }, @@ -27,7 +28,7 @@ export default defineConfig({ cloudflareTest({ wrangler: { configPath: "./wrangler.jsonc" }, miniflare: { - vars: { RETENTION_SECONDS: setting }, + bindings: { RETENTION_SECONDS: setting }, }, }), ], @@ -36,5 +37,6 @@ export default defineConfig({ name: `retention-${setting}`, }, })), - ], + ], + }, });