Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

secrets.test.ts

81 lines2.3 KB

1import { describe, expect, it } from 'vitest'
2​
3import {
4 PUSH_REMOVE_LIST,
5 formatEnvFile,
6 isRemoved,
7 parseConfigFlag,
8 parseEnvFile,
9 withoutRemoved,
10} from './secrets.ts'
11​
12describe('remove list', () => {
13 it('drops Doppler context vars and nothing else on pull', () => {
14 expect(isRemoved('DOPPLER_CONFIG')).toBe(true)
15 expect(isRemoved('DOPPLER_ENVIRONMENT')).toBe(true)
16 expect(isRemoved('DOPPLER_PROJECT')).toBe(true)
17 expect(isRemoved('ADMIN_KEY')).toBe(false)
18 expect(isRemoved('MISTRAL_API_KEY')).toBe(false)
19 expect(isRemoved('BETTER_AUTH_URL')).toBe(false)
20 expect(isRemoved('SEED_SPEECH_API_KEY')).toBe(false)
21 })
22​
23 it('also skips BETTER_AUTH_URL on push (already a wrangler var)', () => {
24 expect(isRemoved('BETTER_AUTH_URL', PUSH_REMOVE_LIST)).toBe(true)
25 expect(isRemoved('ADMIN_KEY', PUSH_REMOVE_LIST)).toBe(false)
26 expect(
27 withoutRemoved(
28 { ADMIN_KEY: 'x', BETTER_AUTH_URL: 'http://localhost:3100' },
29 PUSH_REMOVE_LIST,
30 ),
31 ).toEqual({ ADMIN_KEY: 'x' })
32 })
33​
34 it('strips removed names and empty values, keeps the rest', () => {
35 expect(
36 withoutRemoved({
37 ADMIN_KEY: 'x',
38 DOPPLER_CONFIG: 'dev',
39 DOPPLER_PROJECT: 'modelschemas',
40 MISTRAL_API_KEY: 'm',
41 EMPTY: '',
42 }),
43 ).toEqual({
44 ADMIN_KEY: 'x',
45 MISTRAL_API_KEY: 'm',
46 })
47 })
48})
49​
50describe('parseConfigFlag', () => {
51 it('falls back when --config is absent or valueless', () => {
52 expect(parseConfigFlag([], 'dev')).toBe('dev')
53 expect(parseConfigFlag(['--write'], 'dev')).toBe('dev')
54 expect(parseConfigFlag(['--config'], 'dev')).toBe('dev')
55 })
56​
57 it('reads the value after --config', () => {
58 expect(parseConfigFlag(['--config', 'stg', '--write'], 'dev')).toBe('stg')
59 })
60})
61​
62describe('env file codec', () => {
63 it('round-trips plain, quoted, and escaped values', () => {
64 const secrets = {
65 PLAIN: 'sk-abc',
66 SPACES: 'hello world',
67 QUOTES: `say "hi"`,
68 DOLLAR: 'pre$fix',
69 }
70 const text = formatEnvFile(secrets, ['header'])
71 expect(text.startsWith('# header\n\n')).toBe(true)
72 expect(parseEnvFile(text)).toEqual(secrets)
73 })
74​
75 it('skips comments and unknown lines', () => {
76 expect(parseEnvFile('# comment\nFOO=1\n\nnot-a-key\nBAR=2\n')).toEqual({
77 FOO: '1',
78 BAR: '2',
79 })
80 })
81})