feat: support merging multiple subscriptions
This commit is contained in:
159
internal/automation/prefix_test.go
Normal file
159
internal/automation/prefix_test.go
Normal file
@ -0,0 +1,159 @@
|
||||
package automation
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPrefixProxyNames_RenamesProxies(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"proxies": []interface{}{
|
||||
map[string]interface{}{"name": "HK 01", "type": "ss"},
|
||||
map[string]interface{}{"name": "US 01", "type": "ss"},
|
||||
},
|
||||
}
|
||||
|
||||
refs := PrefixProxyNames("home-sub", data)
|
||||
|
||||
if len(refs) != 2 {
|
||||
t.Fatalf("expected 2 refs, got %d", len(refs))
|
||||
}
|
||||
if refs[0].OriginalName != "HK 01" || refs[0].DisplayName != "home-sub | HK 01" || refs[0].Subscription != "home-sub" {
|
||||
t.Fatalf("unexpected ref[0]: %#v", refs[0])
|
||||
}
|
||||
|
||||
proxies := data["proxies"].([]interface{})
|
||||
if proxies[0].(map[string]interface{})["name"] != "home-sub | HK 01" {
|
||||
t.Fatalf("proxy name not rewritten in place: %#v", proxies[0])
|
||||
}
|
||||
if proxies[1].(map[string]interface{})["name"] != "home-sub | US 01" {
|
||||
t.Fatalf("proxy name not rewritten in place: %#v", proxies[1])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixProxyNames_RenamesGroupsAndRewritesReferences(t *testing.T) {
|
||||
// A subscription that bundles its own proxy-groups, one of which
|
||||
// references a proxy by its original name and another group by its
|
||||
// original name (a common real-world pattern: a "Auto" url-test group
|
||||
// feeding into a top-level "Proxy" select group).
|
||||
data := map[string]interface{}{
|
||||
"proxies": []interface{}{
|
||||
map[string]interface{}{"name": "HK 01", "type": "ss"},
|
||||
map[string]interface{}{"name": "US 01", "type": "ss"},
|
||||
},
|
||||
"proxy-groups": []interface{}{
|
||||
map[string]interface{}{
|
||||
"name": "Auto",
|
||||
"type": "url-test",
|
||||
"proxies": []interface{}{"HK 01", "US 01"},
|
||||
},
|
||||
map[string]interface{}{
|
||||
"name": "Proxy",
|
||||
"type": "select",
|
||||
"proxies": []interface{}{"Auto", "HK 01", "DIRECT"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
refs := PrefixProxyNames("home-sub", data)
|
||||
if len(refs) != 2 {
|
||||
t.Fatalf("expected 2 proxy refs, got %d", len(refs))
|
||||
}
|
||||
|
||||
groups := data["proxy-groups"].([]interface{})
|
||||
auto := groups[0].(map[string]interface{})
|
||||
proxy := groups[1].(map[string]interface{})
|
||||
|
||||
if auto["name"] != "home-sub | Auto" {
|
||||
t.Fatalf("group name not prefixed: %#v", auto["name"])
|
||||
}
|
||||
if proxy["name"] != "home-sub | Proxy" {
|
||||
t.Fatalf("group name not prefixed: %#v", proxy["name"])
|
||||
}
|
||||
|
||||
wantAutoProxies := []interface{}{"home-sub | HK 01", "home-sub | US 01"}
|
||||
if !reflect.DeepEqual(auto["proxies"], wantAutoProxies) {
|
||||
t.Fatalf("Auto group's proxy references not rewritten: %#v", auto["proxies"])
|
||||
}
|
||||
|
||||
// "Proxy" group references the "Auto" group (by its original name) and
|
||||
// "HK 01" (a proxy, by its original name) — both must be rewritten to
|
||||
// their new display names, while "DIRECT" (a builtin policy, not
|
||||
// anything we renamed) must be left untouched.
|
||||
wantProxyProxies := []interface{}{"home-sub | Auto", "home-sub | HK 01", "DIRECT"}
|
||||
if !reflect.DeepEqual(proxy["proxies"], wantProxyProxies) {
|
||||
t.Fatalf("Proxy group's references not rewritten: %#v", proxy["proxies"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixProxyNames_NoProxyGroups(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"proxies": []interface{}{
|
||||
map[string]interface{}{"name": "HK 01", "type": "ss"},
|
||||
},
|
||||
}
|
||||
refs := PrefixProxyNames("home-sub", data)
|
||||
if len(refs) != 1 {
|
||||
t.Fatalf("expected 1 ref, got %d", len(refs))
|
||||
}
|
||||
if _, ok := data["proxy-groups"]; ok {
|
||||
t.Fatalf("proxy-groups should not have been created out of thin air")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixProxyNames_RewritesRuleTargets(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"proxies": []interface{}{
|
||||
map[string]interface{}{"name": "HK 01", "type": "ss"},
|
||||
},
|
||||
"proxy-groups": []interface{}{
|
||||
map[string]interface{}{
|
||||
"name": "Auto",
|
||||
"type": "url-test",
|
||||
"proxies": []interface{}{"HK 01"},
|
||||
},
|
||||
},
|
||||
"rules": []interface{}{
|
||||
"DOMAIN-SUFFIX,google.com,Auto",
|
||||
"GEOIP,CN,DIRECT",
|
||||
"IP-CIDR,127.0.0.0/8,HK 01,no-resolve",
|
||||
"MATCH,Auto",
|
||||
"not,a,valid,rule,with,no,special,meaning,but,still,has,commas,Auto",
|
||||
"nocomma",
|
||||
},
|
||||
}
|
||||
|
||||
PrefixProxyNames("home-sub", data)
|
||||
|
||||
rules := data["rules"].([]interface{})
|
||||
want := []interface{}{
|
||||
"DOMAIN-SUFFIX,google.com,home-sub | Auto",
|
||||
"GEOIP,CN,DIRECT",
|
||||
"IP-CIDR,127.0.0.0/8,home-sub | HK 01,no-resolve",
|
||||
"MATCH,home-sub | Auto",
|
||||
"not,a,valid,rule,with,no,special,meaning,but,still,has,commas,home-sub | Auto",
|
||||
"nocomma",
|
||||
}
|
||||
if !reflect.DeepEqual(rules, want) {
|
||||
t.Fatalf("got %#v, want %#v", rules, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrefixProxyNames_MalformedEntriesAreSkipped(t *testing.T) {
|
||||
data := map[string]interface{}{
|
||||
"proxies": []interface{}{
|
||||
"not a map",
|
||||
map[string]interface{}{"type": "ss"}, // missing "name"
|
||||
map[string]interface{}{"name": "HK 01", "type": "ss"},
|
||||
},
|
||||
"proxy-groups": []interface{}{
|
||||
"also not a map",
|
||||
map[string]interface{}{"type": "select"}, // missing "name"
|
||||
},
|
||||
}
|
||||
|
||||
refs := PrefixProxyNames("home-sub", data)
|
||||
if len(refs) != 1 || refs[0].OriginalName != "HK 01" {
|
||||
t.Fatalf("expected exactly 1 ref for the well-formed proxy, got %#v", refs)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user