Skip to content

Commit 7049882

Browse files
committed
perf(test): avoid Canvas Git timeout wait
1 parent dbad5e3 commit 7049882

3 files changed

Lines changed: 68 additions & 33 deletions

File tree

‎extensions/canvas/scripts/bundle-a2ui.d.mts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,15 @@ export declare function isBundleHashInputPath(filePath: string, repoRoot?: strin
22
export declare function getLocalRolldownCliCandidates(repoRoot?: string): string[];
33
export declare function getBundleHashRepoInputPaths(repoRoot?: string): string[];
44
export declare function compareNormalizedPaths(left: string, right: string): number;
5+
6+
export declare function listTrackedInputFiles(
7+
runGit: (
8+
command: string,
9+
args: string[],
10+
options: object,
11+
) => {
12+
status: number | null;
13+
stdout: string;
14+
},
15+
repoRoot?: string,
16+
): string[] | null;

‎extensions/canvas/scripts/bundle-a2ui.mjs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ const outputFile =
2323
path.join(pluginDir, "src", "host", "a2ui", "a2ui.bundle.js");
2424
const a2uiAppDir = path.join(pluginDir, "src", "host", "a2ui-app");
2525
const GIT_INPUT_DISCOVERY_TIMEOUT_MS = 5_000;
26-
const repoInputPaths = getBundleHashRepoInputPaths(rootDir);
27-
const relativeRepoInputPaths = repoInputPaths.map((inputPath) =>
28-
normalizePath(path.relative(rootDir, inputPath)),
29-
);
3026

3127
function fail(message) {
3228
console.error(message);
@@ -108,9 +104,12 @@ async function walkFiles(entryPath, files) {
108104
}
109105
}
110106

111-
function listTrackedInputFiles() {
112-
const result = spawnSync("git", ["ls-files", "--", ...relativeRepoInputPaths], {
113-
cwd: rootDir,
107+
export function listTrackedInputFiles(runGit, repoRoot = rootDir) {
108+
const relativeRepoInputPaths = getBundleHashRepoInputPaths(repoRoot).map((inputPath) =>
109+
normalizePath(path.relative(repoRoot, inputPath)),
110+
);
111+
const result = runGit("git", ["ls-files", "--", ...relativeRepoInputPaths], {
112+
cwd: repoRoot,
114113
encoding: "utf8",
115114
killSignal: "SIGKILL",
116115
stdio: ["ignore", "pipe", "pipe"],
@@ -122,14 +121,14 @@ function listTrackedInputFiles() {
122121
const trackedFiles = result.stdout
123122
.split("\n")
124123
.filter(Boolean)
125-
.map((filePath) => path.join(rootDir, filePath))
124+
.map((filePath) => path.join(repoRoot, filePath))
126125
.filter((filePath) => existsSync(filePath))
127126
.filter((filePath) => isBundleHashInputPath(filePath));
128127
return trackedFiles;
129128
}
130129

131130
async function computeHash() {
132-
let files = listTrackedInputFiles();
131+
let files = listTrackedInputFiles(spawnSync);
133132
if (!files) {
134133
files = [];
135134
for (const inputPath of getBundleHashRepoInputPaths(rootDir)) {

‎extensions/canvas/scripts/bundle-a2ui.test.ts‎

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { execFileSync } from "node:child_process";
33
import fs from "node:fs/promises";
44
import path from "node:path";
55
import { resolvePreferredOpenClawTmpDir, withTempWorkspace } from "openclaw/plugin-sdk/temp-path";
6-
import { describe, expect, it } from "vitest";
6+
import { describe, expect, it, vi } from "vitest";
77
import {
88
compareNormalizedPaths,
99
getBundleHashRepoInputPaths,
1010
getLocalRolldownCliCandidates,
1111
isBundleHashInputPath,
12+
listTrackedInputFiles,
1213
} from "./bundle-a2ui.mjs";
1314

1415
describe("scripts/bundle-a2ui.mjs", () => {
@@ -62,43 +63,66 @@ describe("scripts/bundle-a2ui.mjs", () => {
6263
);
6364
});
6465

66+
it("bounds tracked-input discovery and treats a timeout as a fallback", () => {
67+
const repoRoot = path.resolve("repo-root");
68+
const runGit = vi.fn(() => ({ status: null, stdout: "" }));
69+
70+
expect(listTrackedInputFiles(runGit, repoRoot)).toBeNull();
71+
expect(runGit).toHaveBeenCalledWith(
72+
"git",
73+
["ls-files", "--", "package.json", "pnpm-lock.yaml", "extensions/canvas/src/host/a2ui-app"],
74+
{
75+
cwd: repoRoot,
76+
encoding: "utf8",
77+
killSignal: "SIGKILL",
78+
stdio: ["ignore", "pipe", "pipe"],
79+
timeout: 5_000,
80+
},
81+
);
82+
});
83+
6584
it.skipIf(process.platform === "win32")(
66-
"falls back when tracked-input discovery stalls",
85+
"matches the tracked-input hash when Git discovery fails",
6786
async () => {
6887
await withTempWorkspace(
69-
{ rootDir: resolvePreferredOpenClawTmpDir(), prefix: "openclaw-a2ui-git-timeout-" },
88+
{ rootDir: resolvePreferredOpenClawTmpDir(), prefix: "openclaw-a2ui-git-fallback-" },
7089
async ({ dir }) => {
7190
const fakeBinDir = path.join(dir, "bin");
7291
const fakeGitPath = path.join(fakeBinDir, "git");
7392
const outputFile = path.join(dir, "a2ui.bundle.js");
7493
const hashFile = path.join(dir, "a2ui.bundle.hash");
94+
const scriptPath = path.resolve("extensions/canvas/scripts/bundle-a2ui.mjs");
95+
const baseEnv = {
96+
...process.env,
97+
OPENCLAW_A2UI_BUNDLE_HASH_FILE: hashFile,
98+
OPENCLAW_A2UI_BUNDLE_OUT: outputFile,
99+
};
75100
await fs.mkdir(fakeBinDir, { recursive: true });
76-
await fs.writeFile(
77-
fakeGitPath,
78-
`#!${process.execPath}\nsetTimeout(() => {}, 30_000);\n`,
79-
"utf8",
80-
);
101+
await fs.writeFile(fakeGitPath, `#!${process.execPath}\nprocess.exit(1);\n`, "utf8");
81102
await fs.chmod(fakeGitPath, 0o755);
82103

83-
execFileSync(
84-
process.execPath,
85-
[path.resolve("extensions/canvas/scripts/bundle-a2ui.mjs")],
86-
{
87-
cwd: process.cwd(),
88-
encoding: "utf8",
89-
env: {
90-
...process.env,
91-
OPENCLAW_A2UI_BUNDLE_HASH_FILE: hashFile,
92-
OPENCLAW_A2UI_BUNDLE_OUT: outputFile,
93-
PATH: `${fakeBinDir}${path.delimiter}${process.env.PATH ?? ""}`,
94-
},
95-
killSignal: "SIGKILL",
96-
timeout: 12_000,
104+
execFileSync(process.execPath, [scriptPath], {
105+
cwd: process.cwd(),
106+
encoding: "utf8",
107+
env: baseEnv,
108+
killSignal: "SIGKILL",
109+
timeout: 12_000,
110+
});
111+
const trackedHash = await fs.readFile(hashFile, "utf8");
112+
113+
execFileSync(process.execPath, [scriptPath], {
114+
cwd: process.cwd(),
115+
encoding: "utf8",
116+
env: {
117+
...baseEnv,
118+
PATH: `${fakeBinDir}${path.delimiter}${process.env.PATH ?? ""}`,
97119
},
98-
);
120+
killSignal: "SIGKILL",
121+
timeout: 12_000,
122+
});
99123

124+
await expect(fs.readFile(hashFile, "utf8")).resolves.toBe(trackedHash);
100125
await expect(fs.stat(outputFile)).resolves.toBeDefined();
101-
await expect(fs.stat(hashFile)).resolves.toBeDefined();
102126
},
103127
);
104128
},

0 commit comments

Comments
 (0)