Files
cfw-autumn/scripts/setup/setup-test.ts
amianthus aaa754ae02 feat(test-setup): 🎸 run afterOrgCreated for test org and persist stable pkey to env
createTestOrg now calls the production afterOrgCreated hook directly so
the test org goes through the same bootstrap as real orgs (Stripe
Connect, Svix, OrgService.update). Both org branches gate
createStripeAccount on the existing test_stripe_connect to stay
idempotent across re-runs. Pins TEST_ORG_PUBLISHABLE_KEY (a real
KSUID-generated am_pk_test_* value) and persists
UNIT_TEST_AUTUMN_PUBLIC_KEY plus the auto-generated
UNIT_TEST_AUTUMN_SECRET_KEY to server/.env.local + server/.env so test
clients pick them up. Deletes ensureDefaultStripeAccount.ts (replaced by
the gated afterOrgCreated call).
2026-05-16 15:08:29 +01:00

109 lines
3.3 KiB
JavaScript

#!/usr/bin/env node
import chalk from "chalk";
import inquirer from "inquirer";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import {
createTestOrg,
TEST_ORG_CONFIG,
TEST_ORG_PUBLISHABLE_KEY,
} from "../setupTestUtils/createTestOrg.js";
import { mergeEnvFile } from "../dw/helpers/env-files.js";
import { PROJECT_ROOT } from "../dw/constants.js";
// Worktree .env.local loading happens in scripts/preload-env.ts (auto-run by
// Bun via bunfig.toml's `preload`). DATABASE_URL flips to the worktree branch
// before this module's top-level statements execute.
function maskDatabaseUrl(url: string | undefined): string {
if (!url) return "(unset)";
try {
const u = new URL(url);
const host = u.host;
const db = u.pathname.replace(/^\//, "");
return `${u.protocol}//***@${host}/${db}`;
} catch {
return "(unparseable)";
}
}
async function maybeConfirm(yes: boolean): Promise<boolean> {
if (yes) return true;
if (!process.stdin.isTTY) return true;
const target = maskDatabaseUrl(process.env.DATABASE_URL);
const { ready } = await inquirer.prompt([
{
type: "confirm",
name: "ready",
message: chalk.cyan(
`About to seed '${TEST_ORG_CONFIG.slug}' into DATABASE_URL=${target}. Continue?`,
),
default: true,
},
]);
return Boolean(ready);
}
async function main() {
const yes = process.argv.includes("--yes");
console.log(
chalk.magentaBright(
`\n================ Autumn setup-test ================\n`,
),
);
console.log(
chalk.cyan(`Target: ${maskDatabaseUrl(process.env.DATABASE_URL)}\n`),
);
const proceed = await maybeConfirm(yes);
if (!proceed) {
console.log(chalk.yellow("Cancelled."));
process.exit(0);
}
try {
const hadKey = Boolean(process.env.UNIT_TEST_AUTUMN_SECRET_KEY);
const { db } = await import("@server/db/initDrizzle.js");
const autumnSecretKey = await createTestOrg({ db });
if (!hadKey) {
const envPath = join(PROJECT_ROOT, "server", ".env.local");
const existing = existsSync(envPath) ? readFileSync(envPath, "utf-8") : null;
const merged = mergeEnvFile(existing, {
UNIT_TEST_AUTUMN_SECRET_KEY: autumnSecretKey,
});
writeFileSync(envPath, merged);
process.env.UNIT_TEST_AUTUMN_SECRET_KEY = autumnSecretKey;
console.log(
chalk.cyan(`[setup-test] persisted UNIT_TEST_AUTUMN_SECRET_KEY to server/.env.local`),
);
}
const envPath = join(PROJECT_ROOT, "server", ".env.local");
const existing = existsSync(envPath) ? readFileSync(envPath, "utf-8") : null;
const merged = mergeEnvFile(existing, {
UNIT_TEST_AUTUMN_PUBLIC_KEY: TEST_ORG_PUBLISHABLE_KEY,
});
writeFileSync(envPath, merged);
process.env.UNIT_TEST_AUTUMN_PUBLIC_KEY = TEST_ORG_PUBLISHABLE_KEY;
console.log(
chalk.cyan(`[setup-test] persisted UNIT_TEST_AUTUMN_PUBLIC_KEY to server/.env.local`),
);
console.log(chalk.greenBright("\n✅ setup-test complete"));
console.log(chalk.cyan("Org:"));
console.log(chalk.whiteBright(` slug: ${TEST_ORG_CONFIG.slug}`));
console.log(chalk.whiteBright(` id: ${TEST_ORG_CONFIG.id}`));
console.log(chalk.whiteBright(` key: ${autumnSecretKey}\n`));
process.exit(0);
} catch (error) {
console.error(
chalk.red("\n❌ setup-test failed:"),
error instanceof Error ? error.message : error,
);
process.exit(1);
}
}
main();