103 lines
2.9 KiB
TypeScript
103 lines
2.9 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { readdir, readFile } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
import { PostgreSqlContainer, type StartedPostgreSqlContainer } from "@testcontainers/postgresql";
|
|
import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";
|
|
import { Pool } from "pg";
|
|
|
|
import * as schema from "@/db/schema";
|
|
|
|
type DownloadSeed = {
|
|
editorialId: string;
|
|
usageId: string;
|
|
assetId: string;
|
|
};
|
|
|
|
export type PostgresTestDatabase = {
|
|
seedEditorialWithUsage(): Promise<DownloadSeed>;
|
|
insertDownload(input: DownloadSeed & { idempotencyKey: string }): Promise<void>;
|
|
stop(): Promise<void>;
|
|
};
|
|
|
|
export async function startPostgres(): Promise<PostgresTestDatabase> {
|
|
const container = await new PostgreSqlContainer("postgres:17-alpine")
|
|
.withDatabase("portal")
|
|
.withUsername("portal")
|
|
.withPassword("portal")
|
|
.start();
|
|
const pool = new Pool({ connectionString: container.getConnectionUri() });
|
|
|
|
await applyMigration(pool);
|
|
|
|
const db = drizzle({ client: pool, schema });
|
|
|
|
return {
|
|
async seedEditorialWithUsage() {
|
|
return seedEditorialWithUsage(db);
|
|
},
|
|
async insertDownload(input) {
|
|
await db.insert(schema.downloadEvents).values({
|
|
editorialAccountId: input.editorialId,
|
|
usageId: input.usageId,
|
|
immichAssetId: input.assetId,
|
|
requestId: randomUUID(),
|
|
idempotencyKey: input.idempotencyKey,
|
|
});
|
|
},
|
|
async stop() {
|
|
await pool.end();
|
|
await stopContainer(container);
|
|
},
|
|
};
|
|
}
|
|
|
|
async function applyMigration(pool: Pool) {
|
|
const migrationDirectory = path.join(process.cwd(), "drizzle");
|
|
const migrationFiles = (await readdir(migrationDirectory))
|
|
.filter((file) => /^\d+_.*\.sql$/.test(file))
|
|
.sort();
|
|
for (const file of migrationFiles) {
|
|
await pool.query(await readFile(path.join(migrationDirectory, file), "utf8"));
|
|
}
|
|
}
|
|
|
|
async function seedEditorialWithUsage(db: NodePgDatabase<typeof schema>): Promise<DownloadSeed> {
|
|
const userId = randomUUID();
|
|
const editorialId = randomUUID();
|
|
const usageId = randomUUID();
|
|
const assetId = "immich-asset-1";
|
|
|
|
await db.insert(schema.users).values({
|
|
id: userId,
|
|
name: "Test Redaktion",
|
|
email: `test-${userId}@example.com`,
|
|
emailVerified: true,
|
|
});
|
|
await db.insert(schema.editorialAccounts).values({
|
|
id: editorialId,
|
|
userId,
|
|
name: "Test Redaktion",
|
|
contactName: "Testkontakt",
|
|
loginEmail: `test-${userId}@example.com`,
|
|
status: "active",
|
|
currentPriceCents: 1500,
|
|
});
|
|
await db.insert(schema.usages).values({
|
|
id: usageId,
|
|
editorialAccountId: editorialId,
|
|
immichAssetId: assetId,
|
|
filenameSnapshot: "bild.jpg",
|
|
immichAlbumIdSnapshot: "immich-album-1",
|
|
albumNameSnapshot: "Presse",
|
|
purpose: "Artikel",
|
|
priceCents: 1500,
|
|
});
|
|
|
|
return { editorialId, usageId, assetId };
|
|
}
|
|
|
|
async function stopContainer(container: StartedPostgreSqlContainer) {
|
|
await container.stop();
|
|
}
|