117 lines
3.7 KiB
TypeScript
117 lines
3.7 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
import { afterAll, expect, test, vi } from "vitest";
|
|
|
|
import { downloadEvents } from "@/db/schema";
|
|
import { createUsageService, createUsageServiceRepository } from "@/modules/usages/service";
|
|
import { requestFingerprint } from "@/modules/usages/types";
|
|
import { startPostgres } from "./postgres";
|
|
|
|
const testDb = await startPostgres({ through: "0001_editorial_approval_notice.sql" });
|
|
|
|
afterAll(() => testDb.stop());
|
|
|
|
test("0002 preserves an equivalent retry of a populated legacy accounting intent", async () => {
|
|
const userId = randomUUID();
|
|
const editorialId = randomUUID();
|
|
const usageId = randomUUID();
|
|
const eventId = randomUUID();
|
|
const requestId = randomUUID();
|
|
const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
|
const albumId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
|
const idempotencyKey = "cccccccc-cccc-4ccc-8ccc-cccccccccccc";
|
|
const authorizedAt = new Date("2026-08-09T08:00:00.000Z");
|
|
|
|
await testDb.pool.query(
|
|
`insert into users (id, name, email, email_verified)
|
|
values ($1, 'Legacy Redaktion', $2, true)`,
|
|
[userId, `${userId}@example.com`],
|
|
);
|
|
await testDb.pool.query(
|
|
`insert into editorial_accounts (
|
|
id, user_id, name, contact_name, login_email, status, current_price_cents
|
|
) values ($1, $2, 'Legacy Redaktion', 'Legacy Kontakt', $3, 'active', 2300)`,
|
|
[editorialId, userId, `${userId}@example.com`],
|
|
);
|
|
await testDb.pool.query(
|
|
`insert into album_grants (editorial_account_id, immich_album_id)
|
|
values ($1, $2)`,
|
|
[editorialId, albumId.toUpperCase()],
|
|
);
|
|
await testDb.pool.query(
|
|
`insert into usages (
|
|
id, editorial_account_id, immich_asset_id, filename_snapshot,
|
|
immich_album_id_snapshot, album_name_snapshot, purpose,
|
|
internal_reference, price_cents, billing_status, created_at
|
|
) values ($1, $2, $3, 'legacy.jpg', $4, 'Legacy Album',
|
|
'Titelgeschichte', 'Ausgabe 8/26', 2300, 'open', $5)`,
|
|
[usageId, editorialId, assetId.toUpperCase(), albumId.toUpperCase(), authorizedAt],
|
|
);
|
|
await testDb.pool.query(
|
|
`insert into download_events (
|
|
id, editorial_account_id, usage_id, immich_asset_id, authorized_at,
|
|
status, request_id, idempotency_key
|
|
) values ($1, $2, $3, $4, $5, 'authorized', $6, $7)`,
|
|
[
|
|
eventId,
|
|
editorialId,
|
|
usageId,
|
|
assetId.toUpperCase(),
|
|
authorizedAt,
|
|
requestId,
|
|
idempotencyKey.toUpperCase(),
|
|
],
|
|
);
|
|
|
|
await testDb.applyRemainingMigrations();
|
|
|
|
const immich = {
|
|
listAlbumsForAsset: vi.fn(),
|
|
getAsset: vi.fn(),
|
|
};
|
|
const usageService = createUsageService({
|
|
repository: createUsageServiceRepository(testDb.database),
|
|
immich,
|
|
});
|
|
const input = {
|
|
editorialId: editorialId.toUpperCase(),
|
|
assetId: assetId.toUpperCase(),
|
|
albumId: albumId.toUpperCase(),
|
|
purpose: "Titelgeschichte",
|
|
reference: "Ausgabe 8/26",
|
|
idempotencyKey: idempotencyKey.toUpperCase(),
|
|
};
|
|
|
|
await expect(usageService.createNewUsage(input)).resolves.toEqual({
|
|
id: eventId,
|
|
usageId,
|
|
assetId,
|
|
priceCents: 2300,
|
|
billable: true,
|
|
status: "authorized",
|
|
authorizedAt,
|
|
});
|
|
expect(immich.listAlbumsForAsset).not.toHaveBeenCalled();
|
|
expect(immich.getAsset).not.toHaveBeenCalled();
|
|
|
|
const [migrated] = await testDb.database
|
|
.select()
|
|
.from(downloadEvents)
|
|
.where(eq(downloadEvents.id, eventId));
|
|
expect(migrated).toMatchObject({
|
|
immichAssetId: assetId,
|
|
idempotencyKey,
|
|
intentKind: "new_usage",
|
|
legacyIntent: true,
|
|
requestFingerprint: requestFingerprint({
|
|
kind: "new_usage",
|
|
editorialId,
|
|
assetId,
|
|
albumId,
|
|
purpose: "Titelgeschichte",
|
|
reference: "Ausgabe 8/26",
|
|
}),
|
|
});
|
|
});
|