96 lines
4.1 KiB
JavaScript
96 lines
4.1 KiB
JavaScript
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import assert from "node:assert/strict";
|
|
import { afterEach, describe, it } from "vitest";
|
|
|
|
import { formatProductionSeedArtifactResult, verifyProductionSeedArtifact } from "./verify-production-seed-artifact.mjs";
|
|
|
|
const VALID_BUILD_SCRIPT =
|
|
"pnpm --dir ../.. exec esbuild packages/db/src/permissions-seed.server.ts --bundle --platform=node --format=esm --packages=external --log-level=warning --outfile=packages/db/dist/permissions-seed.server.js && pnpm --dir ../.. exec esbuild prisma/seed.ts --bundle --platform=node --format=esm --external:@prisma/client --external:@prisma/adapter-pg --external:pg --external:pino --external:bcryptjs --alias:~=./apps/web/app --log-level=warning --outfile=packages/db/dist/production-seed.server.js";
|
|
|
|
const fixtures = [];
|
|
|
|
function writeFile(rootDir, relativePath, content) {
|
|
const absolutePath = path.join(rootDir, relativePath);
|
|
fs.mkdirSync(path.dirname(absolutePath), { recursive: true });
|
|
fs.writeFileSync(absolutePath, content);
|
|
}
|
|
|
|
function createFixture({ buildScript = VALID_BUILD_SCRIPT, artifact = "import { PrismaClient } from '@prisma/client';\nconsole.log(PrismaClient);\n" } = {}) {
|
|
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "production-seed-artifact-"));
|
|
fixtures.push(rootDir);
|
|
writeFile(
|
|
rootDir,
|
|
"packages/db/package.json",
|
|
`${JSON.stringify({ scripts: { build: buildScript } }, null, 2)}\n`,
|
|
);
|
|
writeFile(
|
|
rootDir,
|
|
"prisma/seed.ts",
|
|
'import { db } from "~/utils/db.server";\nasync function seed() { try { await Promise.resolve(); } finally { await db.$disconnect(); } }\nseed();\n',
|
|
);
|
|
if (artifact !== null) {
|
|
writeFile(rootDir, "packages/db/dist/production-seed.server.js", artifact);
|
|
}
|
|
return rootDir;
|
|
}
|
|
|
|
afterEach(() => {
|
|
while (fixtures.length > 0) {
|
|
fs.rmSync(fixtures.pop(), { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe("verify-production-seed-artifact", () => {
|
|
it("passes for a production seed build that bundles workspace packages", () => {
|
|
const result = verifyProductionSeedArtifact(createFixture());
|
|
|
|
assert.deepEqual(result.failures, []);
|
|
assert.ok(result.passes.some((pass) => /build script emits packages\/db\/dist\/production-seed\.server\.js/.test(pass)));
|
|
assert.ok(result.passes.some((pass) => /disconnects the shared Prisma client/.test(pass)));
|
|
assert.ok(result.passes.some((pass) => /production seed build bundles workspace packages/.test(pass)));
|
|
assert.ok(result.passes.some((pass) => /contains no bare @toolctrl\/\* imports/.test(pass)));
|
|
});
|
|
|
|
it("fails when the production seed build externalizes all packages", () => {
|
|
const result = verifyProductionSeedArtifact(
|
|
createFixture({
|
|
buildScript: VALID_BUILD_SCRIPT.replace(
|
|
"--external:@prisma/client --external:@prisma/adapter-pg --external:pg",
|
|
"--packages=external",
|
|
),
|
|
}),
|
|
);
|
|
|
|
assert.match(result.failures.join("\n"), /production seed build must not externalize all packages/);
|
|
});
|
|
|
|
it("fails when the built artifact keeps bare workspace imports", () => {
|
|
const result = verifyProductionSeedArtifact(
|
|
createFixture({
|
|
artifact: 'import { runDbSeed } from "@toolctrl/db/seed.server";\nrunDbSeed();\n',
|
|
}),
|
|
);
|
|
|
|
assert.match(result.failures.join("\n"), /must not contain bare @toolctrl\/\* imports/);
|
|
});
|
|
|
|
it("fails when the artifact has not been built yet", () => {
|
|
const result = verifyProductionSeedArtifact(createFixture({ artifact: null }));
|
|
const output = formatProductionSeedArtifactResult(result);
|
|
|
|
assert.match(result.failures.join("\n"), /run pnpm build/);
|
|
assert.match(output, /^FAIL Production seed artifact verification/);
|
|
});
|
|
|
|
it("fails when the production seed does not disconnect the shared Prisma client", () => {
|
|
const rootDir = createFixture();
|
|
writeFile(rootDir, "prisma/seed.ts", 'import SeedService from "~/utils/db/seed/SeedService";\nSeedService.seed();\n');
|
|
|
|
const result = verifyProductionSeedArtifact(rootDir);
|
|
|
|
assert.match(result.failures.join("\n"), /must disconnect the shared Prisma client/);
|
|
});
|
|
});
|