cdnPreloadScript.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import { execFileSync } from "node:child_process";
  2. import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
  3. import { tmpdir } from "node:os";
  4. import { dirname, join, resolve } from "node:path";
  5. import { fileURLToPath } from "node:url";
  6. import { afterEach, describe, expect, it } from "vitest";
  7. // cdn-preload.sh 是纯 bash 脚本,Windows 上没有原生 bash 环境(或行为不一致),跳过整个测试套件。
  8. const isWindows = process.platform === "win32";
  9. const tempDirs: string[] = [];
  10. const TEST_FILE_DIR = dirname(fileURLToPath(import.meta.url));
  11. const REPO_ROOT = resolve(TEST_FILE_DIR, "../../..");
  12. function makeTempDir(prefix: string) {
  13. const dir = mkdtempSync(join(tmpdir(), prefix));
  14. tempDirs.push(dir);
  15. return dir;
  16. }
  17. function writeExecutable(filePath: string, content: string) {
  18. writeFileSync(filePath, content, { encoding: "utf-8", mode: 0o755 });
  19. }
  20. afterEach(() => {
  21. for (const dir of tempDirs.splice(0)) {
  22. rmSync(dir, { recursive: true, force: true });
  23. }
  24. });
  25. describe.skipIf(isWindows)("cdn-preload.sh", () => {
  26. it("默认预热 upload 阶段写出的所有有效文件大小记录", () => {
  27. const workspaceDir = makeTempDir("cdn-preload-workspace-");
  28. const binDir = join(workspaceDir, "bin");
  29. const aliyunLogPath = join(workspaceDir, "aliyun.log");
  30. const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
  31. mkdirSync(binDir, { recursive: true });
  32. writeFileSync(
  33. cdnUrlsIn,
  34. [
  35. "https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.dmg\t3145728",
  36. "https://cdn-zcode.z.ai/zcode/electron/releases/update/mac/arm64/latest-mac.yml\t128",
  37. "https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.zip",
  38. "",
  39. ].join("\n"),
  40. "utf-8",
  41. );
  42. writeExecutable(
  43. join(binDir, "aliyun"),
  44. `#!/bin/bash
  45. set -euo pipefail
  46. echo "$*" >> "${aliyunLogPath}"
  47. if [[ "$2" == "PushObjectCache" ]]; then
  48. printf '%s\n' '{"PushTaskId":"task-123"}'
  49. exit 0
  50. fi
  51. if [[ "$2" == "DescribeRefreshTaskById" ]]; then
  52. printf '%s\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
  53. exit 0
  54. fi
  55. echo "unexpected CDN command: $*" >&2
  56. exit 1
  57. `,
  58. );
  59. writeExecutable(
  60. join(binDir, "jq"),
  61. `#!/usr/bin/env node
  62. const fs = require("fs");
  63. const input = fs.readFileSync(0, "utf8");
  64. const payload = JSON.parse(input || "{}");
  65. const args = process.argv.slice(2);
  66. const filter = args[args.length - 1] || "";
  67. if (filter.includes("PushTaskId")) {
  68. process.stdout.write(payload.PushTaskId || "");
  69. process.exit(0);
  70. }
  71. if (filter.includes("Tasks[0].Status")) {
  72. process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
  73. process.exit(0);
  74. }
  75. if (filter.includes("Tasks[0].Process")) {
  76. process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
  77. process.exit(0);
  78. }
  79. process.stdout.write("\\n");
  80. `,
  81. );
  82. const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
  83. cwd: REPO_ROOT,
  84. env: {
  85. ...process.env,
  86. CDN_DOMAIN: "",
  87. PATH: `${binDir}:${process.env.PATH ?? ""}`,
  88. CDN_URLS_IN: cdnUrlsIn,
  89. POLL_INTERVAL: "0",
  90. },
  91. timeout: 25_000,
  92. stdio: "pipe",
  93. encoding: "utf-8",
  94. });
  95. expect(output).toContain("no minimum file size threshold is applied");
  96. expect(output).toContain("Filtered URLs: 2, Skipped URLs: 1");
  97. expect(output).toContain(
  98. "skip preload (missing/invalid recorded size): https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.zip",
  99. );
  100. const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
  101. .trim()
  102. .split("\n")
  103. .filter((line) => line.length > 0);
  104. expect(aliyunCalls).toHaveLength(4);
  105. const pushCalls = aliyunCalls.filter((line) => line.includes("PushObjectCache"));
  106. const describeCalls = aliyunCalls.filter((line) => line.includes("DescribeRefreshTaskById"));
  107. expect(pushCalls).toHaveLength(2);
  108. expect(pushCalls.some((line) => line.includes("latest-mac.yml"))).toBe(true);
  109. expect(pushCalls.some((line) => line.includes("ZCode-1.3.0-mac-arm64.dmg"))).toBe(true);
  110. expect(pushCalls.every((line) => !line.includes("--Area"))).toBe(true);
  111. expect(output).not.toContain("--Area");
  112. expect(describeCalls).toHaveLength(2);
  113. }, 30_000);
  114. it("多 CDN 账号预热时应按 URL 域名选择 aliyun profile 并用同一 profile 轮询", () => {
  115. const workspaceDir = makeTempDir("cdn-preload-profile-");
  116. const binDir = join(workspaceDir, "bin");
  117. const aliyunLogPath = join(workspaceDir, "aliyun.log");
  118. const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
  119. mkdirSync(binDir, { recursive: true });
  120. writeFileSync(
  121. cdnUrlsIn,
  122. [
  123. "https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-linux-arm64.AppImage\t3145728",
  124. "https://cdn.zcode-ai.com/zcode/electron/releases/1.3.0/ZCode-1.3.0-linux-arm64.AppImage\t3145728",
  125. ].join("\n"),
  126. "utf-8",
  127. );
  128. writeExecutable(
  129. join(binDir, "aliyun"),
  130. `#!/bin/bash
  131. set -euo pipefail
  132. echo "$*" >> "${aliyunLogPath}"
  133. profile=""
  134. for ((i = 1; i <= $#; i++)); do
  135. if [[ "\${!i}" == "--profile" ]]; then
  136. next_index=$((i + 1))
  137. profile="\${!next_index}"
  138. break
  139. fi
  140. done
  141. if [[ "$2" == "PushObjectCache" ]]; then
  142. if [[ "$profile" == "z-ai-account" ]]; then
  143. printf '%s\\n' '{"PushTaskId":"task-z-ai"}'
  144. elif [[ "$profile" == "zcode-ai-account" ]]; then
  145. printf '%s\\n' '{"PushTaskId":"task-zcode-ai"}'
  146. else
  147. echo "unexpected profile: $profile" >&2
  148. exit 1
  149. fi
  150. exit 0
  151. fi
  152. if [[ "$2" == "DescribeRefreshTaskById" ]]; then
  153. printf '%s\\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
  154. exit 0
  155. fi
  156. echo "unexpected CDN command: $*" >&2
  157. exit 1
  158. `,
  159. );
  160. writeExecutable(
  161. join(binDir, "jq"),
  162. `#!/usr/bin/env node
  163. const fs = require("fs");
  164. const input = fs.readFileSync(0, "utf8");
  165. const payload = JSON.parse(input || "{}");
  166. const filter = process.argv.at(-1) || "";
  167. if (filter.includes("PushTaskId")) process.stdout.write(payload.PushTaskId || "");
  168. else if (filter.includes("Tasks[0].Status")) process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
  169. else if (filter.includes("Tasks[0].Process")) process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
  170. else process.stdout.write("\\n");
  171. `,
  172. );
  173. const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
  174. cwd: REPO_ROOT,
  175. env: {
  176. ...process.env,
  177. PATH: `${binDir}:${process.env.PATH ?? ""}`,
  178. CDN_URLS_IN: cdnUrlsIn,
  179. CDN_DOMAIN: "cdn-zcode.z.ai,cdn.zcode-ai.com",
  180. CDN_PRELOAD_PROFILE: "z-ai-account,zcode-ai-account",
  181. POLL_INTERVAL: "0",
  182. },
  183. timeout: 25_000,
  184. stdio: "pipe",
  185. encoding: "utf-8",
  186. });
  187. expect(output).toContain("SUCCESS: All CDN tasks are Complete.");
  188. const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
  189. .trim()
  190. .split("\n")
  191. .filter((line) => line.length > 0);
  192. const pushCalls = aliyunCalls.filter((line) => line.includes("PushObjectCache"));
  193. const describeCalls = aliyunCalls.filter((line) => line.includes("DescribeRefreshTaskById"));
  194. expect(pushCalls).toHaveLength(2);
  195. expect(describeCalls).toHaveLength(2);
  196. expect(
  197. pushCalls.some(
  198. (line) => line.includes("cdn-zcode.z.ai") && line.includes("--profile z-ai-account"),
  199. ),
  200. ).toBe(true);
  201. expect(
  202. pushCalls.some(
  203. (line) => line.includes("cdn.zcode-ai.com") && line.includes("--profile zcode-ai-account"),
  204. ),
  205. ).toBe(true);
  206. expect(pushCalls.every((line) => !line.includes("--region"))).toBe(true);
  207. expect(pushCalls.every((line) => !line.includes("--Area"))).toBe(true);
  208. expect(
  209. describeCalls.some(
  210. (line) => line.includes("task-z-ai") && line.includes("--profile z-ai-account"),
  211. ),
  212. ).toBe(true);
  213. expect(
  214. describeCalls.some(
  215. (line) => line.includes("task-zcode-ai") && line.includes("--profile zcode-ai-account"),
  216. ),
  217. ).toBe(true);
  218. expect(describeCalls.every((line) => !line.includes("--region"))).toBe(true);
  219. }, 30_000);
  220. it("显式配置 PRELOAD_MIN_BYTES 时仍可按文件大小筛选预热对象", () => {
  221. const workspaceDir = makeTempDir("cdn-preload-threshold-");
  222. const binDir = join(workspaceDir, "bin");
  223. const aliyunLogPath = join(workspaceDir, "aliyun.log");
  224. const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
  225. mkdirSync(binDir, { recursive: true });
  226. writeFileSync(
  227. cdnUrlsIn,
  228. [
  229. "https://cdn.codegeex.cn/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.dmg\t3145728",
  230. "https://cdn.codegeex.cn/zcode/electron/releases/update/mac/arm64/latest-mac.yml\t128",
  231. ].join("\n"),
  232. "utf-8",
  233. );
  234. writeExecutable(
  235. join(binDir, "aliyun"),
  236. `#!/bin/bash
  237. set -euo pipefail
  238. echo "$*" >> "${aliyunLogPath}"
  239. if [[ "$2" == "PushObjectCache" ]]; then
  240. printf '%s\n' '{"PushTaskId":"task-threshold"}'
  241. exit 0
  242. fi
  243. if [[ "$2" == "DescribeRefreshTaskById" ]]; then
  244. printf '%s\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
  245. exit 0
  246. fi
  247. echo "unexpected CDN command: $*" >&2
  248. exit 1
  249. `,
  250. );
  251. writeExecutable(
  252. join(binDir, "jq"),
  253. `#!/usr/bin/env node
  254. const fs = require("fs");
  255. const input = fs.readFileSync(0, "utf8");
  256. const payload = JSON.parse(input || "{}");
  257. const filter = process.argv.at(-1) || "";
  258. if (filter.includes("PushTaskId")) process.stdout.write(payload.PushTaskId || "");
  259. else if (filter.includes("Tasks[0].Status")) process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
  260. else if (filter.includes("Tasks[0].Process")) process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
  261. else process.stdout.write("\\n");
  262. `,
  263. );
  264. const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
  265. cwd: REPO_ROOT,
  266. env: {
  267. ...process.env,
  268. PATH: `${binDir}:${process.env.PATH ?? ""}`,
  269. CDN_URLS_IN: cdnUrlsIn,
  270. POLL_INTERVAL: "0",
  271. PRELOAD_MIN_BYTES: "2097152",
  272. },
  273. timeout: 25_000,
  274. stdio: "pipe",
  275. encoding: "utf-8",
  276. });
  277. expect(output).toContain("Filtered URLs: 1, Skipped URLs: 1");
  278. expect(output).toContain("skip preload (128 bytes < 2097152)");
  279. const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
  280. .trim()
  281. .split("\n")
  282. .filter((line) => line.length > 0);
  283. expect(aliyunCalls).toHaveLength(2);
  284. expect(aliyunCalls[0]).toContain("PushObjectCache");
  285. expect(aliyunCalls[0]).toContain("ZCode-1.3.0-mac-arm64.dmg");
  286. expect(aliyunCalls[1]).toContain("DescribeRefreshTaskById");
  287. }, 30_000);
  288. it("提交 CDN 预热遇到 EOF 后会重试并继续轮询", () => {
  289. const workspaceDir = makeTempDir("cdn-preload-retry-");
  290. const binDir = join(workspaceDir, "bin");
  291. const aliyunLogPath = join(workspaceDir, "aliyun.log");
  292. const attemptPath = join(workspaceDir, "push-attempts.txt");
  293. const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
  294. mkdirSync(binDir, { recursive: true });
  295. writeFileSync(
  296. cdnUrlsIn,
  297. "https://cdn.codegeex.cn/zcode/electron/releases/1.6.0/ZCode-1.6.0-mac-arm64.dmg\t3145728\n",
  298. "utf-8",
  299. );
  300. writeExecutable(
  301. join(binDir, "aliyun"),
  302. `#!/bin/bash
  303. set -euo pipefail
  304. echo "$*" >> "${aliyunLogPath}"
  305. if [[ "$2" == "PushObjectCache" ]]; then
  306. attempts="0"
  307. [[ -f "${attemptPath}" ]] && attempts="$(cat "${attemptPath}")"
  308. attempts="$((attempts + 1))"
  309. printf '%s' "$attempts" > "${attemptPath}"
  310. if [[ "$attempts" == "1" ]]; then
  311. echo 'ERROR: Post "https://cdn.example.invalid/": EOF' >&2
  312. exit 1
  313. fi
  314. printf '%s\n' '{"PushTaskId":"task-retry"}'
  315. exit 0
  316. fi
  317. if [[ "$2" == "DescribeRefreshTaskById" ]]; then
  318. printf '%s\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
  319. exit 0
  320. fi
  321. echo "unexpected CDN command: $*" >&2
  322. exit 1
  323. `,
  324. );
  325. writeExecutable(
  326. join(binDir, "jq"),
  327. `#!/usr/bin/env node
  328. const fs = require("fs");
  329. const input = fs.readFileSync(0, "utf8");
  330. const payload = JSON.parse(input || "{}");
  331. const filter = process.argv.at(-1) || "";
  332. if (filter.includes("PushTaskId")) process.stdout.write(payload.PushTaskId || "");
  333. else if (filter.includes("Tasks[0].Status")) process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
  334. else if (filter.includes("Tasks[0].Process")) process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
  335. else process.stdout.write("\\n");
  336. `,
  337. );
  338. const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
  339. cwd: REPO_ROOT,
  340. env: {
  341. ...process.env,
  342. PATH: `${binDir}:${process.env.PATH ?? ""}`,
  343. CDN_URLS_IN: cdnUrlsIn,
  344. POLL_INTERVAL: "0",
  345. CDN_PRELOAD_RETRY_SLEEP: "0",
  346. },
  347. timeout: 25_000,
  348. stdio: "pipe",
  349. encoding: "utf-8",
  350. });
  351. expect(output).toContain("SUCCESS: All CDN tasks are Complete.");
  352. const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
  353. .trim()
  354. .split("\n")
  355. .filter((line) => line.length > 0);
  356. expect(aliyunCalls.filter((line) => line.includes("PushObjectCache"))).toHaveLength(2);
  357. expect(aliyunCalls.filter((line) => line.includes("DescribeRefreshTaskById"))).toHaveLength(1);
  358. }, 30_000);
  359. it("提交 CDN 预热遇到 SignatureNonceUsed 时按已触达服务端处理", () => {
  360. const workspaceDir = makeTempDir("cdn-preload-nonce-used-");
  361. const binDir = join(workspaceDir, "bin");
  362. const aliyunLogPath = join(workspaceDir, "aliyun.log");
  363. const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
  364. mkdirSync(binDir, { recursive: true });
  365. writeFileSync(
  366. cdnUrlsIn,
  367. "https://cdn.codegeex.cn/zcode/electron/releases/1.6.0/ZCode-1.6.0-mac-arm64.dmg\t3145728\n",
  368. "utf-8",
  369. );
  370. writeExecutable(
  371. join(binDir, "aliyun"),
  372. `#!/bin/bash
  373. set -euo pipefail
  374. echo "$*" >> "${aliyunLogPath}"
  375. if [[ "$2" == "PushObjectCache" ]]; then
  376. cat >&2 <<'JSON'
  377. {"RequestId":"43BEE5F4-664A-3F6C-AD1C-846C2730779B","Message":"Specified signature nonce was used already.","Code":"SignatureNonceUsed"}
  378. JSON
  379. exit 1
  380. fi
  381. echo "unexpected CDN command: $*" >&2
  382. exit 1
  383. `,
  384. );
  385. writeExecutable(
  386. join(binDir, "jq"),
  387. `#!/usr/bin/env node
  388. process.stdout.write("\\n");
  389. `,
  390. );
  391. const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
  392. cwd: REPO_ROOT,
  393. env: {
  394. ...process.env,
  395. PATH: `${binDir}:${process.env.PATH ?? ""}`,
  396. CDN_URLS_IN: cdnUrlsIn,
  397. POLL_INTERVAL: "0",
  398. CDN_PRELOAD_RETRY_SLEEP: "0",
  399. },
  400. timeout: 25_000,
  401. stdio: "pipe",
  402. encoding: "utf-8",
  403. });
  404. expect(output).toContain(
  405. "No task IDs returned; 1 CDN preload request(s) may already be submitted.",
  406. );
  407. const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
  408. .trim()
  409. .split("\n")
  410. .filter((line) => line.length > 0);
  411. expect(aliyunCalls.filter((line) => line.includes("PushObjectCache"))).toHaveLength(1);
  412. expect(aliyunCalls.some((line) => line.includes("DescribeRefreshTaskById"))).toBe(false);
  413. }, 30_000);
  414. it("提交 CDN 预热重试耗尽后不阻塞后续流程", () => {
  415. const workspaceDir = makeTempDir("cdn-preload-soft-fail-");
  416. const binDir = join(workspaceDir, "bin");
  417. const aliyunLogPath = join(workspaceDir, "aliyun.log");
  418. const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
  419. mkdirSync(binDir, { recursive: true });
  420. writeFileSync(
  421. cdnUrlsIn,
  422. "https://cdn.codegeex.cn/zcode/electron/releases/1.6.0/ZCode-1.6.0-mac-arm64.dmg\t3145728\n",
  423. "utf-8",
  424. );
  425. writeExecutable(
  426. join(binDir, "aliyun"),
  427. `#!/bin/bash
  428. set -euo pipefail
  429. echo "$*" >> "${aliyunLogPath}"
  430. if [[ "$2" == "PushObjectCache" ]]; then
  431. echo 'ERROR: Post "https://cdn.example.invalid/": EOF' >&2
  432. exit 1
  433. fi
  434. echo "unexpected CDN command: $*" >&2
  435. exit 1
  436. `,
  437. );
  438. writeExecutable(
  439. join(binDir, "jq"),
  440. `#!/usr/bin/env node
  441. process.stdout.write("\\n");
  442. `,
  443. );
  444. const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
  445. cwd: REPO_ROOT,
  446. env: {
  447. ...process.env,
  448. PATH: `${binDir}:${process.env.PATH ?? ""}`,
  449. CDN_URLS_IN: cdnUrlsIn,
  450. CDN_PRELOAD_MAX_ATTEMPTS: "2",
  451. CDN_PRELOAD_RETRY_SLEEP: "0",
  452. },
  453. timeout: 25_000,
  454. stdio: "pipe",
  455. encoding: "utf-8",
  456. });
  457. expect(output).toContain(
  458. "No tasks submitted. CDN preload is best-effort; continue release flow.",
  459. );
  460. const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
  461. .trim()
  462. .split("\n")
  463. .filter((line) => line.length > 0);
  464. expect(aliyunCalls.filter((line) => line.includes("PushObjectCache"))).toHaveLength(2);
  465. expect(aliyunCalls.some((line) => line.includes("DescribeRefreshTaskById"))).toBe(false);
  466. }, 30_000);
  467. });