Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 136 additions & 3 deletions packages/cli-kit/src/private/node/conf-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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<ConfSchema>({cwd})
Expand All @@ -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<ConfSchema>({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
Expand All @@ -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<ConfSchema>({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<ConfSchema>({cwd})
Expand All @@ -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<ConfSchema>({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<ConfSchema>({cwd})
Expand All @@ -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<ConfSchema>({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<ConfSchema>({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<ConfSchema>({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<ConfSchema>({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<ConfSchema>({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', () => {
Expand Down
23 changes: 17 additions & 6 deletions packages/cli-kit/src/private/node/conf-store.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -28,6 +29,8 @@ interface Cache {
export interface ConfSchema {
sessionStore: string
currentSessionId?: string
devSessionStore?: string
currentDevSessionId?: string
cache?: Cache
}

Expand All @@ -45,14 +48,22 @@ function cliKitStore() {
return _instance
}

function sessionStoreKey(): 'devSessionStore' | 'sessionStore' {
return isLocalEnvironment() ? 'devSessionStore' : 'sessionStore'
}

function currentSessionIdKey(): 'currentDevSessionId' | 'currentSessionId' {
return isLocalEnvironment() ? 'currentDevSessionId' : 'currentSessionId'
}

/**
* Get session.
*
* @returns Session.
*/
export function getSessions(config: LocalStorage<ConfSchema> = cliKitStore()): string | undefined {
outputDebug(outputContent`Getting session store...`)
return config.get('sessionStore')
return config.get(sessionStoreKey())
}

/**
Expand All @@ -62,15 +73,15 @@ export function getSessions(config: LocalStorage<ConfSchema> = cliKitStore()): s
*/
export function setSessions(session: string, config: LocalStorage<ConfSchema> = cliKitStore()): void {
outputDebug(outputContent`Setting session store...`)
config.set('sessionStore', session)
config.set(sessionStoreKey(), session)
}

/**
* Remove session.
*/
export function removeSessions(config: LocalStorage<ConfSchema> = cliKitStore()): void {
outputDebug(outputContent`Removing session store...`)
config.delete('sessionStore')
config.delete(sessionStoreKey())
}

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

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

/**
* Remove current session ID.
*/
export function removeCurrentSessionId(config: LocalStorage<ConfSchema> = cliKitStore()): void {
outputDebug(outputContent`Removing current session ID...`)
config.delete('currentSessionId')
config.delete(currentSessionIdKey())
}

type CacheValueForKey<TKey extends keyof Cache> = NonNullable<Cache[TKey]>['value']
Expand Down
Loading