@@ -14,11 +14,18 @@ import {
1414 setCachedPartnerAccountStatus ,
1515 runWithRateLimit ,
1616} from './conf-store.js'
17+ import { isLocalEnvironment } from './context/service.js'
1718import { LocalStorage } from '../../public/node/local-storage.js'
1819import { inTemporaryDirectory } from '../../public/node/fs.js'
1920
2021import { afterEach , beforeEach , describe , expect , test , vi } from 'vitest'
2122
23+ vi . mock ( './context/service.js' )
24+
25+ beforeEach ( ( ) => {
26+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( false )
27+ } )
28+
2229describe ( 'getSession' , ( ) => {
2330 test ( 'returns the content of the SessionStore key' , async ( ) => {
2431 await inTemporaryDirectory ( async ( cwd ) => {
@@ -68,7 +75,7 @@ describe('removeSession', () => {
6875} )
6976
7077describe ( 'getCurrentSessionId' , ( ) => {
71- test ( 'returns the content of the currentSessionId key' , async ( ) => {
78+ test ( 'returns the content of the currentSessionId key in production ' , async ( ) => {
7279 await inTemporaryDirectory ( async ( cwd ) => {
7380 // Given
7481 const config = new LocalStorage < ConfSchema > ( { cwd} )
@@ -82,6 +89,21 @@ describe('getCurrentSessionId', () => {
8289 } )
8390 } )
8491
92+ test ( 'returns the content of the currentDevSessionId key in dev' , async ( ) => {
93+ await inTemporaryDirectory ( async ( cwd ) => {
94+ // Given
95+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( true )
96+ const config = new LocalStorage < ConfSchema > ( { cwd} )
97+ config . set ( 'currentDevSessionId' , 'dev-user-456' )
98+
99+ // When
100+ const got = getCurrentSessionId ( config )
101+
102+ // Then
103+ expect ( got ) . toEqual ( 'dev-user-456' )
104+ } )
105+ } )
106+
85107 test ( 'returns undefined when currentSessionId is not set' , async ( ) => {
86108 await inTemporaryDirectory ( async ( cwd ) => {
87109 // Given
@@ -94,10 +116,24 @@ describe('getCurrentSessionId', () => {
94116 expect ( got ) . toBeUndefined ( )
95117 } )
96118 } )
119+
120+ test ( 'does not return dev session when in production' , async ( ) => {
121+ await inTemporaryDirectory ( async ( cwd ) => {
122+ // Given
123+ const config = new LocalStorage < ConfSchema > ( { cwd} )
124+ config . set ( 'currentDevSessionId' , 'dev-user' )
125+
126+ // When
127+ const got = getCurrentSessionId ( config )
128+
129+ // Then
130+ expect ( got ) . toBeUndefined ( )
131+ } )
132+ } )
97133} )
98134
99135describe ( 'setCurrentSessionId' , ( ) => {
100- test ( 'saves the desired content in the currentSessionId key ' , async ( ) => {
136+ test ( 'saves to currentSessionId in production ' , async ( ) => {
101137 await inTemporaryDirectory ( async ( cwd ) => {
102138 // Given
103139 const config = new LocalStorage < ConfSchema > ( { cwd} )
@@ -107,12 +143,28 @@ describe('setCurrentSessionId', () => {
107143
108144 // Then
109145 expect ( config . get ( 'currentSessionId' ) ) . toEqual ( 'user-456' )
146+ expect ( config . get ( 'currentDevSessionId' ) ) . toBeUndefined ( )
147+ } )
148+ } )
149+
150+ test ( 'saves to currentDevSessionId in dev' , async ( ) => {
151+ await inTemporaryDirectory ( async ( cwd ) => {
152+ // Given
153+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( true )
154+ const config = new LocalStorage < ConfSchema > ( { cwd} )
155+
156+ // When
157+ setCurrentSessionId ( 'dev-user-789' , config )
158+
159+ // Then
160+ expect ( config . get ( 'currentDevSessionId' ) ) . toEqual ( 'dev-user-789' )
161+ expect ( config . get ( 'currentSessionId' ) ) . toBeUndefined ( )
110162 } )
111163 } )
112164} )
113165
114166describe ( 'removeCurrentSessionId' , ( ) => {
115- test ( 'removes the currentSessionId key' , async ( ) => {
167+ test ( 'removes the currentSessionId key in production ' , async ( ) => {
116168 await inTemporaryDirectory ( async ( cwd ) => {
117169 // Given
118170 const config = new LocalStorage < ConfSchema > ( { cwd} )
@@ -125,6 +177,87 @@ describe('removeCurrentSessionId', () => {
125177 expect ( config . get ( 'currentSessionId' ) ) . toBeUndefined ( )
126178 } )
127179 } )
180+
181+ test ( 'removes the currentDevSessionId key in dev' , async ( ) => {
182+ await inTemporaryDirectory ( async ( cwd ) => {
183+ // Given
184+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( true )
185+ const config = new LocalStorage < ConfSchema > ( { cwd} )
186+ config . set ( 'currentDevSessionId' , 'dev-user' )
187+
188+ // When
189+ removeCurrentSessionId ( config )
190+
191+ // Then
192+ expect ( config . get ( 'currentDevSessionId' ) ) . toBeUndefined ( )
193+ } )
194+ } )
195+ } )
196+
197+ describe ( 'session environment isolation' , ( ) => {
198+ test ( 'getSessions returns production sessions in production' , async ( ) => {
199+ await inTemporaryDirectory ( async ( cwd ) => {
200+ // Given
201+ const config = new LocalStorage < ConfSchema > ( { cwd} )
202+ config . set ( 'sessionStore' , 'prod-sessions' )
203+ config . set ( 'devSessionStore' , 'dev-sessions' )
204+
205+ // When
206+ const got = getSessions ( config )
207+
208+ // Then
209+ expect ( got ) . toEqual ( 'prod-sessions' )
210+ } )
211+ } )
212+
213+ test ( 'getSessions returns dev sessions in dev' , async ( ) => {
214+ await inTemporaryDirectory ( async ( cwd ) => {
215+ // Given
216+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( true )
217+ const config = new LocalStorage < ConfSchema > ( { cwd} )
218+ config . set ( 'sessionStore' , 'prod-sessions' )
219+ config . set ( 'devSessionStore' , 'dev-sessions' )
220+
221+ // When
222+ const got = getSessions ( config )
223+
224+ // Then
225+ expect ( got ) . toEqual ( 'dev-sessions' )
226+ } )
227+ } )
228+
229+ test ( 'setSessions writes to devSessionStore in dev without affecting production' , async ( ) => {
230+ await inTemporaryDirectory ( async ( cwd ) => {
231+ // Given
232+ const config = new LocalStorage < ConfSchema > ( { cwd} )
233+ setSessions ( 'prod-sessions' , config )
234+
235+ // When
236+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( true )
237+ setSessions ( 'dev-sessions' , config )
238+
239+ // Then
240+ expect ( config . get ( 'sessionStore' ) ) . toEqual ( 'prod-sessions' )
241+ expect ( config . get ( 'devSessionStore' ) ) . toEqual ( 'dev-sessions' )
242+ } )
243+ } )
244+
245+ test ( 'removeSessions only removes sessions for the current environment' , async ( ) => {
246+ await inTemporaryDirectory ( async ( cwd ) => {
247+ // Given
248+ const config = new LocalStorage < ConfSchema > ( { cwd} )
249+ config . set ( 'sessionStore' , 'prod-sessions' )
250+ config . set ( 'devSessionStore' , 'dev-sessions' )
251+
252+ // When
253+ vi . mocked ( isLocalEnvironment ) . mockReturnValue ( true )
254+ removeSessions ( config )
255+
256+ // Then
257+ expect ( config . get ( 'devSessionStore' ) ) . toBeUndefined ( )
258+ expect ( config . get ( 'sessionStore' ) ) . toEqual ( 'prod-sessions' )
259+ } )
260+ } )
128261} )
129262
130263describe ( 'cacheRetrieveOrRepopulate' , ( ) => {
0 commit comments