mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 14:11:35 +00:00
refactor(core): dedupe shared config and runtime helpers
This commit is contained in:
22
src/test-utils/chunk-test-helpers.ts
Normal file
22
src/test-utils/chunk-test-helpers.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export function countLines(text: string): number {
|
||||
return text.split("\n").length;
|
||||
}
|
||||
|
||||
export function hasBalancedFences(chunk: string): boolean {
|
||||
let open: { markerChar: string; markerLen: number } | null = null;
|
||||
for (const line of chunk.split("\n")) {
|
||||
const match = line.match(/^( {0,3})(`{3,}|~{3,})(.*)$/);
|
||||
if (!match) {
|
||||
continue;
|
||||
}
|
||||
const marker = match[2];
|
||||
if (!open) {
|
||||
open = { markerChar: marker[0], markerLen: marker.length };
|
||||
continue;
|
||||
}
|
||||
if (open.markerChar === marker[0] && marker.length >= open.markerLen) {
|
||||
open = null;
|
||||
}
|
||||
}
|
||||
return open === null;
|
||||
}
|
||||
16
src/test-utils/exec-assertions.ts
Normal file
16
src/test-utils/exec-assertions.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { expect } from "vitest";
|
||||
|
||||
export function expectSingleNpmInstallIgnoreScriptsCall(params: {
|
||||
calls: Array<[unknown, { cwd?: string } | undefined]>;
|
||||
expectedCwd: string;
|
||||
}) {
|
||||
const npmCalls = params.calls.filter((call) => Array.isArray(call[0]) && call[0][0] === "npm");
|
||||
expect(npmCalls.length).toBe(1);
|
||||
const first = npmCalls[0];
|
||||
if (!first) {
|
||||
throw new Error("expected npm install call");
|
||||
}
|
||||
const [argv, opts] = first;
|
||||
expect(argv).toEqual(["npm", "install", "--omit=dev", "--silent", "--ignore-scripts"]);
|
||||
expect(opts?.cwd).toBe(params.expectedCwd);
|
||||
}
|
||||
25
src/test-utils/mock-http-response.ts
Normal file
25
src/test-utils/mock-http-response.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { ServerResponse } from "node:http";
|
||||
|
||||
export function createMockServerResponse(): ServerResponse & { body?: string } {
|
||||
const headers: Record<string, string> = {};
|
||||
const res: {
|
||||
headersSent: boolean;
|
||||
statusCode: number;
|
||||
body?: string;
|
||||
setHeader: (key: string, value: string) => unknown;
|
||||
end: (body?: string) => unknown;
|
||||
} = {
|
||||
headersSent: false,
|
||||
statusCode: 200,
|
||||
setHeader: (key: string, value: string) => {
|
||||
headers[key.toLowerCase()] = value;
|
||||
return res;
|
||||
},
|
||||
end: (body?: string) => {
|
||||
res.headersSent = true;
|
||||
res.body = body;
|
||||
return res;
|
||||
},
|
||||
};
|
||||
return res as unknown as ServerResponse & { body?: string };
|
||||
}
|
||||
@@ -92,3 +92,18 @@ export async function getDeterministicFreePortBlock(params?: {
|
||||
|
||||
throw new Error("failed to acquire a free port block");
|
||||
}
|
||||
|
||||
export async function getFreePortBlockWithPermissionFallback(params: {
|
||||
offsets: number[];
|
||||
fallbackBase: number;
|
||||
}): Promise<number> {
|
||||
try {
|
||||
return await getDeterministicFreePortBlock({ offsets: params.offsets });
|
||||
} catch (err) {
|
||||
const code = (err as NodeJS.ErrnoException | undefined)?.code;
|
||||
if (code === "EPERM" || code === "EACCES") {
|
||||
return params.fallbackBase + (process.pid % 10_000);
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user