import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import { describe, it } from "node:test"; import { fileURLToPath } from "node:url"; import pkgRaw from "../../package.json" with { type: "json" }; interface IExportsEntry { browser?: string; node?: string; types?: string; default?: string; } interface IPackageJsonContract { exports?: Record; typesVersions?: Record>; } const pkg = pkgRaw as IPackageJsonContract; const __dirname = fileURLToPath(new URL(".", import.meta.url)); const packageSrcDir = path.resolve(__dirname, "../package"); const packageRootDir = path.resolve(__dirname, "../../"); const distDir = path.resolve(packageRootDir, "dist"); // eslint-disable-next-line security/detect-non-literal-fs-filename const isDistEmpty = !fs.existsSync(distDir) || fs.readdirSync(distDir).length === 0; const shouldCheckDist = process.env.CHECK_DIST_PUBLIC_API === "1"; const requiredSourceEntrypoints: Record = { ".": "index.ts", components: "components/index.ts", plugins: "plugins/index.ts", utils: "utils/index.ts", "utils/node": "utils/node/index.ts", "utils/react": "utils/react/index.ts", constants: "constants/index.ts", typings: "types/index.d.ts", }; describe("Public subpaths smoke (runtime, source)", () => { const importCases: { path: string; assertFn: (mod: Record) => void; }[] = [ { path: "./index.ts", assertFn: (mod) => { assert.ok("components" in mod, 'Expected root export "components"'); assert.ok("plugins" in mod, 'Expected root export "plugins"'); assert.ok("utils" in mod, 'Expected root export "utils"'); assert.ok("constants" in mod, 'Expected root export "constants"'); }, }, { path: "./components/index.ts", assertFn: (mod) => assert.ok("Btn" in mod, 'Expected components export "Btn"'), }, { path: "./components/btn/index.tsx", assertFn: (mod) => assert.ok("Btn" in mod, 'Expected component subpath export "Btn"'), }, { path: "./plugins/index.ts", assertFn: (mod) => assert.ok("Core" in mod, 'Expected plugins export "Core"'), }, { path: "./plugins/collection/index.ts", assertFn: (mod) => assert.ok("Collection" in mod, 'Expected plugin subpath export "Collection"'), }, { path: "./utils/index.ts", assertFn: (mod) => assert.ok("getCfg" in mod, 'Expected utils export "getCfg"'), }, { path: "./utils/react/index.ts", assertFn: (mod) => assert.ok("useCN" in mod, 'Expected react utils export "useCN"'), }, { path: "./constants/index.ts", assertFn: (mod) => assert.ok("constants" in mod, 'Expected constants export "constants"'), }, ]; importCases.forEach(({ path: target, assertFn }) => { it(`imports "${target}"`, async () => { const mod = await import(target); assert.ok(mod && typeof mod === "object", `Module "${target}" should resolve to an object`); assertFn(mod as Record); }); }); }); describe("Public subpaths smoke (typings)", () => { const map = pkg.typesVersions?.["*"] ?? {}; const required = [ ".", "components", "plugins", "utils", "utils/node", "utils/react", "constants", "typings", ]; it("keeps key typesVersions entries and files for core subpaths", () => { required.forEach((key) => { assert.ok(key in map, `Missing typesVersions key "${key}"`); const entries = map[key]; assert.ok(Array.isArray(entries) && entries.length > 0, `typesVersions "${key}" must have at least one path`); entries.forEach((entry) => { assert.ok(entry.startsWith("./dist/"), `typesVersions "${key}" path must point to dist: "${entry}"`); assert.ok(entry.endsWith(".d.mts"), `typesVersions "${key}" must point to .d.mts: "${entry}"`); }); }); }); it("has source entrypoints for required public subpaths", () => { Object.entries(requiredSourceEntrypoints).forEach(([ key, sourceEntry ]) => { const absPath = path.resolve(packageSrcDir, sourceEntry); // eslint-disable-next-line security/detect-non-literal-fs-filename assert.ok(fs.existsSync(absPath), `Missing source entrypoint for "${key}": "${sourceEntry}"`); }); }); it("keeps key exports mapped to types fields", () => { const exportsMap = pkg.exports ?? {}; const requiredExports = [ ".", "./components", "./plugins", "./utils", "./utils/node", "./utils/react", "./constants", "./typings", ]; requiredExports.forEach((key) => { assert.ok(key in exportsMap, `Missing export key "${key}"`); const value = exportsMap[key]; assert.ok(typeof value === "object" && !!value, `Export "${key}" must be an object`); assert.ok(typeof (value as IExportsEntry).types === "string", `Export "${key}" must define "types"`); }); }); it("keeps exports pointing to dist artifacts by contract", () => { const exportsMap = pkg.exports ?? {}; Object.entries(exportsMap).forEach(([ key, value ]) => { if (typeof value !== "object" || !value) { return; } const typedValue = value as IExportsEntry; if (typedValue.types) { assert.ok(typedValue.types.startsWith("./dist/"), `Export "${key}" types path must point to dist`); assert.ok(typedValue.types.endsWith(".d.mts"), `Export "${key}" types path must end with .d.mts`); } if (typedValue.default) { assert.ok(typedValue.default.startsWith("./dist/"), `Export "${key}" default path must point to dist`); } [ typedValue.browser, typedValue.node ].forEach((target) => { if (target) { assert.ok(target.startsWith("./dist/"), `Export "${key}" conditional path must point to dist`); } }); }); }); it("publishes explicit scoped style entrypoints", () => { const exportsMap = pkg.exports ?? {}; assert.ok("./styles/scoped" in exportsMap); assert.ok("./styles/scoped/components/*" in exportsMap); }); it("keeps non-pattern export targets resolvable", { skip: !shouldCheckDist || isDistEmpty }, () => { const exportsMap = pkg.exports ?? {}; Object.entries(exportsMap).forEach(([ key, value ]) => { if (typeof value !== "object" || !value) { return; } Object.entries(value).forEach(([ condition, target ]) => { if (typeof target === "string" && !target.includes("*")) { const targetPath = path.resolve(packageRootDir, target); // eslint-disable-next-line security/detect-non-literal-fs-filename assert.ok(fs.existsSync(targetPath), `Export "${key}" condition "${condition}" points to missing file "${target}"`); } }); }); }); it("resolves package root for local source checks", () => { // eslint-disable-next-line security/detect-non-literal-fs-filename assert.ok(fs.existsSync(packageRootDir), "Package root must exist"); }); });