68 lines
2.6 KiB
JavaScript
68 lines
2.6 KiB
JavaScript
const assert = require("node:assert/strict");
|
|
const { randomBytes } = require("node:crypto");
|
|
const { spawnSync } = require("node:child_process");
|
|
const { PrismaClient } = require("@prisma/client");
|
|
|
|
const databaseUrl = process.env.DATABASE_URL;
|
|
if (!databaseUrl) throw new Error("DATABASE_URL is required for integration tests");
|
|
|
|
const schema = `pb_test_${randomBytes(8).toString("hex")}`;
|
|
const testUrl = new URL(databaseUrl);
|
|
testUrl.searchParams.set("schema", schema);
|
|
const testEnvironment = { ...process.env, DATABASE_URL: testUrl.toString(), NODE_ENV: "test" };
|
|
const administrator = new PrismaClient();
|
|
const fixtureDatabase = new PrismaClient({ datasourceUrl: testUrl.toString() });
|
|
|
|
function run(command, args) {
|
|
const result = spawnSync(command, args, { stdio: "inherit", env: testEnvironment });
|
|
if (result.error) throw result.error;
|
|
return result.status ?? 1;
|
|
}
|
|
|
|
async function main() {
|
|
let created = false;
|
|
let status = 1;
|
|
try {
|
|
await administrator.$executeRawUnsafe(`CREATE SCHEMA "${schema}"`);
|
|
created = true;
|
|
if (run("pnpm", ["exec", "prisma", "migrate", "deploy"]) !== 0) return status;
|
|
if (run("pnpm", ["db:seed"]) !== 0) return status;
|
|
const initialTrustCount = await fixtureDatabase.trustContent.count();
|
|
const initialFormCount = await fixtureDatabase.inquiryForm.count();
|
|
const initialReleases = await fixtureDatabase.brandRelease.findMany({ orderBy: { id: "asc" } });
|
|
if (run("pnpm", ["db:seed"]) !== 0) return status;
|
|
assert.equal(
|
|
await fixtureDatabase.trustContent.count(),
|
|
initialTrustCount,
|
|
"Repeated seed must not duplicate trust content",
|
|
);
|
|
assert.equal(
|
|
await fixtureDatabase.inquiryForm.count(),
|
|
initialFormCount,
|
|
"Repeated seed must not duplicate forms",
|
|
);
|
|
assert.deepEqual(
|
|
await fixtureDatabase.brandRelease.findMany({ orderBy: { id: "asc" } }),
|
|
initialReleases,
|
|
"Repeated seed must preserve release history and snapshots",
|
|
);
|
|
await fixtureDatabase.adminUser.updateMany({
|
|
where: { username: "admin" },
|
|
data: { mustChangePassword: false },
|
|
});
|
|
console.log("Seed idempotency verified: content, forms and release history preserved");
|
|
status = run(process.execPath, ["--test", "test/http.integration.test.js"]);
|
|
return status;
|
|
} finally {
|
|
await fixtureDatabase.$disconnect();
|
|
if (created) await administrator.$executeRawUnsafe(`DROP SCHEMA "${schema}" CASCADE`);
|
|
await administrator.$disconnect();
|
|
process.exitCode = status;
|
|
}
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exitCode = 1;
|
|
});
|