test: trim UI and entry test overhead

This commit is contained in:
Peter Steinberger
2026-04-17 20:23:07 +01:00
parent 087f1584df
commit 809f42eeea
4 changed files with 39 additions and 35 deletions

View File

@@ -12,9 +12,6 @@
* do not show AI-facing envelope metadata as user text.
*/
import { z } from "zod";
import { safeParseJsonWithSchema } from "../../utils/zod-parse.js";
const LEADING_TIMESTAMP_PREFIX_RE = /^\[[A-Za-z]{3} \d{4}-\d{2}-\d{2} \d{2}:\d{2}[^\]]*\] */;
/**
@@ -35,7 +32,6 @@ const UNTRUSTED_CONTEXT_HEADER =
const ACTIVE_MEMORY_OPEN_TAG = "<active_memory_plugin>";
const ACTIVE_MEMORY_CLOSE_TAG = "</active_memory_plugin>";
const [CONVERSATION_INFO_SENTINEL, SENDER_INFO_SENTINEL] = INBOUND_META_SENTINELS;
const InboundMetaBlockSchema = z.record(z.string(), z.unknown());
// Pre-compiled fast-path regex — avoids line-by-line parse when no blocks present.
const SENTINEL_FAST_RE = new RegExp(
@@ -64,6 +60,18 @@ function restoreNeutralizedMarkdownFences(value: unknown): unknown {
);
}
function parseJsonObjectRecord(jsonText: string): Record<string, unknown> | null {
try {
const parsed: unknown = JSON.parse(jsonText);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
return null;
}
return parsed as Record<string, unknown>;
} catch {
return null;
}
}
function parseInboundMetaBlock(lines: string[], sentinel: string): Record<string, unknown> | null {
for (let i = 0; i < lines.length; i++) {
if (lines[i]?.trim() !== sentinel) {
@@ -86,7 +94,7 @@ function parseInboundMetaBlock(lines: string[], sentinel: string): Record<string
if (!jsonText) {
return null;
}
const parsed = safeParseJsonWithSchema(InboundMetaBlockSchema, jsonText);
const parsed = parseJsonObjectRecord(jsonText);
return parsed ? (restoreNeutralizedMarkdownFences(parsed) as Record<string, unknown>) : null;
}
return null;

View File

@@ -79,6 +79,12 @@ async function importEntry(scope: string) {
);
}
async function flushEntrySideEffects() {
await Promise.resolve();
await Promise.resolve();
await new Promise((resolve) => setTimeout(resolve, 0));
}
describe("entry root version fast path", () => {
let originalArgv: string[];
let originalGatewayToken: string | undefined;
@@ -109,13 +115,9 @@ describe("entry root version fast path", () => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await importEntry("commit-tagged");
await vi.waitFor(
() => {
expect(logSpy).toHaveBeenCalledWith("OpenClaw 9.9.9-test (abc1234)");
expect(exitSpy).toHaveBeenCalledWith(0);
},
{ interval: 1 },
);
await flushEntrySideEffects();
expect(logSpy).toHaveBeenCalledWith("OpenClaw 9.9.9-test (abc1234)");
expect(exitSpy).toHaveBeenCalledWith(0);
logSpy.mockRestore();
});
@@ -125,13 +127,9 @@ describe("entry root version fast path", () => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await importEntry("plain-version");
await vi.waitFor(
() => {
expect(logSpy).toHaveBeenCalledWith("OpenClaw 9.9.9-test");
expect(exitSpy).toHaveBeenCalledWith(0);
},
{ interval: 1 },
);
await flushEntrySideEffects();
expect(logSpy).toHaveBeenCalledWith("OpenClaw 9.9.9-test");
expect(exitSpy).toHaveBeenCalledWith(0);
logSpy.mockRestore();
});
@@ -141,12 +139,8 @@ describe("entry root version fast path", () => {
const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await importEntry("container-target");
await vi.waitFor(
() => {
expect(runCliMock).toHaveBeenCalledWith(["node", "openclaw", "--version"]);
},
{ interval: 1 },
);
await flushEntrySideEffects();
expect(runCliMock).toHaveBeenCalledWith(["node", "openclaw", "--version"]);
expect(logSpy).not.toHaveBeenCalled();
expect(exitSpy).not.toHaveBeenCalled();
@@ -159,12 +153,8 @@ describe("entry root version fast path", () => {
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
await importEntry("gateway-override");
await vi.waitFor(
() => {
expect(runCliMock).toHaveBeenCalledWith(["node", "openclaw", "--version"]);
},
{ interval: 1 },
);
await flushEntrySideEffects();
expect(runCliMock).toHaveBeenCalledWith(["node", "openclaw", "--version"]);
expect(errorSpy).not.toHaveBeenCalled();
expect(exitSpy).not.toHaveBeenCalled();