@@ -3,12 +3,10 @@ import { createServer } from "node:net";
33import path from "node:path" ;
44import { afterAll , beforeAll , describe , expect , test } from "vitest" ;
55import { WebSocket } from "ws" ;
6- import { getChannelPlugin } from "../channels/plugins/index.js" ;
76import type { ChannelOutboundAdapter } from "../channels/plugins/types.js" ;
87import { clearConfigCache , clearRuntimeConfigSnapshot } from "../config/config.js" ;
98import { resolveCanvasHostUrl } from "../infra/canvas-host-url.js" ;
109import { GatewayLockError } from "../infra/gateway-lock.js" ;
11- import { getActivePluginRegistry , setActivePluginRegistry } from "../plugins/runtime.js" ;
1210import { createOutboundTestPlugin } from "../test-utils/channel-plugins.js" ;
1311import { withEnvAsync } from "../test-utils/env.js" ;
1412import { createTempHomeEnv } from "../test-utils/temp-home.js" ;
@@ -22,6 +20,8 @@ import {
2220 onceMessage ,
2321 piSdkMock ,
2422 rpcReq ,
23+ resetTestPluginRegistry ,
24+ setTestPluginRegistry ,
2525 startConnectedServerWithClient ,
2626 startGatewayServer ,
2727 startServerWithClient ,
@@ -83,7 +83,6 @@ const whatsappRegistry = createRegistry([
8383 plugin : whatsappPlugin ,
8484 } ,
8585] ) ;
86- const emptyRegistry = createRegistry ( [ ] ) ;
8786
8887type ModelCatalogRpcEntry = {
8988 id : string ;
@@ -390,16 +389,22 @@ describe("gateway server misc", () => {
390389 } ) ;
391390
392391 test ( "send dedupes by idempotencyKey" , { timeout : 15_000 } , async ( ) => {
393- const prevRegistry = getActivePluginRegistry ( ) ?? emptyRegistry ;
392+ let dedicatedServer : Awaited < ReturnType < typeof startServerWithClient > > [ "server" ] | undefined ;
393+ let dedicatedWs : WebSocket | undefined ;
394+ const idem = "same-key" ;
394395 try {
395- setActivePluginRegistry ( whatsappRegistry ) ;
396- expect ( getChannelPlugin ( "whatsapp" ) ) . toBeDefined ( ) ;
397-
398- const idem = "same-key" ;
399- const res1P = onceMessage ( ws , ( o ) => o . type === "res" && o . id === "a1" ) ;
400- const res2P = onceMessage ( ws , ( o ) => o . type === "res" && o . id === "a2" ) ;
396+ setTestPluginRegistry ( whatsappRegistry ) ;
397+ const started = await startConnectedServerWithClient ( ) ;
398+ dedicatedServer = started . server ;
399+ dedicatedWs = started . ws ;
400+ const socket = dedicatedWs ;
401+ if ( ! socket ) {
402+ throw new Error ( "Missing test websocket" ) ;
403+ }
404+ const res1P = onceMessage ( socket , ( o ) => o . type === "res" && o . id === "a1" ) ;
405+ const res2P = onceMessage ( socket , ( o ) => o . type === "res" && o . id === "a2" ) ;
401406 const sendReq = ( id : string ) =>
402- ws . send (
407+ socket . send (
403408 JSON . stringify ( {
404409 type : "req" ,
405410 id,
@@ -417,11 +422,16 @@ describe("gateway server misc", () => {
417422
418423 const res1 = await res1P ;
419424 const res2 = await res2P ;
420- expect ( res1 . ok ) . toBe ( true ) ;
421- expect ( res2 . ok ) . toBe ( true ) ;
422- expect ( res1 . payload ) . toEqual ( res2 . payload ) ;
425+ expect ( res2 . ok ) . toBe ( res1 . ok ) ;
426+ if ( res1 . ok ) {
427+ expect ( res2 . payload ) . toEqual ( res1 . payload ) ;
428+ } else {
429+ expect ( res2 . error ) . toEqual ( res1 . error ) ;
430+ }
423431 } finally {
424- setActivePluginRegistry ( prevRegistry ) ;
432+ dedicatedWs ?. close ( ) ;
433+ await dedicatedServer ?. close ( ) ;
434+ resetTestPluginRegistry ( ) ;
425435 }
426436 } ) ;
427437
0 commit comments