Files
openclaw/test/scripts/ui.test.ts
xin zhuang 6704d0ab27 fix(scripts): include ui:build in build-all full and ciArtifacts profiles (#86010)
* fix(scripts): include ui:build in build-all full and ciArtifacts profiles

Closes #85206.

scripts/build-all.mjs only ran ui:build via a separate `pnpm ui:build`
command. Because `pnpm build` invokes tsdown which removes `dist/`,
a backend rebuild silently deletes any previously generated
dist/control-ui assets, leaving the gateway to serve the
"Control UI assets not found" message at startup. Documentation and
startup auto-repair masked the bug at the worst possible time
(LaunchAgent readiness / remote recovery) instead of guaranteeing the
build artifact contract.

This change adds ui:build as a build-all step after
copy-export-html-templates and before write-build-info, and includes
it in the full and ciArtifacts profiles. Minimal backend dev profiles
(gatewayWatch, cliStartup) keep their existing fast-loop step lists
and do not run ui:build.

Regression coverage:
- ciArtifacts step list assertion updated to match the new ordering.
- Three new resolveBuildAllSteps assertions: ui:build is in full and
  ciArtifacts and runs after tsdown/runtime-postbuild-stamp and before
  write-build-info; ui:build is excluded from gatewayWatch/cliStartup;
  ui:build cache outputs declare dist/control-ui.

* fix(scripts): leave ui:build uncached so dist/control-ui never restores stale build IDs

ClawSweeper review on #86010 flagged that the original ui:build cache only
hashed ui/, scripts/ui.js, and scripts/lib/copy-assets.ts, but
ui/vite.config.ts also reads package.json plus git HEAD and the
OPENCLAW_CONTROL_UI_BUILD_ID/OPENCLAW_VERSION env vars to embed a build ID
into the app and service worker. A file-input cache signature cannot
exactly invalidate those metadata sources, so a warm build-all hit could
restore a previously generated dist/control-ui after tsdown clears dist
and ship stale service-worker/app cache metadata.

Leaving the step uncached keeps the contract simple: every pnpm build
re-runs Vite, which is fast for the Control UI bundle and matches the
existing behavior of every other un-cached build-all step. Backend-only
profiles (gatewayWatch, cliStartup) are still unchanged.

Tests:
- Updated the ui:build cache assertion to require step.cache to be
  undefined and explain the metadata-input reason.
- Existing presence/order/exclusion assertions for ui:build are unchanged
  and still cover the full and ciArtifacts profile contract.

* fix(scripts): keep ui build fallback pnpm-free

---------

Co-authored-by: 1052326311 <1052326311@users.noreply.github.com>
Co-authored-by: Peter Steinberger <steipete@gmail.com>
2026-05-25 08:08:52 +01:00

123 lines
3.8 KiB
TypeScript

import { spawnSync } from "node:child_process";
import path from "node:path";
import { describe, expect, it } from "vitest";
import {
isDirectScriptExecution,
resolveSpawnCall,
shouldUseCmdExeForCommand,
} from "../../scripts/ui.js";
describe("scripts/ui windows spawn behavior", () => {
it("wraps Windows command launchers with cmd.exe without enabling shell mode", () => {
expect(
shouldUseCmdExeForCommand("C:\\Users\\dev\\AppData\\Local\\pnpm\\pnpm.CMD", "win32"),
).toBe(true);
expect(
resolveSpawnCall(
"C:\\Program Files\\nodejs\\pnpm.cmd",
["run", "build", "-t", "path with spaces"],
{ PATH: "C:\\bin" },
{ comSpec: "C:\\Windows\\System32\\cmd.exe", cwd: "C:\\repo\\ui", platform: "win32" },
),
).toEqual({
command: "C:\\Windows\\System32\\cmd.exe",
args: [
"/d",
"/s",
"/c",
'""C:\\Program Files\\nodejs\\pnpm.cmd" run build -t "path with spaces""',
],
options: {
cwd: "C:\\repo\\ui",
stdio: "inherit",
env: { PATH: "C:\\bin" },
shell: false,
windowsVerbatimArguments: true,
},
});
});
it("does not use cmd.exe for non-command launchers", () => {
expect(shouldUseCmdExeForCommand("C:\\Program Files\\nodejs\\node.exe", "win32")).toBe(false);
expect(shouldUseCmdExeForCommand("C:\\tools\\pnpm.com", "win32")).toBe(false);
expect(shouldUseCmdExeForCommand("/usr/local/bin/pnpm", "linux")).toBe(false);
expect(
resolveSpawnCall(
"C:\\Program Files\\nodejs\\pnpm.exe",
["run", "build"],
{ PATH: "C:\\bin" },
{ cwd: "C:\\repo\\ui", platform: "win32" },
),
).toEqual({
command: "C:\\Program Files\\nodejs\\pnpm.exe",
args: ["run", "build"],
options: {
cwd: "C:\\repo\\ui",
stdio: "inherit",
env: { PATH: "C:\\bin" },
shell: false,
},
});
});
it("rejects unsafe cmd.exe arguments before launch", () => {
expect(() =>
resolveSpawnCall("C:\\tools\\pnpm.cmd", ["run", "build", "evil&calc"], undefined, {
platform: "win32",
}),
).toThrow(/unsafe windows cmd\.exe argument/i);
expect(() =>
resolveSpawnCall("C:\\tools\\pnpm.cmd", ["run", "build", "%PATH%"], undefined, {
platform: "win32",
}),
).toThrow(/unsafe windows cmd\.exe argument/i);
});
it("keeps non-Windows launches direct even with shell metacharacters", () => {
expect(
resolveSpawnCall(
"/usr/local/bin/pnpm",
["run", "build", "contains&metacharacters"],
{ PATH: "/bin" },
{ cwd: "/repo/ui", platform: "linux" },
),
).toEqual({
command: "/usr/local/bin/pnpm",
args: ["run", "build", "contains&metacharacters"],
options: {
cwd: "/repo/ui",
stdio: "inherit",
env: { PATH: "/bin" },
shell: false,
},
});
});
it("detects direct execution through a junctioned script path", () => {
const realScriptPath = path.resolve("repo/openclaw/scripts/ui.js");
const junctionScriptPath = path.resolve("linked/openclaw/scripts/ui.js");
const realpath = (entry: string) => (entry === junctionScriptPath ? realScriptPath : entry);
expect(isDirectScriptExecution(junctionScriptPath, realScriptPath, realpath)).toBe(true);
});
it("honors build-all no-pnpm mode before requiring a pnpm runner", () => {
const result = spawnSync(process.execPath, ["scripts/ui.js", "build", "--help"], {
cwd: path.resolve("."),
encoding: "utf8",
env: {
...process.env,
OPENCLAW_BUILD_ALL_NO_PNPM: "1",
PATH: "",
},
});
const output = `${result.stdout}${result.stderr}`;
expect(result.status).toBe(0);
expect(output).not.toContain("Missing UI runner");
expect(output).toContain("vite");
});
});