Files
ai-kit/test/cjs-condition.test.js
d0fddb42b0 feat: the package is @bitbaum/ai-kit — npm's typosquat guard owns the bare name (#25)
First-publish attempt returned E403: 'Package name too similar to existing
package aikit' — the unscoped name is permanently unpublishable, no auth or
token changes that. Scoped to the org that owns the repos: @bitbaum/ai-kit.
Self-referencing imports in src/tests and the README install line follow.
77/77 tests green under the new name; consumers' import-path updates ride
the Wave-7 pin swap, which touches those exact files anyway.


Claude-Session: https://claude.ai/code/session_01WqKqMnHQHSmkGFfc5t7Rxn

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-09-03 07:27:48 -07:00

46 lines
2.1 KiB
JavaScript

/**
* The grounding and registry subpaths must load under BOTH loaders.
*
* Why a require condition exists at all: OrangeCat's jest runs ts-jest in CJS
* mode, and its config documents the trap in its own words — the transform
* compiles only .ts/.tsx, so "an ESM-only .js dependency cannot be whitelisted
* into working via transformIgnorePatterns alone". That is the failure class
* that broke AOZ's Jest inside ai-forms in v0.3, and the v0.4 lesson applies
* unchanged: fix it in the package, not with per-adopter workarounds. A CJS
* consumer resolves `require` and gets dist-cjs; everything else resolves
* `default` and gets ESM dist, exactly as before.
*
* This test is the mutation-proof: delete the dist-cjs build or the marker
* package.json and the createRequire half fails; break the ESM build and the
* import half fails.
*/
import { test } from "node:test";
import assert from "node:assert/strict";
import { createRequire } from "node:module";
import * as esmGrounding from "@bitbaum/ai-kit/grounding";
import * as esmRegistry from "@bitbaum/ai-kit/registry";
const require = createRequire(import.meta.url);
test("ESM import serves the grounding surface", () => {
assert.equal(typeof esmGrounding.verifyAnswer, "function");
assert.equal(esmGrounding.NOT_RECORDED, "<not recorded>");
assert.equal(typeof esmRegistry.defineRegistry, "function");
});
test("CJS require serves the SAME surface through the require condition", () => {
const g = require("@bitbaum/ai-kit/grounding");
const r = require("@bitbaum/ai-kit/registry");
assert.equal(typeof g.verifyAnswer, "function");
assert.equal(g.NOT_RECORDED, "<not recorded>");
assert.equal(typeof r.defineRegistry, "function");
// Same behavior, not merely same names: both loaders must agree on a verdict.
const answer = "Your contact is Ilya Druzhnikov (UZH).";
const viaEsm = esmGrounding.verifyAnswer({ answer, facts: [], userMessage: "who?" });
const viaCjs = g.verifyAnswer({ answer, facts: [], userMessage: "who?" });
assert.equal(viaEsm.ok, viaCjs.ok);
assert.equal(viaEsm.ok, false);
});