| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- import { execFileSync } from "node:child_process";
- import { mkdtempSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
- import { tmpdir } from "node:os";
- import { dirname, join, resolve } from "node:path";
- import { fileURLToPath } from "node:url";
- import { afterEach, describe, expect, it } from "vitest";
- // cdn-preload.sh 是纯 bash 脚本,Windows 上没有原生 bash 环境(或行为不一致),跳过整个测试套件。
- const isWindows = process.platform === "win32";
- const tempDirs: string[] = [];
- const TEST_FILE_DIR = dirname(fileURLToPath(import.meta.url));
- const REPO_ROOT = resolve(TEST_FILE_DIR, "../../..");
- function makeTempDir(prefix: string) {
- const dir = mkdtempSync(join(tmpdir(), prefix));
- tempDirs.push(dir);
- return dir;
- }
- function writeExecutable(filePath: string, content: string) {
- writeFileSync(filePath, content, { encoding: "utf-8", mode: 0o755 });
- }
- afterEach(() => {
- for (const dir of tempDirs.splice(0)) {
- rmSync(dir, { recursive: true, force: true });
- }
- });
- describe.skipIf(isWindows)("cdn-preload.sh", () => {
- it("默认预热 upload 阶段写出的所有有效文件大小记录", () => {
- const workspaceDir = makeTempDir("cdn-preload-workspace-");
- const binDir = join(workspaceDir, "bin");
- const aliyunLogPath = join(workspaceDir, "aliyun.log");
- const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
- mkdirSync(binDir, { recursive: true });
- writeFileSync(
- cdnUrlsIn,
- [
- "https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.dmg\t3145728",
- "https://cdn-zcode.z.ai/zcode/electron/releases/update/mac/arm64/latest-mac.yml\t128",
- "https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.zip",
- "",
- ].join("\n"),
- "utf-8",
- );
- writeExecutable(
- join(binDir, "aliyun"),
- `#!/bin/bash
- set -euo pipefail
- echo "$*" >> "${aliyunLogPath}"
- if [[ "$2" == "PushObjectCache" ]]; then
- printf '%s\n' '{"PushTaskId":"task-123"}'
- exit 0
- fi
- if [[ "$2" == "DescribeRefreshTaskById" ]]; then
- printf '%s\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
- exit 0
- fi
- echo "unexpected CDN command: $*" >&2
- exit 1
- `,
- );
- writeExecutable(
- join(binDir, "jq"),
- `#!/usr/bin/env node
- const fs = require("fs");
- const input = fs.readFileSync(0, "utf8");
- const payload = JSON.parse(input || "{}");
- const args = process.argv.slice(2);
- const filter = args[args.length - 1] || "";
- if (filter.includes("PushTaskId")) {
- process.stdout.write(payload.PushTaskId || "");
- process.exit(0);
- }
- if (filter.includes("Tasks[0].Status")) {
- process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
- process.exit(0);
- }
- if (filter.includes("Tasks[0].Process")) {
- process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
- process.exit(0);
- }
- process.stdout.write("\\n");
- `,
- );
- const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
- cwd: REPO_ROOT,
- env: {
- ...process.env,
- CDN_DOMAIN: "",
- PATH: `${binDir}:${process.env.PATH ?? ""}`,
- CDN_URLS_IN: cdnUrlsIn,
- POLL_INTERVAL: "0",
- },
- timeout: 25_000,
- stdio: "pipe",
- encoding: "utf-8",
- });
- expect(output).toContain("no minimum file size threshold is applied");
- expect(output).toContain("Filtered URLs: 2, Skipped URLs: 1");
- expect(output).toContain(
- "skip preload (missing/invalid recorded size): https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.zip",
- );
- const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
- .trim()
- .split("\n")
- .filter((line) => line.length > 0);
- expect(aliyunCalls).toHaveLength(4);
- const pushCalls = aliyunCalls.filter((line) => line.includes("PushObjectCache"));
- const describeCalls = aliyunCalls.filter((line) => line.includes("DescribeRefreshTaskById"));
- expect(pushCalls).toHaveLength(2);
- expect(pushCalls.some((line) => line.includes("latest-mac.yml"))).toBe(true);
- expect(pushCalls.some((line) => line.includes("ZCode-1.3.0-mac-arm64.dmg"))).toBe(true);
- expect(pushCalls.every((line) => !line.includes("--Area"))).toBe(true);
- expect(output).not.toContain("--Area");
- expect(describeCalls).toHaveLength(2);
- }, 30_000);
- it("多 CDN 账号预热时应按 URL 域名选择 aliyun profile 并用同一 profile 轮询", () => {
- const workspaceDir = makeTempDir("cdn-preload-profile-");
- const binDir = join(workspaceDir, "bin");
- const aliyunLogPath = join(workspaceDir, "aliyun.log");
- const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
- mkdirSync(binDir, { recursive: true });
- writeFileSync(
- cdnUrlsIn,
- [
- "https://cdn-zcode.z.ai/zcode/electron/releases/1.3.0/ZCode-1.3.0-linux-arm64.AppImage\t3145728",
- "https://cdn.zcode-ai.com/zcode/electron/releases/1.3.0/ZCode-1.3.0-linux-arm64.AppImage\t3145728",
- ].join("\n"),
- "utf-8",
- );
- writeExecutable(
- join(binDir, "aliyun"),
- `#!/bin/bash
- set -euo pipefail
- echo "$*" >> "${aliyunLogPath}"
- profile=""
- for ((i = 1; i <= $#; i++)); do
- if [[ "\${!i}" == "--profile" ]]; then
- next_index=$((i + 1))
- profile="\${!next_index}"
- break
- fi
- done
- if [[ "$2" == "PushObjectCache" ]]; then
- if [[ "$profile" == "z-ai-account" ]]; then
- printf '%s\\n' '{"PushTaskId":"task-z-ai"}'
- elif [[ "$profile" == "zcode-ai-account" ]]; then
- printf '%s\\n' '{"PushTaskId":"task-zcode-ai"}'
- else
- echo "unexpected profile: $profile" >&2
- exit 1
- fi
- exit 0
- fi
- if [[ "$2" == "DescribeRefreshTaskById" ]]; then
- printf '%s\\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
- exit 0
- fi
- echo "unexpected CDN command: $*" >&2
- exit 1
- `,
- );
- writeExecutable(
- join(binDir, "jq"),
- `#!/usr/bin/env node
- const fs = require("fs");
- const input = fs.readFileSync(0, "utf8");
- const payload = JSON.parse(input || "{}");
- const filter = process.argv.at(-1) || "";
- if (filter.includes("PushTaskId")) process.stdout.write(payload.PushTaskId || "");
- else if (filter.includes("Tasks[0].Status")) process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
- else if (filter.includes("Tasks[0].Process")) process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
- else process.stdout.write("\\n");
- `,
- );
- const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
- cwd: REPO_ROOT,
- env: {
- ...process.env,
- PATH: `${binDir}:${process.env.PATH ?? ""}`,
- CDN_URLS_IN: cdnUrlsIn,
- CDN_DOMAIN: "cdn-zcode.z.ai,cdn.zcode-ai.com",
- CDN_PRELOAD_PROFILE: "z-ai-account,zcode-ai-account",
- POLL_INTERVAL: "0",
- },
- timeout: 25_000,
- stdio: "pipe",
- encoding: "utf-8",
- });
- expect(output).toContain("SUCCESS: All CDN tasks are Complete.");
- const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
- .trim()
- .split("\n")
- .filter((line) => line.length > 0);
- const pushCalls = aliyunCalls.filter((line) => line.includes("PushObjectCache"));
- const describeCalls = aliyunCalls.filter((line) => line.includes("DescribeRefreshTaskById"));
- expect(pushCalls).toHaveLength(2);
- expect(describeCalls).toHaveLength(2);
- expect(
- pushCalls.some(
- (line) => line.includes("cdn-zcode.z.ai") && line.includes("--profile z-ai-account"),
- ),
- ).toBe(true);
- expect(
- pushCalls.some(
- (line) => line.includes("cdn.zcode-ai.com") && line.includes("--profile zcode-ai-account"),
- ),
- ).toBe(true);
- expect(pushCalls.every((line) => !line.includes("--region"))).toBe(true);
- expect(pushCalls.every((line) => !line.includes("--Area"))).toBe(true);
- expect(
- describeCalls.some(
- (line) => line.includes("task-z-ai") && line.includes("--profile z-ai-account"),
- ),
- ).toBe(true);
- expect(
- describeCalls.some(
- (line) => line.includes("task-zcode-ai") && line.includes("--profile zcode-ai-account"),
- ),
- ).toBe(true);
- expect(describeCalls.every((line) => !line.includes("--region"))).toBe(true);
- }, 30_000);
- it("显式配置 PRELOAD_MIN_BYTES 时仍可按文件大小筛选预热对象", () => {
- const workspaceDir = makeTempDir("cdn-preload-threshold-");
- const binDir = join(workspaceDir, "bin");
- const aliyunLogPath = join(workspaceDir, "aliyun.log");
- const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
- mkdirSync(binDir, { recursive: true });
- writeFileSync(
- cdnUrlsIn,
- [
- "https://cdn.codegeex.cn/zcode/electron/releases/1.3.0/ZCode-1.3.0-mac-arm64.dmg\t3145728",
- "https://cdn.codegeex.cn/zcode/electron/releases/update/mac/arm64/latest-mac.yml\t128",
- ].join("\n"),
- "utf-8",
- );
- writeExecutable(
- join(binDir, "aliyun"),
- `#!/bin/bash
- set -euo pipefail
- echo "$*" >> "${aliyunLogPath}"
- if [[ "$2" == "PushObjectCache" ]]; then
- printf '%s\n' '{"PushTaskId":"task-threshold"}'
- exit 0
- fi
- if [[ "$2" == "DescribeRefreshTaskById" ]]; then
- printf '%s\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
- exit 0
- fi
- echo "unexpected CDN command: $*" >&2
- exit 1
- `,
- );
- writeExecutable(
- join(binDir, "jq"),
- `#!/usr/bin/env node
- const fs = require("fs");
- const input = fs.readFileSync(0, "utf8");
- const payload = JSON.parse(input || "{}");
- const filter = process.argv.at(-1) || "";
- if (filter.includes("PushTaskId")) process.stdout.write(payload.PushTaskId || "");
- else if (filter.includes("Tasks[0].Status")) process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
- else if (filter.includes("Tasks[0].Process")) process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
- else process.stdout.write("\\n");
- `,
- );
- const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
- cwd: REPO_ROOT,
- env: {
- ...process.env,
- PATH: `${binDir}:${process.env.PATH ?? ""}`,
- CDN_URLS_IN: cdnUrlsIn,
- POLL_INTERVAL: "0",
- PRELOAD_MIN_BYTES: "2097152",
- },
- timeout: 25_000,
- stdio: "pipe",
- encoding: "utf-8",
- });
- expect(output).toContain("Filtered URLs: 1, Skipped URLs: 1");
- expect(output).toContain("skip preload (128 bytes < 2097152)");
- const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
- .trim()
- .split("\n")
- .filter((line) => line.length > 0);
- expect(aliyunCalls).toHaveLength(2);
- expect(aliyunCalls[0]).toContain("PushObjectCache");
- expect(aliyunCalls[0]).toContain("ZCode-1.3.0-mac-arm64.dmg");
- expect(aliyunCalls[1]).toContain("DescribeRefreshTaskById");
- }, 30_000);
- it("提交 CDN 预热遇到 EOF 后会重试并继续轮询", () => {
- const workspaceDir = makeTempDir("cdn-preload-retry-");
- const binDir = join(workspaceDir, "bin");
- const aliyunLogPath = join(workspaceDir, "aliyun.log");
- const attemptPath = join(workspaceDir, "push-attempts.txt");
- const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
- mkdirSync(binDir, { recursive: true });
- writeFileSync(
- cdnUrlsIn,
- "https://cdn.codegeex.cn/zcode/electron/releases/1.6.0/ZCode-1.6.0-mac-arm64.dmg\t3145728\n",
- "utf-8",
- );
- writeExecutable(
- join(binDir, "aliyun"),
- `#!/bin/bash
- set -euo pipefail
- echo "$*" >> "${aliyunLogPath}"
- if [[ "$2" == "PushObjectCache" ]]; then
- attempts="0"
- [[ -f "${attemptPath}" ]] && attempts="$(cat "${attemptPath}")"
- attempts="$((attempts + 1))"
- printf '%s' "$attempts" > "${attemptPath}"
- if [[ "$attempts" == "1" ]]; then
- echo 'ERROR: Post "https://cdn.example.invalid/": EOF' >&2
- exit 1
- fi
- printf '%s\n' '{"PushTaskId":"task-retry"}'
- exit 0
- fi
- if [[ "$2" == "DescribeRefreshTaskById" ]]; then
- printf '%s\n' '{"Tasks":[{"Status":"Complete","Process":"100%"}]}'
- exit 0
- fi
- echo "unexpected CDN command: $*" >&2
- exit 1
- `,
- );
- writeExecutable(
- join(binDir, "jq"),
- `#!/usr/bin/env node
- const fs = require("fs");
- const input = fs.readFileSync(0, "utf8");
- const payload = JSON.parse(input || "{}");
- const filter = process.argv.at(-1) || "";
- if (filter.includes("PushTaskId")) process.stdout.write(payload.PushTaskId || "");
- else if (filter.includes("Tasks[0].Status")) process.stdout.write(payload.Tasks?.[0]?.Status || "Unknown");
- else if (filter.includes("Tasks[0].Process")) process.stdout.write(payload.Tasks?.[0]?.Process || "0%");
- else process.stdout.write("\\n");
- `,
- );
- const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
- cwd: REPO_ROOT,
- env: {
- ...process.env,
- PATH: `${binDir}:${process.env.PATH ?? ""}`,
- CDN_URLS_IN: cdnUrlsIn,
- POLL_INTERVAL: "0",
- CDN_PRELOAD_RETRY_SLEEP: "0",
- },
- timeout: 25_000,
- stdio: "pipe",
- encoding: "utf-8",
- });
- expect(output).toContain("SUCCESS: All CDN tasks are Complete.");
- const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
- .trim()
- .split("\n")
- .filter((line) => line.length > 0);
- expect(aliyunCalls.filter((line) => line.includes("PushObjectCache"))).toHaveLength(2);
- expect(aliyunCalls.filter((line) => line.includes("DescribeRefreshTaskById"))).toHaveLength(1);
- }, 30_000);
- it("提交 CDN 预热遇到 SignatureNonceUsed 时按已触达服务端处理", () => {
- const workspaceDir = makeTempDir("cdn-preload-nonce-used-");
- const binDir = join(workspaceDir, "bin");
- const aliyunLogPath = join(workspaceDir, "aliyun.log");
- const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
- mkdirSync(binDir, { recursive: true });
- writeFileSync(
- cdnUrlsIn,
- "https://cdn.codegeex.cn/zcode/electron/releases/1.6.0/ZCode-1.6.0-mac-arm64.dmg\t3145728\n",
- "utf-8",
- );
- writeExecutable(
- join(binDir, "aliyun"),
- `#!/bin/bash
- set -euo pipefail
- echo "$*" >> "${aliyunLogPath}"
- if [[ "$2" == "PushObjectCache" ]]; then
- cat >&2 <<'JSON'
- {"RequestId":"43BEE5F4-664A-3F6C-AD1C-846C2730779B","Message":"Specified signature nonce was used already.","Code":"SignatureNonceUsed"}
- JSON
- exit 1
- fi
- echo "unexpected CDN command: $*" >&2
- exit 1
- `,
- );
- writeExecutable(
- join(binDir, "jq"),
- `#!/usr/bin/env node
- process.stdout.write("\\n");
- `,
- );
- const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
- cwd: REPO_ROOT,
- env: {
- ...process.env,
- PATH: `${binDir}:${process.env.PATH ?? ""}`,
- CDN_URLS_IN: cdnUrlsIn,
- POLL_INTERVAL: "0",
- CDN_PRELOAD_RETRY_SLEEP: "0",
- },
- timeout: 25_000,
- stdio: "pipe",
- encoding: "utf-8",
- });
- expect(output).toContain(
- "No task IDs returned; 1 CDN preload request(s) may already be submitted.",
- );
- const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
- .trim()
- .split("\n")
- .filter((line) => line.length > 0);
- expect(aliyunCalls.filter((line) => line.includes("PushObjectCache"))).toHaveLength(1);
- expect(aliyunCalls.some((line) => line.includes("DescribeRefreshTaskById"))).toBe(false);
- }, 30_000);
- it("提交 CDN 预热重试耗尽后不阻塞后续流程", () => {
- const workspaceDir = makeTempDir("cdn-preload-soft-fail-");
- const binDir = join(workspaceDir, "bin");
- const aliyunLogPath = join(workspaceDir, "aliyun.log");
- const cdnUrlsIn = join(workspaceDir, "cdn-urls.txt");
- mkdirSync(binDir, { recursive: true });
- writeFileSync(
- cdnUrlsIn,
- "https://cdn.codegeex.cn/zcode/electron/releases/1.6.0/ZCode-1.6.0-mac-arm64.dmg\t3145728\n",
- "utf-8",
- );
- writeExecutable(
- join(binDir, "aliyun"),
- `#!/bin/bash
- set -euo pipefail
- echo "$*" >> "${aliyunLogPath}"
- if [[ "$2" == "PushObjectCache" ]]; then
- echo 'ERROR: Post "https://cdn.example.invalid/": EOF' >&2
- exit 1
- fi
- echo "unexpected CDN command: $*" >&2
- exit 1
- `,
- );
- writeExecutable(
- join(binDir, "jq"),
- `#!/usr/bin/env node
- process.stdout.write("\\n");
- `,
- );
- const output = execFileSync("bash", ["scripts/cdn-preload.sh"], {
- cwd: REPO_ROOT,
- env: {
- ...process.env,
- PATH: `${binDir}:${process.env.PATH ?? ""}`,
- CDN_URLS_IN: cdnUrlsIn,
- CDN_PRELOAD_MAX_ATTEMPTS: "2",
- CDN_PRELOAD_RETRY_SLEEP: "0",
- },
- timeout: 25_000,
- stdio: "pipe",
- encoding: "utf-8",
- });
- expect(output).toContain(
- "No tasks submitted. CDN preload is best-effort; continue release flow.",
- );
- const aliyunCalls = readFileSync(aliyunLogPath, "utf-8")
- .trim()
- .split("\n")
- .filter((line) => line.length > 0);
- expect(aliyunCalls.filter((line) => line.includes("PushObjectCache"))).toHaveLength(2);
- expect(aliyunCalls.some((line) => line.includes("DescribeRefreshTaskById"))).toBe(false);
- }, 30_000);
- });
|