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}
6363
64- 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+ ) ;
6572 const 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}
6877
6978async function runMainAgentDeliveryWithSession ( params : {
@@ -89,7 +98,7 @@ async function runMainAgentDeliveryWithSession(params: {
8998 ...params . request ,
9099 } ) ;
91100 expect ( res . ok ) . toBe ( true ) ;
92- return latestAgentCall ( ) ;
101+ return await waitForAgentCall ( String ( params . request . idempotencyKey ) ) ;
93102 } finally {
94103 testState . allowFrom = undefined ;
95104 }
@@ -160,8 +169,19 @@ const defaultRegistry = createRegistry([
160169] ) ;
161170
162171describe ( "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 ;
164176 setRegistry ( 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 ( ) => {
165185 testState . allowFrom = [ "+436769770569" ] ;
166186 await setTestSessionStore ( {
167187 entries : {
@@ -182,7 +202,7 @@ describe("gateway server agent", () => {
182202 } ) ;
183203 expect ( res . ok ) . toBe ( true ) ;
184204
185- const call = latestAgentCall ( ) ;
205+ const call = await waitForAgentCall ( "idem-agent-last-stale" ) ;
186206 expectChannels ( call , "whatsapp" ) ;
187207 expect ( call . to ) . toBe ( "+1555" ) ;
188208 expect ( call . deliveryTargetMode ) . toBe ( "implicit" ) ;
@@ -191,7 +211,6 @@ describe("gateway server agent", () => {
191211 } ) ;
192212
193213 test ( "agent forwards sessionKey to agentCommand" , async ( ) => {
194- setRegistry ( defaultRegistry ) ;
195214 await setTestSessionStore ( {
196215 entries : {
197216 "agent:main:subagent:abc" : {
@@ -207,7 +226,7 @@ describe("gateway server agent", () => {
207226 } ) ;
208227 expect ( res . ok ) . toBe ( true ) ;
209228
210- const call = latestAgentCall ( ) ;
229+ const call = await waitForAgentCall ( "idem-agent-subkey" ) ;
211230 expect ( call . sessionKey ) . toBe ( "agent:main:subagent:abc" ) ;
212231 expect ( call . sessionId ) . toBe ( "sess-sub" ) ;
213232 expectChannels ( call , "webchat" ) ;
@@ -216,7 +235,6 @@ describe("gateway server agent", () => {
216235 } ) ;
217236
218237 test ( "agent preserves spawnDepth on subagent sessions" , async ( ) => {
219- setRegistry ( defaultRegistry ) ;
220238 await setTestSessionStore ( {
221239 entries : {
222240 "agent:main:subagent:depth" : {
@@ -245,7 +263,6 @@ describe("gateway server agent", () => {
245263 } ) ;
246264
247265 test ( "agent derives sessionKey from agentId" , async ( ) => {
248- setRegistry ( defaultRegistry ) ;
249266 await setTestSessionStore ( {
250267 agentId : "ops" ,
251268 entries : {
@@ -263,13 +280,12 @@ describe("gateway server agent", () => {
263280 } ) ;
264281 expect ( res . ok ) . toBe ( true ) ;
265282
266- const call = latestAgentCall ( ) ;
283+ const call = await waitForAgentCall ( "idem-agent-id" ) ;
267284 expect ( call . sessionKey ) . toBe ( "agent:ops:main" ) ;
268285 expect ( call . sessionId ) . toBe ( "sess-ops" ) ;
269286 } ) ;
270287
271288 test ( "agent rejects unknown reply channel" , async ( ) => {
272- setRegistry ( defaultRegistry ) ;
273289 const res = await rpcReq ( ws , "agent" , {
274290 message : "hi" ,
275291 replyChannel : "unknown-channel" ,
@@ -283,7 +299,6 @@ describe("gateway server agent", () => {
283299 } ) ;
284300
285301 test ( "agent rejects mismatched agentId and sessionKey" , async ( ) => {
286- setRegistry ( defaultRegistry ) ;
287302 testState . agentsConfig = { list : [ { id : "ops" } ] } ;
288303 const res = await rpcReq ( ws , "agent" , {
289304 message : "hi" ,
@@ -299,7 +314,6 @@ describe("gateway server agent", () => {
299314 } ) ;
300315
301316 test ( "agent rejects malformed agent-prefixed session keys" , async ( ) => {
302- setRegistry ( defaultRegistry ) ;
303317 const res = await rpcReq ( ws , "agent" , {
304318 message : "hi" ,
305319 sessionKey : "agent:main" ,
@@ -391,7 +405,6 @@ describe("gateway server agent", () => {
391405 } ) ;
392406
393407 test ( "agent forwards image attachments as images[]" , async ( ) => {
394- setRegistry ( defaultRegistry ) ;
395408 await setTestSessionStore ( {
396409 entries : {
397410 main : {
@@ -414,7 +427,7 @@ describe("gateway server agent", () => {
414427 } ) ;
415428 expect ( res . ok ) . toBe ( true ) ;
416429
417- const call = latestAgentCall ( ) ;
430+ const call = await waitForAgentCall ( "idem-agent-attachments" ) ;
418431 expect ( call . sessionKey ) . toBe ( "agent:main:main" ) ;
419432 expectChannels ( call , "webchat" ) ;
420433 expect ( typeof call . message ) . toBe ( "string" ) ;
@@ -429,7 +442,6 @@ describe("gateway server agent", () => {
429442 } ) ;
430443
431444 test ( "agent errors when delivery requested and no last channel exists" , async ( ) => {
432- setRegistry ( defaultRegistry ) ;
433445 testState . allowFrom = [ "+1555" ] ;
434446 try {
435447 await setTestSessionStore ( {
@@ -493,7 +505,6 @@ describe("gateway server agent", () => {
493505 idempotencyKey : "idem-agent-last-signal" ,
494506 } ,
495507 ] ) ( "agent routes main last-channel $name" , async ( tc ) => {
496- setRegistry ( defaultRegistry ) ;
497508 await setTestSessionStore ( {
498509 entries : {
499510 main : {
@@ -513,7 +524,7 @@ describe("gateway server agent", () => {
513524 } ) ;
514525 expect ( res . ok ) . toBe ( true ) ;
515526
516- const call = latestAgentCall ( ) ;
527+ const call = await waitForAgentCall ( tc . idempotencyKey ) ;
517528 expectChannels ( call , tc . lastChannel ) ;
518529 expect ( call . to ) . toBe ( tc . lastTo ) ;
519530 expect ( call . deliver ) . toBe ( true ) ;
0 commit comments