Files
suaveplan/packages/media/audio/audio-controllers/tests/purity.test.ts
2026-05-27 23:47:53 +01:00

57 lines
2.1 KiB
TypeScript

import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "@suaveplan/testing/runner";
const PKG_DIR = join(__dirname, "..");
const DIST_DIR = join(PKG_DIR, "dist");
function walk(dir: string, out: string[] = []): string[] {
if (!existsSync(dir)) return out;
for (const name of readdirSync(dir)) {
const p = join(dir, name);
const s = statSync(p);
if (s.isDirectory()) walk(p, out);
else if (/\.(js|mjs|cjs)$/.test(p)) out.push(p);
}
return out;
}
describe("purity", () => {
it("dist references no Node globals", { timeout: 30_000 }, () => {
if (!existsSync(DIST_DIR)) {
// dist will exist after build; skip purity if not yet built so the
// test suite can still run on a fresh clone.
expect(true).toBe(true);
return;
}
const offenders: string[] = [];
for (const file of walk(DIST_DIR)) {
const src = readFileSync(file, "utf8");
if (/\bnode:[a-z]+\b/.test(src)) offenders.push(`${file}: node:*`);
if (/\bprocess\.(env|cwd|platform)\b/.test(src))
offenders.push(`${file}: process`);
if (/\bBuffer\.from\b/.test(src)) offenders.push(`${file}: Buffer`);
}
expect(offenders).toEqual([]);
});
it("importing the package mutates no global state", {
timeout: 5000,
}, async () => {
const filter = (keys: string[]): string[] =>
keys
.filter(
(k) =>
!k.startsWith("__zod") &&
!k.startsWith("__vitest") &&
!k.startsWith("__VITEST")
)
.sort();
const beforeKeys = filter(Object.keys(globalThis));
const mod = await import("../src/index.js");
expect(mod.MidiControllerPlugin).toBeDefined();
const afterKeys = filter(Object.keys(globalThis));
expect(afterKeys).toEqual(beforeKeys);
});
});