diff --git a/.gitignore b/.gitignore index 187db5a..1bf087f 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,6 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json # Agents .claude/settings.local.json + +# Local test media for seed scripts +apps/api/src/scripts/fixtures diff --git a/apps/api/src/scripts/seed-test-verification.ts b/apps/api/src/scripts/seed-test-verification.ts index cb22590..23c24a1 100644 --- a/apps/api/src/scripts/seed-test-verification.ts +++ b/apps/api/src/scripts/seed-test-verification.ts @@ -1,49 +1,57 @@ import { Gender, inToCm, lbToKg, minToMs, secToMs } from "@blade-and-brawn/domain"; +import { S3Client } from "bun"; +import { readdir } from "node:fs/promises"; +import path from "node:path"; +import { parseArgs } from "node:util"; import { db } from "../database/db"; import { env, log } from "../util"; import type { AccountRole } from "../services/accounts"; import { VerificationsService } from "../services/verifications"; -const TEST_DISCORD_ID = "test-verification-seed"; -const TEST_ASSESSMENT_NAME = "Test Verification Player"; +const DEFAULT_DISCORD_ID = "test-verification-seed"; +const FIXTURES_DIR = path.join(import.meta.dir, "fixtures"); +const BUCKET = `verification-media${env.NODE_ENV === "production" ? "" : "-dev"}`; + +const { values: args } = parseArgs({ + args: Bun.argv.slice(2), + options: { "discord-id": { type: "string", default: DEFAULT_DISCORD_ID } }, +}); +const discordId = args["discord-id"]; + +const r2 = new S3Client({ + accessKeyId: env.R2_ACCESS_KEY_ID, + secretAccessKey: env.R2_SECRET_ACCESS_KEY, + bucket: BUCKET, + endpoint: env.R2_URL, +}); async function seedTestAccount(): Promise { const existing = await db.selectFrom("accounts") .select("id") - .where("discord_id", "=", TEST_DISCORD_ID) + .where("discord_id", "=", discordId) .executeTakeFirst(); if (existing) { - log.info({ id: existing.id }, "test account already seeded, reusing"); + log.info({ id: existing.id, discordId }, "account already exists, reusing"); return existing.id; } const account = await db.insertInto("accounts") .values({ - discord_id: TEST_DISCORD_ID, + discord_id: discordId, name: "Test Player", role: "user" satisfies AccountRole, }) .returning("id") .executeTakeFirstOrThrow(); - log.info({ id: account.id }, "seeded test account"); + log.info({ id: account.id, discordId }, "seeded test account"); return account.id; } async function seedTestAssessment(accountId: string): Promise { - const existing = await db.selectFrom("assessments") - .select("id") - .where("account_id", "=", accountId) - .where("name", "=", TEST_ASSESSMENT_NAME) - .executeTakeFirst(); - if (existing) { - log.info({ id: existing.id }, "test assessment already seeded, reusing"); - return existing.id; - } - const result = await db.insertInto("assessments") .values({ account_id: accountId, - name: TEST_ASSESSMENT_NAME, + name: `Test Verification ${new Date().toISOString()}`, gender: Gender.Male, age: 22, weight: lbToKg(180), @@ -60,8 +68,20 @@ async function seedTestAssessment(accountId: string): Promise { return result.id; } +async function uploadFixtures(assessmentId: string) { + const names = await readdir(FIXTURES_DIR).catch(() => []); + if (names.length === 0) log.warn({ dir: FIXTURES_DIR }, "no fixture media found, verification will have no media"); + + for (const name of names) { + const file = Bun.file(path.join(FIXTURES_DIR, name)); + const key = `${assessmentId}/${crypto.randomUUID()}`; + await r2.write(key, file, { type: file.type }); + log.info({ key, name, size: file.size }, "uploaded fixture media"); + } +} + (async () => { - const answer = prompt(`Seed a test verification request in the database (${env.DATABASE_URL})? (y/N)`); + const answer = prompt(`Seed a test verification for discord id "${discordId}" in the database (${env.DATABASE_URL}) and R2 bucket "${BUCKET}"? (y/N)`); if (answer?.trim().toLowerCase() !== "y") { log.info("Aborted"); await db.destroy(); @@ -74,7 +94,9 @@ async function seedTestAssessment(accountId: string): Promise { const verifications = new VerificationsService(); const verification = await verifications.create(assessmentId); - if (!verification) throw new Error("Test assessment already has a verification; clear the verifications table first"); + if (!verification) throw new Error("Failed to create test verification"); + + await uploadFixtures(assessmentId); const submitted = await verifications.submit(verification.id); if (!submitted) throw new Error("Failed to submit test verification"); diff --git a/apps/api/src/server.ts b/apps/api/src/server.ts index dc85239..b356d0a 100644 --- a/apps/api/src/server.ts +++ b/apps/api/src/server.ts @@ -53,7 +53,7 @@ const r2 = { verificationMedia: new S3Client({ accessKeyId: env.R2_ACCESS_KEY_ID, secretAccessKey: env.R2_SECRET_ACCESS_KEY, - bucket: "verification-media", + bucket: `verification-media${env.NODE_ENV === "production" ? "" : "-dev"}`, endpoint: env.R2_URL, }) };