Skip to content

Commit af52e62

Browse files
authored
fix(test): prepare shared builds before parallel workers (#131297)
1 parent 4ef9446 commit af52e62

13 files changed

Lines changed: 430 additions & 115 deletions

‎extensions/qa-lab/src/suite-process-lifecycle.test-support.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import path from "node:path";
22
import { fileURLToPath } from "node:url";
33
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
4-
import { resolveQaGatewayChildCommand } from "./gateway-child-command.js";
54
import { runQaSuite } from "./suite-launch.runtime.js";
65

76
const repoRoot = fileURLToPath(new URL("../../../", import.meta.url));
@@ -13,17 +12,12 @@ if (!outputDir || scenarioIds.length === 0) {
1312
}
1413

1514
try {
16-
const sutOpenClawCommand = {
17-
...resolveQaGatewayChildCommand(repoRoot),
18-
usePackagedPlugins: false,
19-
};
2015
const result = await runQaSuite({
2116
repoRoot,
2217
outputDir: path.relative(repoRoot, outputDir),
2318
providerMode: "mock-openai",
2419
scenarioIds,
2520
concurrency: 4,
26-
sutOpenClawCommand,
2721
});
2822
const failed = result.result.scenarios.filter((scenario) => scenario.status !== "pass");
2923
if (failed.length > 0) {

‎extensions/qa-lab/src/suite-process-lifecycle.test.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { spawn, spawnSync, type ChildProcess } from "node:child_process";
2-
import { existsSync } from "node:fs";
32
import fs from "node:fs/promises";
43
import net from "node:net";
54
import path from "node:path";
@@ -33,13 +32,8 @@ function buildSuiteProcessEnv(outputDir: string) {
3332
OPENCLAW_HOME: home,
3433
OPENCLAW_STATE_DIR: path.join(home, ".openclaw"),
3534
OPENCLAW_CONFIG_PATH: path.join(home, ".openclaw", "openclaw.json"),
36-
OPENCLAW_BUILD_PRIVATE_QA: "1",
3735
OPENCLAW_QA_SUITE_PROGRESS: "1",
38-
OPENCLAW_RUN_NODE_SKIP_DTS_BUILD: "1",
3936
};
40-
if (!existsSync(path.join(repoRoot, "dist", "index.js"))) {
41-
env.OPENCLAW_FORCE_BUILD = "1";
42-
}
4337
delete env.VITEST;
4438
delete env.VITEST_POOL_ID;
4539
delete env.VITEST_WORKER_ID;

‎scripts/lib/ci-changed-node-test-plan.mts‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ import {
2222
splitExtensionTestJobTargets,
2323
} from "./extension-test-plan.mts";
2424
import { buildPluginSdkEntrySources, publicPluginSdkEntrypoints } from "./plugin-sdk-entries.mts";
25+
import {
26+
resolveVitestPretestBuildMode,
27+
type VitestPretestBuildMode,
28+
} from "./vitest-build-prerequisites.mts";
2529

2630
type ChangedNodeTestShard = {
2731
checkName: string;
2832
configs: string[];
2933
includePatterns?: string[];
3034
planConcurrency?: number;
31-
pretestBuildMode?: "private-qa" | "runtime";
35+
pretestBuildMode?: VitestPretestBuildMode;
3236
requiresDist: boolean;
3337
runner: string;
3438
shardName: string;
@@ -45,12 +49,6 @@ const CHANGED_NODE_TEST_TARGETS_PER_JOB = 12;
4549
// processes starve each other on 4-vCPU runners and push otherwise healthy
4650
// integration tests past the global timeout.
4751
const SERIAL_CHANGED_TARGET_RE = /^extensions\/memory-core\//u;
48-
const PRETEST_RUNTIME_BUILD_TARGETS = new Set([
49-
"test/e2e/qa-lab/runtime/gateway-support-export-runtime.test.ts",
50-
]);
51-
const PRETEST_PRIVATE_QA_BUILD_TARGETS = new Set([
52-
"extensions/qa-lab/src/suite-process-lifecycle.test.ts",
53-
]);
5452
const BOUNDARY_NODE_TEST_CONFIG = "test/vitest/vitest.boundary.config.ts";
5553
const DOCKER_SEED_LANE_ORDER = [
5654
"mcp-channels",
@@ -326,10 +324,9 @@ function createChangedTargetShards(
326324
shardName: `${names.shardName}${suffix}`,
327325
targets: chunk,
328326
};
329-
if (chunk.some((target) => PRETEST_PRIVATE_QA_BUILD_TARGETS.has(target))) {
330-
shard.pretestBuildMode = "private-qa";
331-
} else if (chunk.some((target) => PRETEST_RUNTIME_BUILD_TARGETS.has(target))) {
332-
shard.pretestBuildMode = "runtime";
327+
const pretestBuildMode = resolveVitestPretestBuildMode([{ includePatterns: chunk }]);
328+
if (pretestBuildMode) {
329+
shard.pretestBuildMode = pretestBuildMode;
333330
}
334331
if (chunk.some((target) => SERIAL_CHANGED_TARGET_RE.test(target))) {
335332
shard.planConcurrency = 1;
@@ -375,8 +372,11 @@ function createChangedExtensionConfigShards(extensionRoots: string[]) {
375372
runner: DEFAULT_NODE_TEST_RUNNER,
376373
shardName: `changed-extensions-config${suffix}`,
377374
};
378-
if (roots.includes("extensions/qa-lab")) {
379-
shard.pretestBuildMode = "private-qa";
375+
const pretestBuildMode = resolveVitestPretestBuildMode([
376+
{ configs: [config], includePatterns },
377+
]);
378+
if (pretestBuildMode) {
379+
shard.pretestBuildMode = pretestBuildMode;
380380
}
381381
if (includePatterns) {
382382
shard.includePatterns = includePatterns;

‎scripts/lib/ci-node-test-plan.mts‎

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ import {
2222
} from "../../test/vitest/vitest.unit-fast-paths.mjs";
2323
import { boundaryTestFiles, isUnitConfigTestFile } from "../../test/vitest/vitest.unit-paths.mjs";
2424
import { listTrackedTestFiles } from "./list-test-files.mts";
25+
import {
26+
resolveVitestPretestBuildMode,
27+
type VitestPretestBuildMode as NodeTestPretestBuildMode,
28+
} from "./vitest-build-prerequisites.mts";
2529

2630
type NodeTestShardGroup = {
2731
shard_name: string;
@@ -58,15 +62,6 @@ type NodeTestPlanOptions = {
5862
};
5963

6064
type CompactNodeTestPlanMode = "pull-request" | "push";
61-
type NodeTestPretestBuildMode = "private-qa" | "runtime";
62-
63-
const PRETEST_RUNTIME_BUILD_FILES = new Set([
64-
"test/e2e/qa-lab/runtime/gateway-support-export-runtime.test.ts",
65-
]);
66-
67-
function resolvePretestBuildMode(paths: readonly string[]): NodeTestPretestBuildMode | undefined {
68-
return paths.some((file) => PRETEST_RUNTIME_BUILD_FILES.has(file)) ? "runtime" : undefined;
69-
}
7065

7166
type PolicyTestWatch = {
7267
ownerGlobs?: readonly string[];
@@ -137,7 +132,7 @@ type CompactNodeTestShard = Omit<NodeTestShard, "configs" | "groups"> & {
137132
groups: NodeTestShardGroup[];
138133
};
139134

140-
type NodeTestSplitShard = Omit<NodeTestShard, "checkName" | "runner"> & {
135+
type NodeTestSplitShard = Omit<NodeTestShard, "checkName" | "runner" | "pretestBuildMode"> & {
141136
includeExternalConfigs?: boolean;
142137
runner?: string;
143138
};
@@ -1588,19 +1583,12 @@ function createToolingSplitShards(): NodeTestSplitShard[] {
15881583
listCompactToolingTestFiles(),
15891584
COMPACT_TOOLING_NODE_TEST_GROUPS,
15901585
stripeFileWeight,
1591-
).map((includePatterns, index) => {
1592-
const pretestBuildMode = resolvePretestBuildMode(includePatterns);
1593-
const shard: NodeTestSplitShard = {
1594-
shardName: `core-tooling-${index + 1}`,
1595-
configs: [TOOLING_CONFIG],
1596-
includePatterns,
1597-
requiresDist: false,
1598-
};
1599-
if (pretestBuildMode) {
1600-
shard.pretestBuildMode = pretestBuildMode;
1601-
}
1602-
return shard;
1603-
}),
1586+
).map((includePatterns, index) => ({
1587+
shardName: `core-tooling-${index + 1}`,
1588+
configs: [TOOLING_CONFIG],
1589+
includePatterns,
1590+
requiresDist: false,
1591+
})),
16041592
{
16051593
shardName: "core-tooling-isolated",
16061594
configs: ["test/vitest/vitest.tooling-docker.config.ts", TOOLING_ISOLATED_CONFIG],
@@ -1908,28 +1896,32 @@ export function createNodeTestShards(options: NodeTestPlanOptions = {}): NodeTes
19081896
return [];
19091897
}
19101898

1899+
const pretestBuildMode = resolveVitestPretestBuildMode([
1900+
{ configs: splitConfigs, includePatterns: splitShard.includePatterns },
1901+
]);
1902+
19111903
return [
19121904
{
19131905
checkName: formatNodeTestShardCheckName(splitShard.shardName),
19141906
shardName: splitShard.shardName,
19151907
configs: splitConfigs,
19161908
...(splitShard.env ? { env: splitShard.env } : {}),
19171909
...(splitShard.includePatterns ? { includePatterns: splitShard.includePatterns } : {}),
1918-
...(splitShard.pretestBuildMode
1919-
? { pretestBuildMode: splitShard.pretestBuildMode }
1920-
: {}),
1910+
...(pretestBuildMode ? { pretestBuildMode } : {}),
19211911
runner: splitShard.runner ?? DEFAULT_NODE_TEST_RUNNER,
19221912
requiresDist: splitShard.requiresDist,
19231913
},
19241914
];
19251915
});
19261916
}
19271917

1918+
const pretestBuildMode = resolveVitestPretestBuildMode([{ configs }]);
19281919
return [
19291920
{
19301921
checkName: formatNodeTestShardCheckName(shard.name),
19311922
shardName: shard.name,
19321923
configs,
1924+
...(pretestBuildMode ? { pretestBuildMode } : {}),
19331925
runner: DEFAULT_NODE_TEST_RUNNER,
19341926
requiresDist: DIST_DEPENDENT_NODE_SHARD_NAMES.has(shard.name),
19351927
},
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { spawn } from "node:child_process";
2+
import { matchesGlob } from "node:path";
3+
import { fullSuiteVitestShards } from "../../test/vitest/vitest.test-shards.mjs";
4+
5+
export type VitestPretestBuildMode = "private-qa" | "runtime";
6+
type SetupCommandRunner = (args: string[], env: NodeJS.ProcessEnv) => Promise<number>;
7+
8+
type TestSelection = {
9+
configs?: readonly string[];
10+
includePatterns?: readonly string[] | null;
11+
};
12+
13+
// These process tests consume built runtime artifacts. Prepare their strongest
14+
// prerequisite before admitting any workers: a child build invalidates dist
15+
// while unrelated workers may still be importing its public plugin facades.
16+
// Strongest first: a private-QA build also satisfies ordinary runtime readers.
17+
const runtimeConsumers = [
18+
{
19+
file: "extensions/qa-lab/src/suite-process-lifecycle.test.ts",
20+
config: "test/vitest/vitest.extension-qa.config.ts",
21+
mode: "private-qa",
22+
},
23+
{
24+
file: "test/e2e/qa-lab/runtime/gateway-support-export-runtime.test.ts",
25+
config: "test/vitest/vitest.tooling.config.ts",
26+
mode: "runtime",
27+
},
28+
] as const;
29+
30+
export function resolveVitestPretestBuildMode(
31+
selections: readonly TestSelection[],
32+
): VitestPretestBuildMode | undefined {
33+
return runtimeConsumers.find(({ file, config }) =>
34+
selections.some(({ configs, includePatterns }) =>
35+
includePatterns
36+
? includePatterns.some((pattern) => matchesGlob(file, pattern))
37+
: configs?.some(
38+
(selected) =>
39+
selected === config ||
40+
selected === "vitest.config.ts" ||
41+
selected === "test/vitest/vitest.config.ts" ||
42+
fullSuiteVitestShards.some(
43+
(shard) => shard.config === selected && shard.projects.includes(config),
44+
),
45+
),
46+
),
47+
)?.mode;
48+
}
49+
50+
export function isE2eBuildSkipped(env: NodeJS.ProcessEnv) {
51+
return env.OPENCLAW_E2E_SKIP_BUILD === "1" || env.OPENCLAW_E2E_USE_PREBUILT_DIST === "1";
52+
}
53+
54+
function runE2eSetupCommand(args: string[], env: NodeJS.ProcessEnv): Promise<number> {
55+
const child = spawn(process.execPath, args, {
56+
cwd: process.cwd(),
57+
detached: false,
58+
env,
59+
stdio: ["inherit", "pipe", "pipe"],
60+
});
61+
child.stdout.pipe(process.stdout, { end: false });
62+
child.stderr.pipe(process.stderr, { end: false });
63+
64+
return new Promise((resolve, reject) => {
65+
child.once("error", reject);
66+
child.once("close", (status, signal) => {
67+
if (signal) {
68+
reject(new Error(`E2E setup command terminated by ${signal}: ${args.join(" ")}`));
69+
return;
70+
}
71+
resolve(status ?? 1);
72+
});
73+
});
74+
}
75+
76+
export async function runE2eGlobalSetup(
77+
runCommand: SetupCommandRunner = runE2eSetupCommand,
78+
env: NodeJS.ProcessEnv = process.env,
79+
): Promise<void> {
80+
// Focused suites may own their fixtures; prebuilt consumers already have the
81+
// complete surface. Neither may start another shared artifact writer.
82+
if (isE2eBuildSkipped(env)) {
83+
return;
84+
}
85+
const commands = [
86+
{
87+
args: ["scripts/run-node.mjs", "--version"],
88+
env: {
89+
...env,
90+
OPENCLAW_BUILD_PRIVATE_QA: "1",
91+
OPENCLAW_RUN_NODE_SKIP_DTS_BUILD: "0",
92+
},
93+
},
94+
{
95+
args: ["--import", "tsx", "scripts/tsdown-build.mts", "--config", "tsdown.ai.config.ts"],
96+
env,
97+
},
98+
];
99+
for (const { args, env: commandEnv } of commands) {
100+
const status = await runCommand(args, commandEnv);
101+
if (status !== 0) {
102+
throw new Error(`E2E setup command failed with exit code ${status}: ${args.join(" ")}`);
103+
}
104+
}
105+
}

‎scripts/test-projects.mts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import fs from "node:fs";
55
import { performance } from "node:perf_hooks";
66
import pMap from "p-map";
77
import { formatMs } from "./lib/check-timing-summary.mts";
8+
import { runManagedCommand } from "./lib/managed-child-process.mts";
9+
import {
10+
isE2eBuildSkipped,
11+
resolveVitestPretestBuildMode,
12+
runE2eGlobalSetup,
13+
} from "./lib/vitest-build-prerequisites.mts";
814
import {
915
isCiLikeEnv,
1016
resolveLocalFullSuiteProfile,
@@ -304,6 +310,35 @@ async function main() {
304310
return;
305311
}
306312

313+
const pretestBuildMode = resolveVitestPretestBuildMode(
314+
runSpecs.map((spec) => ({ configs: [spec.config], includePatterns: spec.includePatterns })),
315+
);
316+
const runBuildCommand = (commandArgs: string[], env: NodeJS.ProcessEnv) =>
317+
runManagedCommand({ bin: process.execPath, args: commandArgs, cwd: process.cwd(), env });
318+
const e2eSpecs = runSpecs.filter((spec) => spec.config === "test/vitest/vitest.e2e.config.ts");
319+
if (e2eSpecs.length > 0) {
320+
if (!isE2eBuildSkipped(baseEnv)) {
321+
console.error("[test] preparing E2E runtime before Vitest workers");
322+
await runE2eGlobalSetup(runBuildCommand, baseEnv);
323+
// E2E preparation also covers runtime/private-QA readers. Only a completed
324+
// owner may tell config-level setup to reuse that shared generation.
325+
for (const spec of e2eSpecs) {
326+
spec.env = { ...spec.env, OPENCLAW_E2E_USE_PREBUILT_DIST: "1" };
327+
}
328+
}
329+
} else if (pretestBuildMode) {
330+
console.error(`[test] preparing ${pretestBuildMode} runtime before Vitest workers`);
331+
const code = await runBuildCommand(["scripts/run-node.mjs", "--version"], {
332+
...baseEnv,
333+
...(pretestBuildMode === "private-qa" ? { OPENCLAW_BUILD_PRIVATE_QA: "1" } : {}),
334+
});
335+
if (code !== 0) {
336+
printTestSummary("failed", 0, performance.now() - suiteStartedAt);
337+
process.exitCode = code;
338+
return;
339+
}
340+
}
341+
307342
const isFullSuiteRun =
308343
targetArgs.length === 0 &&
309344
changedTargetArgs === null &&

‎test/scripts/ci-changed-node-test-plan.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,17 @@ describe("CI changed Node test plan", () => {
543543
]);
544544
});
545545

546+
it("routes lifecycle edits to the prepared QA config without losing boundary coverage", () => {
547+
const target = "extensions/qa-lab/src/suite-process-lifecycle.test.ts";
548+
expect(createChangedNodeTestShards([target])).toEqual([
549+
expect.objectContaining({
550+
configs: ["test/vitest/vitest.extension-qa.config.ts"],
551+
pretestBuildMode: "private-qa",
552+
}),
553+
expect.objectContaining({ configs: ["test/vitest/vitest.boundary.config.ts"] }),
554+
]);
555+
});
556+
546557
it("fails safe when a targeted config needs special shard setup", () => {
547558
expect(createChangedNodeTestShards(["scripts/docs-i18n/main.go"])).toBeNull();
548559
expect(createChangedNodeTestShards(["src/tui/tui-pty-harness.e2e.test.ts"])).toBeNull();

0 commit comments

Comments
 (0)