fix: harden download history and attempt ledger

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 16:45:40 +02:00
1 parent 510fde7afa
commit ba673ad732
19 files changed
+2287 -311

No files matched your search

+18 -24
View File
@@ -432,6 +432,7 @@ test("a repeat remains free after a price change and creates no additional usage
const editorialId = await seedEditorial();
const { usageService, downloadService } = createServices();
const first = await usageService.createNewUsage(newInput(editorialId));
await markDelivered(first.id);
await database
.update(editorialAccounts)
.set({ currentPriceCents: 2_100 })
@@ -456,6 +457,7 @@ test("same-key repeat concurrency creates one free event", async () => {
const editorialId = await seedEditorial();
const { usageService, downloadService } = createServices();
const first = await usageService.createNewUsage(newInput(editorialId));
await markDelivered(first.id);
const repeatInput = {
editorialId,
usageId: first.usageId,
@@ -476,7 +478,7 @@ test("same-key repeat concurrency creates one free event", async () => {
expect(eventCount.value).toBe(1);
});
test("returns a repeat winner committed while waiting before later suspension is checked", async () => {
test("rechecks suspension after an idempotent repeat winner commits", async () => {
const editorialId = await seedEditorial();
const { usageService } = createServices();
const first = await usageService.createNewUsage(newInput(editorialId));
@@ -486,20 +488,8 @@ test("returns a repeat winner committed while waiting before later suspension is
note: "Web-Ausgabe",
idempotencyKey: randomUUID(),
};
const realRepository = createRepeatDownloadRepository(database);
let releaseFastMiss!: () => void;
const fastMiss = new Promise<void>((resolve) => {
releaseFastMiss = resolve;
});
const downloadService = createDownloadService({
repository: {
...realRepository,
async findIntent(requestEditorialId, key) {
const intent = await realRepository.findIntent(requestEditorialId, key);
releaseFastMiss();
return intent;
},
},
repository: createRepeatDownloadRepository(database),
});
const eventId = randomUUID();
const authorizedAt = new Date("2026-08-09T11:00:00.000Z");
@@ -509,7 +499,6 @@ test("returns a repeat winner committed while waiting before later suspension is
try {
const pending = downloadService.createRepeatDownload(input);
await fastMiss;
await locker.query(
`insert into download_events (
id, editorial_account_id, usage_id, immich_asset_id, authorized_at,
@@ -538,15 +527,7 @@ test("returns a repeat winner committed while waiting before later suspension is
]);
await locker.query("commit");
await expect(pending).resolves.toEqual({
id: eventId,
usageId: first.usageId,
assetId,
priceCents: 0,
billable: false,
status: "authorized",
authorizedAt,
});
await expect(pending).rejects.toMatchObject({ code: "EDITORIAL_NOT_ACTIVE" });
} finally {
await locker.query("rollback");
locker.release();
@@ -585,3 +566,16 @@ test("repeat ownership is scoped to the active editorial", async () => {
}),
).rejects.toMatchObject({ code: "USAGE_NOT_FOUND" });
});
async function markDelivered(downloadId: string): Promise<void> {
const deliveredAt = new Date();
await database
.update(downloadEvents)
.set({
status: "completed",
startedAt: deliveredAt,
completedAt: deliveredAt,
transferredBytes: 1,
})
.where(eq(downloadEvents.id, downloadId));
}