skillhub/cli/test/unit/stores/credentials-store.test.ts
XiaoSeS 717165bd69
Some checks are pending
Deploy Docs / build (push) Waiting to run
Deploy Docs / Deploy (push) Blocked by required conditions
Security / Dependency Review (push) Waiting to run
Security / CodeQL (java-kotlin) (push) Waiting to run
Security / CodeQL (javascript-typescript) (push) Waiting to run
Security / CodeQL (python) (push) Waiting to run
fix(cli): preserve shared SkillHub state fields (#835)
Signed-off-by: XiaoSeS <87064762+XiaoSeS@users.noreply.github.com>
2026-09-08 19:41:49 +08:00

64 lines
2.5 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import { readFile } from 'node:fs/promises'
import { normalize } from 'node:path'
import { createTempHome } from '../../helpers/temp-env'
import { CredentialsStore } from '../../../src/stores/credentials-store'
describe('CredentialsStore', () => {
test('stores tokens under user home .skillhub only', async () => {
const env = await createTempHome()
const store = new CredentialsStore(env.home)
await store.setToken('https://registry.example.com', 'sk_test')
expect(await store.getToken('https://registry.example.com')).toBe('sk_test')
expect(normalize(store.path)).toBe(normalize(`${env.home}/.skillhub/credentials.json`))
expect(await Bun.file(`${env.cwd}/credentials.json`).exists()).toBe(false)
})
test('treats a third-party credentials file without tokens as logged out', async () => {
const env = await createTempHome()
const store = new CredentialsStore(env.home)
await Bun.write(store.path, JSON.stringify({
user: { token: 'third-party-token', host: 'https://api.skillhub.cn' }
}))
expect(await store.getToken('https://registry.example.com')).toBeUndefined()
})
test('setToken preserves third-party and unknown fields', async () => {
const env = await createTempHome()
const store = new CredentialsStore(env.home)
await Bun.write(store.path, JSON.stringify({
user: { token: 'third-party-token', host: 'https://api.skillhub.cn' },
futureField: { enabled: true }
}))
await store.setToken('https://registry.example.com', 'sk_test')
const saved = JSON.parse(await readFile(store.path, 'utf-8'))
expect(saved).toEqual({
user: { token: 'third-party-token', host: 'https://api.skillhub.cn' },
futureField: { enabled: true },
tokens: { 'https://registry.example.com': 'sk_test' }
})
})
test('deleteToken removes only the selected first-party token', async () => {
const env = await createTempHome()
const store = new CredentialsStore(env.home)
await Bun.write(store.path, JSON.stringify({
user: { token: 'third-party-token', host: 'https://api.skillhub.cn' },
tokens: {
'https://registry-a.example.com': 'sk_a',
'https://registry-b.example.com': 'sk_b'
}
}))
await store.deleteToken('https://registry-a.example.com')
const saved = JSON.parse(await readFile(store.path, 'utf-8'))
expect(saved).toEqual({
user: { token: 'third-party-token', host: 'https://api.skillhub.cn' },
tokens: { 'https://registry-b.example.com': 'sk_b' }
})
})
})