Skip to content

Commit 1a34c48

Browse files
committed
fix(e2e): stream OpenAI web search request logs
1 parent d638611 commit 1a34c48

2 files changed

Lines changed: 210 additions & 13 deletions

File tree

‎scripts/e2e/lib/openai-web-search-minimal/assertions.mjs‎

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,97 @@ import fs from "node:fs";
22

33
const command = process.argv[2];
44

5+
const ERROR_DETAIL_TAIL_BYTES = 64 * 1024;
6+
const REQUEST_LOG_SCAN_CHUNK_BYTES = 64 * 1024;
7+
const RESPONSE_PREVIEW_BYTES = 8 * 1024;
8+
const RESPONSE_PREVIEW_COUNT = 5;
9+
10+
function tailText(text, maxBytes = ERROR_DETAIL_TAIL_BYTES) {
11+
if (Buffer.byteLength(text, "utf8") <= maxBytes) {
12+
return text;
13+
}
14+
return Buffer.from(text, "utf8").subarray(-maxBytes).toString("utf8");
15+
}
16+
17+
function readTextFileTail(file, maxBytes = ERROR_DETAIL_TAIL_BYTES) {
18+
let stat;
19+
try {
20+
stat = fs.statSync(file);
21+
} catch {
22+
return "";
23+
}
24+
if (!stat.isFile() || stat.size <= 0) {
25+
return "";
26+
}
27+
28+
const length = Math.min(maxBytes, stat.size);
29+
const start = stat.size - length;
30+
const fd = fs.openSync(file, "r");
31+
try {
32+
const buffer = Buffer.alloc(length);
33+
const bytesRead = fs.readSync(fd, buffer, 0, length, start);
34+
return buffer.subarray(0, bytesRead).toString("utf8");
35+
} finally {
36+
fs.closeSync(fd);
37+
}
38+
}
39+
40+
function scanTextFileLines(file, onLine) {
41+
const fd = fs.openSync(file, "r");
42+
try {
43+
const buffer = Buffer.alloc(REQUEST_LOG_SCAN_CHUNK_BYTES);
44+
let carry = "";
45+
let lineNumber = 1;
46+
while (true) {
47+
const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, null);
48+
if (bytesRead <= 0) {
49+
break;
50+
}
51+
const text = carry + buffer.subarray(0, bytesRead).toString("utf8");
52+
const lines = text.split(/\r?\n/u);
53+
carry = lines.pop() ?? "";
54+
for (const line of lines) {
55+
onLine(line, lineNumber);
56+
lineNumber += 1;
57+
}
58+
}
59+
if (carry.length > 0) {
60+
onLine(carry, lineNumber);
61+
}
62+
} finally {
63+
fs.closeSync(fd);
64+
}
65+
}
66+
67+
function scanSuccessRequest(logPath) {
68+
let responseCount = 0;
69+
let success;
70+
const recentResponses = [];
71+
scanTextFileLines(logPath, (line, lineNumber) => {
72+
const trimmed = line.trim();
73+
if (!trimmed) {
74+
return;
75+
}
76+
const entry = JSON.parse(trimmed);
77+
if (entry.path !== "/v1/responses") {
78+
return;
79+
}
80+
responseCount += 1;
81+
const bodyText = JSON.stringify(entry.body);
82+
if (recentResponses.length >= RESPONSE_PREVIEW_COUNT) {
83+
recentResponses.shift();
84+
}
85+
recentResponses.push({
86+
line: lineNumber,
87+
bodyTail: tailText(bodyText, RESPONSE_PREVIEW_BYTES),
88+
});
89+
if (!success && bodyText.includes("OPENCLAW_SCHEMA_E2E_OK")) {
90+
success = entry;
91+
}
92+
});
93+
return { responseCount, success, recentResponses };
94+
}
95+
596
function assertPatchBehavior() {
697
return import("../../../../dist/extensions/openai/native-web-search.js").then(
798
({ patchOpenAINativeWebSearchPayload }) => {
@@ -40,21 +131,16 @@ function assertPatchBehavior() {
40131

41132
function assertSuccessRequest() {
42133
const logPath = process.argv[3];
43-
const entries = fs
44-
.readFileSync(logPath, "utf8")
45-
.trim()
46-
.split(/\n+/u)
47-
.filter(Boolean)
48-
.map((line) => JSON.parse(line));
49-
const responseEntries = entries.filter((entry) => entry.path === "/v1/responses");
50-
if (responseEntries.length < 1) {
51-
throw new Error(`mock OpenAI /v1/responses was not used. Requests: ${JSON.stringify(entries)}`);
134+
const { responseCount, success, recentResponses } = scanSuccessRequest(logPath);
135+
if (responseCount < 1) {
136+
throw new Error(
137+
`mock OpenAI /v1/responses was not used. Request log tail: ${readTextFileTail(logPath)}`,
138+
);
52139
}
53-
const success = responseEntries.find((entry) =>
54-
JSON.stringify(entry.body).includes("OPENCLAW_SCHEMA_E2E_OK"),
55-
);
56140
if (!success) {
57-
throw new Error(`missing success request. Requests: ${JSON.stringify(responseEntries)}`);
141+
throw new Error(
142+
`missing success request. Recent /v1/responses: ${JSON.stringify(recentResponses)}`,
143+
);
58144
}
59145
const tools = Array.isArray(success.body.tools) ? success.body.tools : [];
60146
const hasWebSearch = tools.some(
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { spawnSync } from "node:child_process";
2+
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+
import { tmpdir } from "node:os";
4+
import path from "node:path";
5+
import { describe, expect, it } from "vitest";
6+
7+
const ASSERTIONS_SCRIPT = "scripts/e2e/lib/openai-web-search-minimal/assertions.mjs";
8+
9+
function runAssertSuccessRequest(logPath: string) {
10+
return spawnSync(process.execPath, [ASSERTIONS_SCRIPT, "assert-success-request", logPath], {
11+
encoding: "utf8",
12+
});
13+
}
14+
15+
describe("openai web-search minimal assertions", () => {
16+
it("accepts a success request with web_search and non-minimal reasoning", () => {
17+
const dir = mkdtempSync(path.join(tmpdir(), "openclaw-web-search-minimal-"));
18+
try {
19+
const logPath = path.join(dir, "requests.jsonl");
20+
writeFileSync(
21+
logPath,
22+
`${JSON.stringify({
23+
body: {
24+
input: "OPENCLAW_SCHEMA_E2E_OK",
25+
reasoning: { effort: "low" },
26+
tools: [{ type: "web_search" }],
27+
},
28+
path: "/v1/responses",
29+
})}\n`,
30+
);
31+
32+
expect(runAssertSuccessRequest(logPath).status).toBe(0);
33+
} finally {
34+
rmSync(dir, { force: true, recursive: true });
35+
}
36+
});
37+
38+
it("finds success requests split across large scan chunks", () => {
39+
const dir = mkdtempSync(path.join(tmpdir(), "openclaw-web-search-minimal-"));
40+
try {
41+
const logPath = path.join(dir, "requests.jsonl");
42+
writeFileSync(
43+
logPath,
44+
`${JSON.stringify({ path: "/health", body: { pad: "x".repeat(70 * 1024) } })}\n${JSON.stringify(
45+
{
46+
body: {
47+
input: "OPENCLAW_SCHEMA_E2E_OK",
48+
reasoning: { effort: "low" },
49+
tools: [{ type: "web_search" }],
50+
},
51+
path: "/v1/responses",
52+
},
53+
)}\n`,
54+
);
55+
56+
expect(runAssertSuccessRequest(logPath).status).toBe(0);
57+
} finally {
58+
rmSync(dir, { force: true, recursive: true });
59+
}
60+
});
61+
62+
it("bounds diagnostics when the OpenAI responses endpoint was not used", () => {
63+
const dir = mkdtempSync(path.join(tmpdir(), "openclaw-web-search-minimal-"));
64+
try {
65+
const logPath = path.join(dir, "requests.jsonl");
66+
writeFileSync(
67+
logPath,
68+
`${JSON.stringify({
69+
body: {
70+
old: `DO_NOT_DUMP_OLD_REQUESTS${"x".repeat(70 * 1024)}`,
71+
},
72+
path: "/health",
73+
})}\n`,
74+
);
75+
76+
const result = runAssertSuccessRequest(logPath);
77+
78+
expect(result.status).not.toBe(0);
79+
expect(result.stderr).toContain("Request log tail:");
80+
expect(result.stderr).not.toContain("DO_NOT_DUMP_OLD_REQUESTS");
81+
} finally {
82+
rmSync(dir, { force: true, recursive: true });
83+
}
84+
});
85+
86+
it("bounds diagnostics when no success response is present", () => {
87+
const dir = mkdtempSync(path.join(tmpdir(), "openclaw-web-search-minimal-"));
88+
try {
89+
const logPath = path.join(dir, "requests.jsonl");
90+
writeFileSync(
91+
logPath,
92+
`${JSON.stringify({
93+
body: {
94+
input: `DO_NOT_DUMP_OLD_RESPONSE${"x".repeat(70 * 1024)}recent response tail`,
95+
tools: [{ type: "web_search" }],
96+
},
97+
path: "/v1/responses",
98+
})}\n`,
99+
);
100+
101+
const result = runAssertSuccessRequest(logPath);
102+
103+
expect(result.status).not.toBe(0);
104+
expect(result.stderr).toContain("Recent /v1/responses:");
105+
expect(result.stderr).toContain("recent response tail");
106+
expect(result.stderr).not.toContain("DO_NOT_DUMP_OLD_RESPONSE");
107+
} finally {
108+
rmSync(dir, { force: true, recursive: true });
109+
}
110+
});
111+
});

0 commit comments

Comments
 (0)