83 lines
2.7 KiB
Go
83 lines
2.7 KiB
Go
package corecfg
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestPopFirstProxyGroup(t *testing.T) {
|
|
data := map[string]interface{}{
|
|
"proxy-groups": []interface{}{
|
|
map[string]interface{}{"name": "Proxy", "type": "select", "proxies": []interface{}{"Auto"}},
|
|
map[string]interface{}{"name": "Auto", "type": "url-test", "proxies": []interface{}{"HK"}},
|
|
},
|
|
}
|
|
|
|
got := popFirstProxyGroup(data)
|
|
|
|
want := map[string]interface{}{"name": "Proxy", "type": "select", "proxies": []interface{}{"Auto"}}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("got %#v, want %#v", got, want)
|
|
}
|
|
|
|
remaining := data["proxy-groups"].([]interface{})
|
|
if len(remaining) != 1 || remaining[0].(map[string]interface{})["name"] != "Auto" {
|
|
t.Fatalf("expected only the Auto group to remain, got %#v", remaining)
|
|
}
|
|
}
|
|
|
|
func TestPopFirstProxyGroupEmpty(t *testing.T) {
|
|
if got := popFirstProxyGroup(map[string]interface{}{}); got != nil {
|
|
t.Fatalf("expected nil, got %#v", got)
|
|
}
|
|
}
|
|
|
|
func TestMergeProxySelection(t *testing.T) {
|
|
combined := map[string]interface{}{
|
|
"proxy-groups": []interface{}{
|
|
map[string]interface{}{"name": "sub-a | Auto", "type": "url-test", "proxies": []interface{}{"sub-a | HK"}},
|
|
},
|
|
"rules": []interface{}{
|
|
"DOMAIN,example.com,sub-a | Proxy",
|
|
"DOMAIN,other.com,sub-b | Proxy",
|
|
},
|
|
}
|
|
groups := []map[string]interface{}{
|
|
{"name": "sub-a | Proxy", "type": "select", "proxies": []interface{}{"sub-a | Auto", "DIRECT"}},
|
|
{"name": "sub-b | Proxy", "type": "select", "proxies": []interface{}{"sub-b | Auto", "DIRECT"}},
|
|
}
|
|
|
|
mergeProxySelection(combined, groups)
|
|
|
|
proxyGroups := combined["proxy-groups"].([]interface{})
|
|
if len(proxyGroups) != 2 {
|
|
t.Fatalf("expected 2 proxy-groups (merged selection + sub-a | Auto), got %#v", proxyGroups)
|
|
}
|
|
|
|
merged := proxyGroups[0].(map[string]interface{})
|
|
if merged["name"] != selectionGroupName {
|
|
t.Fatalf("expected first group to be %q, got %#v", selectionGroupName, merged["name"])
|
|
}
|
|
wantProxies := []interface{}{"sub-a | Auto", "DIRECT", "sub-b | Auto"}
|
|
if !reflect.DeepEqual(merged["proxies"], wantProxies) {
|
|
t.Fatalf("got proxies %#v, want %#v", merged["proxies"], wantProxies)
|
|
}
|
|
|
|
wantRules := []interface{}{
|
|
"DOMAIN,example.com," + selectionGroupName,
|
|
"DOMAIN,other.com," + selectionGroupName,
|
|
}
|
|
if !reflect.DeepEqual(combined["rules"], wantRules) {
|
|
t.Fatalf("got rules %#v, want %#v", combined["rules"], wantRules)
|
|
}
|
|
}
|
|
|
|
func TestMergeProxySelectionNoGroups(t *testing.T) {
|
|
combined := map[string]interface{}{"proxy-groups": []interface{}{"unchanged"}}
|
|
mergeProxySelection(combined, nil)
|
|
|
|
if !reflect.DeepEqual(combined["proxy-groups"], []interface{}{"unchanged"}) {
|
|
t.Fatalf("expected combined to be untouched, got %#v", combined)
|
|
}
|
|
}
|