diff --git a/packages/cli-kit/src/private/node/conf-store.test.ts b/packages/cli-kit/src/private/node/conf-store.test.ts index 444776ab71..0faf6a1378 100644 --- a/packages/cli-kit/src/private/node/conf-store.test.ts +++ b/packages/cli-kit/src/private/node/conf-store.test.ts @@ -14,11 +14,18 @@ import { setCachedPartnerAccountStatus, runWithRateLimit, } from './conf-store.js' +import {isLocalEnvironment} from './context/service.js' import {LocalStorage} from '../../public/node/local-storage.js' import {inTemporaryDirectory} from '../../public/node/fs.js' import {afterEach, beforeEach, describe, expect, test, vi} from 'vitest' +vi.mock('./context/service.js') + +beforeEach(() => { + vi.mocked(isLocalEnvironment).mockReturnValue(false) +}) + describe('getSession', () => { test('returns the content of the SessionStore key', async () => { await inTemporaryDirectory(async (cwd) => { @@ -68,7 +75,7 @@ describe('removeSession', () => { }) describe('getCurrentSessionId', () => { - test('returns the content of the currentSessionId key', async () => { + test('returns the content of the currentSessionId key in production', async () => { await inTemporaryDirectory(async (cwd) => { // Given const config = new LocalStorage({cwd}) @@ -82,6 +89,21 @@ describe('getCurrentSessionId', () => { }) }) + test('returns the content of the currentDevSessionId key in dev', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + vi.mocked(isLocalEnvironment).mockReturnValue(true) + const config = new LocalStorage({cwd}) + config.set('currentDevSessionId', 'dev-user-456') + + // When + const got = getCurrentSessionId(config) + + // Then + expect(got).toEqual('dev-user-456') + }) + }) + test('returns undefined when currentSessionId is not set', async () => { await inTemporaryDirectory(async (cwd) => { // Given @@ -94,10 +116,24 @@ describe('getCurrentSessionId', () => { expect(got).toBeUndefined() }) }) + + test('does not return dev session when in production', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + const config = new LocalStorage({cwd}) + config.set('currentDevSessionId', 'dev-user') + + // When + const got = getCurrentSessionId(config) + + // Then + expect(got).toBeUndefined() + }) + }) }) describe('setCurrentSessionId', () => { - test('saves the desired content in the currentSessionId key', async () => { + test('saves to currentSessionId in production', async () => { await inTemporaryDirectory(async (cwd) => { // Given const config = new LocalStorage({cwd}) @@ -107,12 +143,28 @@ describe('setCurrentSessionId', () => { // Then expect(config.get('currentSessionId')).toEqual('user-456') + expect(config.get('currentDevSessionId')).toBeUndefined() + }) + }) + + test('saves to currentDevSessionId in dev', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + vi.mocked(isLocalEnvironment).mockReturnValue(true) + const config = new LocalStorage({cwd}) + + // When + setCurrentSessionId('dev-user-789', config) + + // Then + expect(config.get('currentDevSessionId')).toEqual('dev-user-789') + expect(config.get('currentSessionId')).toBeUndefined() }) }) }) describe('removeCurrentSessionId', () => { - test('removes the currentSessionId key', async () => { + test('removes the currentSessionId key in production', async () => { await inTemporaryDirectory(async (cwd) => { // Given const config = new LocalStorage({cwd}) @@ -125,6 +177,87 @@ describe('removeCurrentSessionId', () => { expect(config.get('currentSessionId')).toBeUndefined() }) }) + + test('removes the currentDevSessionId key in dev', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + vi.mocked(isLocalEnvironment).mockReturnValue(true) + const config = new LocalStorage({cwd}) + config.set('currentDevSessionId', 'dev-user') + + // When + removeCurrentSessionId(config) + + // Then + expect(config.get('currentDevSessionId')).toBeUndefined() + }) + }) +}) + +describe('session environment isolation', () => { + test('getSessions returns production sessions in production', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + const config = new LocalStorage({cwd}) + config.set('sessionStore', 'prod-sessions') + config.set('devSessionStore', 'dev-sessions') + + // When + const got = getSessions(config) + + // Then + expect(got).toEqual('prod-sessions') + }) + }) + + test('getSessions returns dev sessions in dev', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + vi.mocked(isLocalEnvironment).mockReturnValue(true) + const config = new LocalStorage({cwd}) + config.set('sessionStore', 'prod-sessions') + config.set('devSessionStore', 'dev-sessions') + + // When + const got = getSessions(config) + + // Then + expect(got).toEqual('dev-sessions') + }) + }) + + test('setSessions writes to devSessionStore in dev without affecting production', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + const config = new LocalStorage({cwd}) + setSessions('prod-sessions', config) + + // When + vi.mocked(isLocalEnvironment).mockReturnValue(true) + setSessions('dev-sessions', config) + + // Then + expect(config.get('sessionStore')).toEqual('prod-sessions') + expect(config.get('devSessionStore')).toEqual('dev-sessions') + }) + }) + + test('removeSessions only removes sessions for the current environment', async () => { + await inTemporaryDirectory(async (cwd) => { + // Given + const config = new LocalStorage({cwd}) + config.set('sessionStore', 'prod-sessions') + config.set('devSessionStore', 'dev-sessions') + + // When + vi.mocked(isLocalEnvironment).mockReturnValue(true) + removeSessions(config) + + // Then + expect(config.get('devSessionStore')).toBeUndefined() + expect(config.get('sessionStore')).toEqual('prod-sessions') + }) + }) }) describe('cacheRetrieveOrRepopulate', () => { diff --git a/packages/cli-kit/src/private/node/conf-store.ts b/packages/cli-kit/src/private/node/conf-store.ts index 1bc85399fd..1c585eae41 100644 --- a/packages/cli-kit/src/private/node/conf-store.ts +++ b/packages/cli-kit/src/private/node/conf-store.ts @@ -1,3 +1,4 @@ +import {isLocalEnvironment} from './context/service.js' import {isUnitTest} from '../../public/node/context/local.js' import {LocalStorage} from '../../public/node/local-storage.js' import {outputContent, outputDebug} from '../../public/node/output.js' @@ -28,6 +29,8 @@ interface Cache { export interface ConfSchema { sessionStore: string currentSessionId?: string + devSessionStore?: string + currentDevSessionId?: string cache?: Cache } @@ -45,6 +48,14 @@ function cliKitStore() { return _instance } +function sessionStoreKey(): 'devSessionStore' | 'sessionStore' { + return isLocalEnvironment() ? 'devSessionStore' : 'sessionStore' +} + +function currentSessionIdKey(): 'currentDevSessionId' | 'currentSessionId' { + return isLocalEnvironment() ? 'currentDevSessionId' : 'currentSessionId' +} + /** * Get session. * @@ -52,7 +63,7 @@ function cliKitStore() { */ export function getSessions(config: LocalStorage = cliKitStore()): string | undefined { outputDebug(outputContent`Getting session store...`) - return config.get('sessionStore') + return config.get(sessionStoreKey()) } /** @@ -62,7 +73,7 @@ export function getSessions(config: LocalStorage = cliKitStore()): s */ export function setSessions(session: string, config: LocalStorage = cliKitStore()): void { outputDebug(outputContent`Setting session store...`) - config.set('sessionStore', session) + config.set(sessionStoreKey(), session) } /** @@ -70,7 +81,7 @@ export function setSessions(session: string, config: LocalStorage = */ export function removeSessions(config: LocalStorage = cliKitStore()): void { outputDebug(outputContent`Removing session store...`) - config.delete('sessionStore') + config.delete(sessionStoreKey()) } /** @@ -80,7 +91,7 @@ export function removeSessions(config: LocalStorage = cliKitStore()) */ export function getCurrentSessionId(config: LocalStorage = cliKitStore()): string | undefined { outputDebug(outputContent`Getting current session ID...`) - return config.get('currentSessionId') + return config.get(currentSessionIdKey()) } /** @@ -90,7 +101,7 @@ export function getCurrentSessionId(config: LocalStorage = cliKitSto */ export function setCurrentSessionId(sessionId: string, config: LocalStorage = cliKitStore()): void { outputDebug(outputContent`Setting current session ID...`) - config.set('currentSessionId', sessionId) + config.set(currentSessionIdKey(), sessionId) } /** @@ -98,7 +109,7 @@ export function setCurrentSessionId(sessionId: string, config: LocalStorage = cliKitStore()): void { outputDebug(outputContent`Removing current session ID...`) - config.delete('currentSessionId') + config.delete(currentSessionIdKey()) } type CacheValueForKey = NonNullable['value']