-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathenv-files.test.ts
More file actions
58 lines (50 loc) · 1.92 KB
/
Copy pathenv-files.test.ts
File metadata and controls
58 lines (50 loc) · 1.92 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
import { describe, expect, it } from 'bun:test'
import {
isPlaceholder,
isUsableSecret,
parseEnv,
reconcileEnvContent,
upsertEnv,
} from './env-files.ts'
describe('placeholder detection', () => {
it('recognizes underscore and hyphen template prefixes', () => {
expect(isPlaceholder('your_secret_key')).toBe(true)
expect(isPlaceholder('your-secure-production-auth-secret-here')).toBe(true)
expect(isUsableSecret('BETTER_AUTH_SECRET', 'your-secure-production-auth-secret-here')).toBe(
false
)
expect(isPlaceholder('yourActualSecret')).toBe(false)
})
})
describe('upsertEnv', () => {
it('writes the value that parseEnv will use', () => {
const updated = upsertEnv('RESEND_API_KEY=old\nOTHER=value\n', 'RESEND_API_KEY', 'current')
expect(parseEnv(updated).get('RESEND_API_KEY')).toBe('current')
})
it('fails fast when duplicate active entries make the effective write ambiguous', () => {
expect(() =>
upsertEnv(
'RESEND_API_KEY=old\nexport RESEND_API_KEY=current\n',
'RESEND_API_KEY',
'replacement'
)
).toThrow('Duplicate active RESEND_API_KEY entries')
})
})
describe('reconcileEnvContent', () => {
it('applies provider removals and replacements to one snapshot', () => {
const reconciled = reconcileEnvContent(
'SMTP_HOST=old-host\nSMTP_PORT=587\nRESEND_API_KEY=old-key\n',
['SMTP_HOST', 'SMTP_PORT'],
{ RESEND_API_KEY: 'new-key' }
)
expect(parseEnv(reconciled)).toEqual(new Map([['RESEND_API_KEY', 'new-key']]))
})
it('fails before returning content when a replacement key is duplicated', () => {
const content = 'SMTP_HOST=old-host\nRESEND_API_KEY=old-key\nRESEND_API_KEY=newer-key\n'
expect(() =>
reconcileEnvContent(content, ['SMTP_HOST'], { RESEND_API_KEY: 'replacement' })
).toThrow('Duplicate active RESEND_API_KEY entries')
expect(parseEnv(content).get('SMTP_HOST')).toBe('old-host')
})
})