|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestLoadCLIConfig_DefaultsToPrd(t *testing.T) { |
| 8 | + t.Setenv("TERO_ENV", "") |
| 9 | + t.Setenv("TERO_API_ENDPOINT", "") |
| 10 | + t.Setenv("TERO_POWERSYNC_ENDPOINT", "") |
| 11 | + t.Setenv("TERO_CHAT_ENDPOINT", "") |
| 12 | + t.Setenv("WORKOS_CLIENT_ID", "") |
| 13 | + t.Setenv("TERO_DEBUG", "") |
| 14 | + |
| 15 | + cfg := LoadCLIConfig() |
| 16 | + |
| 17 | + if cfg.Env != "prd" { |
| 18 | + t.Errorf("Env = %q, want %q", cfg.Env, "prd") |
| 19 | + } |
| 20 | + if cfg.Environment() != "prd" { |
| 21 | + t.Errorf("Environment() = %q, want %q", cfg.Environment(), "prd") |
| 22 | + } |
| 23 | + if cfg.APIEndpoint != "https://api.usetero.com" { |
| 24 | + t.Errorf("APIEndpoint = %q, want production", cfg.APIEndpoint) |
| 25 | + } |
| 26 | + if cfg.PowerSyncEndpoint != "https://powersync.usetero.com" { |
| 27 | + t.Errorf("PowerSyncEndpoint = %q, want production", cfg.PowerSyncEndpoint) |
| 28 | + } |
| 29 | + if cfg.ChatEndpoint != "https://chat.usetero.com" { |
| 30 | + t.Errorf("ChatEndpoint = %q, want production", cfg.ChatEndpoint) |
| 31 | + } |
| 32 | + if cfg.WorkOSClientID != "client_01JQCC2D06JF9ASFA6GRHMFA3N" { |
| 33 | + t.Errorf("WorkOSClientID = %q, want production client", cfg.WorkOSClientID) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestLoadCLIConfig_Local(t *testing.T) { |
| 38 | + t.Setenv("TERO_ENV", "local") |
| 39 | + t.Setenv("TERO_API_ENDPOINT", "") |
| 40 | + t.Setenv("TERO_POWERSYNC_ENDPOINT", "") |
| 41 | + t.Setenv("TERO_CHAT_ENDPOINT", "") |
| 42 | + t.Setenv("WORKOS_CLIENT_ID", "") |
| 43 | + |
| 44 | + cfg := LoadCLIConfig() |
| 45 | + |
| 46 | + if cfg.Env != "local" { |
| 47 | + t.Errorf("Env = %q, want %q", cfg.Env, "local") |
| 48 | + } |
| 49 | + if cfg.APIEndpoint != "http://localhost:8081" { |
| 50 | + t.Errorf("APIEndpoint = %q, want localhost", cfg.APIEndpoint) |
| 51 | + } |
| 52 | + if cfg.PowerSyncEndpoint != "http://localhost:8084" { |
| 53 | + t.Errorf("PowerSyncEndpoint = %q, want localhost", cfg.PowerSyncEndpoint) |
| 54 | + } |
| 55 | + if cfg.ChatEndpoint != "http://localhost:8083" { |
| 56 | + t.Errorf("ChatEndpoint = %q, want localhost", cfg.ChatEndpoint) |
| 57 | + } |
| 58 | + if cfg.WorkOSClientID != "client_01JQCC2CJMTB8AY2JRMZXFY9R1" { |
| 59 | + t.Errorf("WorkOSClientID = %q, want local/dev client", cfg.WorkOSClientID) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestLoadCLIConfig_Dev(t *testing.T) { |
| 64 | + t.Setenv("TERO_ENV", "dev") |
| 65 | + t.Setenv("TERO_API_ENDPOINT", "") |
| 66 | + t.Setenv("TERO_POWERSYNC_ENDPOINT", "") |
| 67 | + t.Setenv("TERO_CHAT_ENDPOINT", "") |
| 68 | + t.Setenv("WORKOS_CLIENT_ID", "") |
| 69 | + |
| 70 | + cfg := LoadCLIConfig() |
| 71 | + |
| 72 | + if cfg.Env != "dev" { |
| 73 | + t.Errorf("Env = %q, want %q", cfg.Env, "dev") |
| 74 | + } |
| 75 | + if cfg.APIEndpoint != "https://api.usetero.dev" { |
| 76 | + t.Errorf("APIEndpoint = %q, want dev", cfg.APIEndpoint) |
| 77 | + } |
| 78 | + if cfg.PowerSyncEndpoint != "https://powersync.usetero.dev" { |
| 79 | + t.Errorf("PowerSyncEndpoint = %q, want dev", cfg.PowerSyncEndpoint) |
| 80 | + } |
| 81 | + if cfg.ChatEndpoint != "https://chat.usetero.dev" { |
| 82 | + t.Errorf("ChatEndpoint = %q, want dev", cfg.ChatEndpoint) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestLoadCLIConfig_Prd(t *testing.T) { |
| 87 | + t.Setenv("TERO_ENV", "prd") |
| 88 | + t.Setenv("TERO_API_ENDPOINT", "") |
| 89 | + t.Setenv("TERO_POWERSYNC_ENDPOINT", "") |
| 90 | + t.Setenv("TERO_CHAT_ENDPOINT", "") |
| 91 | + t.Setenv("WORKOS_CLIENT_ID", "") |
| 92 | + |
| 93 | + cfg := LoadCLIConfig() |
| 94 | + |
| 95 | + if cfg.Env != "prd" { |
| 96 | + t.Errorf("Env = %q, want %q", cfg.Env, "prd") |
| 97 | + } |
| 98 | + if cfg.APIEndpoint != "https://api.usetero.com" { |
| 99 | + t.Errorf("APIEndpoint = %q, want production", cfg.APIEndpoint) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func TestLoadCLIConfig_UnknownEnvFallsToPrd(t *testing.T) { |
| 104 | + t.Setenv("TERO_ENV", "staging") |
| 105 | + t.Setenv("TERO_API_ENDPOINT", "") |
| 106 | + t.Setenv("TERO_POWERSYNC_ENDPOINT", "") |
| 107 | + t.Setenv("TERO_CHAT_ENDPOINT", "") |
| 108 | + t.Setenv("WORKOS_CLIENT_ID", "") |
| 109 | + |
| 110 | + cfg := LoadCLIConfig() |
| 111 | + |
| 112 | + if cfg.Env != "staging" { |
| 113 | + t.Errorf("Env = %q, want %q", cfg.Env, "staging") |
| 114 | + } |
| 115 | + if cfg.Environment() != "staging" { |
| 116 | + t.Errorf("Environment() = %q, want %q", cfg.Environment(), "staging") |
| 117 | + } |
| 118 | + if cfg.APIEndpoint != "https://api.usetero.com" { |
| 119 | + t.Errorf("APIEndpoint = %q, want production fallback", cfg.APIEndpoint) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestLoadCLIConfig_EnvVarOverridesDefaults(t *testing.T) { |
| 124 | + t.Setenv("TERO_ENV", "dev") |
| 125 | + t.Setenv("TERO_API_ENDPOINT", "http://custom:9999") |
| 126 | + t.Setenv("TERO_POWERSYNC_ENDPOINT", "") |
| 127 | + t.Setenv("TERO_CHAT_ENDPOINT", "") |
| 128 | + t.Setenv("WORKOS_CLIENT_ID", "") |
| 129 | + |
| 130 | + cfg := LoadCLIConfig() |
| 131 | + |
| 132 | + if cfg.APIEndpoint != "http://custom:9999" { |
| 133 | + t.Errorf("APIEndpoint = %q, want custom override", cfg.APIEndpoint) |
| 134 | + } |
| 135 | + // Other fields still use dev defaults |
| 136 | + if cfg.PowerSyncEndpoint != "https://powersync.usetero.dev" { |
| 137 | + t.Errorf("PowerSyncEndpoint = %q, want dev default", cfg.PowerSyncEndpoint) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestLoadCLIConfig_Debug(t *testing.T) { |
| 142 | + t.Setenv("TERO_ENV", "prd") |
| 143 | + t.Setenv("TERO_API_ENDPOINT", "") |
| 144 | + t.Setenv("TERO_POWERSYNC_ENDPOINT", "") |
| 145 | + t.Setenv("TERO_CHAT_ENDPOINT", "") |
| 146 | + t.Setenv("WORKOS_CLIENT_ID", "") |
| 147 | + |
| 148 | + t.Setenv("TERO_DEBUG", "true") |
| 149 | + cfg := LoadCLIConfig() |
| 150 | + if !cfg.Debug { |
| 151 | + t.Error("Debug should be true when TERO_DEBUG=true") |
| 152 | + } |
| 153 | + |
| 154 | + t.Setenv("TERO_DEBUG", "1") |
| 155 | + cfg = LoadCLIConfig() |
| 156 | + if !cfg.Debug { |
| 157 | + t.Error("Debug should be true when TERO_DEBUG=1") |
| 158 | + } |
| 159 | + |
| 160 | + t.Setenv("TERO_DEBUG", "") |
| 161 | + cfg = LoadCLIConfig() |
| 162 | + if cfg.Debug { |
| 163 | + t.Error("Debug should be false when TERO_DEBUG is empty") |
| 164 | + } |
| 165 | +} |
0 commit comments