






















@@ -1,7 +1,7 @@
11import fs from "node:fs/promises";
22import os from "node:os";
33import path from "node:path";
4-import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
4+import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
55import type { ChannelPlugin } from "../channels/plugins/types.js";
66import { createChannelTestPluginBase } from "../test-utils/channel-plugins.js";
77import { setRegistry } from "./server.agent.gateway-server-agent.mocks.js";
@@ -61,9 +61,18 @@ async function setTestSessionStore(params: {
6161});
6262}
636364-function latestAgentCall(): AgentCommandCall {
64+async function waitForAgentCall(runId: string): Promise<AgentCommandCall> {
65+await vi.waitFor(() =>
66+expect(
67+(vi.mocked(agentCommand).mock.calls as unknown as Array<[AgentCommandCall]>).some(
68+([call]) => call.runId === runId,
69+),
70+).toBe(true),
71+);
6572const calls = vi.mocked(agentCommand).mock.calls as unknown as Array<[unknown]>;
66-return calls.at(-1)?.[0] as AgentCommandCall;
73+return calls.find(
74+([call]) => (call as AgentCommandCall).runId === runId,
75+)?.[0] as AgentCommandCall;
6776}
68776978async function runMainAgentDeliveryWithSession(params: {
@@ -89,7 +98,7 @@ async function runMainAgentDeliveryWithSession(params: {
8998 ...params.request,
9099});
91100expect(res.ok).toBe(true);
92-return latestAgentCall();
101+return await waitForAgentCall(String(params.request.idempotencyKey));
93102} finally {
94103testState.allowFrom = undefined;
95104}
@@ -160,8 +169,19 @@ const defaultRegistry = createRegistry([
160169]);
161170162171describe("gateway server agent", () => {
163-test("agent marks implicit delivery when lastTo is stale", async () => {
172+beforeEach(() => {
173+vi.mocked(agentCommand).mockClear();
174+testState.agentsConfig = undefined;
175+testState.allowFrom = undefined;
164176setRegistry(defaultRegistry);
177+});
178+179+afterEach(() => {
180+testState.agentsConfig = undefined;
181+testState.allowFrom = undefined;
182+});
183+184+test("agent marks implicit delivery when lastTo is stale", async () => {
165185testState.allowFrom = ["+436769770569"];
166186await setTestSessionStore({
167187entries: {
@@ -182,7 +202,7 @@ describe("gateway server agent", () => {
182202});
183203expect(res.ok).toBe(true);
184204185-const call = latestAgentCall();
205+const call = await waitForAgentCall("idem-agent-last-stale");
186206expectChannels(call, "whatsapp");
187207expect(call.to).toBe("+1555");
188208expect(call.deliveryTargetMode).toBe("implicit");
@@ -191,7 +211,6 @@ describe("gateway server agent", () => {
191211});
192212193213test("agent forwards sessionKey to agentCommand", async () => {
194-setRegistry(defaultRegistry);
195214await setTestSessionStore({
196215entries: {
197216"agent:main:subagent:abc": {
@@ -207,7 +226,7 @@ describe("gateway server agent", () => {
207226});
208227expect(res.ok).toBe(true);
209228210-const call = latestAgentCall();
229+const call = await waitForAgentCall("idem-agent-subkey");
211230expect(call.sessionKey).toBe("agent:main:subagent:abc");
212231expect(call.sessionId).toBe("sess-sub");
213232expectChannels(call, "webchat");
@@ -216,7 +235,6 @@ describe("gateway server agent", () => {
216235});
217236218237test("agent preserves spawnDepth on subagent sessions", async () => {
219-setRegistry(defaultRegistry);
220238await setTestSessionStore({
221239entries: {
222240"agent:main:subagent:depth": {
@@ -245,7 +263,6 @@ describe("gateway server agent", () => {
245263});
246264247265test("agent derives sessionKey from agentId", async () => {
248-setRegistry(defaultRegistry);
249266await setTestSessionStore({
250267agentId: "ops",
251268entries: {
@@ -263,13 +280,12 @@ describe("gateway server agent", () => {
263280});
264281expect(res.ok).toBe(true);
265282266-const call = latestAgentCall();
283+const call = await waitForAgentCall("idem-agent-id");
267284expect(call.sessionKey).toBe("agent:ops:main");
268285expect(call.sessionId).toBe("sess-ops");
269286});
270287271288test("agent rejects unknown reply channel", async () => {
272-setRegistry(defaultRegistry);
273289const res = await rpcReq(ws, "agent", {
274290message: "hi",
275291replyChannel: "unknown-channel",
@@ -283,7 +299,6 @@ describe("gateway server agent", () => {
283299});
284300285301test("agent rejects mismatched agentId and sessionKey", async () => {
286-setRegistry(defaultRegistry);
287302testState.agentsConfig = { list: [{ id: "ops" }] };
288303const res = await rpcReq(ws, "agent", {
289304message: "hi",
@@ -299,7 +314,6 @@ describe("gateway server agent", () => {
299314});
300315301316test("agent rejects malformed agent-prefixed session keys", async () => {
302-setRegistry(defaultRegistry);
303317const res = await rpcReq(ws, "agent", {
304318message: "hi",
305319sessionKey: "agent:main",
@@ -391,7 +405,6 @@ describe("gateway server agent", () => {
391405});
392406393407test("agent forwards image attachments as images[]", async () => {
394-setRegistry(defaultRegistry);
395408await setTestSessionStore({
396409entries: {
397410main: {
@@ -414,7 +427,7 @@ describe("gateway server agent", () => {
414427});
415428expect(res.ok).toBe(true);
416429417-const call = latestAgentCall();
430+const call = await waitForAgentCall("idem-agent-attachments");
418431expect(call.sessionKey).toBe("agent:main:main");
419432expectChannels(call, "webchat");
420433expect(typeof call.message).toBe("string");
@@ -429,7 +442,6 @@ describe("gateway server agent", () => {
429442});
430443431444test("agent errors when delivery requested and no last channel exists", async () => {
432-setRegistry(defaultRegistry);
433445testState.allowFrom = ["+1555"];
434446try {
435447await setTestSessionStore({
@@ -493,7 +505,6 @@ describe("gateway server agent", () => {
493505idempotencyKey: "idem-agent-last-signal",
494506},
495507])("agent routes main last-channel $name", async (tc) => {
496-setRegistry(defaultRegistry);
497508await setTestSessionStore({
498509entries: {
499510main: {
@@ -513,7 +524,7 @@ describe("gateway server agent", () => {
513524});
514525expect(res.ok).toBe(true);
515526516-const call = latestAgentCall();
527+const call = await waitForAgentCall(tc.idempotencyKey);
517528expectChannels(call, tc.lastChannel);
518529expect(call.to).toBe(tc.lastTo);
519530expect(call.deliver).toBe(true);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。