import { readFileSync } from "node:fs"; import { join } from "node:path"; import { describe, expect, it } from "@suaveplan/testing/runner"; const PKG_DIR = join(__dirname, ".."); describe("purity", () => { it("importing the package adds no Suaveplan-specific globals", { timeout: 5000, }, async () => { const banned = ["__suaveplan_audio_loudness__", "LoudnessMeter"]; await import("../src/index.js"); for (const k of banned) expect(Object.hasOwn(globalThis, k)).toBe(false); }); it("dependency set is limited to the declared workspace and zod", { timeout: 5000, }, () => { const pkg = JSON.parse( readFileSync(join(PKG_DIR, "package.json"), "utf8") ); const allowed = new Set([ "@suaveplan/audio-core", "@suaveplan/error", "@suaveplan/opentelemetry", "@suaveplan/types", "@opentelemetry/api", "tslib", "zod", ]); for (const dep of Object.keys(pkg.dependencies ?? {})) expect(allowed.has(dep)).toBe(true); }); it("source references no browser or Node globals", { timeout: 5000, }, () => { // Static-scan the source tree (dist is build output; not checked here). const srcGlobs = [ "src/meter/loudness-meter.ts", "src/algorithms/lufs.ts", "src/algorithms/true-peak.ts", "src/algorithms/loudness-range.ts", "src/filters/k-weighting.ts", "src/error-codes/error-codes.ts", "src/index.ts", ]; // Only match identifier-use occurrences (e.g. `window.fetch`, // `document.body`, `process.env`, `Buffer.from`), not the // English-prose words "window" or "document" inside doc-comments // and identifiers like `sourceLength`. const banned = [ /\bwindow\.\w/, /\bwindow\[/, /\bdocument\.\w/, /\bdocument\[/, /\bnavigator\.\w/, /\bAudioContext\b/, /\bAudioBuffer\b/, /\bAnalyserNode\b/, /\brequire\(["']node:fs["']\)/, /from\s+["']node:fs["']/, /from\s+["']node:path["']/, /\bprocess\.env/, /\bprocess\.argv/, /\bBuffer\.\w+/, ]; for (const rel of srcGlobs) { const src = readFileSync(join(PKG_DIR, rel), "utf8"); for (const re of banned) expect(re.test(src)).toBe(false); } }); });