1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
package proxy
import (
"encoding/json"
"os"
"testing"
)
func TestLoadConfigsMissingFile(t *testing.T) {
root, err := LoadConfigs("/nonexistent/path/config.json")
if err != nil {
t.Fatalf("expected no error for missing file, got %v", err)
}
if len(root.Proxies) == 0 {
t.Error("expected default proxy when file is absent")
}
}
func TestLoadConfigsValid(t *testing.T) {
cfg := RootConfig{
APIPort: 9090,
Proxies: []Config{
{
Type: "claude-code",
Port: 9880,
Upstream: "https://api.anthropic.com",
PrivacyGuard: GuardConfig{
Detectors: []string{"EMAIL", "PHONE"},
Whitelist: []string{"claude"},
},
},
},
}
data, _ := json.Marshal(cfg)
f, err := os.CreateTemp("", "config-*.json")
if err != nil {
t.Fatal(err)
}
defer os.Remove(f.Name())
f.Write(data)
f.Close()
got, err := LoadConfigs(f.Name())
if err != nil {
t.Fatalf("LoadConfigs error: %v", err)
}
if got.APIPort != 9090 {
t.Errorf("APIPort = %d, want 9090", got.APIPort)
}
if len(got.Proxies) != 1 {
t.Fatalf("len(Proxies) = %d, want 1", len(got.Proxies))
}
if got.Proxies[0].Port != 9880 {
t.Errorf("Port = %d, want 9880", got.Proxies[0].Port)
}
if len(got.Proxies[0].PrivacyGuard.Detectors) != 2 {
t.Errorf("Detectors = %v, want [EMAIL PHONE]", got.Proxies[0].PrivacyGuard.Detectors)
}
}
func TestLoadConfigsEmptyProxiesGetsDefault(t *testing.T) {
raw := `{"api_port": 8080, "proxies": []}`
f, _ := os.CreateTemp("", "config-*.json")
defer os.Remove(f.Name())
f.WriteString(raw)
f.Close()
got, err := LoadConfigs(f.Name())
if err != nil {
t.Fatalf("LoadConfigs error: %v", err)
}
if len(got.Proxies) != 1 {
t.Errorf("expected default proxy injected, got %d proxies", len(got.Proxies))
}
}
func TestLoadConfigsInvalidJSON(t *testing.T) {
f, _ := os.CreateTemp("", "config-*.json")
defer os.Remove(f.Name())
f.WriteString(`{not valid json`)
f.Close()
_, err := LoadConfigs(f.Name())
if err == nil {
t.Error("expected error for invalid JSON")
}
}
func TestLoadConfigsLegacyArrayFormat(t *testing.T) {
raw := `[
{"type":"claude-code","port":9880,"upstream":"https://api.anthropic.com","privacy_guard":{"detectors":["EMAIL"],"whitelist":[]}}
]`
f, _ := os.CreateTemp("", "config-legacy-*.json")
defer os.Remove(f.Name())
_, _ = f.WriteString(raw)
_ = f.Close()
got, err := LoadConfigs(f.Name())
if err != nil {
t.Fatalf("LoadConfigs legacy format error: %v", err)
}
if len(got.Proxies) != 1 {
t.Fatalf("expected 1 proxy from legacy config, got %d", len(got.Proxies))
}
if got.Proxies[0].Port != 9880 {
t.Fatalf("legacy proxy port = %d, want 9880", got.Proxies[0].Port)
}
}
|