110 lines
3.6 KiB
Go
110 lines
3.6 KiB
Go
package corecfg
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"gitea.epss.net.cn/klesh/ss/internal/automation"
|
|
)
|
|
|
|
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 TestRewritePoppedGroupRef(t *testing.T) {
|
|
groupRefs := []automation.ProxyRef{
|
|
{OriginalName: "Auto", DisplayName: "sub-a | Auto", Subscription: "sub-a"},
|
|
{OriginalName: "AnotherGroup", DisplayName: "sub-a | AnotherGroup", Subscription: "sub-a"},
|
|
}
|
|
|
|
got := rewritePoppedGroupRef(groupRefs)
|
|
|
|
if len(got) != 2 {
|
|
t.Fatalf("expected 2 refs, got %#v", got)
|
|
}
|
|
if got[0].DisplayName != selectionGroupName || got[0].OriginalName != "Auto" {
|
|
t.Fatalf("expected the popped group's DisplayName rewritten to %q, got %#v", selectionGroupName, got[0])
|
|
}
|
|
if got[1].DisplayName != "sub-a | AnotherGroup" {
|
|
t.Fatalf("expected the non-popped group left untouched, got %#v", got[1])
|
|
}
|
|
}
|
|
|
|
func TestRewritePoppedGroupRefEmpty(t *testing.T) {
|
|
if got := rewritePoppedGroupRef(nil); len(got) != 0 {
|
|
t.Fatalf("expected no-op on empty input, got %#v", got)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|