Files
openclaw/test/scripts/build-all.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

380 lines
13 KiB
TypeScript

import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { describe, expect, it } from "vitest";
import {
BUILD_ALL_PROFILES,
BUILD_ALL_STEPS,
resolveBuildAllStepCacheState,
resolveBuildAllStep,
resolveBuildAllSteps,
restoreBuildAllStepCacheOutputs,
writeBuildAllStepCacheStamp,
} from "../../scripts/build-all.mjs";
function getBuildAllStep(label: string) {
const step = BUILD_ALL_STEPS.find((entry) => entry.label === label);
if (!step) {
throw new Error(`Missing build-all step ${label}`);
}
return step;
}
function withBuildCacheFixture(
run: (fixture: {
rootDir: string;
inputPath: string;
outputPath: string;
step: {
label: string;
cache: {
inputs: string[];
outputs: string[];
};
};
}) => void,
) {
const rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-build-cache-"));
try {
const inputPath = path.join(rootDir, "src/input.ts");
const outputPath = path.join(rootDir, "dist/output.js");
fs.mkdirSync(path.dirname(inputPath), { recursive: true });
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(inputPath, "input");
fs.writeFileSync(outputPath, "output");
run({
rootDir,
inputPath,
outputPath,
step: {
label: "cached",
cache: {
inputs: ["src"],
outputs: ["dist"],
},
},
});
} finally {
fs.rmSync(rootDir, { force: true, recursive: true });
}
}
describe("resolveBuildAllStep", () => {
it("routes pnpm steps through the npm_execpath pnpm runner on Windows", () => {
const step = getBuildAllStep("plugins:assets:build");
const result = resolveBuildAllStep(step, {
platform: "win32",
nodeExecPath: "C:\\Program Files\\nodejs\\node.exe",
npmExecPath: "C:/Users/test/AppData/Local/pnpm/10.32.1/bin/pnpm.cjs",
env: {},
});
expect(result).toEqual({
command: "C:\\Program Files\\nodejs\\node.exe",
args: ["C:/Users/test/AppData/Local/pnpm/10.32.1/bin/pnpm.cjs", "plugins:assets:build"],
options: {
stdio: "inherit",
env: {},
shell: false,
windowsVerbatimArguments: undefined,
},
});
});
it("keeps node steps on the current node binary", () => {
const step = getBuildAllStep("runtime-postbuild");
const result = resolveBuildAllStep(step, {
nodeExecPath: "/custom/node",
env: { FOO: "bar" },
});
expect(result).toEqual({
command: "/custom/node",
args: ["scripts/runtime-postbuild.mjs"],
options: {
stdio: "inherit",
env: { FOO: "bar" },
},
});
});
it("can route pnpm script steps through direct node entrypoints", () => {
const step = getBuildAllStep("plugins:assets:build");
const result = resolveBuildAllStep(step, {
nodeExecPath: "/custom/node",
env: { OPENCLAW_BUILD_ALL_NO_PNPM: "1" },
});
expect(result).toEqual({
command: "/custom/node",
args: ["scripts/bundled-plugin-assets.mjs", "--phase", "build"],
options: {
stdio: "inherit",
env: { OPENCLAW_BUILD_ALL_NO_PNPM: "1" },
},
});
});
it("adds heap headroom for plugin-sdk dts on Windows", () => {
const step = getBuildAllStep("build:plugin-sdk:dts");
const result = resolveBuildAllStep(step, {
platform: "win32",
nodeExecPath: "C:\\Program Files\\nodejs\\node.exe",
npmExecPath: "C:/Users/test/AppData/Local/pnpm/10.32.1/bin/pnpm.cjs",
env: { FOO: "bar" },
});
expect(result).toEqual({
command: "C:\\Program Files\\nodejs\\node.exe",
args: ["C:/Users/test/AppData/Local/pnpm/10.32.1/bin/pnpm.cjs", "build:plugin-sdk:dts"],
options: {
stdio: "inherit",
env: {
FOO: "bar",
NODE_OPTIONS: "--max-old-space-size=4096",
},
shell: false,
windowsVerbatimArguments: undefined,
},
});
});
it("keeps plugin-sdk dts cache metadata aligned with declaration inputs", () => {
const step = getBuildAllStep("build:plugin-sdk:dts");
expect(step.cache?.inputs).toEqual(expect.arrayContaining(["packages/memory-host-sdk/src"]));
expect(step.cache?.outputs).toEqual(expect.arrayContaining(["dist/plugin-sdk/packages"]));
});
});
describe("resolveBuildAllSteps", () => {
it("keeps the full profile aligned with the declared steps", () => {
expect(resolveBuildAllSteps("full")).toEqual(BUILD_ALL_STEPS);
expect(BUILD_ALL_PROFILES.full).toEqual(BUILD_ALL_STEPS.map((step) => step.label));
});
it("uses a runtime artifact plus plugin SDK export profile for ci artifacts", () => {
expect(resolveBuildAllSteps("ciArtifacts").map((step) => step.label)).toEqual([
"plugins:assets:build",
"tsdown",
"check-cli-bootstrap-imports",
"runtime-postbuild",
"build-stamp",
"runtime-postbuild-stamp",
"build:plugin-sdk:dts",
"write-plugin-sdk-entry-dts",
"check-plugin-sdk-exports",
"plugins:assets:copy",
"copy-hook-metadata",
"copy-export-html-templates",
"ui:build",
"write-build-info",
"write-cli-startup-metadata",
"write-cli-compat",
]);
});
it("uses a minimal built runtime profile for gateway watch regression", () => {
expect(resolveBuildAllSteps("gatewayWatch").map((step) => step.label)).toEqual([
"tsdown",
"check-cli-bootstrap-imports",
"runtime-postbuild",
"build-stamp",
"runtime-postbuild-stamp",
]);
});
it("uses a CLI startup profile without generated plugin assets", () => {
expect(resolveBuildAllSteps("cliStartup").map((step) => step.label)).toEqual([
"tsdown",
"check-cli-bootstrap-imports",
"runtime-postbuild",
"build-stamp",
"runtime-postbuild-stamp",
"write-cli-startup-metadata",
"write-cli-compat",
]);
});
it("writes the runtime postbuild stamp after the build stamp", () => {
const labels = resolveBuildAllSteps("full").map((step) => step.label);
expect(labels).toContain("runtime-postbuild");
expect(labels).toContain("build-stamp");
expect(labels).toContain("runtime-postbuild-stamp");
expect(labels.indexOf("runtime-postbuild-stamp")).toBeGreaterThan(
labels.indexOf("build-stamp"),
);
});
it("includes ui:build in the full and ciArtifacts profiles after runtime postbuild", () => {
for (const profile of ["full", "ciArtifacts"]) {
const labels = resolveBuildAllSteps(profile).map((step) => step.label);
expect(labels).toContain("ui:build");
// Control UI bundling must run after tsdown clears dist so that
// dist/control-ui survives `pnpm build` without a second command.
expect(labels.indexOf("ui:build")).toBeGreaterThan(labels.indexOf("tsdown"));
expect(labels.indexOf("ui:build")).toBeGreaterThan(labels.indexOf("runtime-postbuild-stamp"));
// ui:build must run before write-build-info so the build manifest can
// see the final dist/control-ui assets.
expect(labels.indexOf("ui:build")).toBeLessThan(labels.indexOf("write-build-info"));
}
});
it("keeps ui:build out of minimal backend-only profiles", () => {
for (const profile of ["gatewayWatch", "cliStartup"]) {
const labels = resolveBuildAllSteps(profile).map((step) => step.label);
expect(labels).not.toContain("ui:build");
}
});
it("does not cache ui:build because Vite reads package.json, git HEAD, and env metadata", () => {
// ui/vite.config.ts derives the Control UI build ID from package.json,
// git HEAD, and OPENCLAW_CONTROL_UI_BUILD_ID env, so a file-input
// signature cannot exactly invalidate generated assets. Leaving this
// step uncached avoids restoring stale service-worker/app cache
// metadata after `tsdown` clears `dist`.
const step = getBuildAllStep("ui:build");
expect(step.kind).toBe("pnpm");
expect(step.pnpmArgs).toEqual(["ui:build"]);
expect(step.cache).toBeUndefined();
});
it("does not cache plugin-sdk entry shims over compiled JS", () => {
const step = getBuildAllStep("write-plugin-sdk-entry-dts");
expect(step.cache).toBeUndefined();
});
it("does not cache hook metadata over compiled hook handlers", () => {
const step = getBuildAllStep("copy-hook-metadata");
expect(step.cache).toBeUndefined();
});
it("rejects unknown build profiles", () => {
expect(() => resolveBuildAllSteps("wat")).toThrow("Unknown build profile: wat");
});
});
describe("resolveBuildAllStepCacheState", () => {
it("marks cacheable steps fresh when the input signature matches", () => {
withBuildCacheFixture(({ rootDir, step }) => {
const cacheState = resolveBuildAllStepCacheState(step, { rootDir });
writeBuildAllStepCacheStamp(step, cacheState, { rootDir });
const fresh = resolveBuildAllStepCacheState(step, { rootDir });
expect(fresh.cacheable).toBe(true);
expect(fresh.fresh).toBe(true);
expect(fresh.reason).toBe("fresh");
expect(fresh.inputFiles).toBe(1);
expect(fresh.outputFiles).toBe(1);
expect(fresh.restorable).toBe(false);
expect(fresh.relativeOutputFiles).toEqual(["dist/output.js"]);
expect(fresh.stampedOutputs).toEqual(["dist/output.js"]);
expect(typeof fresh.signature).toBe("string");
expect(fresh.signature).toHaveLength(64);
expect(fresh.outputRoot).toBe(
path.join(rootDir, ".artifacts/build-all-cache/cached/outputs"),
);
expect(fresh.stampPath).toBe(
path.join(rootDir, ".artifacts/build-all-cache/cached/stamp.json"),
);
expect(fresh).toEqual({
cacheable: true,
fresh: true,
inputFiles: 1,
outputFiles: 1,
outputRoot: fresh.outputRoot,
reason: "fresh",
relativeOutputFiles: ["dist/output.js"],
restorable: false,
signature: fresh.signature,
stampedOutputs: ["dist/output.js"],
stampPath: fresh.stampPath,
});
});
});
it("marks cacheable steps stale when an input changes", () => {
withBuildCacheFixture(({ rootDir, inputPath, step }) => {
const cacheState = resolveBuildAllStepCacheState(step, { rootDir });
writeBuildAllStepCacheStamp(step, cacheState, { rootDir });
fs.writeFileSync(inputPath, "changed");
const stale = resolveBuildAllStepCacheState(step, { rootDir });
expect(stale.cacheable).toBe(true);
expect(stale.fresh).toBe(false);
expect(stale.reason).toBe("stale");
expect(stale.inputFiles).toBe(1);
expect(stale.outputFiles).toBe(1);
expect(stale.restorable).toBe(false);
expect(stale.relativeOutputFiles).toEqual(["dist/output.js"]);
expect(stale.stampedOutputs).toEqual(["dist/output.js"]);
expect(typeof stale.signature).toBe("string");
expect(stale.signature).toHaveLength(64);
expect(stale.outputRoot).toBe(
path.join(rootDir, ".artifacts/build-all-cache/cached/outputs"),
);
expect(stale.stampPath).toBe(
path.join(rootDir, ".artifacts/build-all-cache/cached/stamp.json"),
);
expect(stale).toEqual({
cacheable: true,
fresh: false,
inputFiles: 1,
outputFiles: 1,
outputRoot: stale.outputRoot,
reason: "stale",
relativeOutputFiles: ["dist/output.js"],
restorable: false,
signature: stale.signature,
stampedOutputs: ["dist/output.js"],
stampPath: stale.stampPath,
});
});
});
it("restores cached outputs when generated files were removed", () => {
withBuildCacheFixture(({ rootDir, outputPath, step }) => {
const cacheState = resolveBuildAllStepCacheState(step, { rootDir });
writeBuildAllStepCacheStamp(step, cacheState, { rootDir });
fs.rmSync(path.join(rootDir, "dist"), { force: true, recursive: true });
const restorable = resolveBuildAllStepCacheState(step, { rootDir });
expect(restorable.cacheable).toBe(true);
expect(restorable.fresh).toBe(true);
expect(restorable.reason).toBe("fresh-cache");
expect(restorable.inputFiles).toBe(1);
expect(restorable.outputFiles).toBe(0);
expect(restorable.restorable).toBe(true);
expect(restorable.relativeOutputFiles).toEqual([]);
expect(restorable.stampedOutputs).toEqual(["dist/output.js"]);
expect(typeof restorable.signature).toBe("string");
expect(restorable.signature).toHaveLength(64);
expect(restorable.outputRoot).toBe(
path.join(rootDir, ".artifacts/build-all-cache/cached/outputs"),
);
expect(restorable.stampPath).toBe(
path.join(rootDir, ".artifacts/build-all-cache/cached/stamp.json"),
);
expect(restorable).toEqual({
cacheable: true,
fresh: true,
inputFiles: 1,
outputFiles: 0,
outputRoot: restorable.outputRoot,
reason: "fresh-cache",
relativeOutputFiles: [],
restorable: true,
signature: restorable.signature,
stampedOutputs: ["dist/output.js"],
stampPath: restorable.stampPath,
});
expect(restoreBuildAllStepCacheOutputs(restorable, { rootDir })).toBe(true);
expect(fs.readFileSync(outputPath, "utf8")).toBe("output");
});
});
});