catalog.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { describe, expect, it } from "vitest";
  2. import { readFile } from "node:fs/promises";
  3. import {
  4. preinstalledToolsCatalogSchema,
  5. resolvePreinstalledToolsCatalog,
  6. } from "@zcode/shared/preinstalled-tools";
  7. const resource = {
  8. toolId: "qpdf",
  9. resourceId: "qpdf-12.3-darwin-arm64-r1",
  10. version: "12.3",
  11. platforms: [{ platform: "darwin", arch: "arm64" }],
  12. binaryArch: "arm64",
  13. archive: {
  14. format: "tar.gz",
  15. sizeBytes: 100,
  16. sha256: "a".repeat(64),
  17. path: "qpdf/qpdf-12.3-darwin-arm64-r1/qpdf-12.3-darwin-arm64-r1.tar.gz",
  18. },
  19. installedSizeBytes: 200,
  20. requiredFiles: ["bin/qpdf"],
  21. entries: [{ name: "qpdf", target: "bin/qpdf", args: [], env: [] }],
  22. runtimeChecks: [
  23. { entry: "qpdf", args: ["--version"], expectedOutput: "qpdf version 12.3", timeoutMs: 30000 },
  24. ],
  25. };
  26. const tool = { toolId: "qpdf", displayName: "qpdf", resources: [resource] };
  27. describe("published built-in resource catalog", () => {
  28. it("ships the 15 published archives with canonical relative paths", async () => {
  29. const catalog = preinstalledToolsCatalogSchema.parse(
  30. JSON.parse(
  31. await readFile(
  32. new URL("../../packages/desktop/src/preinstalled-tools/catalog.json", import.meta.url),
  33. "utf8",
  34. ),
  35. ),
  36. );
  37. expect(catalog.map((entry) => entry.toolId)).toEqual([
  38. "python",
  39. "qpdf",
  40. "pandoc",
  41. "poppler",
  42. "libreoffice",
  43. ]);
  44. expect(catalog.flatMap((entry) => entry.resources)).toHaveLength(15);
  45. for (const entry of catalog)
  46. for (const resource of entry.resources) {
  47. expect(resource.archive.path).toBe(
  48. `${entry.toolId}/${resource.resourceId}/${resource.resourceId}.tar.gz`,
  49. );
  50. }
  51. for (const [platform, arch] of [
  52. ["darwin", "arm64"],
  53. ["darwin", "x64"],
  54. ["win32", "x64"],
  55. ]) {
  56. const { tools, downloadBaseUrlsError } = resolvePreinstalledToolsCatalog(
  57. catalog,
  58. platform!,
  59. arch!,
  60. ["https://cdn.example.com/zcode/preinstalled-tools"],
  61. );
  62. expect(downloadBaseUrlsError).toBeUndefined();
  63. expect(tools.every((item) => item.manifest !== null)).toBe(true);
  64. }
  65. expect(
  66. resolvePreinstalledToolsCatalog(catalog, "win32", "arm64", ["https://cdn.example.com"])
  67. .tools.filter((item) => item.manifest)
  68. .map((item) => item.toolId),
  69. ).toEqual(["pandoc", "poppler"]);
  70. for (const arch of ["x64", "arm64"]) {
  71. expect(
  72. resolvePreinstalledToolsCatalog(catalog, "linux", arch, [
  73. "https://cdn.example.com",
  74. ]).tools.every((item) => item.manifest === null),
  75. ).toBe(true);
  76. }
  77. });
  78. });
  79. describe("preinstalled resource catalog publication", () => {
  80. it("accepts unpublished tools without download targets", () => {
  81. expect(
  82. preinstalledToolsCatalogSchema.safeParse([
  83. tool,
  84. { toolId: "python", displayName: "Python", resources: [] },
  85. ]).success,
  86. ).toBe(true);
  87. });
  88. it("rejects duplicate tool identities", () => {
  89. expect(preinstalledToolsCatalogSchema.safeParse([tool, tool]).success).toBe(false);
  90. });
  91. it("rejects overlapping platform resources and entry names across tools", () => {
  92. expect(
  93. preinstalledToolsCatalogSchema.safeParse([
  94. { ...tool, resources: [resource, { ...resource, resourceId: "qpdf-other" }] },
  95. ]).success,
  96. ).toBe(false);
  97. expect(
  98. preinstalledToolsCatalogSchema.safeParse([
  99. tool,
  100. { toolId: "python", displayName: "Python", resources: [{ ...resource, toolId: "python" }] },
  101. ]).success,
  102. ).toBe(false);
  103. });
  104. it("rejects missing entry targets, foreign runtime checks, unsafe paths and full URLs", () => {
  105. for (const broken of [
  106. { ...resource, requiredFiles: ["other"] },
  107. { ...resource, entries: [...resource.entries, ...resource.entries] },
  108. { ...resource, runtimeChecks: [{ ...resource.runtimeChecks[0], entry: "foreign" }] },
  109. { ...resource, downloadUrls: ["https://explicit.example.com/qpdf.tar.gz"] },
  110. {
  111. ...resource,
  112. archive: {
  113. ...resource.archive,
  114. downloadUrls: ["https://explicit.example.com/qpdf.tar.gz"],
  115. },
  116. },
  117. {
  118. ...resource,
  119. archive: {
  120. ...resource.archive,
  121. downloadUrlsByRegion: {
  122. domestic: ["https://cn.example.com/qpdf.tar.gz"],
  123. overseas: ["https://global.example.com/qpdf.tar.gz"],
  124. },
  125. },
  126. },
  127. ...[
  128. "https://cdn.example.com/qpdf.tar.gz",
  129. "/qpdf.tar.gz",
  130. "../qpdf.tar.gz",
  131. "qpdf\\a.tar.gz",
  132. ].map((path) => ({ ...resource, archive: { ...resource.archive, path } })),
  133. { ...resource, archive: { ...resource.archive, path: undefined } },
  134. { ...resource, toolId: "python" },
  135. ])
  136. expect(
  137. preinstalledToolsCatalogSchema.safeParse([{ ...tool, resources: [broken] }]).success,
  138. ).toBe(false);
  139. });
  140. it("allows one tool's entry on distinct native and emulated platforms", () => {
  141. const windows = {
  142. ...resource,
  143. resourceId: "qpdf-win-x64-r1",
  144. platforms: [
  145. { platform: "win32", arch: "x64" },
  146. { platform: "win32", arch: "arm64" },
  147. ],
  148. binaryArch: "x64",
  149. };
  150. expect(
  151. preinstalledToolsCatalogSchema.safeParse([{ ...tool, resources: [resource, windows] }])
  152. .success,
  153. ).toBe(true);
  154. });
  155. it("accepts declared installation rewrites and rejects unsafe paths or placeholders", () => {
  156. const rewrite = [{ path: "lib/data.bin", placeholder: "/build/prefix", mode: "binary" }];
  157. expect(
  158. preinstalledToolsCatalogSchema.parse([{ ...tool, resources: [{ ...resource, rewrite }] }])[0]
  159. ?.resources[0]?.rewrite,
  160. ).toEqual(rewrite);
  161. for (const item of [
  162. { ...rewrite[0], path: "../outside" },
  163. { ...rewrite[0], path: "lib\\data.bin" },
  164. { ...rewrite[0], placeholder: "" },
  165. { ...rewrite[0], placeholder: "bad\0prefix" },
  166. { ...rewrite[0], mode: "script" },
  167. ])
  168. expect(
  169. preinstalledToolsCatalogSchema.safeParse([
  170. { ...tool, resources: [{ ...resource, rewrite: [item] }] },
  171. ]).success,
  172. ).toBe(false);
  173. });
  174. });
  175. it("accepts directory-prefix rewrites on POSIX resources but rejects Windows declarations", () => {
  176. const rewrite = [
  177. { path: "lib/data.bin", placeholder: "/build/prefix", mode: "binary-dir-prefix" },
  178. ];
  179. expect(
  180. preinstalledToolsCatalogSchema.parse([{ ...tool, resources: [{ ...resource, rewrite }] }])[0]
  181. ?.resources[0]?.rewrite,
  182. ).toEqual(rewrite);
  183. expect(
  184. preinstalledToolsCatalogSchema.safeParse([
  185. {
  186. ...tool,
  187. resources: [{ ...resource, rewrite, platforms: [{ platform: "win32", arch: "x64" }] }],
  188. },
  189. ]).success,
  190. ).toBe(false);
  191. });
  192. describe("download address assembly", () => {
  193. const catalog = [
  194. {
  195. ...tool,
  196. resources: [{ ...resource, archive: { ...resource.archive, path: "qpdf/v1+b/a b.tar.gz" } }],
  197. },
  198. ];
  199. it("joins the encoded path onto each base in order and drops repeated candidates", () => {
  200. const {
  201. tools: [configuration],
  202. downloadBaseUrlsError,
  203. } = resolvePreinstalledToolsCatalog(catalog, "darwin", "arm64", [
  204. "https://cn.example.com/zcode/preinstalled-tools/",
  205. "http://127.0.0.1:8080",
  206. "https://cn.example.com/zcode/preinstalled-tools",
  207. ]);
  208. expect(downloadBaseUrlsError).toBeUndefined();
  209. expect(configuration?.manifest?.downloadUrls).toEqual([
  210. "https://cn.example.com/zcode/preinstalled-tools/qpdf/v1%2Bb/a%20b.tar.gz",
  211. "http://127.0.0.1:8080/qpdf/v1%2Bb/a%20b.tar.gz",
  212. ]);
  213. expect(
  214. resolvePreinstalledToolsCatalog(catalog, "linux", "x64", ["https://cn.example.com"]).tools[0]
  215. ?.manifest,
  216. ).toBeNull();
  217. });
  218. // 基址写错曾让整个装配失败,Main 以空列表交给预装进程,已安装资源被当作移出 catalog 删除。
  219. it("keeps every tool without a target for missing or non-HTTP bases instead of falling back", () => {
  220. for (const bases of [
  221. [],
  222. ["file:///tmp/tools"],
  223. ["not-a-url"],
  224. ["cdn.example.com/zcode/preinstalled-tools"],
  225. ["https://cn.example.com", "ftp://x.example.com"],
  226. ]) {
  227. const { tools, downloadBaseUrlsError } = resolvePreinstalledToolsCatalog(
  228. catalog,
  229. "darwin",
  230. "arm64",
  231. bases,
  232. );
  233. expect(tools).toEqual([{ toolId: "qpdf", displayName: "qpdf", manifest: null }]);
  234. expect(downloadBaseUrlsError).toBeDefined();
  235. }
  236. });
  237. it("still rejects an invalid catalog even when bases are invalid", () => {
  238. expect(() =>
  239. resolvePreinstalledToolsCatalog([tool, tool], "darwin", "arm64", ["not-a-url"]),
  240. ).toThrow();
  241. });
  242. });