sharpComputerUseRequirement.test.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import { isSharpRequiredByComputerUse } from "../scripts/sharp-package-assets.mjs";
  6. const roots: string[] = [];
  7. function createWorkspace(cuaManifest: Record<string, unknown> | null, location: "root" | "desktop") {
  8. const root = mkdtempSync(join(tmpdir(), "zcode-sharp-cua-"));
  9. roots.push(root);
  10. const desktopRoot = join(root, "packages", "desktop");
  11. mkdirSync(desktopRoot, { recursive: true });
  12. if (cuaManifest) {
  13. const base = location === "root" ? root : desktopRoot;
  14. const cuaDir = join(base, "node_modules", "@zcode", "zcode-cua");
  15. mkdirSync(cuaDir, { recursive: true });
  16. writeFileSync(join(cuaDir, "package.json"), JSON.stringify(cuaManifest));
  17. }
  18. return desktopRoot;
  19. }
  20. afterEach(() => {
  21. while (roots.length) rmSync(roots.pop()!, { recursive: true, force: true });
  22. });
  23. describe("isSharpRequiredByComputerUse", () => {
  24. it("真实 Computer Use 包声明 sharp 时需要暂存 sharp", () => {
  25. const desktopRoot = createWorkspace(
  26. { name: "@zcode/zcode-cua", dependencies: { sharp: "^0.34.0" } },
  27. "root",
  28. );
  29. expect(isSharpRequiredByComputerUse({ desktopPackageRoot: desktopRoot })).toBe(true);
  30. });
  31. it("不声明 sharp 的占位包不需要 sharp,优先读取离桌面包最近的安装", () => {
  32. const desktopRoot = createWorkspace({ name: "@zcode/zcode-cua", version: "0.6.3" }, "desktop");
  33. expect(isSharpRequiredByComputerUse({ desktopPackageRoot: desktopRoot })).toBe(false);
  34. });
  35. it("找不到 Computer Use 包时不需要 sharp", () => {
  36. const desktopRoot = createWorkspace(null, "root");
  37. expect(isSharpRequiredByComputerUse({ desktopPackageRoot: desktopRoot })).toBe(false);
  38. });
  39. });