mirror of
https://github.com/iflytek/skillhub.git
synced 2026-08-28 11:25:00 +00:00
Add 36 test cases covering: - Path traversal and symlink attack prevention in archive extraction - SkillHubClient error handling (401/403/404/network) for all endpoints - Inventory store concurrent writes and stale lock recovery - Config store read/write round-trip - Platform utilities (package-manager, updater, paths) - Output formatting (printResult, humanize) Tests use cross-platform commands (node) instead of shell builtins for CI compatibility across macOS/Linux/Windows.
42 lines
1.5 KiB
TypeScript
42 lines
1.5 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
import { CliError } from '../../../src/shared/errors'
|
|
import { printResult, renderError } from '../../../src/shared/output'
|
|
|
|
describe('renderError', () => {
|
|
test('renders stable json error to stderr payload', () => {
|
|
const error = new CliError('authentication failed', 2, { registry: 'https://registry.example.com' })
|
|
expect(renderError(error, true)).toBe(JSON.stringify({
|
|
ok: false,
|
|
message: 'authentication failed',
|
|
exitCode: 2,
|
|
details: { registry: 'https://registry.example.com' }
|
|
}))
|
|
})
|
|
|
|
test('renders human error without stack trace', () => {
|
|
const error = new CliError('registry unreachable', 3, {
|
|
registry: 'https://registry.example.com',
|
|
next: 'check network or pass --registry'
|
|
})
|
|
expect(renderError(error, false)).toBe([
|
|
'Error: registry unreachable',
|
|
'Context: registry https://registry.example.com',
|
|
'Next: check network or pass --registry'
|
|
].join('\n'))
|
|
})
|
|
})
|
|
|
|
describe('printResult', () => {
|
|
test('returns string as-is in non-JSON mode', () => {
|
|
expect(printResult('hello', false)).toBe('hello')
|
|
})
|
|
|
|
test('wraps string in JSON envelope', () => {
|
|
expect(printResult('hello', true)).toBe(JSON.stringify({ ok: true, message: 'hello' }))
|
|
})
|
|
|
|
test('formats object in non-JSON mode', () => {
|
|
const result = printResult({ ok: true, handle: 'me', email: 'a@b' }, false)
|
|
expect(result).toBe('handle: me\nemail: a@b')
|
|
})
|
|
})
|