@@ -3,11 +3,10 @@ import { ErrorCodes } from "../../packages/gateway-protocol/src/index.js";
33import type { SessionEntry } from "../config/sessions/types.js" ;
44
55const hoisted = vi . hoisted ( ( ) => ( {
6- loadSessionStoreMock : vi . fn ( ) ,
76 updateSessionStoreMock : vi . fn ( ) ,
87 listSessionsFromStoreMock : vi . fn ( ) ,
98 migrateAndPruneGatewaySessionStoreKeyMock : vi . fn ( ) ,
10- resolveGatewaySessionStoreTargetMock : vi . fn ( ) ,
9+ resolveGatewaySessionStoreTargetWithStoreMock : vi . fn ( ) ,
1110 loadCombinedSessionStoreForGatewayMock : vi . fn ( ) ,
1211 listAgentIdsMock : vi . fn ( ) ,
1312} ) ) ;
@@ -27,7 +26,6 @@ vi.mock("../config/sessions.js", async () => {
2726 await vi . importActual < typeof import ( "../config/sessions.js" ) > ( "../config/sessions.js" ) ;
2827 return {
2928 ...actual ,
30- loadSessionStore : hoisted . loadSessionStoreMock ,
3129 updateSessionStore : hoisted . updateSessionStoreMock ,
3230 } ;
3331} ) ;
@@ -38,7 +36,8 @@ vi.mock("./session-utils.js", async () => {
3836 ...actual ,
3937 listSessionsFromStore : hoisted . listSessionsFromStoreMock ,
4038 migrateAndPruneGatewaySessionStoreKey : hoisted . migrateAndPruneGatewaySessionStoreKeyMock ,
41- resolveGatewaySessionStoreTarget : hoisted . resolveGatewaySessionStoreTargetMock ,
39+ resolveGatewaySessionStoreTargetWithStore :
40+ hoisted . resolveGatewaySessionStoreTargetWithStoreMock ,
4241 loadCombinedSessionStoreForGateway : hoisted . loadCombinedSessionStoreForGatewayMock ,
4342 } ;
4443} ) ;
@@ -49,6 +48,7 @@ describe("resolveSessionKeyFromResolveParams", () => {
4948 const canonicalKey = "agent:main:canon" ;
5049 const legacyKey = "agent:main:legacy" ;
5150 const storePath = "/tmp/sessions.json" ;
51+ let targetStore : Record < string , SessionEntry > ;
5252
5353 const expectResolveToCanonicalKey = async (
5454 p : Parameters < typeof resolveSessionKeyFromResolveParams > [ 0 ] [ "p" ] ,
@@ -66,37 +66,33 @@ describe("resolveSessionKeyFromResolveParams", () => {
6666 } ;
6767
6868 beforeEach ( ( ) => {
69- hoisted . loadSessionStoreMock . mockReset ( ) ;
7069 hoisted . updateSessionStoreMock . mockReset ( ) ;
7170 hoisted . listSessionsFromStoreMock . mockReset ( ) ;
7271 hoisted . migrateAndPruneGatewaySessionStoreKeyMock . mockReset ( ) ;
73- hoisted . resolveGatewaySessionStoreTargetMock . mockReset ( ) ;
72+ hoisted . resolveGatewaySessionStoreTargetWithStoreMock . mockReset ( ) ;
7473 hoisted . loadCombinedSessionStoreForGatewayMock . mockReset ( ) ;
7574 hoisted . listAgentIdsMock . mockReset ( ) ;
75+ targetStore = { } ;
7676 // Default: all agents are known (main is always present).
7777 hoisted . listAgentIdsMock . mockReturnValue ( [ "main" ] ) ;
78- hoisted . resolveGatewaySessionStoreTargetMock . mockReturnValue ( {
78+ hoisted . resolveGatewaySessionStoreTargetWithStoreMock . mockImplementation ( ( ) => ( {
7979 canonicalKey,
8080 storeKeys : [ canonicalKey , legacyKey ] ,
8181 storePath,
82- } ) ;
82+ store : targetStore ,
83+ } ) ) ;
8384 hoisted . migrateAndPruneGatewaySessionStoreKeyMock . mockReturnValue ( { primaryKey : canonicalKey } ) ;
8485 hoisted . updateSessionStoreMock . mockImplementation (
8586 async ( _path : string , updater : ( store : Record < string , SessionEntry > ) => void ) => {
86- const store = hoisted . loadSessionStoreMock . mock . results [ 0 ] ?. value as
87- | Record < string , SessionEntry >
88- | undefined ;
89- if ( store ) {
90- updater ( store ) ;
91- }
87+ updater ( targetStore ) ;
9288 } ,
9389 ) ;
9490 } ) ;
9591
9692 it ( "hides canonical keys that fail the spawnedBy visibility filter" , async ( ) => {
97- hoisted . loadSessionStoreMock . mockReturnValue ( {
93+ targetStore = {
9894 [ canonicalKey ] : { sessionId : "sess-1" , updatedAt : 1 } ,
99- } ) ;
95+ } ;
10096 hoisted . listSessionsFromStoreMock . mockReturnValue ( { sessions : [ ] } ) ;
10197
10298 await expect (
@@ -129,7 +125,7 @@ describe("resolveSessionKeyFromResolveParams", () => {
129125 updatedAt : now - i ,
130126 } ;
131127 }
132- hoisted . loadSessionStoreMock . mockReturnValue ( store ) ;
128+ targetStore = store ;
133129
134130 await expectResolveToCanonicalKey ( { key : canonicalKey , spawnedBy : "controller-1" } ) ;
135131 } ) ;
@@ -138,7 +134,7 @@ describe("resolveSessionKeyFromResolveParams", () => {
138134 const store = {
139135 [ legacyKey ] : { sessionId : "sess-legacy" , spawnedBy : "controller-1" , updatedAt : Date . now ( ) } ,
140136 } satisfies Record < string , SessionEntry > ;
141- hoisted . loadSessionStoreMock . mockImplementation ( ( ) => store ) ;
137+ targetStore = store ;
142138
143139 await expectResolveToCanonicalKey ( { key : canonicalKey , spawnedBy : "controller-1" } ) ;
144140
@@ -150,13 +146,14 @@ describe("resolveSessionKeyFromResolveParams", () => {
150146
151147 it ( "rejects sessions belonging to a deleted agent (key-based lookup)" , async ( ) => {
152148 const deletedAgentKey = "agent:deleted-agent:main" ;
153- hoisted . resolveGatewaySessionStoreTargetMock . mockReturnValue ( {
149+ targetStore = {
150+ [ deletedAgentKey ] : { sessionId : "sess-orphan" , updatedAt : 1 } ,
151+ } ;
152+ hoisted . resolveGatewaySessionStoreTargetWithStoreMock . mockReturnValue ( {
154153 canonicalKey : deletedAgentKey ,
155154 storeKeys : [ deletedAgentKey ] ,
156155 storePath,
157- } ) ;
158- hoisted . loadSessionStoreMock . mockReturnValue ( {
159- [ deletedAgentKey ] : { sessionId : "sess-orphan" , updatedAt : 1 } ,
156+ store : targetStore ,
160157 } ) ;
161158 // "deleted-agent" is not in the known agents list.
162159 hoisted . listAgentIdsMock . mockReturnValue ( [ "main" ] ) ;
@@ -177,13 +174,14 @@ describe("resolveSessionKeyFromResolveParams", () => {
177174
178175 it ( "rejects non-alias agent:main sessions when main is no longer configured" , async ( ) => {
179176 const staleMainKey = "agent:main:guildchat:direct:u1" ;
180- hoisted . resolveGatewaySessionStoreTargetMock . mockReturnValue ( {
177+ targetStore = {
178+ [ staleMainKey ] : { sessionId : "sess-stale-main" , updatedAt : 1 } ,
179+ } ;
180+ hoisted . resolveGatewaySessionStoreTargetWithStoreMock . mockReturnValue ( {
181181 canonicalKey : staleMainKey ,
182182 storeKeys : [ staleMainKey ] ,
183183 storePath,
184- } ) ;
185- hoisted . loadSessionStoreMock . mockReturnValue ( {
186- [ staleMainKey ] : { sessionId : "sess-stale-main" , updatedAt : 1 } ,
184+ store : targetStore ,
187185 } ) ;
188186 hoisted . listAgentIdsMock . mockReturnValue ( [ "ops" ] ) ;
189187
0 commit comments