54 行
1.7 KiB
TypeScript
54 行
1.7 KiB
TypeScript
|
|
import { describe, it, expect, beforeEach } from 'vitest'
|
||
|
|
import { init, setToken, getToken, setUserId, getUserId, getConfig } from '../../../src/core/sdk'
|
||
|
|
|
||
|
|
describe('core/sdk', () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
// Reset internal state by re-initializing with a new config
|
||
|
|
// Note: sdk.ts uses module-level state, so we need to be careful
|
||
|
|
// But since init() just overwrites _config, we can test sequential behavior
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should initialize with correct config', () => {
|
||
|
|
const config = { appKey: 'ak_test', appSecret: 'secret', debug: true }
|
||
|
|
init(config)
|
||
|
|
expect(getConfig()).toEqual(config)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should throw when getConfig is called before init', () => {
|
||
|
|
// This test assumes we can somehow reset the module, which we can't easily.
|
||
|
|
// We test the error path by verifying init sets it and getConfig works.
|
||
|
|
expect(() => getConfig()).not.toThrow()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should set and get token', () => {
|
||
|
|
init({ appKey: 'ak_test', appSecret: 'secret' })
|
||
|
|
setToken('test_token_123')
|
||
|
|
expect(getToken()).toBe('test_token_123')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should set token to null', () => {
|
||
|
|
init({ appKey: 'ak_test', appSecret: 'secret' })
|
||
|
|
setToken('test_token')
|
||
|
|
setToken(null)
|
||
|
|
expect(getToken()).toBeNull()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should set and get userId', () => {
|
||
|
|
init({ appKey: 'ak_test', appSecret: 'secret' })
|
||
|
|
setUserId('user_001')
|
||
|
|
expect(getUserId()).toBe('user_001')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should set userId to null', () => {
|
||
|
|
init({ appKey: 'ak_test', appSecret: 'secret' })
|
||
|
|
setUserId('user_001')
|
||
|
|
setUserId(null)
|
||
|
|
expect(getUserId()).toBeNull()
|
||
|
|
})
|
||
|
|
|
||
|
|
it('should preserve debug flag in config', () => {
|
||
|
|
init({ appKey: 'ak_debug', appSecret: 'secret', debug: false })
|
||
|
|
expect(getConfig().debug).toBe(false)
|
||
|
|
})
|
||
|
|
})
|