import { expect, test, vi } from "vitest"; import { createDownloadService, type PersistedRepeatIntent, type RepeatDownloadRepository, type RepeatIntentInput, } from "./service"; import { UsageAccountingError } from "@/modules/usages/types"; 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(); const events: RepeatIntentInput[] = []; let active = true; let usageOwner = editorialId; let snapshotPrice = 1_500; const repository: RepeatDownloadRepository = { async findIntent(requestEditorialId, key) { return intents.get(`${requestEditorialId}:${key}`) ?? null; }, async createRepeatIntent(input) { if (!active) throw new UsageAccountingError("EDITORIAL_NOT_ACTIVE"); if (usageOwner !== input.editorialId) throw new UsageAccountingError("USAGE_NOT_FOUND"); const key = `${input.editorialId}:${input.idempotencyKey}`; const existing = intents.get(key); if (existing) return existing; events.push(input); const intent: PersistedRepeatIntent = { id: `download-${events.length}`, usageId: input.usageId, assetId, priceCents: 0, billable: false, status: "authorized", authorizedAt: new Date("2026-08-09T09:00:00.000Z"), requestFingerprint: input.requestFingerprint, kind: "repeat_download", }; intents.set(key, intent); return intent; }, }; return { service: createDownloadService({ repository }), repository, events, setActive(value: boolean) { active = value; }, setUsageOwner(value: string) { usageOwner = value; }, setSnapshotPrice(value: number) { snapshotPrice = value; }, getSnapshotPrice() { return snapshotPrice; }, }; } test("creates only a free authorized event for an owned prior usage", async () => { const { service, events, getSnapshotPrice } = createService(); const intent = await service.createRepeatDownload({ editorialId, usageId, note: " erneuter Abruf ", idempotencyKey, }); expect(intent).toMatchObject({ usageId, assetId, priceCents: 0, billable: false, status: "authorized", }); expect(events).toEqual([expect.objectContaining({ note: "erneuter Abruf" })]); expect(getSnapshotPrice()).toBe(1_500); }); test("a changed usage snapshot price never changes the free repeat price", async () => { const { service, setSnapshotPrice } = createService(); setSnapshotPrice(9_900); await expect( service.createRepeatDownload({ editorialId, usageId, idempotencyKey }), ).resolves.toMatchObject({ priceCents: 0, billable: false }); }); test("rejects a usage owned by another editorial without creating an event", async () => { const { service, events, setUsageOwner } = createService(); setUsageOwner("66666666-6666-4666-8666-666666666666"); await expect( service.createRepeatDownload({ editorialId, usageId, idempotencyKey }), ).rejects.toMatchObject({ code: "USAGE_NOT_FOUND" }); expect(events).toHaveLength(0); }); test("rejects a repeat for an inactive editorial", async () => { const { service, events, setActive } = createService(); setActive(false); await expect( service.createRepeatDownload({ editorialId, usageId, idempotencyKey }), ).rejects.toMatchObject({ code: "EDITORIAL_NOT_ACTIVE" }); expect(events).toHaveLength(0); }); test("returns the same free intent once for an equivalent retry", async () => { const { service, events } = createService(); const input = { editorialId, usageId, note: "noch einmal", idempotencyKey }; const first = await service.createRepeatDownload(input); const second = await service.createRepeatDownload(input); expect(second).toEqual(first); 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 }); await expect( service.createRepeatDownload({ editorialId, usageId, note: "Print", idempotencyKey }), ).rejects.toMatchObject({ code: "IDEMPOTENCY_CONFLICT" }); expect(events).toHaveLength(1); }); test.each([ ["x".repeat(201), idempotencyKey], [undefined, "not-a-uuid"], ] as const)("rejects malformed repeat input before repository side effects", async (note, key) => { const { service, repository } = createService(); const findIntent = vi.spyOn(repository, "findIntent"); await expect( service.createRepeatDownload({ editorialId, usageId, note, idempotencyKey: key }), ).rejects.toMatchObject({ code: "INVALID_INPUT" }); expect(findIntent).not.toHaveBeenCalled(); });