preinstalled-tools.test.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import {
  2. mkdtemp,
  3. mkdir,
  4. readdir,
  5. rm,
  6. writeFile,
  7. } from "node:fs/promises";
  8. import { tmpdir } from "node:os";
  9. import { fileURLToPath } from "node:url";
  10. import { delimiter, join } from "node:path";
  11. import { describe, expect, it } from "vitest";
  12. import {
  13. preinstalledToolManifestSchema,
  14. preinstalledToolInstallationSchema,
  15. resolvePreinstalledToolsRelease,
  16. } from "@zcode/shared/preinstalled-tools";
  17. import { resolvePreinstalledToolPaths, resolvePreinstalledToolsBinDir } from "@zcode/shared/preinstalled-tools/node";
  18. import { NodeExecutionAdapter } from "../src/exec/node-execution-adapter.js";
  19. import type { ExecutionRequest } from "@zcode/contracts";
  20. const resource = {
  21. toolId: "libreoffice", resourceId: "libreoffice-26.2.0-darwin-arm64-r1", version: "26.2.0",
  22. platforms: [{ platform: "darwin", arch: "arm64" }], binaryArch: "arm64",
  23. archive: {
  24. format: "tar.gz", sizeBytes: 100, sha256: "a".repeat(64),
  25. path: "libreoffice/libreoffice-26.2.0-darwin-arm64-r1/libreoffice-26.2.0-darwin-arm64-r1.tar.gz",
  26. },
  27. downloadUrls: ["https://cdn.example.com/libreoffice.tar.gz"], installedSizeBytes: 200,
  28. entries: [{ name: "zcode-soffice", target: "LibreOffice.app/Contents/MacOS/soffice", args: [], env: [] }],
  29. requiredFiles: ["LibreOffice.app/Contents/MacOS/soffice"],
  30. runtimeChecks: [{ entry: "zcode-soffice", args: ["--version"], expectedOutput: "LibreOffice 26.2.0", timeoutMs: 30000 }],
  31. };
  32. describe("preinstalled document tool contract", () => {
  33. it("accepts a complete resource and rejects missing, mistyped and unsafe values", () => {
  34. expect(preinstalledToolManifestSchema.parse(resource)).toEqual(resource);
  35. for (const patch of [
  36. { platforms: [{ platform: "unknown", arch: "arm64" }] },
  37. { binaryArch: "ia32" },
  38. { archive: { ...resource.archive, sizeBytes: "100" } },
  39. { archive: { ...resource.archive, sha256: "bad" } },
  40. { archive: { ...resource.archive, path: "https://cdn.example.com/libreoffice.tar.gz" } },
  41. { entries: [{ ...resource.entries[0], target: "../outside" }] },
  42. { entries: [{ ...resource.entries[0], target: "C:/outside" }] },
  43. { requiredFiles: [] },
  44. { requiredFiles: ["not-the-entry"] },
  45. { runtimeChecks: [] },
  46. { downloadUrls: [] },
  47. ]) expect(preinstalledToolManifestSchema.safeParse({ ...resource, ...patch }).success).toBe(false);
  48. });
  49. it("validates the fixed per-tool record and keeps releases and tools isolated", () => {
  50. const dataRoot = join(tmpdir(), ".zcode");
  51. const paths = (["production", "preview", "development"] as const).map((release) =>
  52. resolvePreinstalledToolPaths({ dataRoot, release, toolId: "libreoffice" }),
  53. );
  54. expect(new Set(paths.map((path) => path.rootDir)).size).toBe(3);
  55. expect(paths[0]?.recordPath).toBe(join(dataRoot, "preinstalled-tools", "production", "tools", "libreoffice", "installation.json"));
  56. const other = resolvePreinstalledToolPaths({ dataRoot, release: "production", toolId: "qpdf" });
  57. expect(other.recordPath).not.toBe(paths[0]?.recordPath);
  58. expect(other.stagingDir).not.toBe(paths[0]?.stagingDir);
  59. expect(other.binDir).toBe(paths[0]?.binDir);
  60. expect(resolvePreinstalledToolsBinDir({ dataRoot, release: "production" })).toBe(paths[0]?.binDir);
  61. const record = {
  62. schemaVersion: 1, toolId: "libreoffice", resourceId: resource.resourceId, version: resource.version,
  63. platform: "darwin", arch: "arm64", installationDir: join(paths[0]!.versionsDir, resource.resourceId),
  64. installedSizeBytes: resource.installedSizeBytes, requiredFiles: resource.requiredFiles, entries: resource.entries,
  65. installedAt: "2026-10-28T00:00:00.000Z",
  66. };
  67. expect(preinstalledToolInstallationSchema.safeParse(record).success).toBe(true);
  68. for (const patch of [
  69. { entries: undefined }, { requiredFiles: [] }, { installationDir: "relative" }, { platform: "plan9" },
  70. { installationId: "f30db43a-5d2c-434b-a2a0-8297ee2fb45f" },
  71. ]) expect(preinstalledToolInstallationSchema.safeParse({ ...record, ...patch }).success).toBe(false);
  72. expect(resolvePreinstalledToolsRelease({ runtimeEnv: "development", productFlavor: "preview" })).toBe("development");
  73. expect(resolvePreinstalledToolsRelease({ runtimeEnv: "production", productFlavor: "preview" })).toBe("preview");
  74. expect(resolvePreinstalledToolsRelease({ runtimeEnv: "production", productFlavor: "production" })).toBe("production");
  75. });
  76. it.skipIf(process.platform === "win32")(
  77. "finds entries published and removed after Bash snapshot creation in the same session",
  78. async () => {
  79. const root = await mkdtemp(join(tmpdir(), "zcode-preinstalled-tools-"));
  80. const home = join(root, "中文 空格");
  81. const bin = join(home, "bin");
  82. await mkdir(bin, { recursive: true });
  83. const entry = join(bin, "zcode-soffice");
  84. const adapter = new NodeExecutionAdapter({ outputRootDir: root });
  85. const quote = (value: string) => "'" + value.replaceAll("'", "'\\''") + "'";
  86. const request: ExecutionRequest = {
  87. command: {
  88. mode: "shell", shellProfile: "posix-bash",
  89. shellOverride: { dialect: "posix", path: "/bin/bash", display: { name: "bash" }, source: "user-config" },
  90. command: 'printf "%s\\n%s" "$ZCODE_PREINSTALLED_TOOLS_AVAILABLE" "${PATH%%:*}"',
  91. },
  92. cwd: home,
  93. env: { set: { HOME: home, PATH: bin + delimiter + process.env.PATH, ZCODE_PREINSTALLED_TOOLS_AVAILABLE: "1" } },
  94. timeoutMs: 10000,
  95. trace: { sessionId: "preinstalled-tools", attributes: { toolCallId: "before-install" } },
  96. };
  97. try {
  98. const initial = await adapter.run(request);
  99. expect(initial.exitCode).toBe(0);
  100. expect(initial.stdout.text.trim()).toBe("1\n" + bin);
  101. const snapshots = await readdir(join(root, "shell-snapshots"));
  102. expect(snapshots.some((name) => name.endsWith(".sh"))).toBe(true);
  103. await writeFile(entry, '#!/bin/sh\nprintf "LibreOffice 26.8.1\\n"\n', { mode: 0o755 });
  104. for (const name of ["documents", "pdf", "presentations", "spreadsheets"]) {
  105. const helper = fileURLToPath(new URL(`../../${name}-plugin/scripts/zcode_libreoffice.py`, import.meta.url));
  106. const continued = await adapter.run({ ...request, command: { ...request.command,
  107. command: `command -v zcode-soffice; python3 ${quote(helper)} check` } });
  108. expect(continued.exitCode, continued.stdout.text + continued.stderr.text).toBe(0);
  109. expect(continued.stdout.text).toContain(entry);
  110. expect(JSON.parse(continued.stdout.text.split("\n").find((line) => line.startsWith("{"))!).source).toBe("managed");
  111. }
  112. await rm(entry);
  113. const removed = await adapter.run({ ...request, command: { ...request.command, command: 'command -v zcode-soffice || printf "ENTRY_REMOVED"' } });
  114. expect(removed.stdout.text.trim()).toBe("ENTRY_REMOVED");
  115. expect(await readdir(join(root, "shell-snapshots"))).toEqual(snapshots);
  116. } finally {
  117. await adapter.close();
  118. await rm(root, { force: true, recursive: true });
  119. }
  120. },
  121. );
  122. });