forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyok.test.ts
More file actions
145 lines (125 loc) · 4.63 KB
/
Copy pathbyok.test.ts
File metadata and controls
145 lines (125 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { describe, it, expect } from 'vitest'
import {
BYOK_PROVIDERS,
BYOK_HEADER_NAMES,
BYOK_ENV_NAMES,
withByokHeaders,
byokEnvVars,
isByokActive,
type ByokKeys
} from './byok'
import { byokFingerprint } from './byokFingerprint'
const fullKeys: ByokKeys = {
openrouter: 'sk-or',
openai: 'sk-openai',
anthropic: 'sk-ant',
gemini: 'gm-key',
deepgram: 'dg-key'
}
describe('withByokHeaders', () => {
it('attaches a header for each non-empty key (full set)', () => {
const out = withByokHeaders({}, fullKeys)
expect(out).toEqual({
'X-BYOK-OpenRouter': 'sk-or',
'X-BYOK-OpenAI': 'sk-openai',
'X-BYOK-Anthropic': 'sk-ant',
'X-BYOK-Gemini': 'gm-key',
'X-BYOK-Deepgram': 'dg-key'
})
})
it('attaches only the provided providers (partial set)', () => {
const out = withByokHeaders({}, { openai: 'sk-openai', gemini: 'gm-key' })
expect(out).toEqual({ 'X-BYOK-OpenAI': 'sk-openai', 'X-BYOK-Gemini': 'gm-key' })
expect(out['X-BYOK-Anthropic']).toBeUndefined()
})
it('trims key values before attaching', () => {
const out = withByokHeaders({}, { openai: ' sk-openai ' })
expect(out['X-BYOK-OpenAI']).toBe('sk-openai')
})
it('skips empty and whitespace-only keys', () => {
const out = withByokHeaders({}, { openai: '', anthropic: ' ', gemini: 'gm' })
expect(out['X-BYOK-OpenAI']).toBeUndefined()
expect(out['X-BYOK-Anthropic']).toBeUndefined()
expect(out['X-BYOK-Gemini']).toBe('gm')
})
it('preserves existing headers and returns a new object (no mutation)', () => {
const headers = { Authorization: 'Bearer abc' }
const keys: ByokKeys = { openai: 'sk-openai' }
const out = withByokHeaders(headers, keys)
expect(out.Authorization).toBe('Bearer abc')
expect(out['X-BYOK-OpenAI']).toBe('sk-openai')
// inputs untouched
expect(headers).toEqual({ Authorization: 'Bearer abc' })
expect(keys).toEqual({ openai: 'sk-openai' })
expect(out).not.toBe(headers)
})
})
describe('isByokActive', () => {
it('is true when an LLM provider has a non-empty key', () => {
expect(isByokActive(fullKeys)).toBe(true)
})
it('is false with only a transcription key', () => {
expect(isByokActive({ deepgram: 'dg-key' })).toBe(false)
})
it('is false when a provider key is only whitespace', () => {
expect(isByokActive({ gemini: ' ' })).toBe(false)
})
it('is false for an empty map', () => {
expect(isByokActive({})).toBe(false)
})
it('covers every supported provider', () => {
expect(BYOK_PROVIDERS).toEqual(['openrouter', 'openai', 'anthropic', 'gemini', 'deepgram'])
expect(Object.keys(BYOK_HEADER_NAMES).sort()).toEqual([...BYOK_PROVIDERS].sort())
})
})
describe('byokEnvVars', () => {
it('returns the complete OMI_BYOK_* set when all four keys are present', () => {
expect(byokEnvVars(fullKeys)).toEqual({
OMI_BYOK_OPENROUTER: 'sk-or',
OMI_BYOK_OPENAI: 'sk-openai',
OMI_BYOK_ANTHROPIC: 'sk-ant',
OMI_BYOK_GEMINI: 'gm-key',
OMI_BYOK_DEEPGRAM: 'dg-key'
})
})
it('returns configured capabilities for a partial set', () => {
const partial: ByokKeys = { ...fullKeys }
delete partial.deepgram
expect(byokEnvVars(partial)).toEqual({
OMI_BYOK_OPENROUTER: 'sk-or',
OMI_BYOK_OPENAI: 'sk-openai',
OMI_BYOK_ANTHROPIC: 'sk-ant',
OMI_BYOK_GEMINI: 'gm-key'
})
})
it('omits a provider key when it is only whitespace', () => {
expect(byokEnvVars({ ...fullKeys, gemini: ' ' }).OMI_BYOK_GEMINI).toBeUndefined()
})
it('returns {} for an empty map', () => {
expect(byokEnvVars({})).toEqual({})
})
it('trims key values before injecting', () => {
expect(byokEnvVars({ ...fullKeys, openai: ' sk-openai ' }).OMI_BYOK_OPENAI).toBe('sk-openai')
})
it('names cover every supported provider', () => {
expect(Object.keys(BYOK_ENV_NAMES).sort()).toEqual([...BYOK_PROVIDERS].sort())
})
})
describe('byokFingerprint', () => {
it('produces 64 lowercase hex chars', () => {
const fp = byokFingerprint('sk-some-key')
expect(fp).toMatch(/^[a-f0-9]{64}$/)
})
it('is deterministic and matches the known SHA-256 of the input', () => {
// Reference: sha256("abc") = ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
expect(byokFingerprint('abc')).toBe(
'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'
)
expect(byokFingerprint('abc')).toBe(byokFingerprint('abc'))
})
it('is whitespace-insensitive (hashes the trimmed key, matching the wire value)', () => {
const fp = byokFingerprint(' abc ')
expect(fp).toBe(byokFingerprint('abc'))
expect(fp).toMatch(/^[a-f0-9]{64}$/)
})
})