package main import ( "os" "path/filepath" "testing" "fiatjaf.com/nostr" ) func TestDevKeyPersistsAcrossCalls(t *testing.T) { t.Setenv("RELAY_SECRET_KEY", "") t.Setenv("RELAY_ENV", "") dir := t.TempDir() first := loadOrGenerateKey(dir) // The key file should now exist and a second call must return the same key. if _, err := os.Stat(filepath.Join(dir, "relay.key")); err != nil { t.Fatalf("dev key not persisted: %v", err) } second := loadOrGenerateKey(dir) if first != second { t.Fatal("dev identity should be stable across restarts") } } func TestExplicitKeyIsUsed(t *testing.T) { sk := nostr.Generate() t.Setenv("RELAY_SECRET_KEY", sk.Hex()) t.Setenv("RELAY_ENV", "production") // explicit key wins even in production got := loadOrGenerateKey(t.TempDir()) if got != sk { t.Fatal("explicit RELAY_SECRET_KEY should be used verbatim") } } // Note: the production-without-key path calls log.Fatal (os.Exit), which can't // be exercised in-process without a subprocess harness; its behavior is simple // and covered by manual verification + the isProduction() guard below. func TestIsProduction(t *testing.T) { t.Setenv("RELAY_ENV", "production") if !isProduction() { t.Fatal("RELAY_ENV=production should be detected") } t.Setenv("RELAY_ENV", "") if isProduction() { t.Fatal("unset RELAY_ENV should not be production") } }