























1+// Resolve OpenClaw ref tests cover the release workflow ref resolver script.
2+import { execFileSync, spawnSync } from "node:child_process";
3+import { join } from "node:path";
4+import { afterEach, describe, expect, it } from "vitest";
5+import { createTempDirTracker } from "../helpers/temp-dir.js";
6+7+const SCRIPT_PATH = "scripts/github/resolve-openclaw-ref.sh";
8+const tempDirs = createTempDirTracker();
9+10+afterEach(() => {
11+tempDirs.cleanup();
12+});
13+14+function git(cwd: string, args: string[]): string {
15+return execFileSync("git", args, {
16+ cwd,
17+encoding: "utf8",
18+stdio: ["ignore", "pipe", "pipe"],
19+}).trim();
20+}
21+22+function createRemoteRepo() {
23+const repo = tempDirs.make("openclaw-ref-remote-");
24+git(repo, ["init", "-q", "-b", "main"]);
25+git(repo, ["config", "user.email", "test-user"]);
26+git(repo, ["config", "user.name", "Test User"]);
27+execFileSync("bash", ["-c", "printf seed > seed.txt"], { cwd: repo });
28+git(repo, ["add", "seed.txt"]);
29+git(repo, ["commit", "-qm", "seed"]);
30+const sha = git(repo, ["rev-parse", "HEAD"]);
31+git(repo, ["branch", "release/test"]);
32+git(repo, ["branch", "ambiguous"]);
33+git(repo, ["-c", "tag.gpgSign=false", "tag", "v2026.6.21"]);
34+git(repo, ["-c", "tag.gpgSign=false", "tag", "ambiguous"]);
35+return { repo, sha };
36+}
37+38+function runResolver(remote: string, args: string[]) {
39+return spawnSync("bash", [SCRIPT_PATH, ...args], {
40+cwd: process.cwd(),
41+encoding: "utf8",
42+env: {
43+ ...process.env,
44+OPENCLAW_REF_REMOTE: remote,
45+},
46+});
47+}
48+49+function parseOutput(output: string): Record<string, string> {
50+return Object.fromEntries(
51+output
52+.trim()
53+.split("\n")
54+.filter(Boolean)
55+.map((line) => {
56+const separator = line.indexOf("=");
57+return [line.slice(0, separator), line.slice(separator + 1)];
58+}),
59+);
60+}
61+62+function expectSuccessfulOutput(result: ReturnType<typeof runResolver>): Record<string, string> {
63+expect(result.status).toBe(0);
64+expect(result.stderr).toBe("");
65+return parseOutput(result.stdout);
66+}
67+68+describe("scripts/github/resolve-openclaw-ref.sh", () => {
69+it("resolves branch and tag refs with git ls-remote", () => {
70+const { repo, sha } = createRemoteRepo();
71+72+expect(expectSuccessfulOutput(runResolver(repo, ["--ref", "release/test"]))).toEqual({
73+fallback: "false",
74+fast: "true",
75+ref_kind: "branch",
76+ sha,
77+});
78+expect(expectSuccessfulOutput(runResolver(repo, ["--ref", "v2026.6.21"]))).toEqual({
79+fallback: "false",
80+fast: "true",
81+ref_kind: "tag",
82+ sha,
83+});
84+});
85+86+it("accepts full commit SHA refs without remote lookup", () => {
87+const { repo, sha } = createRemoteRepo();
88+const result = runResolver(repo, ["--ref", sha.toUpperCase()]);
89+90+expect(expectSuccessfulOutput(result)).toEqual({
91+fallback: "true",
92+fast: "false",
93+ref_kind: "sha",
94+ sha,
95+});
96+});
97+98+it("writes fallback outputs for unresolved refs when a caller supplies an expected SHA", () => {
99+const { repo, sha } = createRemoteRepo();
100+const outputPath = join(tempDirs.make("openclaw-ref-output-"), "github-output.txt");
101+const result = runResolver(repo, [
102+"--ref",
103+"missing-ref",
104+"--expected-sha",
105+sha,
106+"--fallback-ok",
107+"--github-output",
108+outputPath,
109+]);
110+111+expect(result.status).toBe(0);
112+expect(result.stdout).toBe("");
113+expect(parseOutput(execFileSync("cat", [outputPath], { encoding: "utf8" }))).toEqual({
114+fallback: "true",
115+fast: "false",
116+ref_kind: "unknown",
117+ sha,
118+});
119+});
120+121+it("does not let fallback mode hide remote lookup failures", () => {
122+const missingRemote = join(tempDirs.make("openclaw-ref-missing-"), "missing.git");
123+const result = runResolver(missingRemote, [
124+"--ref",
125+"missing-ref",
126+"--expected-sha",
127+"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
128+"--fallback-ok",
129+]);
130+131+expect(result.status).toBe(1);
132+expect(result.stderr).toContain("does not appear to be a git repository");
133+expect(result.stdout).toBe("");
134+});
135+136+it("rejects ambiguous branch and tag names before emitting outputs", () => {
137+const { repo } = createRemoteRepo();
138+const result = runResolver(repo, ["--ref", "ambiguous"]);
139+140+expect(result.status).toBe(1);
141+expect(result.stderr).toContain("Ref resolved ambiguously as both branch and tag: ambiguous");
142+expect(result.stdout).toBe("");
143+});
144+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。