Skip to content

Commit 5620229

Browse files
committed
refactor: reuse e2e text tail helper
1 parent 58c46ec commit 5620229

1 file changed

Lines changed: 3 additions & 32 deletions

File tree

scripts/e2e/lib/agent-turn-output.mjs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs";
2+
import { readTextFileTail, tailText } from "./text-file-utils.mjs";
23

34
const ERROR_DETAIL_TAIL_BYTES = 64 * 1024;
45
const REPLY_TEXT_PREVIEW_BYTES = 8 * 1024;
@@ -15,13 +16,6 @@ function textByteLength(text) {
1516
return Buffer.byteLength(text, "utf8");
1617
}
1718

18-
function tailText(text, maxBytes = ERROR_DETAIL_TAIL_BYTES) {
19-
if (textByteLength(text) <= maxBytes) {
20-
return text;
21-
}
22-
return Buffer.from(text, "utf8").subarray(-maxBytes).toString("utf8");
23-
}
24-
2519
function summarizeReplyTexts(replyTexts) {
2620
const previewStart = Math.max(0, replyTexts.length - REPLY_TEXT_PREVIEW_COUNT);
2721
const recent = replyTexts.slice(previewStart).map((text, index) => ({
@@ -32,29 +26,6 @@ function summarizeReplyTexts(replyTexts) {
3226
return JSON.stringify({ count: replyTexts.length, recent });
3327
}
3428

35-
function readTextFileTail(file, maxBytes = ERROR_DETAIL_TAIL_BYTES) {
36-
let stat;
37-
try {
38-
stat = fs.statSync(file);
39-
} catch {
40-
return "";
41-
}
42-
if (!stat.isFile() || stat.size <= 0) {
43-
return "";
44-
}
45-
46-
const length = Math.min(maxBytes, stat.size);
47-
const start = stat.size - length;
48-
const fd = fs.openSync(file, "r");
49-
try {
50-
const buffer = Buffer.alloc(length);
51-
const bytesRead = fs.readSync(fd, buffer, 0, length, start);
52-
return buffer.subarray(0, bytesRead).toString("utf8");
53-
} finally {
54-
fs.closeSync(fd);
55-
}
56-
}
57-
5829
function fileContainsPattern(file, pattern) {
5930
let stat;
6031
try {
@@ -198,7 +169,7 @@ export function assertAgentReplyContainsMarker(marker, outputPath) {
198169
if (replyTexts.some((text) => text.includes(marker))) {
199170
return;
200171
}
201-
const outputTail = tailText(output);
172+
const outputTail = tailText(output, ERROR_DETAIL_TAIL_BYTES);
202173
throw new Error(
203174
`agent reply payload did not contain marker ${marker}. Reply payload summary: ${summarizeReplyTexts(replyTexts)}. Output tail: ${outputTail}`,
204175
);
@@ -208,6 +179,6 @@ export function assertOpenAiRequestLogUsed(requestLogPath, label = "mock OpenAI
208179
if (fileContainsPattern(requestLogPath, OPENAI_REQUEST_PATH_PATTERN)) {
209180
return;
210181
}
211-
const requestLogTail = readTextFileTail(requestLogPath);
182+
const requestLogTail = readTextFileTail(requestLogPath, ERROR_DETAIL_TAIL_BYTES);
212183
throw new Error(`${label} was not used. Request log tail: ${requestLogTail}`);
213184
}

0 commit comments

Comments
 (0)