Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions pkgs/pkarr-worker/test/worker.retention.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, number> = {
"0": SEVEN_DAYS_MS,
"7d": SEVEN_DAYS_MS,
"8640000000000": SEVEN_DAYS_MS,
"3600": 3_600_000,
};

const SEED_TAGS: Record<string, number> = {
"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);
});
});
42 changes: 37 additions & 5 deletions pkgs/pkarr-worker/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@
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" },
}),
],
test: {
projects: [
{
plugins: [
cloudflareTest({
wrangler: { configPath: "./wrangler.jsonc" },
}),
],
test: {
include: ["test/worker.test.ts", "test/deploy-*.test.ts"],
name: "default",
},
},
...retentionSettings.map((setting) => ({
plugins: [
cloudflareTest({
wrangler: { configPath: "./wrangler.jsonc" },
miniflare: {
bindings: { RETENTION_SECONDS: setting },
},
}),
],
test: {
include: ["test/worker.retention.test.ts"],
name: `retention-${setting}`,
},
})),
],
},
});