Files
openclaw/scripts/lib/gateway-server-test-plan.mts
Peter Steinberger c70aee247e refactor(scripts): migrate JavaScript tools to TypeScript (#121005)
* refactor(scripts): migrate JavaScript tools to TypeScript

* fix(ci): keep changed-scope preflight zero-install

* fix(ci): preserve zero-install script owners

* fix(ci): complete script migration follow-through

* fix(release): keep stable closeout zero-install

* fix(scripts): preserve standalone execution boundaries

* fix(scripts): repair standalone loader boundaries

* fix(scripts): normalize gateway observation ids

* fix(scripts): keep Docker packager standalone

* test(scripts): preserve rebase cleanup helpers

* test(sessions): use tracked temp directory
2026-08-09 07:21:35 -07:00

73 lines
2.4 KiB
TypeScript

// Bounds the non-isolated Gateway server project before its shared module graph
// reaches the V8 worker heap limit.
import { spawnSync } from "node:child_process";
import fs from "node:fs";
import path from "node:path";
import { isGatewayServerTestFile } from "../../test/vitest/vitest.gateway-server-paths.mjs";
export const GATEWAY_SERVER_TEST_PROCESS_COUNT = 4;
function normalizePath(value: string): string {
return value.replaceAll("\\", "/");
}
function listRepoFilesRecursive(root: string, cwd: string): string[] {
const entries = fs.readdirSync(root, { withFileTypes: true });
return entries.flatMap((entry) => {
const absolute = path.join(root, entry.name);
if (entry.isDirectory()) {
return listRepoFilesRecursive(absolute, cwd);
}
return entry.isFile() ? [normalizePath(path.relative(cwd, absolute))] : [];
});
}
function listGatewayFilesFromGit(cwd: string): string[] | null {
const result = spawnSync("git", ["ls-files", "--", "src/gateway"], {
cwd,
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
if (result.status !== 0) {
return null;
}
return result.stdout
.split("\n")
.map((line) => normalizePath(line.trim()))
.filter((line) => line.length > 0);
}
export function listGatewayServerTestTargets(cwd = process.cwd()): string[] {
const gatewayDir = path.join(cwd, "src/gateway");
if (!fs.existsSync(gatewayDir)) {
return [];
}
return (listGatewayFilesFromGit(cwd) ?? listRepoFilesRecursive(gatewayDir, cwd))
.filter(isGatewayServerTestFile)
.toSorted((a, b) => a.localeCompare(b));
}
export function splitTestTargetChunks(targets: string[], chunkCount: number): string[][] {
if (targets.length === 0) {
return [];
}
const normalizedChunkCount = Math.min(chunkCount, targets.length);
const baseSize = Math.floor(targets.length / normalizedChunkCount);
const remainder = targets.length % normalizedChunkCount;
const chunks: string[][] = [];
let offset = 0;
for (let index = 0; index < normalizedChunkCount; index += 1) {
const chunkSize = baseSize + (index < remainder ? 1 : 0);
chunks.push(targets.slice(offset, offset + chunkSize));
offset += chunkSize;
}
return chunks;
}
export function createGatewayServerTestTargetChunks(cwd = process.cwd()): string[][] {
return splitTestTargetChunks(
listGatewayServerTestTargets(cwd),
GATEWAY_SERVER_TEST_PROCESS_COUNT,
);
}