fix: harden usage accounting idempotency

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 10:59:12 +02:00
1 parent 22fe6bf659
commit 3de1530074
14 files changed
+708 -107

No files matched your search

+23 -5
View File
@@ -8,9 +8,10 @@ import {
} from "./service";
import { UsageAccountingError } from "@/modules/usages/types";
const editorialId = "11111111-1111-4111-8111-111111111111";
const usageId = "44444444-4444-4444-8444-444444444444";
const idempotencyKey = "55555555-5555-4555-8555-555555555555";
const editorialId = "1111111a-1111-4111-8111-11111111111a";
const usageId = "4444444d-4444-4444-8444-44444444444d";
const idempotencyKey = "5555555e-5555-4555-8555-55555555555e";
const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
function createService() {
const intents = new Map<string, PersistedRepeatIntent>();
@@ -33,7 +34,7 @@ function createService() {
const intent: PersistedRepeatIntent = {
id: `download-${events.length}`,
usageId: input.usageId,
assetId: "asset-1",
assetId,
priceCents: 0,
billable: false,
status: "authorized",
@@ -77,7 +78,7 @@ test("creates only a free authorized event for an owned prior usage", async () =
expect(intent).toMatchObject({
usageId,
assetId: "asset-1",
assetId,
priceCents: 0,
billable: false,
status: "authorized",
@@ -126,6 +127,23 @@ test("returns the same free intent once for an equivalent retry", async () => {
expect(events).toHaveLength(1);
});
test("treats mixed-case UUID input as the same repeat intent", async () => {
const { service, events } = createService();
const uppercaseInput = {
editorialId: editorialId.toUpperCase(),
usageId: usageId.toUpperCase(),
idempotencyKey: idempotencyKey.toUpperCase(),
};
const first = await service.createRepeatDownload(uppercaseInput);
const second = await service.createRepeatDownload({ editorialId, usageId, idempotencyKey });
expect(second).toEqual(first);
expect(events).toEqual([
expect.objectContaining({ editorialId, usageId, idempotencyKey }),
]);
});
test("rejects reuse of a repeat key with a materially different note", async () => {
const { service, events } = createService();
await service.createRepeatDownload({ editorialId, usageId, note: "Web", idempotencyKey });