Skip to content

Commit 76c0d8b

Browse files
committed
fix(qa): require ready transport accounts
1 parent 1b499e2 commit 76c0d8b

6 files changed

Lines changed: 249 additions & 141 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { withTempDir } from "openclaw/plugin-sdk/test-env";
2+
import { describe, expect, it, vi } from "vitest";
3+
import { createQaBusState } from "./bus-state.js";
4+
import { createQaCrablineTransportAdapter } from "./crabline-transport.js";
5+
6+
async function withTelegramCrablineTransport(
7+
run: (transport: Awaited<ReturnType<typeof createQaCrablineTransportAdapter>>) => Promise<void>,
8+
) {
9+
await withTempDir("qa-crabline-transport-", async (outputDir) => {
10+
const transport = await createQaCrablineTransportAdapter({
11+
outputDir,
12+
selection: {
13+
capabilityMatrixPath: "crabline-fake-provider-capabilities.json",
14+
channel: "telegram",
15+
channelDriver: "crabline",
16+
smokeArtifactPath: "crabline-fake-provider-smoke.json",
17+
},
18+
state: createQaBusState(),
19+
});
20+
try {
21+
await run(transport);
22+
} finally {
23+
await transport.cleanup?.();
24+
}
25+
});
26+
}
27+
28+
describe("Crabline transport readiness", () => {
29+
it("waits for the selected account to finish its channel readiness lifecycle", async () => {
30+
await withTelegramCrablineTransport(async (transport) => {
31+
const statuses = [
32+
{ accountId: "other", connected: true, lifecycle: "ready", running: true },
33+
{
34+
accountId: transport.accountId,
35+
connected: false,
36+
lifecycle: "starting",
37+
running: true,
38+
},
39+
{
40+
accountId: transport.accountId,
41+
connected: true,
42+
lifecycle: "starting",
43+
running: true,
44+
},
45+
{
46+
accountId: transport.accountId,
47+
connected: true,
48+
lifecycle: "blocked",
49+
running: true,
50+
},
51+
{
52+
accountId: transport.accountId,
53+
connected: true,
54+
lifecycle: "ready",
55+
restartPending: false,
56+
running: true,
57+
},
58+
];
59+
const call = vi.fn(async () => ({ channelAccounts: { telegram: [statuses.shift()] } }));
60+
61+
await transport.waitReady({ gateway: { call }, timeoutMs: 2_000, pollIntervalMs: 1 });
62+
expect(call).toHaveBeenCalledTimes(5);
63+
});
64+
});
65+
66+
it("rejects a ready channel account belonging to another Crabline identity", async () => {
67+
await withTelegramCrablineTransport(async (transport) => {
68+
const call = vi.fn().mockResolvedValue({
69+
channelAccounts: {
70+
telegram: [{ accountId: "other", connected: true, lifecycle: "ready", running: true }],
71+
},
72+
});
73+
74+
await expect(
75+
transport.waitReady({ gateway: { call }, timeoutMs: 5, pollIntervalMs: 1 }),
76+
).rejects.toThrow('telegram account "default" not reported; available accounts: other');
77+
});
78+
});
79+
});

‎extensions/qa-lab/src/crabline-transport.ts‎

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Qa Lab plugin module implements Crabline local-provider transport behavior.
22
import fs from "node:fs/promises";
33
import path from "node:path";
4-
import { setTimeout as sleep } from "node:timers/promises";
54
import {
65
startOpenClawCrablineAdapter,
76
type OpenClawCrablineChannelDriverSelection,
87
type OpenClawCrablineInbound,
98
type StartedOpenClawCrablineAdapter,
109
} from "@openclaw/crabline";
1110
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
12-
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
1311
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
1412
import {
1513
isRecord,
@@ -23,10 +21,10 @@ import {
2321
resolveCrablineStateConversation,
2422
resolveTelegramQaSenderId,
2523
} from "./crabline-provider-targets.js";
26-
import { QaSuiteInfraError } from "./errors.js";
2724
import { discardIgnoredResponseBody } from "./ignored-response-body.js";
2825
import {
2926
QaStateBackedTransportAdapter,
27+
waitForQaTransportAccountReady,
3028
waitForQaTransportOutboundSequence,
3129
} from "./qa-transport.js";
3230
import type {
@@ -164,64 +162,6 @@ function readTelegramLifecycleEvent(params: {
164162
};
165163
}
166164

167-
async function waitForCrablineReady(params: {
168-
accountId: string;
169-
channel: string;
170-
gateway: QaTransportGatewayClient;
171-
timeoutMs?: number;
172-
pollIntervalMs?: number;
173-
}) {
174-
const timeoutMs = params.timeoutMs ?? 45_000;
175-
const pollIntervalMs = params.pollIntervalMs ?? 500;
176-
const startedAt = Date.now();
177-
let lastAccountStatus = `no ${params.channel} accounts reported`;
178-
let lastProbeError: string | null = null;
179-
180-
while (Date.now() - startedAt < timeoutMs) {
181-
try {
182-
const payload = (await params.gateway.call(
183-
"channels.status",
184-
{ probe: false, timeoutMs: 2_000 },
185-
{ timeoutMs: 5_000 },
186-
)) as {
187-
channelAccounts?: Record<
188-
string,
189-
Array<{
190-
accountId?: string;
191-
running?: boolean;
192-
restartPending?: boolean;
193-
}>
194-
>;
195-
};
196-
const accounts = payload.channelAccounts?.[params.channel] ?? [];
197-
const account = accounts.find((entry) => entry.accountId === params.accountId) ?? accounts[0];
198-
lastProbeError = null;
199-
lastAccountStatus = account
200-
? JSON.stringify({
201-
accountId: account.accountId ?? null,
202-
running: account.running ?? null,
203-
restartPending: account.restartPending ?? null,
204-
})
205-
: `no ${params.channel} accounts reported`;
206-
if (account?.running && account.restartPending !== true) {
207-
return;
208-
}
209-
} catch (error) {
210-
lastProbeError = formatErrorMessage(error);
211-
}
212-
await sleep(pollIntervalMs);
213-
}
214-
215-
throw new QaSuiteInfraError(
216-
"transport_ready_timeout",
217-
[
218-
`timed out after ${timeoutMs}ms waiting for ${params.channel} ready`,
219-
`last status: ${lastAccountStatus}`,
220-
...(lastProbeError ? [`last probe error: ${lastProbeError}`] : []),
221-
].join("; "),
222-
);
223-
}
224-
225165
async function postCrablineInbound(params: {
226166
adapter: StartedOpenClawCrablineAdapter;
227167
providerInbound: OpenClawCrablineInbound;
@@ -449,7 +389,7 @@ class QaCrablineTransport extends QaStateBackedTransportAdapter {
449389
timeoutMs?: number;
450390
pollIntervalMs?: number;
451391
}) =>
452-
waitForCrablineReady({
392+
waitForQaTransportAccountReady({
453393
...params,
454394
accountId: this.#adapter.accountId,
455395
channel: this.#adapter.channel,

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

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,39 @@ describe("qa channel transport", () => {
6060
});
6161
});
6262

63-
it("waits until the qa-channel default account is running", async () => {
63+
it("waits until the qa-channel default account is connected and ready", async () => {
6464
const transport = createQaChannelTransport(createQaBusState());
65-
const call = vi
66-
.fn()
67-
.mockResolvedValueOnce({
68-
channelAccounts: {
69-
"qa-channel": [{ accountId: "default", running: false }],
70-
},
71-
})
72-
.mockResolvedValueOnce({
73-
channelAccounts: {
74-
"qa-channel": [{ accountId: "default", running: true, restartPending: false }],
75-
},
76-
});
65+
const statuses = [
66+
{ accountId: "default", connected: false, lifecycle: "stopped", running: false },
67+
{ accountId: "default", connected: false, lifecycle: "starting", running: true },
68+
{ accountId: "default", connected: true, lifecycle: "starting", running: true },
69+
{ accountId: "default", connected: true, lifecycle: "blocked", running: true },
70+
{
71+
accountId: "default",
72+
connected: true,
73+
lifecycle: "ready",
74+
restartPending: true,
75+
running: true,
76+
},
77+
{
78+
accountId: "default",
79+
connected: true,
80+
lifecycle: "ready",
81+
restartPending: false,
82+
running: true,
83+
},
84+
];
85+
const call = vi.fn(async () => ({
86+
channelAccounts: { "qa-channel": [statuses.shift()] },
87+
}));
7788

7889
await transport.waitReady({
7990
gateway: { call },
8091
timeoutMs: 2_000,
8192
pollIntervalMs: 1,
8293
});
8394

84-
expect(call).toHaveBeenCalledTimes(2);
95+
expect(call).toHaveBeenCalledTimes(6);
8596
});
8697

8798
it("does not report another running account as the default account", async () => {
@@ -112,7 +123,7 @@ describe("qa channel transport", () => {
112123
pollIntervalMs: 1,
113124
}),
114125
).rejects.toThrow(
115-
'timed out after 5ms waiting for qa-channel ready; last status: {"accountId":"default","running":false,"restartPending":true}',
126+
'timed out after 5ms waiting for qa-channel ready; last status: {"accountId":"default","running":false,"connected":null,"lifecycle":null,"restartPending":true,"lastError":null}',
116127
);
117128
});
118129

‎extensions/qa-lab/src/qa-channel-transport.ts‎

Lines changed: 11 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// Qa Lab plugin module implements qa channel transport behavior.
2-
import { setTimeout as sleep } from "node:timers/promises";
32
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts";
4-
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
53
import type { QaBusState } from "./bus-state.js";
6-
import { QaSuiteInfraError } from "./errors.js";
74
import { getQaProvider } from "./providers/index.js";
85
import {
96
QaStateBackedTransportAdapter,
7+
waitForQaTransportAccountReady,
108
waitForQaTransportOutboundSequence,
119
} from "./qa-transport.js";
1210
import type {
@@ -24,66 +22,6 @@ const QA_CHANNEL_ACCOUNT_ID = "default";
2422
export const QA_CHANNEL_REQUIRED_PLUGIN_IDS = Object.freeze([QA_CHANNEL_ID]);
2523
export const QA_CHANNEL_DEFAULT_SUITE_CONCURRENCY = 4;
2624

27-
async function waitForQaChannelReady(params: {
28-
gateway: QaTransportGatewayClient;
29-
timeoutMs?: number;
30-
pollIntervalMs?: number;
31-
}) {
32-
const timeoutMs = params.timeoutMs ?? 45_000;
33-
const pollIntervalMs = params.pollIntervalMs ?? 500;
34-
const startedAt = Date.now();
35-
let lastAccountStatus = "no qa-channel accounts reported";
36-
let lastProbeError: string | null = null;
37-
38-
while (Date.now() - startedAt < timeoutMs) {
39-
try {
40-
const payload = (await params.gateway.call(
41-
"channels.status",
42-
{ probe: false, timeoutMs: 2_000 },
43-
{ timeoutMs: 5_000 },
44-
)) as {
45-
channelAccounts?: Record<
46-
string,
47-
Array<{
48-
accountId?: string;
49-
running?: boolean;
50-
restartPending?: boolean;
51-
}>
52-
>;
53-
};
54-
const accounts = payload.channelAccounts?.[QA_CHANNEL_ID] ?? [];
55-
const account = accounts.find((entry) => entry.accountId === QA_CHANNEL_ACCOUNT_ID);
56-
lastProbeError = null;
57-
lastAccountStatus = account
58-
? JSON.stringify({
59-
accountId: account.accountId ?? null,
60-
running: account.running ?? null,
61-
restartPending: account.restartPending ?? null,
62-
})
63-
: accounts.length > 0
64-
? `qa-channel account "${QA_CHANNEL_ACCOUNT_ID}" not reported; available accounts: ${accounts
65-
.map((entry) => entry.accountId ?? "unknown")
66-
.join(", ")}`
67-
: "no qa-channel accounts reported";
68-
if (account?.running && account.restartPending !== true) {
69-
return;
70-
}
71-
} catch (error) {
72-
lastProbeError = formatErrorMessage(error);
73-
}
74-
await sleep(pollIntervalMs);
75-
}
76-
77-
throw new QaSuiteInfraError(
78-
"transport_ready_timeout",
79-
[
80-
`timed out after ${timeoutMs}ms waiting for qa-channel ready`,
81-
`last status: ${lastAccountStatus}`,
82-
...(lastProbeError ? [`last probe error: ${lastProbeError}`] : []),
83-
].join("; "),
84-
);
85-
}
86-
8725
export function createQaChannelGatewayConfig(params: {
8826
baseUrl: string;
8927
transportPolicy?: QaTransportPolicy;
@@ -171,7 +109,16 @@ class QaChannelTransport extends QaStateBackedTransportAdapter {
171109

172110
createGatewayConfig = ({ baseUrl }: { baseUrl: string }) =>
173111
createQaChannelGatewayConfig({ baseUrl, transportPolicy: this.#transportPolicy });
174-
waitReady = waitForQaChannelReady;
112+
waitReady = (params: {
113+
gateway: QaTransportGatewayClient;
114+
timeoutMs?: number;
115+
pollIntervalMs?: number;
116+
}) =>
117+
waitForQaTransportAccountReady({
118+
...params,
119+
accountId: QA_CHANNEL_ACCOUNT_ID,
120+
channel: QA_CHANNEL_ID,
121+
});
175122
buildAgentDelivery = ({ target }: { target: string }) => ({
176123
channel: QA_CHANNEL_ID,
177124
replyChannel: QA_CHANNEL_ID,

0 commit comments

Comments
 (0)