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

secrets.test.ts

103 lines3.1 KB

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