Skip to content

Commit 520f6f7

Browse files
authored
refactor(qa): share standard live transport CLI composition (#135325)
1 parent 3ce74e3 commit 520f6f7

14 files changed

Lines changed: 431 additions & 172 deletions

‎extensions/qa-lab/src/live-transports/cli.test.ts‎

Lines changed: 261 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,88 @@ import { Command } from "commander";
33
import type { QaRunnerCliContribution } from "openclaw/plugin-sdk/qa-runner-runtime";
44
import { beforeEach, describe, expect, it, vi } from "vitest";
55

6-
const { listQaRunnerCliContributions, runLiveTransportQaSuiteCommand, runTelegram } = vi.hoisted(
7-
() => ({
8-
listQaRunnerCliContributions: vi.fn<() => QaRunnerCliContribution[]>(() => []),
9-
runLiveTransportQaSuiteCommand: vi.fn(),
10-
runTelegram: vi.fn(),
11-
}),
12-
);
6+
const {
7+
adapterRuntimeLoads,
8+
createDiscordAdapter,
9+
createSlackAdapter,
10+
createWhatsAppAdapter,
11+
listQaRunnerCliContributions,
12+
runLiveTransportQaSuiteCommand,
13+
runTelegram,
14+
suiteRuntimeLoads,
15+
} = vi.hoisted(() => ({
16+
adapterRuntimeLoads: { discord: 0, slack: 0, whatsapp: 0 },
17+
createDiscordAdapter: vi.fn(async () => ({ id: "discord" })),
18+
createSlackAdapter: vi.fn(async () => ({ id: "slack" })),
19+
createWhatsAppAdapter: vi.fn(async () => ({ id: "whatsapp" })),
20+
listQaRunnerCliContributions: vi.fn<() => QaRunnerCliContribution[]>(() => []),
21+
runLiveTransportQaSuiteCommand: vi.fn(),
22+
runTelegram: vi.fn(),
23+
suiteRuntimeLoads: { count: 0 },
24+
}));
1325

1426
vi.mock("openclaw/plugin-sdk/qa-runner-runtime", () => ({ listQaRunnerCliContributions }));
15-
vi.mock("./shared/live-transport-suite.runtime.js", () => ({ runLiveTransportQaSuiteCommand }));
27+
vi.mock("./shared/live-transport-suite.runtime.js", () => {
28+
suiteRuntimeLoads.count += 1;
29+
return {
30+
runLiveTransportQaSuiteCommand,
31+
runStandardLiveTransportQaSuiteCommand: runLiveTransportQaSuiteCommand,
32+
};
33+
});
1634
vi.mock("./telegram/cli.runtime.js", () => ({ runQaTelegramCommand: runTelegram }));
35+
vi.mock("./discord/adapter.runtime.js", () => {
36+
adapterRuntimeLoads.discord += 1;
37+
return { createDiscordQaTransportAdapter: createDiscordAdapter };
38+
});
39+
vi.mock("./slack/adapter.runtime.js", () => {
40+
adapterRuntimeLoads.slack += 1;
41+
return { createSlackQaTransportAdapter: createSlackAdapter };
42+
});
43+
vi.mock("./whatsapp/adapter.runtime.js", () => {
44+
adapterRuntimeLoads.whatsapp += 1;
45+
return { createWhatsAppQaTransportAdapter: createWhatsAppAdapter };
46+
});
1747

1848
import { listLiveTransportQaAdapterFactories, listLiveTransportQaCliRegistrations } from "./cli.js";
1949

50+
const STANDARD_LANES = [
51+
{
52+
commandName: "discord",
53+
description: "Run the Discord live QA lane against a private guild bot-to-bot harness",
54+
label: "Discord",
55+
},
56+
{
57+
commandName: "slack",
58+
description: "Run the Slack live QA lane against a private bot-to-bot channel harness",
59+
label: "Slack",
60+
},
61+
{
62+
commandName: "whatsapp",
63+
description: "Run the WhatsApp live QA lane against two pre-linked Web sessions",
64+
label: "WhatsApp",
65+
},
66+
] as const;
67+
68+
function requireRegistration(commandName: string) {
69+
const registration = listLiveTransportQaCliRegistrations().find(
70+
(candidate) => candidate.commandName === commandName,
71+
);
72+
if (!registration) {
73+
throw new Error(`missing ${commandName} QA registration`);
74+
}
75+
return registration;
76+
}
77+
78+
function registerCommand(commandName: string) {
79+
const qa = new Command("qa").exitOverride().configureOutput({ writeErr: () => {} });
80+
requireRegistration(commandName).register(qa);
81+
const command = qa.commands.find((candidate) => candidate.name() === commandName);
82+
if (!command) {
83+
throw new Error(`missing ${commandName} Commander command`);
84+
}
85+
return { command, qa };
86+
}
87+
2088
describe("live transport QA contributions", () => {
2189
beforeEach(() => {
2290
vi.clearAllMocks();
@@ -33,6 +101,17 @@ describe("live transport QA contributions", () => {
33101
]);
34102
});
35103

104+
it("registers all three dedicated commands without loading suite or adapter runtimes", () => {
105+
const suiteLoadsBefore = suiteRuntimeLoads.count;
106+
const adapterLoadsBefore = { ...adapterRuntimeLoads };
107+
for (const { commandName } of STANDARD_LANES) {
108+
registerCommand(commandName);
109+
}
110+
111+
expect(suiteRuntimeLoads.count).toBe(suiteLoadsBefore);
112+
expect(adapterRuntimeLoads).toEqual(adapterLoadsBefore);
113+
});
114+
36115
it.each(["discord", "slack", "whatsapp"] as const)(
37116
"routes the shipped %s command through the shared suite host",
38117
async (commandName) => {
@@ -44,15 +123,184 @@ describe("live transport QA contributions", () => {
44123

45124
await qa.parseAsync(["node", "openclaw", commandName, "--scenario", `${commandName}-canary`]);
46125

47-
expect(runLiveTransportQaSuiteCommand).toHaveBeenCalledWith(
48-
expect.objectContaining({
49-
channelId: commandName,
50-
options: expect.objectContaining({ scenarioIds: [`${commandName}-canary`] }),
51-
}),
126+
expect(runLiveTransportQaSuiteCommand).toHaveBeenCalledWith({
127+
channelId: commandName,
128+
options: expect.objectContaining({ scenarioIds: [`${commandName}-canary`] }),
129+
});
130+
},
131+
);
132+
133+
it.each(STANDARD_LANES)(
134+
"preserves the actual $commandName Commander contract",
135+
({ commandName, description, label }) => {
136+
const { command } = registerCommand(commandName);
137+
138+
expect(command.description()).toBe(description);
139+
expect(
140+
command.options.map((option) => ({
141+
defaultValue: option.defaultValue,
142+
description: option.description,
143+
flags: option.flags,
144+
})),
145+
).toEqual([
146+
{
147+
defaultValue: undefined,
148+
description: "Repository root to target when running from a neutral cwd",
149+
flags: "--repo-root <path>",
150+
},
151+
{
152+
defaultValue: undefined,
153+
description: `${label} QA artifact directory`,
154+
flags: "--output-dir <path>",
155+
},
156+
{
157+
defaultValue: "live-frontier",
158+
description: "Provider mode: mock-openai, aimock, live-frontier",
159+
flags: "--provider-mode <mode>",
160+
},
161+
{
162+
defaultValue: undefined,
163+
description: "Primary provider/model ref",
164+
flags: "--model <ref>",
165+
},
166+
{
167+
defaultValue: undefined,
168+
description: "Alternate provider/model ref",
169+
flags: "--alt-model <ref>",
170+
},
171+
{
172+
defaultValue: [],
173+
description: `Run only the named ${label} QA scenario (repeatable)`,
174+
flags: "--scenario <id>",
175+
},
176+
{
177+
defaultValue: undefined,
178+
description: "Enable provider fast mode where supported",
179+
flags: "--fast",
180+
},
181+
{
182+
defaultValue: false,
183+
description: "Write artifacts without setting a failing exit code when scenarios fail",
184+
flags: "--allow-failures",
185+
},
186+
{
187+
defaultValue: "sut",
188+
description: `Temporary ${label} account id inside the QA gateway config`,
189+
flags: "--sut-account <id>",
190+
},
191+
{
192+
defaultValue: undefined,
193+
description: `Credential source for ${label} QA: env or convex (default: env)`,
194+
flags: "--credential-source <source>",
195+
},
196+
{
197+
defaultValue: undefined,
198+
description:
199+
"Credential role for convex auth: maintainer or ci (default: ci in CI, maintainer otherwise)",
200+
flags: "--credential-role <role>",
201+
},
202+
]);
203+
expect(command.helpInformation()).toContain(`Usage: qa ${commandName} [options]`);
204+
},
205+
);
206+
207+
it.each(STANDARD_LANES)(
208+
"preserves $commandName defaults, optional fields, duplicate scenarios, and dispatch errors",
209+
async ({ commandName }) => {
210+
const { qa } = registerCommand(commandName);
211+
212+
await qa.parseAsync([
213+
"node",
214+
"openclaw",
215+
commandName,
216+
"--scenario",
217+
" first ",
218+
"--scenario",
219+
" ",
220+
"--scenario",
221+
"first",
222+
"--scenario",
223+
"second",
224+
]);
225+
expect(runLiveTransportQaSuiteCommand).toHaveBeenLastCalledWith({
226+
channelId: commandName,
227+
options: {
228+
allowFailures: false,
229+
alternateModel: undefined,
230+
credentialRole: undefined,
231+
credentialSource: undefined,
232+
failFast: undefined,
233+
fastMode: undefined,
234+
listScenarios: undefined,
235+
outputDir: undefined,
236+
primaryModel: undefined,
237+
profile: undefined,
238+
providerMode: "live-frontier",
239+
repoRoot: undefined,
240+
scenarioIds: ["first", "first", "second"],
241+
sutAccountId: "sut",
242+
},
243+
});
244+
245+
const failure = new Error(`${commandName} suite failed`);
246+
runLiveTransportQaSuiteCommand.mockRejectedValueOnce(failure);
247+
const next = registerCommand(commandName).qa.parseAsync(["node", "openclaw", commandName]);
248+
await expect(next).rejects.toBe(failure);
249+
},
250+
);
251+
252+
it.each(["discord", "slack", "whatsapp"] as const)(
253+
"rejects a missing %s option value before suite dispatch",
254+
async (commandName) => {
255+
const { qa } = registerCommand(commandName);
256+
257+
await expect(qa.parseAsync(["node", "openclaw", commandName, "--model"])).rejects.toThrow(
258+
"option '--model <ref>' argument missing",
52259
);
260+
expect(runLiveTransportQaSuiteCommand).not.toHaveBeenCalled();
53261
},
54262
);
55263

264+
it("keeps all three adapter runtimes lazy and preserves factory failure identity", async () => {
265+
const suiteLoadsBefore = suiteRuntimeLoads.count;
266+
const adapterLoadsBefore = { ...adapterRuntimeLoads };
267+
268+
for (const { commandName } of STANDARD_LANES) {
269+
const factory = requireRegistration(commandName).adapterFactory;
270+
if (!factory) {
271+
throw new Error(`missing ${commandName} QA adapter factory`);
272+
}
273+
const create =
274+
commandName === "discord"
275+
? createDiscordAdapter
276+
: commandName === "slack"
277+
? createSlackAdapter
278+
: createWhatsAppAdapter;
279+
const failure = new Error(`${commandName} adapter failed`);
280+
create.mockRejectedValueOnce(failure);
281+
282+
const pending = factory.create({
283+
adapterOptions: {},
284+
channelId: commandName,
285+
credentials: {
286+
acquire: vi.fn(),
287+
startHeartbeat: vi.fn(),
288+
},
289+
driver: "live",
290+
messages: {
291+
addInboundMessage: vi.fn(),
292+
addOutboundMessage: vi.fn(),
293+
editMessage: vi.fn(),
294+
},
295+
outputDir: "/qa-output",
296+
});
297+
await expect(pending).rejects.toBe(failure);
298+
expect(adapterRuntimeLoads[commandName]).toBe(adapterLoadsBefore[commandName] + 1);
299+
}
300+
301+
expect(suiteRuntimeLoads.count).toBe(suiteLoadsBefore);
302+
});
303+
56304
it("keeps the specialized Telegram command runner", async () => {
57305
const registration = listLiveTransportQaCliRegistrations().find(
58306
(candidate) => candidate.commandName === "telegram",
Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,20 @@
11
// Qa Lab plugin module implements cli behavior.
22
import {
3-
createLiveTransportQaAdapterFactory,
43
createLazyCliRuntimeLoader,
5-
createLiveTransportQaCliRegistration,
6-
loadLiveTransportQaSuiteRuntime,
4+
createStandardLiveTransportQaCliRegistration,
75
type LiveTransportQaCliRegistration,
8-
type LiveTransportQaCommandOptions,
96
} from "../shared/live-transport-cli.js";
10-
import { resolveDiscordQaScenarioIds } from "./scenario-selection.js";
117

128
const loadDiscordQaAdapterRuntime = createLazyCliRuntimeLoader<
139
typeof import("./adapter.runtime.js")
1410
>(() => import("./adapter.runtime.js"));
1511

16-
async function runQaDiscord(opts: LiveTransportQaCommandOptions) {
17-
const runtime = await loadLiveTransportQaSuiteRuntime();
18-
await runtime.runLiveTransportQaSuiteCommand({
19-
channelId: "discord",
20-
defaultProviderMode: "live-frontier",
21-
options: opts,
22-
selectScenarioIds: resolveDiscordQaScenarioIds,
23-
});
24-
}
25-
2612
export const discordQaCliRegistration: LiveTransportQaCliRegistration =
27-
createLiveTransportQaCliRegistration({
28-
commandName: "discord",
29-
adapterFactory: createLiveTransportQaAdapterFactory({
30-
id: "discord",
31-
supportsModuleFlows: true,
32-
async create(context) {
33-
return (await loadDiscordQaAdapterRuntime()).createDiscordQaTransportAdapter(context);
34-
},
35-
}),
36-
credentialOptions: {
37-
sourceDescription: "Credential source for Discord QA: env or convex (default: env)",
38-
roleDescription:
39-
"Credential role for convex auth: maintainer or ci (default: ci in CI, maintainer otherwise)",
13+
createStandardLiveTransportQaCliRegistration({
14+
channelId: "discord",
15+
channelLabel: "Discord",
16+
async createAdapter(context) {
17+
return (await loadDiscordQaAdapterRuntime()).createDiscordQaTransportAdapter(context);
4018
},
4119
description: "Run the Discord live QA lane against a private guild bot-to-bot harness",
42-
outputDirHelp: "Discord QA artifact directory",
43-
scenarioHelp: "Run only the named Discord QA scenario (repeatable)",
44-
sutAccountHelp: "Temporary Discord account id inside the QA gateway config",
45-
run: runQaDiscord,
4620
});

‎extensions/qa-lab/src/live-transports/discord/scenario-selection.ts‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)