Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/scripts/prune-prisma-client-runtime.test.mjs
T

75 lines
3.3 KiB
JavaScript

// @vitest-environment node
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, test } from "vitest";
import { prunePrismaClientRuntime, verifyGeneratedPrismaRuntimeContract } from "./prune-prisma-client-runtime.mjs";
const fixtures = [];
afterEach(() => {
for (const fixture of fixtures.splice(0)) fs.rmSync(fixture, { force: true, recursive: true });
});
describe("Prisma client runtime pruning", () => {
test("keeps only the PostgreSQL ESM query compiler imported by the generated client", async () => {
const fixture = createFixture();
const runtimePath = path.join(fixture, "node_modules/.pnpm/@prisma+client@7.8.0_fixture/node_modules/@prisma/client/runtime");
const generatedClientPath = path.join(fixture, "generated/class.ts");
write(
generatedClientPath,
[
'await import("@prisma/client/runtime/query_compiler_fast_bg.postgresql.mjs")',
'await import("@prisma/client/runtime/query_compiler_fast_bg.postgresql.wasm-base64.mjs")',
].join("\n")
);
write(path.join(runtimePath, "query_compiler_fast_bg.postgresql.mjs"), "keep-runtime");
write(path.join(runtimePath, "query_compiler_fast_bg.postgresql.wasm-base64.mjs"), "keep-wasm");
write(path.join(runtimePath, "query_compiler_fast_bg.mysql.mjs"), "remove-mysql");
write(path.join(runtimePath, "query_compiler_fast_bg.postgresql.js"), "remove-cjs");
write(path.join(runtimePath, "client.mjs"), "keep-client");
write(path.join(runtimePath, "../generator-build/index.js"), "remove-generator");
const result = await prunePrismaClientRuntime({
generatedClientPath,
nodeModulesPath: path.join(fixture, "node_modules"),
});
assert.equal(result.clientCount, 1);
assert.equal(result.filesRemoved, 3);
assert.ok(result.bytesRemoved > 0);
assert.ok(fs.existsSync(path.join(runtimePath, "query_compiler_fast_bg.postgresql.mjs")));
assert.ok(fs.existsSync(path.join(runtimePath, "query_compiler_fast_bg.postgresql.wasm-base64.mjs")));
assert.ok(fs.existsSync(path.join(runtimePath, "client.mjs")));
assert.ok(!fs.existsSync(path.join(runtimePath, "query_compiler_fast_bg.mysql.mjs")));
assert.ok(!fs.existsSync(path.join(runtimePath, "query_compiler_fast_bg.postgresql.js")));
assert.ok(!fs.existsSync(path.join(runtimePath, "../generator-build")));
});
test("fails closed when the generated client selects another runtime", async () => {
const fixture = createFixture();
const generatedClientPath = path.join(fixture, "generated/class.ts");
write(
generatedClientPath,
[
'await import("@prisma/client/runtime/query_compiler_fast_bg.postgresql.mjs")',
'await import("@prisma/client/runtime/query_compiler_fast_bg.mysql.wasm-base64.mjs")',
].join("\n")
);
await assert.rejects(() => verifyGeneratedPrismaRuntimeContract(generatedClientPath), /generated Prisma runtime contract mismatch/);
});
});
function createFixture() {
const fixture = fs.mkdtempSync(path.join(os.tmpdir(), "prisma-runtime-prune-"));
fixtures.push(fixture);
return fixture;
}
function write(filePath, contents) {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, contents);
}