Skip to content

Commit 99be1aa

Browse files
Store dev sessions separately
1 parent cb018ff commit 99be1aa

File tree

2 files changed

+153
-9
lines changed

2 files changed

+153
-9
lines changed

packages/cli-kit/src/private/node/conf-store.test.ts

Lines changed: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ import {
1414
setCachedPartnerAccountStatus,
1515
runWithRateLimit,
1616
} from './conf-store.js'
17+
import {isLocalEnvironment} from './context/service.js'
1718
import {LocalStorage} from '../../public/node/local-storage.js'
1819
import {inTemporaryDirectory} from '../../public/node/fs.js'
1920

2021
import {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+
2229
describe('getSession', () => {
2330
test('returns the content of the SessionStore key', async () => {
2431
await inTemporaryDirectory(async (cwd) => {
@@ -68,7 +75,7 @@ describe('removeSession', () => {
6875
})
6976

7077
describe('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

99135
describe('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

114166
describe('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

130263
describe('cacheRetrieveOrRepopulate', () => {

packages/cli-kit/src/private/node/conf-store.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {isLocalEnvironment} from './context/service.js'
12
import {isUnitTest} from '../../public/node/context/local.js'
23
import {LocalStorage} from '../../public/node/local-storage.js'
34
import {outputContent, outputDebug} from '../../public/node/output.js'
@@ -28,6 +29,8 @@ interface Cache {
2829
export interface ConfSchema {
2930
sessionStore: string
3031
currentSessionId?: string
32+
devSessionStore?: string
33+
currentDevSessionId?: string
3134
cache?: Cache
3235
}
3336

@@ -45,14 +48,22 @@ function cliKitStore() {
4548
return _instance
4649
}
4750

51+
function sessionStoreKey(): 'devSessionStore' | 'sessionStore' {
52+
return isLocalEnvironment() ? 'devSessionStore' : 'sessionStore'
53+
}
54+
55+
function currentSessionIdKey(): 'currentDevSessionId' | 'currentSessionId' {
56+
return isLocalEnvironment() ? 'currentDevSessionId' : 'currentSessionId'
57+
}
58+
4859
/**
4960
* Get session.
5061
*
5162
* @returns Session.
5263
*/
5364
export function getSessions(config: LocalStorage<ConfSchema> = cliKitStore()): string | undefined {
5465
outputDebug(outputContent`Getting session store...`)
55-
return config.get('sessionStore')
66+
return config.get(sessionStoreKey())
5667
}
5768

5869
/**
@@ -62,15 +73,15 @@ export function getSessions(config: LocalStorage<ConfSchema> = cliKitStore()): s
6273
*/
6374
export function setSessions(session: string, config: LocalStorage<ConfSchema> = cliKitStore()): void {
6475
outputDebug(outputContent`Setting session store...`)
65-
config.set('sessionStore', session)
76+
config.set(sessionStoreKey(), session)
6677
}
6778

6879
/**
6980
* Remove session.
7081
*/
7182
export function removeSessions(config: LocalStorage<ConfSchema> = cliKitStore()): void {
7283
outputDebug(outputContent`Removing session store...`)
73-
config.delete('sessionStore')
84+
config.delete(sessionStoreKey())
7485
}
7586

7687
/**
@@ -80,7 +91,7 @@ export function removeSessions(config: LocalStorage<ConfSchema> = cliKitStore())
8091
*/
8192
export function getCurrentSessionId(config: LocalStorage<ConfSchema> = cliKitStore()): string | undefined {
8293
outputDebug(outputContent`Getting current session ID...`)
83-
return config.get('currentSessionId')
94+
return config.get(currentSessionIdKey())
8495
}
8596

8697
/**
@@ -90,15 +101,15 @@ export function getCurrentSessionId(config: LocalStorage<ConfSchema> = cliKitSto
90101
*/
91102
export function setCurrentSessionId(sessionId: string, config: LocalStorage<ConfSchema> = cliKitStore()): void {
92103
outputDebug(outputContent`Setting current session ID...`)
93-
config.set('currentSessionId', sessionId)
104+
config.set(currentSessionIdKey(), sessionId)
94105
}
95106

96107
/**
97108
* Remove current session ID.
98109
*/
99110
export function removeCurrentSessionId(config: LocalStorage<ConfSchema> = cliKitStore()): void {
100111
outputDebug(outputContent`Removing current session ID...`)
101-
config.delete('currentSessionId')
112+
config.delete(currentSessionIdKey())
102113
}
103114

104115
type CacheValueForKey<TKey extends keyof Cache> = NonNullable<Cache[TKey]>['value']

0 commit comments

Comments
 (0)