DefaultEdgeFunctionSecrets.utils.test.ts68 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { |
| 4 | DEFAULT_EDGE_FUNCTION_SECRETS, |
| 5 | getVisibleDefaultEdgeFunctionSecrets, |
| 6 | isInternalEdgeFunctionSecret, |
| 7 | } from './DefaultEdgeFunctionSecrets.utils' |
| 8 | |
| 9 | describe('isInternalEdgeFunctionSecret', () => { |
| 10 | it.each(['BRIVEN_URL', 'BRIVEN_ANON_KEY', 'BRIVEN_THIS_DOES_NOT_EXIST_YET'])( |
| 11 | 'treats BRIVEN_-prefixed names as internal (%s)', |
| 12 | (name) => { |
| 13 | expect(isInternalEdgeFunctionSecret(name)).toBe(true) |
| 14 | } |
| 15 | ) |
| 16 | |
| 17 | it.each(['SB_REGION', 'SB_EXECUTION_ID', 'DENO_DEPLOYMENT_ID'])( |
| 18 | 'treats hardcoded default name %s as internal', |
| 19 | (name) => { |
| 20 | expect(isInternalEdgeFunctionSecret(name)).toBe(true) |
| 21 | } |
| 22 | ) |
| 23 | |
| 24 | it.each(['MY_API_KEY', 'STRIPE_SECRET', 'sb_region', 'DENO_OTHER_VAR'])( |
| 25 | 'treats user-defined name %s as not internal', |
| 26 | (name) => { |
| 27 | expect(isInternalEdgeFunctionSecret(name)).toBe(false) |
| 28 | } |
| 29 | ) |
| 30 | }) |
| 31 | |
| 32 | describe('getVisibleDefaultEdgeFunctionSecrets', () => { |
| 33 | const runtimeNames = DEFAULT_EDGE_FUNCTION_SECRETS.filter((secret) => secret.isRuntime).map( |
| 34 | (secret) => secret.name |
| 35 | ) |
| 36 | const staticNames = DEFAULT_EDGE_FUNCTION_SECRETS.filter((secret) => !secret.isRuntime).map( |
| 37 | (secret) => secret.name |
| 38 | ) |
| 39 | |
| 40 | it('always includes runtime secrets', () => { |
| 41 | const result = getVisibleDefaultEdgeFunctionSecrets(new Set()) |
| 42 | for (const name of runtimeNames) { |
| 43 | expect(result.map((secret) => secret.name)).toContain(name) |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | it('falls back to the full hardcoded list when API returned no static defaults', () => { |
| 48 | const result = getVisibleDefaultEdgeFunctionSecrets(new Set()) |
| 49 | expect(result.map((secret) => secret.name)).toEqual([...staticNames, ...runtimeNames]) |
| 50 | }) |
| 51 | |
| 52 | it('shows only the static defaults present in the API response', () => { |
| 53 | const apiNames = new Set(['BRIVEN_URL', 'BRIVEN_ANON_KEY', 'MY_USER_SECRET']) |
| 54 | const result = getVisibleDefaultEdgeFunctionSecrets(apiNames) |
| 55 | |
| 56 | expect(result.map((secret) => secret.name)).toEqual([ |
| 57 | 'BRIVEN_URL', |
| 58 | 'BRIVEN_ANON_KEY', |
| 59 | ...runtimeNames, |
| 60 | ]) |
| 61 | }) |
| 62 | |
| 63 | it('does not surface user-defined secret names from the API set', () => { |
| 64 | const apiNames = new Set(['BRIVEN_URL', 'MY_USER_SECRET']) |
| 65 | const result = getVisibleDefaultEdgeFunctionSecrets(apiNames) |
| 66 | expect(result.map((secret) => secret.name)).not.toContain('MY_USER_SECRET') |
| 67 | }) |
| 68 | }) |