|
| 1 | +import { randomUUID } from "node:crypto"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { RequestIdempotencyService } from "~/services/requestIdempotency.server"; |
| 4 | +import { |
| 5 | + scopeRequestIdempotencyHeader, |
| 6 | + scopeRequestIdempotencyKey, |
| 7 | +} from "~/utils/requestIdempotencyKey"; |
| 8 | + |
| 9 | +function createService() { |
| 10 | + return new RequestIdempotencyService({ types: ["trigger", "batch-trigger"] }); |
| 11 | +} |
| 12 | + |
| 13 | +describe("RequestIdempotencyService", () => { |
| 14 | + it("returns the saved entry for a scoped key", async () => { |
| 15 | + const service = createService(); |
| 16 | + const scopedKey = scopeRequestIdempotencyHeader(randomUUID(), ["env-1", "task-a"]); |
| 17 | + |
| 18 | + expect(scopedKey).toBeDefined(); |
| 19 | + |
| 20 | + await service.saveRequest("trigger", scopedKey!, { id: "run_1234" }); |
| 21 | + |
| 22 | + await expect(service.checkRequest("trigger", scopedKey!)).resolves.toEqual({ id: "run_1234" }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("keeps entries isolated by environment, task and request type", async () => { |
| 26 | + const service = createService(); |
| 27 | + const requestIdempotencyKey = randomUUID(); |
| 28 | + const scopedKey = scopeRequestIdempotencyHeader(requestIdempotencyKey, ["env-1", "task-a"]); |
| 29 | + |
| 30 | + await service.saveRequest("trigger", scopedKey!, { id: "run_1234" }); |
| 31 | + |
| 32 | + const otherEnvironment = scopeRequestIdempotencyHeader(requestIdempotencyKey, [ |
| 33 | + "env-2", |
| 34 | + "task-a", |
| 35 | + ]); |
| 36 | + const otherTask = scopeRequestIdempotencyHeader(requestIdempotencyKey, ["env-1", "task-b"]); |
| 37 | + |
| 38 | + await expect(service.checkRequest("trigger", otherEnvironment!)).resolves.toBeUndefined(); |
| 39 | + await expect(service.checkRequest("trigger", otherTask!)).resolves.toBeUndefined(); |
| 40 | + await expect(service.checkRequest("batch-trigger", scopedKey!)).resolves.toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("accepts a scoped body key, not just a scoped header", async () => { |
| 44 | + const service = createService(); |
| 45 | + const scopedKey = scopeRequestIdempotencyKey("my-batch-key", ["env-1", "task-a"]); |
| 46 | + |
| 47 | + await service.saveRequest("batch-trigger", scopedKey!, { id: "batch_1234" }); |
| 48 | + |
| 49 | + await expect(service.checkRequest("batch-trigger", scopedKey!)).resolves.toEqual({ |
| 50 | + id: "batch_1234", |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it("ignores a key that was not scoped, instead of throwing", async () => { |
| 55 | + const service = createService(); |
| 56 | + const unscopedKey = randomUUID(); |
| 57 | + |
| 58 | + await expect( |
| 59 | + service.saveRequest("trigger", unscopedKey, { id: "run_1234" }) |
| 60 | + ).resolves.toBeUndefined(); |
| 61 | + await expect(service.checkRequest("trigger", unscopedKey)).resolves.toBeUndefined(); |
| 62 | + }); |
| 63 | +}); |
0 commit comments