fix(gateway): stabilize imsg alias test coverage

This commit is contained in:
Vincent Koc
2026-04-15 11:24:09 +01:00
parent 804bb0f2c3
commit becd14424d
3 changed files with 21 additions and 11 deletions

View File

@@ -225,6 +225,11 @@ describe("gateway server agent", () => {
test("agent accepts built-in channel alias (imsg)", async () => {
const registry = createRegistry([
{
pluginId: "imessage",
source: "test",
plugin: createStubChannelPlugin({ id: "imessage", label: "iMessage" }),
},
{
pluginId: "msteams",
source: "test",
@@ -245,7 +250,9 @@ describe("gateway server agent", () => {
idempotencyKey: "idem-agent-imsg",
});
expect(resIMessage.ok).toBe(true);
await vi.waitFor(() => {
expect(vi.mocked(agentCommand)).toHaveBeenCalled();
});
expectAgentRoutingCall({ channel: "imessage", deliver: true, fromEnd: 1 });
});

View File

@@ -944,6 +944,14 @@ export async function connectReq(
nonce: connectChallengeNonce,
};
})();
const isResponseForId = (o: unknown): boolean => {
if (!o || typeof o !== "object" || Array.isArray(o)) {
return false;
}
const rec = o as Record<string, unknown>;
return rec.type === "res" && rec.id === id;
};
const responsePromise = onceMessage<ConnectResponse>(ws, isResponseForId, opts?.timeoutMs);
ws.send(
JSON.stringify({
type: "req",
@@ -971,14 +979,7 @@ export async function connectReq(
},
}),
);
const isResponseForId = (o: unknown): boolean => {
if (!o || typeof o !== "object" || Array.isArray(o)) {
return false;
}
const rec = o as Record<string, unknown>;
return rec.type === "res" && rec.id === id;
};
return await onceMessage<ConnectResponse>(ws, isResponseForId, opts?.timeoutMs);
return await responsePromise;
}
export async function connectOk(ws: WebSocket, opts?: Parameters<typeof connectReq>[1]) {
@@ -1042,8 +1043,7 @@ export async function rpcReq<T extends Record<string, unknown>>(
clearSessionStoreCacheForTest();
const { randomUUID } = await import("node:crypto");
const id = randomUUID();
ws.send(JSON.stringify({ type: "req", id, method, params }));
return await onceMessage<{
const responsePromise = onceMessage<{
type: "res";
id: string;
ok: boolean;
@@ -1060,6 +1060,8 @@ export async function rpcReq<T extends Record<string, unknown>>(
},
timeoutMs,
);
ws.send(JSON.stringify({ type: "req", id, method, params }));
return await responsePromise;
}
export async function waitForSystemEvent(timeoutMs = 2000) {