File size: 9,052 Bytes
20f83d9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// @vitest-environment node

/**
 * Direct unit coverage for the Idempotency-Key core (server/_shared/idempotency.ts).
 * Complements the gateway integration test (gateway-idempotency.test.ts) by
 * driving beginIdempotency()/store() straight against a mocked runRedisPipeline,
 * including the branches the gateway path can't easily reach: corrupt-value and
 * per-command-error fail-open, and the store DEL guards (5xx / oversized /
 * non-text body).
 */

import { describe, test, expect, vi, beforeEach } from 'vitest';

const runRedisPipeline = vi.fn();
vi.mock('../_shared/redis', async (importActual) => {
  const actual = await importActual<typeof import('../_shared/redis')>();
  return { ...actual, runRedisPipeline: (...a: unknown[]) => runRedisPipeline(...a) };
});

import { beginIdempotency, isValidIdempotencyKey, peekIdempotency } from '../_shared/idempotency';

const PATH = '/api/scenario/v1/run-scenario';
const BODY = JSON.stringify({ scenario: 'x' });

function makeRequest(body: string = BODY): Request {
  return new Request(`https://www.worldmonitor.app${PATH}`, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body,
  });
}

function begin(key: string, opts: { request?: Request; scope?: string | null } = {}) {
  return beginIdempotency({
    request: opts.request ?? makeRequest(),
    pathname: PATH,
    scope: opts.scope ?? 'user_api_key:acct_1',
    idempotencyKey: key,
    corsHeaders: {},
  });
}

function peek(key: string, opts: { request?: Request; scope?: string | null } = {}) {
  return peekIdempotency({
    request: opts.request ?? makeRequest(),
    pathname: PATH,
    scope: opts.scope ?? 'user_api_key:acct_1',
    idempotencyKey: key,
    corsHeaders: {},
  });
}

async function sha256Hex(str: string): Promise<string> {
  const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(str));
  return Array.from(new Uint8Array(digest))
    .map((b) => b.toString(16).padStart(2, '0'))
    .join('');
}

beforeEach(() => runRedisPipeline.mockReset());

describe('isValidIdempotencyKey', () => {
  test.each([
    ['4f8b9c2e-1a3d-4b6f-8e0a-2c5d7f9b1e34', true],
    ['a'.repeat(255), true],
    ['~!@#$%^&*()_+', true],
    ['', false],
    ['a'.repeat(256), false],
    ['has space', false],
    ['tab\tchar', false],
    ['new\nline', false],
    ['unicodΓ©', false],
  ])('%s β†’ %s', (key, expected) => {
    expect(isValidIdempotencyKey(key)).toBe(expected);
  });
});

describe('beginIdempotency', () => {
  test('malformed key β†’ invalid, never touches Redis', async () => {
    const out = await begin('');
    expect(out.kind).toBe('invalid');
    expect(runRedisPipeline).not.toHaveBeenCalled();
  });

  test('successful claim β†’ proceed', async () => {
    runRedisPipeline.mockResolvedValueOnce([{ result: 'OK' }, { result: null }]);
    const out = await begin('k1');
    expect(out.kind).toBe('proceed');
  });

  test('empty pipeline (Redis down) β†’ disabled (fail-open)', async () => {
    runRedisPipeline.mockResolvedValueOnce([]);
    expect((await begin('k1')).kind).toBe('disabled');
  });

  test('per-command error on the claim β†’ disabled (fail-open)', async () => {
    runRedisPipeline.mockResolvedValueOnce([{ error: 'WRONGTYPE' }, { result: null }]);
    expect((await begin('k1')).kind).toBe('disabled');
  });

  test('key exists but stored value is corrupt β†’ disabled (fail-open)', async () => {
    runRedisPipeline.mockResolvedValueOnce([{ result: null }, { result: 'not-json{' }]);
    expect((await begin('k1')).kind).toBe('disabled');
  });

  test('processing marker β†’ conflict (409)', async () => {
    runRedisPipeline.mockResolvedValueOnce([
      { result: null },
      { result: JSON.stringify({ state: 'processing' }) },
    ]);
    const out = await begin('k1');
    expect(out.kind).toBe('conflict');
    if (out.kind === 'conflict') expect(out.response.status).toBe(409);
  });

  test('completed + matching body hash β†’ replay', async () => {
    const reqHash = await sha256Hex(BODY);
    runRedisPipeline.mockResolvedValueOnce([
      { result: null },
      {
        result: JSON.stringify({
          state: 'completed',
          status: 201,
          contentType: 'application/json',
          reqHash,
          body: JSON.stringify({ id: 'orig' }),
        }),
      },
    ]);
    const out = await begin('k1');
    expect(out.kind).toBe('replay');
    if (out.kind === 'replay') {
      expect(out.response.status).toBe(201);
      expect(out.response.headers.get('Idempotent-Replayed')).toBe('true');
      expect(await out.response.json()).toEqual({ id: 'orig' });
    }
  });

  test('completed + different body hash β†’ mismatch (422)', async () => {
    runRedisPipeline.mockResolvedValueOnce([
      { result: null },
      {
        result: JSON.stringify({
          state: 'completed',
          status: 200,
          contentType: 'application/json',
          reqHash: 'different',
          body: '{}',
        }),
      },
    ]);
    const out = await begin('k1');
    expect(out.kind).toBe('mismatch');
    if (out.kind === 'mismatch') expect(out.response.status).toBe(422);
  });
});

describe('peekIdempotency', () => {
  test('completed + matching body hash β†’ replay without claiming', async () => {
    const reqHash = await sha256Hex(BODY);
    runRedisPipeline.mockResolvedValueOnce([
      {
        result: JSON.stringify({
          state: 'completed',
          status: 202,
          contentType: 'application/json',
          reqHash,
          body: JSON.stringify({ id: 'peeked' }),
        }),
      },
    ]);
    const out = await peek('k1');
    expect(out.kind).toBe('replay');
    if (out.kind === 'replay') {
      expect(out.response.status).toBe(202);
      expect(await out.response.json()).toEqual({ id: 'peeked' });
    }
    expect(runRedisPipeline.mock.calls[0][0][0][0]).toBe('GET');
  });

  test('missing record β†’ miss without claiming', async () => {
    runRedisPipeline.mockResolvedValueOnce([{ result: null }]);
    const out = await peek('k1');
    expect(out.kind).toBe('miss');
    expect(runRedisPipeline.mock.calls[0][0][0][0]).toBe('GET');
  });
});

describe('store() (returned by a successful claim)', () => {
  async function getStore() {
    runRedisPipeline.mockResolvedValueOnce([{ result: 'OK' }, { result: null }]);
    const out = await begin('k1');
    if (out.kind !== 'proceed') throw new Error('expected proceed');
    runRedisPipeline.mockClear();
    runRedisPipeline.mockResolvedValue([{ result: 'OK' }]);
    return out.store;
  }

  function lastCmd() {
    return runRedisPipeline.mock.calls.at(-1)![0][0];
  }

  test('2xx JSON body β†’ SET completed record', async () => {
    const store = await getStore();
    await store(200, new TextEncoder().encode('{"ok":true}').buffer, 'application/json');
    const cmd = lastCmd();
    expect(cmd[0]).toBe('SET');
    expect(JSON.parse(cmd[2]).state).toBe('completed');
  });

  test('stored record round-trips through beginIdempotency replay', async () => {
    const store = await getStore();
    await store(201, new TextEncoder().encode('{"ok":true}').buffer, 'application/json');
    const stored = runRedisPipeline.mock.calls.at(-1)![0][0][2];

    runRedisPipeline.mockReset();
    runRedisPipeline.mockResolvedValueOnce([{ result: null }, { result: stored }]);
    const out = await begin('k1');

    expect(out.kind).toBe('replay');
    if (out.kind === 'replay') {
      expect(out.response.status).toBe(201);
      expect(out.response.headers.get('content-type')).toBe('application/json');
      expect(await out.response.json()).toEqual({ ok: true });
    }
  });

  test('5xx β†’ DEL (release lock)', async () => {
    const store = await getStore();
    await store(503, new TextEncoder().encode('{}').buffer, 'application/json');
    expect(lastCmd()[0]).toBe('DEL');
  });

  test('transient 429 β†’ DEL (release lock)', async () => {
    const store = await getStore();
    await store(429, new TextEncoder().encode('{"retry":true}').buffer, 'application/json');
    expect(lastCmd()[0]).toBe('DEL');
  });

  test('oversized body β†’ DEL (release lock)', async () => {
    const store = await getStore();
    const big = new ArrayBuffer(256 * 1024 + 1);
    await store(200, big, 'application/json');
    expect(lastCmd()[0]).toBe('DEL');
  });

  test('non-text content-type β†’ DEL (release lock)', async () => {
    const store = await getStore();
    await store(200, new ArrayBuffer(8), 'application/octet-stream');
    expect(lastCmd()[0]).toBe('DEL');
  });

  test('completed SET failure releases the processing marker', async () => {
    const store = await getStore();
    runRedisPipeline.mockReset();
    runRedisPipeline
      .mockResolvedValueOnce([])
      .mockResolvedValueOnce([{ result: 1 }]);

    await store(200, new TextEncoder().encode('{"ok":true}').buffer, 'application/json');

    expect(runRedisPipeline.mock.calls[0][0][0][0]).toBe('SET');
    expect(runRedisPipeline.mock.calls[1][0][0][0]).toBe('DEL');
  });
});