Seed script for test verifications

This commit is contained in:
Dominic Ferrando
2026-10-03 18:26:53 -04:00
parent 09e7a9c0fd
commit e497982222
3 changed files with 45 additions and 20 deletions
+3
View File
@@ -38,3 +38,6 @@ report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Agents # Agents
.claude/settings.local.json .claude/settings.local.json
# Local test media for seed scripts
apps/api/src/scripts/fixtures
+41 -19
View File
@@ -1,49 +1,57 @@
import { Gender, inToCm, lbToKg, minToMs, secToMs } from "@blade-and-brawn/domain"; 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 { db } from "../database/db";
import { env, log } from "../util"; import { env, log } from "../util";
import type { AccountRole } from "../services/accounts"; import type { AccountRole } from "../services/accounts";
import { VerificationsService } from "../services/verifications"; import { VerificationsService } from "../services/verifications";
const TEST_DISCORD_ID = "test-verification-seed"; const DEFAULT_DISCORD_ID = "test-verification-seed";
const TEST_ASSESSMENT_NAME = "Test Verification Player"; 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<string> { async function seedTestAccount(): Promise<string> {
const existing = await db.selectFrom("accounts") const existing = await db.selectFrom("accounts")
.select("id") .select("id")
.where("discord_id", "=", TEST_DISCORD_ID) .where("discord_id", "=", discordId)
.executeTakeFirst(); .executeTakeFirst();
if (existing) { 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; return existing.id;
} }
const account = await db.insertInto("accounts") const account = await db.insertInto("accounts")
.values({ .values({
discord_id: TEST_DISCORD_ID, discord_id: discordId,
name: "Test Player", name: "Test Player",
role: "user" satisfies AccountRole, role: "user" satisfies AccountRole,
}) })
.returning("id") .returning("id")
.executeTakeFirstOrThrow(); .executeTakeFirstOrThrow();
log.info({ id: account.id }, "seeded test account"); log.info({ id: account.id, discordId }, "seeded test account");
return account.id; return account.id;
} }
async function seedTestAssessment(accountId: string): Promise<string> { async function seedTestAssessment(accountId: string): Promise<string> {
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") const result = await db.insertInto("assessments")
.values({ .values({
account_id: accountId, account_id: accountId,
name: TEST_ASSESSMENT_NAME, name: `Test Verification ${new Date().toISOString()}`,
gender: Gender.Male, gender: Gender.Male,
age: 22, age: 22,
weight: lbToKg(180), weight: lbToKg(180),
@@ -60,8 +68,20 @@ async function seedTestAssessment(accountId: string): Promise<string> {
return result.id; 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 () => { (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") { if (answer?.trim().toLowerCase() !== "y") {
log.info("Aborted"); log.info("Aborted");
await db.destroy(); await db.destroy();
@@ -74,7 +94,9 @@ async function seedTestAssessment(accountId: string): Promise<string> {
const verifications = new VerificationsService(); const verifications = new VerificationsService();
const verification = await verifications.create(assessmentId); 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); const submitted = await verifications.submit(verification.id);
if (!submitted) throw new Error("Failed to submit test verification"); if (!submitted) throw new Error("Failed to submit test verification");
+1 -1
View File
@@ -53,7 +53,7 @@ const r2 = {
verificationMedia: new S3Client({ verificationMedia: new S3Client({
accessKeyId: env.R2_ACCESS_KEY_ID, accessKeyId: env.R2_ACCESS_KEY_ID,
secretAccessKey: env.R2_SECRET_ACCESS_KEY, secretAccessKey: env.R2_SECRET_ACCESS_KEY,
bucket: "verification-media", bucket: `verification-media${env.NODE_ENV === "production" ? "" : "-dev"}`,
endpoint: env.R2_URL, endpoint: env.R2_URL,
}) })
}; };