refactor: 统一API鉴权并提升代码健壮性

This commit is contained in:
2026-02-22 11:50:48 +08:00
parent 2e2edac6af
commit 93c616793d
20 changed files with 210 additions and 139 deletions
+25
View File
@@ -0,0 +1,25 @@
import { describe, expect, it } from 'vitest'
import { isValidApiKey } from './auth'
describe('isValidApiKey', () => {
it('returns false when provided key is missing', () => {
expect(isValidApiKey(undefined, 'a'.repeat(32))).toBe(false)
expect(isValidApiKey(null, 'a'.repeat(32))).toBe(false)
})
it('returns false when expected key is missing', () => {
expect(isValidApiKey('a'.repeat(32), undefined)).toBe(false)
})
it('returns false when key lengths differ', () => {
expect(isValidApiKey('a'.repeat(31), 'a'.repeat(32))).toBe(false)
})
it('returns false when keys have same length but different value', () => {
expect(isValidApiKey('b'.repeat(32), 'a'.repeat(32))).toBe(false)
})
it('returns true when keys match exactly', () => {
expect(isValidApiKey('a'.repeat(32), 'a'.repeat(32))).toBe(true)
})
})