Personal Telegram developer console for GitHub

test: require durable approval replay protection

davoodmehraban89 committed Oct 3, 2026 at 06:29 UTC 96c810a2f398b5387648b24a8582d4abc14f8a1d
1 file changed +113
test/durable-replay.test.js new
+113
@@ -0,0 +1,113 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { DurableApprovalReplayStore } from '../src/core/durable-replay-store.js';
4 +import { ApprovalReplayGuard } from '../src/durable/approval-replay-guard.js';
5 +
6 +function makeNamespace(handler) {
7 + const ids = [];
8 + return {
9 + ids,
10 + idFromName(name) {
11 + ids.push(String(name));
12 + return `id:${name}`;
13 + },
14 + get(id) {
15 + return {
16 + async fetch(request) {
17 + return handler(id, request);
18 + }
19 + };
20 + }
21 + };
22 +}
23 +
24 +function makeState() {
25 + const data = new Map();
26 + let alarm = null;
27 + return {
28 + data,
29 + get alarm() { return alarm; },
30 + storage: {
31 + async transaction(fn) {
32 + return fn({
33 + get: key => data.get(key),
34 + put: (key, value) => { data.set(key, value); },
35 + delete: key => data.delete(key)
36 + });
37 + },
38 + async setAlarm(value) { alarm = value; },
39 + async deleteAll() { data.clear(); alarm = null; }
40 + }
41 + };
42 +}
43 +
44 +test('DurableApprovalReplayStore fails closed without a Durable Object namespace', async () => {
45 + const store = new DurableApprovalReplayStore();
46 + await assert.rejects(
47 + () => store.consume({ ownerId: '123', nonce: 'abc', expiresAt: Date.now() + 10_000 }),
48 + /not configured/i
49 + );
50 +});
51 +
52 +test('DurableApprovalReplayStore scopes one-time consumption by owner and nonce', async () => {
53 + const namespace = makeNamespace(async (_id, request) => {
54 + const body = await request.json();
55 + return Response.json({ accepted: true, expiresAt: body.expiresAt });
56 + });
57 + const store = new DurableApprovalReplayStore({ namespace });
58 +
59 + const accepted = await store.consume({
60 + ownerId: '123',
61 + nonce: 'nonce-1',
62 + expiresAt: 42_000
63 + });
64 +
65 + assert.equal(accepted, true);
66 + assert.deepEqual(namespace.ids, ['123:nonce-1']);
67 +});
68 +
69 +test('DurableApprovalReplayStore rejects a replay response from the Durable Object', async () => {
70 + const namespace = makeNamespace(async () => Response.json({ accepted: false, reason: 'already_used' }, { status: 409 }));
71 + const store = new DurableApprovalReplayStore({ namespace });
72 +
73 + const accepted = await store.consume({ ownerId: '123', nonce: 'nonce-2', expiresAt: 42_000 });
74 + assert.equal(accepted, false);
75 +});
76 +
77 +test('ApprovalReplayGuard atomically accepts once and schedules cleanup', async () => {
78 + const state = makeState();
79 + const guard = new ApprovalReplayGuard(state);
80 + const expiresAt = Date.now() + 60_000;
81 + const request = () => new Request('https://approval-replay/consume', {
82 + method: 'POST',
83 + headers: { 'content-type': 'application/json' },
84 + body: JSON.stringify({ expiresAt })
85 + });
86 +
87 + const first = await guard.fetch(request());
88 + assert.equal(first.status, 200);
89 + assert.deepEqual(await first.json(), { accepted: true });
90 + assert.equal(state.data.get('used'), expiresAt);
91 + assert.equal(state.alarm, expiresAt);
92 +
93 + const second = await guard.fetch(request());
94 + assert.equal(second.status, 409);
95 + assert.deepEqual(await second.json(), { accepted: false, reason: 'already_used' });
96 +});
97 +
98 +test('ApprovalReplayGuard fails closed for expired requests and alarm removes storage', async () => {
99 + const state = makeState();
100 + const guard = new ApprovalReplayGuard(state);
101 + const response = await guard.fetch(new Request('https://approval-replay/consume', {
102 + method: 'POST',
103 + headers: { 'content-type': 'application/json' },
104 + body: JSON.stringify({ expiresAt: Date.now() - 1 })
105 + }));
106 +
107 + assert.equal(response.status, 410);
108 + assert.equal((await response.json()).accepted, false);
109 +
110 + state.data.set('used', Date.now());
111 + await guard.alarm();
112 + assert.equal(state.data.size, 0);
113 +});