feat(snapshot): config-loader mit env-validierung
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
b6366ea1fe
commit
45df54f2b3
|
|
@ -0,0 +1,15 @@
|
|||
export interface Config {
|
||||
authorPubkeyHex: string
|
||||
bootstrapRelay: string
|
||||
}
|
||||
|
||||
export function loadConfig(): Config {
|
||||
const authorPubkeyHex = Deno.env.get('AUTHOR_PUBKEY_HEX')
|
||||
const bootstrapRelay = Deno.env.get('BOOTSTRAP_RELAY')
|
||||
if (!authorPubkeyHex) throw new Error('AUTHOR_PUBKEY_HEX fehlt in env')
|
||||
if (!/^[0-9a-f]{64}$/i.test(authorPubkeyHex)) {
|
||||
throw new Error('AUTHOR_PUBKEY_HEX muss 64 hex chars sein')
|
||||
}
|
||||
if (!bootstrapRelay) throw new Error('BOOTSTRAP_RELAY fehlt in env')
|
||||
return { authorPubkeyHex, bootstrapRelay }
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
import { assertEquals, assertThrows } from '@std/assert'
|
||||
import { loadConfig } from '../src/core/config.ts'
|
||||
|
||||
Deno.test('loadConfig liest pubkey + bootstrap relay', () => {
|
||||
Deno.env.set('AUTHOR_PUBKEY_HEX', '4fa5d1c413e2b45e10d40bf3562ab701a5331206e359c90baae0e99bfd6c6e41')
|
||||
Deno.env.set('BOOTSTRAP_RELAY', 'wss://relay.primal.net')
|
||||
const cfg = loadConfig()
|
||||
assertEquals(cfg.authorPubkeyHex, '4fa5d1c413e2b45e10d40bf3562ab701a5331206e359c90baae0e99bfd6c6e41')
|
||||
assertEquals(cfg.bootstrapRelay, 'wss://relay.primal.net')
|
||||
})
|
||||
|
||||
Deno.test('loadConfig wirft bei fehlendem AUTHOR_PUBKEY_HEX', () => {
|
||||
Deno.env.delete('AUTHOR_PUBKEY_HEX')
|
||||
Deno.env.set('BOOTSTRAP_RELAY', 'wss://relay.primal.net')
|
||||
assertThrows(() => loadConfig(), Error, 'AUTHOR_PUBKEY_HEX')
|
||||
})
|
||||
|
||||
Deno.test('loadConfig wirft bei ungueltigem hex', () => {
|
||||
Deno.env.set('AUTHOR_PUBKEY_HEX', 'nicht-hex')
|
||||
Deno.env.set('BOOTSTRAP_RELAY', 'wss://relay.primal.net')
|
||||
assertThrows(() => loadConfig(), Error, '64 hex')
|
||||
})
|
||||
Loading…
Reference in New Issue