Skip to content

Commit 8604dbd

Browse files
authored
test(qa): migrate channel streaming evidence to transport flow (#99310)
* test(qa): migrate channel streaming evidence to transport flow * test(qa): enable Telegram previews for streaming smoke * test(qa): stabilize streaming preview evidence * fix(qa): sanitize channel dispatch logs
1 parent df350e6 commit 8604dbd

19 files changed

Lines changed: 864 additions & 669 deletions

‎.agents/skills/channel-message-flows/SKILL.md‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,38 @@ description: "Use when running QA Lab channel message flow evidence."
66
# Channel Message Flows
77

88
Use this from the OpenClaw repo root to run the QA Lab evidence for Telegram
9-
draft/final delivery sequencing. This skill no longer launches a standalone
10-
script; the behavior is owned by the QA scenario and its Vitest-backed e2e test.
9+
draft/final delivery sequencing. The behavior is owned by one transport-native
10+
QA flow that can run through QA Channel or Crabline Telegram.
1111

1212
## QA Scenario
1313

1414
Run the scenario through QA Lab:
1515

1616
```bash
17-
pnpm openclaw qa suite --scenario channel-message-flows
17+
OPENCLAW_BUILD_PRIVATE_QA=1 node scripts/run-node.mjs qa suite \
18+
--provider-mode mock-openai \
19+
--scenario channel-message-flows \
20+
--channel-driver qa-channel
1821
```
1922

20-
Run the focused e2e test directly in a Codex worktree:
23+
Run the same YAML through the real Telegram plugin against Crabline's local
24+
provider server:
2125

2226
```bash
23-
node scripts/run-vitest.mjs extensions/telegram/src/channel-message-flows.qa.e2e.test.ts
27+
OPENCLAW_BUILD_PRIVATE_QA=1 node scripts/run-node.mjs qa suite \
28+
--provider-mode mock-openai \
29+
--scenario channel-message-flows \
30+
--channel-driver crabline \
31+
--channel telegram
2432
```
2533

2634
## References
2735

2836
- `qa/scenarios/channels/channel-message-flows.yaml`
29-
- `extensions/telegram/src/channel-message-flows.qa.e2e.test.ts`
30-
- `extensions/telegram/src/test-support/channel-message-flows.ts`
37+
- `extensions/qa-channel/src/inbound.ts`
38+
- `extensions/qa-lab/src/qa-transport.ts`
39+
- `extensions/qa-lab/src/crabline-transport.ts`
40+
- `extensions/telegram/src/draft-stream.ts`
3141

32-
The scenario covers `channels.streaming` as primary evidence and records
33-
secondary coverage for thread preservation, delivery ordering, and reasoning
34-
preview visibility.
42+
The scenario covers `channels.streaming` as primary evidence and
43+
`runtime.delivery` as secondary evidence.

‎extensions/qa-channel/src/channel.test.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ function createMockQaRuntime(params?: {
115115
replyOptions,
116116
}: {
117117
ctx: { BodyForAgent?: string; Body?: string };
118-
dispatcherOptions: { deliver: (payload: { text: string }) => Promise<void> };
118+
dispatcherOptions: {
119+
deliver: (payload: { text: string }, info: { kind: string }) => Promise<void>;
120+
};
119121
replyOptions?: {
120122
onToolStart?: (payload: {
121123
name?: string;
@@ -128,9 +130,12 @@ function createMockQaRuntime(params?: {
128130
await replyOptions?.onToolStart?.(toolStart);
129131
}
130132
params?.onDispatch?.(ctx as Record<string, unknown>);
131-
await dispatcherOptions.deliver({
132-
text: `qa-echo: ${ctx.BodyForAgent ?? ctx.Body ?? ""}`,
133-
});
133+
await dispatcherOptions.deliver(
134+
{
135+
text: `qa-echo: ${ctx.BodyForAgent ?? ctx.Body ?? ""}`,
136+
},
137+
{ kind: "final" },
138+
);
134139
},
135140
},
136141
inbound: {

‎extensions/qa-channel/src/inbound.test.ts‎

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
// Qa Channel tests cover inbound plugin behavior.
22
import { createPluginRuntimeMock } from "openclaw/plugin-sdk/channel-test-helpers";
3-
import { describe, expect, it, vi } from "vitest";
3+
import { beforeEach, describe, expect, it, vi } from "vitest";
44
import { setQaChannelRuntime } from "../api.js";
5+
import { deleteQaBusMessage, editQaBusMessage, sendQaBusMessage } from "./bus-client.js";
56
import { handleQaInbound, isHttpMediaUrl } from "./inbound.js";
67

8+
vi.mock("./bus-client.js", async (importOriginal) => {
9+
const actual = await importOriginal<typeof import("./bus-client.js")>();
10+
return {
11+
...actual,
12+
deleteQaBusMessage: vi.fn(async () => ({ message: {} })),
13+
editQaBusMessage: vi.fn(async () => ({ message: {} })),
14+
sendQaBusMessage: vi.fn(async () => ({ message: { id: "preview-1" } })),
15+
};
16+
});
17+
718
type HandleQaInboundParams = Parameters<typeof handleQaInbound>[0];
819

920
function createQaInboundParams(
@@ -66,6 +77,161 @@ describe("isHttpMediaUrl", () => {
6677
});
6778

6879
describe("handleQaInbound", () => {
80+
beforeEach(() => {
81+
vi.clearAllMocks();
82+
});
83+
84+
it("publishes partial replies as one edited preview before final delivery", async () => {
85+
const runtime = createPluginRuntimeMock();
86+
setQaChannelRuntime(runtime);
87+
88+
await handleQaInbound(
89+
createQaInboundParams({
90+
message: {
91+
conversation: { id: "qa-room", kind: "group" },
92+
threadId: "42",
93+
},
94+
}),
95+
);
96+
97+
const assembled = firstRunAssembledParams(runtime);
98+
await assembled.replyOptions?.onPartialReply?.({ text: "preview" });
99+
await assembled.replyOptions?.onPartialReply?.({ text: "preview expanded" });
100+
await assembled.delivery.deliver({ text: "final answer" }, { kind: "final" });
101+
102+
expect(sendQaBusMessage).toHaveBeenCalledOnce();
103+
expect(sendQaBusMessage).toHaveBeenCalledWith(
104+
expect.objectContaining({
105+
replyToId: "msg-1",
106+
text: "preview",
107+
threadId: "42",
108+
to: "thread:qa-room/42",
109+
}),
110+
);
111+
expect(editQaBusMessage).toHaveBeenNthCalledWith(
112+
1,
113+
expect.objectContaining({ messageId: "preview-1", text: "preview expanded" }),
114+
);
115+
expect(editQaBusMessage).toHaveBeenNthCalledWith(
116+
2,
117+
expect.objectContaining({ messageId: "preview-1", text: "final answer" }),
118+
);
119+
});
120+
121+
it("keeps block deliveries separate and retains tool calls discovered after a preview", async () => {
122+
const runtime = createPluginRuntimeMock();
123+
setQaChannelRuntime(runtime);
124+
125+
await handleQaInbound(createQaInboundParams());
126+
127+
const assembled = firstRunAssembledParams(runtime);
128+
await assembled.replyOptions?.onPartialReply?.({ text: "preview" });
129+
await assembled.replyOptions?.onToolStart?.({
130+
phase: "start",
131+
name: "search",
132+
args: { query: "qa" },
133+
});
134+
await assembled.delivery.deliver({ text: "tool result" }, { kind: "block" });
135+
await assembled.delivery.deliver({ text: "final answer" }, { kind: "final" });
136+
137+
expect(deleteQaBusMessage).toHaveBeenCalledOnce();
138+
expect(sendQaBusMessage).toHaveBeenCalledTimes(3);
139+
expect(sendQaBusMessage).toHaveBeenNthCalledWith(
140+
2,
141+
expect.objectContaining({
142+
text: "tool result",
143+
toolCalls: [{ name: "search", arguments: { query: "[redacted]" } }],
144+
}),
145+
);
146+
expect(sendQaBusMessage).toHaveBeenNthCalledWith(
147+
3,
148+
expect.objectContaining({
149+
text: "final answer",
150+
toolCalls: [{ name: "search", arguments: { query: "[redacted]" } }],
151+
}),
152+
);
153+
});
154+
155+
it("deletes an active preview when reply dispatch fails", async () => {
156+
const runtime = createPluginRuntimeMock();
157+
setQaChannelRuntime(runtime);
158+
159+
await handleQaInbound(createQaInboundParams());
160+
161+
const assembled = firstRunAssembledParams(runtime);
162+
await assembled.replyOptions?.onPartialReply?.({ text: "unfinished preview" });
163+
assembled.delivery.onError?.(new Error("model failed"), { kind: "final" });
164+
165+
await vi.waitFor(() => {
166+
expect(deleteQaBusMessage).toHaveBeenCalledWith(
167+
expect.objectContaining({ messageId: "preview-1" }),
168+
);
169+
});
170+
});
171+
172+
it("deletes a preview after a queued edit fails", async () => {
173+
const runtime = createPluginRuntimeMock();
174+
setQaChannelRuntime(runtime);
175+
vi.mocked(editQaBusMessage).mockRejectedValueOnce(new Error("edit failed"));
176+
177+
await handleQaInbound(createQaInboundParams());
178+
179+
const assembled = firstRunAssembledParams(runtime);
180+
await assembled.replyOptions?.onPartialReply?.({ text: "first preview" });
181+
await expect(
182+
assembled.replyOptions?.onPartialReply?.({ text: "broken preview" }),
183+
).rejects.toThrow("edit failed");
184+
assembled.delivery.onError?.(new Error("dispatch failed"), { kind: "final" });
185+
186+
await vi.waitFor(() => {
187+
expect(deleteQaBusMessage).toHaveBeenCalledWith(
188+
expect.objectContaining({ messageId: "preview-1" }),
189+
);
190+
});
191+
});
192+
193+
it("escapes control characters in dispatch error logs", async () => {
194+
const runtime = createPluginRuntimeMock();
195+
const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
196+
const c1Control = String.fromCharCode(0x9b);
197+
const lineSeparator = String.fromCodePoint(0x2028);
198+
const paragraphSeparator = String.fromCodePoint(0x2029);
199+
vi.mocked(deleteQaBusMessage).mockRejectedValueOnce(
200+
new Error(`cleanup\nforged\u001b[31m${c1Control}32m${lineSeparator}next`),
201+
);
202+
setQaChannelRuntime(runtime);
203+
204+
try {
205+
await handleQaInbound(createQaInboundParams());
206+
207+
const assembled = firstRunAssembledParams(runtime);
208+
await assembled.replyOptions?.onPartialReply?.({ text: "unfinished preview" });
209+
assembled.delivery.onError?.(new Error(`dispatch\r\nforged${paragraphSeparator}next`), {
210+
kind: "final",
211+
});
212+
213+
await vi.waitFor(() => {
214+
expect(warn).toHaveBeenCalledTimes(2);
215+
});
216+
assembled.delivery.onError?.(undefined, { kind: "final" });
217+
await vi.waitFor(() => {
218+
expect(warn).toHaveBeenCalledTimes(3);
219+
});
220+
const output = warn.mock.calls.flat().join(" ");
221+
expect(output).not.toContain("\r");
222+
expect(output).not.toContain("\n");
223+
expect(output).not.toContain(String.fromCharCode(0x1b));
224+
expect(output).not.toContain(c1Control);
225+
expect(output).not.toContain(lineSeparator);
226+
expect(output).not.toContain(paragraphSeparator);
227+
expect(output).toContain("dispatch\\u000d\\u000aforged\\u2029next");
228+
expect(output).toContain("cleanup\\u000aforged\\u001b[31m\\u009b32m\\u2028next");
229+
expect(output).toContain("[object Undefined]");
230+
} finally {
231+
warn.mockRestore();
232+
}
233+
});
234+
69235
it("marks group messages that match configured mention patterns", async () => {
70236
const runtime = createPluginRuntimeMock();
71237
vi.mocked(runtime.channel.mentions.buildMentionRegexes).mockReturnValue([/\b@?openclaw\b/i]);

0 commit comments

Comments
 (0)