gen-ui-runtime-assets.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { createHash } from "node:crypto";
  2. import { mkdtemp, readFile, rm } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { expect, it } from "vitest";
  6. import { copyGenUiRuntimeAssets } from "./gen-ui-runtime-assets.mjs";
  7. it("packages the same pinned runtime assets used by standalone export", async () => {
  8. const directory = await mkdtemp(join(tmpdir(), "gen-ui-assets-"));
  9. const source = new URL(
  10. "../../../apps/zcode-cli/packages/visualize-plugin/skills/visualize/assets/",
  11. import.meta.url,
  12. );
  13. const manifest = JSON.parse(await readFile(new URL("runtime-manifest.json", source), "utf8"));
  14. try {
  15. // 清单只承载当前资源的完整性数据,避免把无关的构建元数据带入分发包。
  16. expect(Object.keys(manifest)).toEqual(["files"]);
  17. expect(Object.keys(manifest.files).sort()).toEqual([
  18. "calendar.js",
  19. "tweak.js",
  20. "visualize.css",
  21. "visualize.html",
  22. ]);
  23. for (const entry of Object.values(manifest.files)) {
  24. expect(Object.keys(entry as object)).toEqual(["sha256"]);
  25. }
  26. await copyGenUiRuntimeAssets(directory);
  27. const vendors = JSON.parse(await readFile(join(directory, "vendor/manifest.json"), "utf8"));
  28. for (const resource of vendors.resources) {
  29. for (const [file, hash] of [
  30. [resource.file, resource.sha256],
  31. [resource.licenseFile, resource.licenseSha256],
  32. ]) {
  33. const bytes = await readFile(join(directory, "vendor", file));
  34. expect(bytes).toEqual(await readFile(new URL(`vendor/${file}`, source)));
  35. expect(createHash("sha256").update(bytes).digest("hex")).toBe(hash);
  36. }
  37. }
  38. for (const file of ["visualize.css", "visualize.html", "calendar.js"]) {
  39. const bytes = await readFile(join(directory, file));
  40. expect(bytes).toEqual(await readFile(new URL(file, source)));
  41. expect(createHash("sha256").update(bytes).digest("hex")).toBe(manifest.files[file].sha256);
  42. }
  43. const tweak = await readFile(
  44. new URL("../src/renderer/src/plugin-sandbox/genUiTweakRuntime.js", import.meta.url),
  45. );
  46. expect(createHash("sha256").update(tweak).digest("hex")).toBe(
  47. manifest.files["tweak.js"].sha256,
  48. );
  49. } finally {
  50. await rm(directory, { recursive: true, force: true });
  51. }
  52. });