build.test.mjs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import assert from "node:assert/strict";
  2. import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join, resolve } from "node:path";
  5. import test from "node:test";
  6. import { runInNewContext } from "node:vm";
  7. import { build } from "esbuild";
  8. import {
  9. assertZodBundleIdentity,
  10. createZodDedupePlugin,
  11. readRootPackageVersion,
  12. resolveBuildAliases,
  13. resolveBuildOptions,
  14. resolveBuildExternal,
  15. } from "../scripts/build.mjs";
  16. async function withZodFixture(run) {
  17. const directory = await mkdtemp(join(tmpdir(), "zcode-zod-bundle-"));
  18. try {
  19. for (const [consumer, version] of [
  20. ["first", "4.6.5"],
  21. ["second", "4.6.5"],
  22. ["legacy", "3.25.76"],
  23. ]) {
  24. const packageRoot = join(directory, consumer, "node_modules", "zod");
  25. await mkdir(packageRoot, { recursive: true });
  26. await writeFile(
  27. join(packageRoot, "package.json"),
  28. JSON.stringify({
  29. name: "zod",
  30. version,
  31. type: "module",
  32. exports: {
  33. ".": { import: "./index.js", require: "./index.cjs" },
  34. "./v3": "./v3.js",
  35. },
  36. }),
  37. );
  38. await writeFile(
  39. join(packageRoot, "index.js"),
  40. `export const schema = {version: ${JSON.stringify(version)}, format: "esm"};`,
  41. );
  42. await writeFile(
  43. join(packageRoot, "index.cjs"),
  44. `exports.schema = {version: ${JSON.stringify(version)}, format: "cjs"};`,
  45. );
  46. await writeFile(join(packageRoot, "v3.js"), 'export const schema = {format: "compat-v3"};');
  47. await writeFile(
  48. join(directory, consumer, "entry.mjs"),
  49. 'export {schema} from "zod"; export {schema as compat} from "zod/v3";',
  50. );
  51. await writeFile(join(directory, consumer, "entry.cjs"), 'module.exports = require("zod");');
  52. }
  53. const entry = join(directory, "entry.mjs");
  54. await writeFile(
  55. entry,
  56. `
  57. import {schema as first, compat as firstCompat} from "./first/entry.mjs";
  58. import {schema as second, compat as secondCompat} from "./second/entry.mjs";
  59. import {schema as legacy} from "./legacy/entry.mjs";
  60. import cjsFirst from "./first/entry.cjs";
  61. import cjsSecond from "./second/entry.cjs";
  62. export {first, second, legacy, firstCompat, secondCompat, cjsFirst, cjsSecond};
  63. `,
  64. );
  65. const options = {
  66. absWorkingDir: directory,
  67. entryPoints: [entry],
  68. bundle: true,
  69. write: false,
  70. metafile: true,
  71. platform: "node",
  72. format: "cjs",
  73. logLevel: "silent",
  74. };
  75. await run({ directory, options });
  76. } finally {
  77. await rm(directory, { recursive: true, force: true });
  78. }
  79. }
  80. test("Zod dedupe shares equal versions while preserving majors, subpaths and export conditions", async () => {
  81. await withZodFixture(async ({ options }) => {
  82. const result = await build({
  83. ...options,
  84. plugins: [createZodDedupePlugin({ expectedV4Version: "4.6.5" })],
  85. });
  86. const context = { module: { exports: {} } };
  87. runInNewContext(result.outputFiles[0].text, context);
  88. const { first, second, legacy, firstCompat, secondCompat, cjsFirst, cjsSecond } =
  89. context.module.exports;
  90. assert.equal(first, second);
  91. assert.equal(firstCompat, secondCompat);
  92. assert.equal(firstCompat.format, "compat-v3");
  93. assert.notEqual(first, firstCompat);
  94. assert.notEqual(first, legacy);
  95. assert.equal(legacy.version, "3.25.76");
  96. assert.equal(cjsFirst.schema, cjsSecond.schema);
  97. assert.notEqual(first, cjsFirst.schema);
  98. assert.equal(first.format, "esm");
  99. assert.equal(cjsFirst.schema.format, "cjs");
  100. });
  101. });
  102. test("Zod metafile gate rejects duplicate physical packages even when versions match", async () => {
  103. await withZodFixture(async ({ directory, options }) => {
  104. const result = await build(options);
  105. await assert.rejects(
  106. assertZodBundleIdentity(result.metafile, {
  107. workingDirectory: directory,
  108. expectedV4Version: "4.6.5",
  109. }),
  110. /Duplicate Zod 4\.6\.5/,
  111. );
  112. });
  113. });
  114. test("Zod bundle rejects an unexpected v4 version without rewriting it across versions", async () => {
  115. await withZodFixture(async ({ options }) => {
  116. await assert.rejects(
  117. build({ ...options, plugins: [createZodDedupePlugin({ expectedV4Version: "4.4.3" })] }),
  118. /Expected Zod v4 4\.4\.3/,
  119. );
  120. });
  121. });
  122. test("Zod resolver preserves missing-export failures", async () => {
  123. await withZodFixture(async ({ directory, options }) => {
  124. await writeFile(join(directory, "first", "entry.mjs"), 'export * from "zod/not-exported";');
  125. await assert.rejects(
  126. build({ ...options, plugins: [createZodDedupePlugin({ expectedV4Version: "4.6.5" })] }),
  127. /zod\/not-exported/,
  128. );
  129. });
  130. });
  131. test("reads CLI build version from the repository root package", async () => {
  132. const directory = await mkdtemp(join(tmpdir(), "zcode-root-version-"));
  133. try {
  134. await mkdir(join(directory, "packages", "cli"), {
  135. recursive: true,
  136. });
  137. await writeFile(join(directory, "package.json"), JSON.stringify({ version: "9.8.7" }));
  138. await writeFile(
  139. join(directory, "packages", "cli", "package.json"),
  140. JSON.stringify({ version: "1.2.3" }),
  141. );
  142. assert.equal(
  143. await readRootPackageVersion({
  144. root: directory,
  145. }),
  146. "9.8.7",
  147. );
  148. } finally {
  149. await rm(directory, {
  150. force: true,
  151. recursive: true,
  152. });
  153. }
  154. });
  155. test("rejects missing root package versions during CLI builds", async () => {
  156. const directory = await mkdtemp(join(tmpdir(), "zcode-root-version-"));
  157. try {
  158. await writeFile(join(directory, "package.json"), JSON.stringify({ name: "zcode-cli" }));
  159. await assert.rejects(
  160. readRootPackageVersion({
  161. root: directory,
  162. }),
  163. /Root package\.json must define a non-empty string version/,
  164. );
  165. } finally {
  166. await rm(directory, {
  167. force: true,
  168. recursive: true,
  169. });
  170. }
  171. });
  172. test("desktop agent builds minify the app-facing JS bundle", () => {
  173. assert.deepEqual(resolveBuildOptions([]), {
  174. minify: false,
  175. sourcemap: true,
  176. });
  177. assert.deepEqual(resolveBuildOptions(["--desktop-agent"]), {
  178. minify: true,
  179. sourcemap: false,
  180. });
  181. assert.deepEqual(resolveBuildOptions(["--desktop-agent"], { ZCODE_E2E_COVERAGE: "1" }), {
  182. minify: false,
  183. sourcemap: true,
  184. });
  185. });
  186. test("CLI bundle externalizes native koffi so esbuild does not parse platform .node files", () => {
  187. assert.ok(resolveBuildExternal().includes("koffi"));
  188. });
  189. test("Desktop agent build keeps Node-only shared subpaths ahead of the generic alias", () => {
  190. const rootDirectory = resolve("/repo/apps/zcode-cli");
  191. const cliDirectory = resolve(rootDirectory, "packages/cli");
  192. const aliases = resolveBuildAliases({ cliDirectory, rootDirectory });
  193. assert.equal(
  194. aliases["@zcode/shared/workspace-hook-discovery"],
  195. resolve(rootDirectory, "../../packages/shared/src/workspace-hook-discovery.ts"),
  196. );
  197. assert.equal(
  198. aliases["@zcode/shared"],
  199. resolve(rootDirectory, "../../packages/shared/src/index.ts"),
  200. );
  201. assert.ok(
  202. Object.keys(aliases).indexOf("@zcode/shared/workspace-hook-discovery") <
  203. Object.keys(aliases).indexOf("@zcode/shared"),
  204. );
  205. });
  206. test("monotonicity subpath resolves ahead of the generic shared alias", () => {
  207. const rootDirectory = resolve("/repo/apps/zcode-cli");
  208. const aliases = resolveBuildAliases({ rootDirectory });
  209. assert.equal(
  210. aliases["@zcode/shared/workspace-hook-review-monotonicity"],
  211. resolve(rootDirectory, "../../packages/shared/src/workspace-hook-review-monotonicity.ts"),
  212. );
  213. assert.ok(
  214. Object.keys(aliases).indexOf("@zcode/shared/workspace-hook-review-monotonicity") <
  215. Object.keys(aliases).indexOf("@zcode/shared"),
  216. );
  217. });
  218. test("mutation subpath resolves ahead of the generic shared alias", () => {
  219. const rootDirectory = resolve("/repo/apps/zcode-cli");
  220. const aliases = resolveBuildAliases({ rootDirectory });
  221. assert.equal(
  222. aliases["@zcode/shared/workspace-hook-mutation"],
  223. resolve(rootDirectory, "../../packages/shared/src/workspace-hook-mutation.ts"),
  224. );
  225. assert.ok(
  226. Object.keys(aliases).indexOf("@zcode/shared/workspace-hook-mutation") <
  227. Object.keys(aliases).indexOf("@zcode/shared"),
  228. );
  229. });
  230. test("model-selection subpath resolves ahead of the generic shared alias", () => {
  231. const rootDirectory = resolve("/repo/apps/zcode-cli");
  232. const aliases = resolveBuildAliases({ rootDirectory });
  233. assert.equal(
  234. aliases["@zcode/shared/model-selection"],
  235. resolve(rootDirectory, "../../packages/shared/src/model-selection.ts"),
  236. );
  237. assert.ok(
  238. Object.keys(aliases).indexOf("@zcode/shared/model-selection") <
  239. Object.keys(aliases).indexOf("@zcode/shared"),
  240. );
  241. });
  242. test("model configuration schema subpaths resolve before the generic shared alias", () => {
  243. const rootDirectory = resolve("/repo/apps/zcode-cli");
  244. const aliases = resolveBuildAliases({ rootDirectory });
  245. for (const subpath of ["model-config", "config-schema", "process-diagnostic"]) {
  246. const name = `@zcode/shared/${subpath}`;
  247. assert.equal(aliases[name], resolve(rootDirectory, `../../packages/shared/src/${subpath}.ts`));
  248. assert.ok(Object.keys(aliases).indexOf(name) < Object.keys(aliases).indexOf("@zcode/shared"));
  249. }
  250. });
  251. test("built CLI embeds the compact registry without a runtime Fig dependency", async (t) => {
  252. let bundle;
  253. try {
  254. bundle = await readFile(new URL("../dist/zcode.cjs", import.meta.url), "utf8");
  255. } catch {
  256. t.skip("build the CLI before running the bundle content gate");
  257. return;
  258. }
  259. assert.match(bundle, /"@commercelayer\/cli"\s*:/);
  260. assert.match(bundle, /(?:"git"|git)\s*:\s*\[\["git"\]/);
  261. assert.doesNotMatch(bundle, /node_modules\/@withfig\/autocomplete/);
  262. assert.doesNotMatch(bundle, /@withfig\/autocomplete\/build/);
  263. });
  264. test("request security subpath bundles through its edition entry", async () => {
  265. const result = await build({
  266. stdin: {
  267. contents: 'import * as security from "@zcode/shared/request-security"; export { security };',
  268. resolveDir: resolve(import.meta.dirname, ".."),
  269. },
  270. alias: resolveBuildAliases(),
  271. bundle: true,
  272. platform: "node",
  273. format: "esm",
  274. write: false,
  275. metafile: true,
  276. });
  277. assert.ok(
  278. Object.keys(result.metafile.inputs).some((path) =>
  279. path.endsWith("shared/src/request-security-edition/index.ts"),
  280. ),
  281. );
  282. });