projections-barrel.test.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { existsSync, readFileSync } from "node:fs";
  2. import { dirname, join, resolve } from "node:path";
  3. import { fileURLToPath } from "node:url";
  4. import { describe, expect, it } from "vitest";
  5. // `@zcode/dynamic-workflow/projections` 是给作品集浏览器包用的纯投影桶
  6. // (docs/chat/workflow-portfolio.md):站内在冻结的 core.json 上现场重算投影,整条链不能把
  7. // `typescript`(分析器铸造 core 时才用的编译器)拖进前端 bundle。这里不靠打包器报错来发现——
  8. // 直接静态遍历 dist/projections.js 的 ESM import 闭包(tsc 已经擦掉了 type-only import,
  9. // 所以 dist 里剩下的每条 `from "…"` 都是运行时依赖),断言可达文件里没有一条 import 裸
  10. // 说明符 "typescript"。任何一次把投影模块接到 causality-order.ts 之类带编译器的模块上的
  11. // 改动,都会在这里第一时间炸出来,并列出泄漏的文件。
  12. //
  13. // 依赖 dist:turbo 的 test 任务 dependsOn build;在包目录里裸跑 `pnpm test` 前请先
  14. // `pnpm run build`,否则这里检查的是上一次构建的产物。
  15. const distDir = resolve(dirname(fileURLToPath(import.meta.url)), "..", "dist");
  16. const ENTRY = join(distDir, "projections.js");
  17. /** 每条静态 import / re-export 的说明符(含 `export … from`),side-effect import 也算。 */
  18. const SPECIFIER = /(?:^|\n)\s*(?:import|export)\b[^;]*?\bfrom\s*["']([^"']+)["']|(?:^|\n)\s*import\s*["']([^"']+)["']/g;
  19. function specifiersOf(file: string): string[] {
  20. const source = readFileSync(file, "utf8");
  21. const out: string[] = [];
  22. for (const match of source.matchAll(SPECIFIER)) out.push((match[1] ?? match[2]) as string);
  23. return out;
  24. }
  25. /** 可达的 dist 文件与其中出现的裸(非相对)说明符,按发现文件归类。 */
  26. function walk(entry: string): { files: string[]; bare: Map<string, string[]> } {
  27. const files: string[] = [];
  28. const bare = new Map<string, string[]>();
  29. const queue = [entry];
  30. const seen = new Set<string>();
  31. while (queue.length > 0) {
  32. const file = queue.shift() as string;
  33. if (seen.has(file)) continue;
  34. seen.add(file);
  35. files.push(file);
  36. for (const spec of specifiersOf(file)) {
  37. if (spec.startsWith(".") || spec.startsWith("/")) {
  38. queue.push(resolve(dirname(file), spec));
  39. } else {
  40. const list = bare.get(spec) ?? [];
  41. list.push(file);
  42. bare.set(spec, list);
  43. }
  44. }
  45. }
  46. return { bare, files };
  47. }
  48. describe("projections barrel is browser-safe", () => {
  49. it("dist/projections.js exists (run `pnpm run build` first)", () => {
  50. expect(existsSync(ENTRY)).toBe(true);
  51. });
  52. it("no reachable dist file imports typescript (or any other bare specifier)", () => {
  53. const { bare, files } = walk(ENTRY);
  54. // 桶本身加上它再导出的投影模块——闭包不能退化成只有入口文件。
  55. expect(files.length).toBeGreaterThan(5);
  56. const leaks = [...bare.entries()].map(([spec, importers]) => `${spec} <- ${importers.join(", ")}`);
  57. expect(leaks, "bare imports reachable from the browser barrel").toEqual([]);
  58. expect(bare.has("typescript")).toBe(false);
  59. // 反向校验:走同一套遍历,根导出必然到达 typescript——否则说明遍历本身失效。
  60. expect(walk(join(distDir, "index.js")).bare.has("typescript")).toBe(true);
  61. });
  62. it("does not reach the analyzer front-end modules", () => {
  63. const { files } = walk(ENTRY);
  64. const names = files.map((file) => file.slice(distDir.length + 1));
  65. for (const forbidden of ["analysis/causality-order.js", "analysis/analyze.js", "compiler/compile.js", "analysis/domain.js"]) {
  66. expect(names, `${forbidden} must not be reachable`).not.toContain(forbidden);
  67. }
  68. });
  69. });