chromedriverPreflight.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { tmpdir } from "node:os";
  4. import { afterEach, expect, it, vi } from "vitest";
  5. import {
  6. resolveChromedriverBuild,
  7. prepareChromedriver,
  8. cachedChromedriverPath,
  9. } from "../scripts/chromedriver-preflight.mjs";
  10. const roots: string[] = [];
  11. afterEach(async () => {
  12. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  13. });
  14. const chromiumVersion = "152.0.7977.130";
  15. it("目标平台精确驱动存在时直接使用,不查询其他补丁", async () => {
  16. const resolveBuild = vi.fn();
  17. expect(
  18. await resolveChromedriverBuild({
  19. chromiumVersion,
  20. platform: "mac_arm",
  21. canDownload: async () => true,
  22. resolveBuildId: resolveBuild,
  23. }),
  24. ).toBe(chromiumVersion);
  25. expect(resolveBuild).not.toHaveBeenCalled();
  26. });
  27. it("Linux 精确版本缺失时选择官方同一 build 的可用补丁", async () => {
  28. const canDownload = vi.fn(
  29. async ({ buildId }: { buildId: string }) => buildId === "152.0.7977.82",
  30. );
  31. const resolveBuildId = vi.fn(async () => "152.0.7977.82");
  32. expect(
  33. await resolveChromedriverBuild({
  34. chromiumVersion,
  35. platform: "linux",
  36. canDownload,
  37. resolveBuildId,
  38. }),
  39. ).toBe("152.0.7977.82");
  40. expect(resolveBuildId).toHaveBeenCalledWith("chromedriver", "linux", "152.0.7977");
  41. });
  42. it.each(["153.0.8000.1", "152.0.7999.1", undefined])(
  43. "不使用不匹配的查询结果 %s",
  44. async (candidate) => {
  45. await expect(
  46. resolveChromedriverBuild({
  47. chromiumVersion,
  48. platform: "linux",
  49. canDownload: async () => false,
  50. resolveBuildId: async () => candidate,
  51. }),
  52. ).rejects.toThrow("compatible ChromeDriver");
  53. },
  54. );
  55. it("候选补丁仍不可下载时失败", async () => {
  56. await expect(
  57. resolveChromedriverBuild({
  58. chromiumVersion,
  59. platform: "linux",
  60. canDownload: async () => false,
  61. resolveBuildId: async () => "152.0.7977.82",
  62. }),
  63. ).rejects.toThrow("compatible ChromeDriver");
  64. });
  65. it("缓存保存安装器真实路径,离线阶段验证版本与平台后读取,不猜精确补丁目录", async () => {
  66. const cacheDir = await mkdtemp(join(tmpdir(), "zcode-driver-"));
  67. roots.push(cacheDir);
  68. const executablePath = join(cacheDir, "actual-driver");
  69. await writeFile(executablePath, "fixture");
  70. const input = { cacheDir, electronVersion: "44.7.0", platform: "linux" };
  71. await prepareChromedriver({
  72. ...input,
  73. resolveBuild: async () => "152.0.7977.82",
  74. install: async () => ({ executablePath }),
  75. });
  76. expect(await cachedChromedriverPath(input)).toBe(executablePath);
  77. await expect(cachedChromedriverPath({ ...input, electronVersion: "41.0.3" })).rejects.toThrow();
  78. await expect(cachedChromedriverPath({ ...input, platform: "mac_arm" })).rejects.toThrow();
  79. const metadata = join(cacheDir, "zcode-chromedriver-44.7.0-linux.json");
  80. const record = JSON.parse(await readFile(metadata, "utf8"));
  81. await writeFile(metadata, JSON.stringify({ ...record, chromiumVersion: "153.0.8000.1" }));
  82. await expect(cachedChromedriverPath(input)).rejects.toThrow("cache identity mismatch");
  83. await writeFile(metadata, JSON.stringify(record));
  84. await rm(executablePath);
  85. await expect(cachedChromedriverPath(input)).rejects.toThrow();
  86. });
  87. it("安装失败不发布缓存", async () => {
  88. const cacheDir = await mkdtemp(join(tmpdir(), "zcode-driver-"));
  89. roots.push(cacheDir);
  90. const input = { cacheDir, electronVersion: "44.7.0", platform: "linux" };
  91. await expect(
  92. prepareChromedriver({
  93. ...input,
  94. resolveBuild: async () => "152.0.7977.82",
  95. install: async () => {
  96. throw new Error("download failed");
  97. },
  98. }),
  99. ).rejects.toThrow("download failed");
  100. await expect(cachedChromedriverPath(input)).rejects.toThrow();
  101. });
  102. it("缺少 Chromium 映射时不尝试下载不确定的驱动", async () => {
  103. const available = vi.fn(async () => true);
  104. await expect(
  105. resolveChromedriverBuild({
  106. chromiumVersion: undefined,
  107. platform: "linux",
  108. canDownload: available,
  109. }),
  110. ).rejects.toThrow("Chromium mapping unavailable");
  111. expect(available).not.toHaveBeenCalled();
  112. });