frame-contract-artifact.test.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { execFileSync } from "node:child_process";
  2. import { existsSync } from "node:fs";
  3. import { dirname, join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. // SG-02(!2071 review):模块边界重构的承重不变量是"发布/运行时解析路径与
  6. // 源码一致"——宿主直接消费 `@zcode/zcode-cua/frame-contract`,没有任何
  7. // workspace 链接可依赖。这里做产物级验证:
  8. // 1. 真实 node 子进程从 dist 产物加载子路径并核对导出面;
  9. // 2. staging 形态(只有 producer dist/ + package.json,无 node_modules)同样可加载。
  10. // The subpath export is ESM-only ("import" condition), so CJS require.resolve
  11. // cannot see it. Locate the pnpm-hoisted producer copy by walking up from this
  12. // test file to the workspace root.
  13. function findProducerPackageRoot(): string {
  14. let dir = import.meta.dirname;
  15. while (true) {
  16. const candidate = join(dir, "node_modules", "@zcode", "zcode-cua", "package.json");
  17. if (existsSync(candidate)) return dirname(candidate);
  18. const parent = dirname(dir);
  19. if (parent === dir) throw new Error("workspace root not found from " + import.meta.dirname);
  20. dir = parent;
  21. }
  22. }
  23. const producerPackageRoot = findProducerPackageRoot();
  24. const REQUIRED_EXPORTS = [
  25. "OFFICIAL_CUA_FRAME_INTEGRITY_META_KEY",
  26. "OFFICIAL_CUA_IMAGE_INLINE_BASE64_BYTES",
  27. "containsOfficialCuaImageRefCredentialText",
  28. "isOfficialCuaImageRefText",
  29. "parseOfficialCuaImageRef",
  30. "containsImageRefAuthority",
  31. "preserveOfficialCuaFrameResult",
  32. "attestOfficialCuaFrameContent",
  33. "findOfficialCuaFrameContentPair",
  34. "readRasterEnvelopeIdentity",
  35. ] as const;
  36. function runNodeImport(targetDir: string, specifier: string): string[] {
  37. const script = `import(${JSON.stringify(specifier)}).then((m) => console.log(JSON.stringify(Object.keys(m).sort())))`;
  38. const stdout = execFileSync(process.execPath, ["--input-type=module", "-e", script], {
  39. cwd: targetDir,
  40. encoding: "utf8",
  41. env: { ...process.env, NODE_PATH: "" },
  42. });
  43. return JSON.parse(stdout.trim().split("\n").pop()!) as string[];
  44. }
  45. describe("producer frame-contract artifact resolution (SG-02)", () => {
  46. it("loads the installed subpath and exposes the full contract surface", () => {
  47. const keys = runNodeImport(producerPackageRoot, "@zcode/zcode-cua/frame-contract");
  48. for (const name of REQUIRED_EXPORTS) {
  49. expect(keys, `missing export ${name}`).toContain(name);
  50. }
  51. });
  52. });