





















@@ -0,0 +1,96 @@
1+import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
2+import { tmpdir } from "node:os";
3+import { join } from "node:path";
4+import { describe, expect, it } from "vitest";
5+import {
6+assertAgentReplyContainsMarker,
7+assertOpenAiRequestLogUsed,
8+extractAgentReplyTexts,
9+} from "../../scripts/e2e/lib/agent-turn-output.mjs";
10+11+describe("scripts/e2e/lib/agent-turn-output", () => {
12+it("extracts local and gateway agent reply payload text", () => {
13+expect(
14+extractAgentReplyTexts(
15+JSON.stringify({
16+payloads: [{ text: "OPENCLAW_E2E_OK_LOCAL" }],
17+meta: { finalAssistantVisibleText: "visible" },
18+}),
19+),
20+).toEqual(["visible", "OPENCLAW_E2E_OK_LOCAL"]);
21+22+expect(
23+extractAgentReplyTexts(
24+JSON.stringify({
25+result: {
26+payloads: [{ text: "OPENCLAW_E2E_OK_GATEWAY" }],
27+meta: { finalAssistantRawText: "raw" },
28+},
29+}),
30+),
31+).toEqual(["raw", "OPENCLAW_E2E_OK_GATEWAY"]);
32+});
33+34+it("reads compact JSON replies from combined stdout and stderr logs", () => {
35+expect(
36+extractAgentReplyTexts(
37+[
38+"warning: diagnostic on stderr",
39+JSON.stringify({ payloads: [{ text: "OPENCLAW_E2E_OK_COMBINED" }] }),
40+].join("\n"),
41+),
42+).toEqual(["OPENCLAW_E2E_OK_COMBINED"]);
43+});
44+45+it("reads pretty JSON replies from combined stdout and stderr logs", () => {
46+expect(
47+extractAgentReplyTexts(
48+[
49+"warning: diagnostic on stderr",
50+JSON.stringify(
51+{
52+payloads: [{ text: "OPENCLAW_E2E_OK_PRETTY" }],
53+},
54+null,
55+2,
56+),
57+].join("\n"),
58+),
59+).toEqual(["OPENCLAW_E2E_OK_PRETTY"]);
60+});
61+62+it("does not accept markers that only appear outside reply payloads", () => {
63+const dir = mkdtempSync(join(tmpdir(), "openclaw-e2e-agent-output-"));
64+try {
65+const outputPath = join(dir, "agent.log");
66+writeFileSync(
67+outputPath,
68+[
69+"Return marker OPENCLAW_E2E_OK_PROMPT_ECHO",
70+JSON.stringify({ payloads: [{ text: "wrong reply" }] }),
71+].join("\n"),
72+);
73+74+expect(() =>
75+assertAgentReplyContainsMarker("OPENCLAW_E2E_OK_PROMPT_ECHO", outputPath),
76+).toThrow(/agent reply payload did not contain marker/u);
77+} finally {
78+rmSync(dir, { recursive: true, force: true });
79+}
80+});
81+82+it("checks that the mock OpenAI endpoint was actually hit", () => {
83+const dir = mkdtempSync(join(tmpdir(), "openclaw-e2e-request-log-"));
84+try {
85+mkdirSync(dir, { recursive: true });
86+const logPath = join(dir, "requests.jsonl");
87+writeFileSync(logPath, `${JSON.stringify({ path: "/v1/responses" })}\n`);
88+expect(() => assertOpenAiRequestLogUsed(logPath)).not.toThrow();
89+90+writeFileSync(logPath, `${JSON.stringify({ path: "/health" })}\n`);
91+expect(() => assertOpenAiRequestLogUsed(logPath)).toThrow(/was not used/u);
92+} finally {
93+rmSync(dir, { recursive: true, force: true });
94+}
95+});
96+});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。