feat: support merging multiple subscriptions

This commit is contained in:
2026-07-19 23:58:02 +08:00
parent fb4d927220
commit 087869d84f
17 changed files with 1341 additions and 60 deletions

View File

@ -0,0 +1,191 @@
package automation
import (
"testing"
"gopkg.in/yaml.v3"
)
func TestParseConfig_FullSchema(t *testing.T) {
raw := map[string]interface{}{
"proxy-groups": []interface{}{
map[string]interface{}{
"name": "HK Nodes",
"type": "url-test",
"match": map[string]interface{}{
"subscriptions": []interface{}{"home-sub"},
"name-contains": []interface{}{"HK", "Hong Kong"},
},
"url": "http://www.gstatic.com/generate_204",
"interval": 300,
},
},
"rate-filters": []interface{}{
map[string]interface{}{
"name": "Cheap",
"pattern": `([\d.]+)x`,
"operator": "<=",
"value": 1.0,
},
},
"patches": []interface{}{
map[string]interface{}{
"path": "dns.nameserver",
"op": "append",
"value": []interface{}{"1.1.1.1"},
},
},
}
cfg, err := ParseConfig(raw)
if err != nil {
t.Fatalf("ParseConfig() error = %v", err)
}
if len(cfg.ProxyGroups) != 1 {
t.Fatalf("expected 1 proxy-group rule, got %d", len(cfg.ProxyGroups))
}
pg := cfg.ProxyGroups[0]
if pg.Name != "HK Nodes" || pg.Type != "url-test" {
t.Fatalf("unexpected proxy-group rule: %#v", pg)
}
if len(pg.Match.Subscriptions) != 1 || pg.Match.Subscriptions[0] != "home-sub" {
t.Fatalf("unexpected match.subscriptions: %#v", pg.Match.Subscriptions)
}
if len(pg.Match.NameContains) != 2 {
t.Fatalf("unexpected match.name-contains: %#v", pg.Match.NameContains)
}
// "url" and "interval" aren't named fields on ProxyGroupRule — they must
// land in Extra via the inline tag, not get silently dropped.
if pg.Extra["url"] != "http://www.gstatic.com/generate_204" {
t.Fatalf("expected url to be captured in Extra, got %#v", pg.Extra)
}
if _, ok := pg.Extra["name"]; ok {
t.Fatalf("named field 'name' leaked into Extra: %#v", pg.Extra)
}
if _, ok := pg.Extra["match"]; ok {
t.Fatalf("named field 'match' leaked into Extra: %#v", pg.Extra)
}
if len(cfg.RateFilters) != 1 || cfg.RateFilters[0].Operator != "<=" || cfg.RateFilters[0].Value != 1.0 {
t.Fatalf("unexpected rate-filters: %#v", cfg.RateFilters)
}
if len(cfg.Patches) != 1 || cfg.Patches[0].Path != "dns.nameserver" || cfg.Patches[0].Op != "append" {
t.Fatalf("unexpected patches: %#v", cfg.Patches)
}
}
func TestParseConfig_RealWorldYAMLRoundTrip(t *testing.T) {
// Simulates the actual path: core-config.yaml is loaded via
// yamlutil.Unmarshal into map[string]interface{}, and the "ssm" key's
// raw value is what gets handed to ParseConfig.
yamlSrc := []byte(`
ssm:
proxy-groups:
- name: HK Nodes
type: select
match:
name-contains: [HK]
patches:
- path: rules
op: prepend
value: ["DOMAIN,example.com,DIRECT"]
`)
var full map[string]interface{}
if err := yaml.Unmarshal(yamlSrc, &full); err != nil {
t.Fatalf("yaml.Unmarshal() error = %v", err)
}
cfg, err := ParseConfig(full["ssm"])
if err != nil {
t.Fatalf("ParseConfig() error = %v", err)
}
if len(cfg.ProxyGroups) != 1 || cfg.ProxyGroups[0].Name != "HK Nodes" {
t.Fatalf("unexpected proxy-groups: %#v", cfg.ProxyGroups)
}
if len(cfg.Patches) != 1 || cfg.Patches[0].Op != "prepend" {
t.Fatalf("unexpected patches: %#v", cfg.Patches)
}
}
func TestApply_EndToEnd(t *testing.T) {
proxies := []ProxyRef{
{OriginalName: "HK 01 | 1.0x", DisplayName: "sub-a | HK 01 | 1.0x", Subscription: "sub-a"},
{OriginalName: "HK 02 | 2.5x", DisplayName: "sub-a | HK 02 | 2.5x", Subscription: "sub-a"},
{OriginalName: "SG 01 | 0.5x", DisplayName: "sub-b | SG 01 | 0.5x", Subscription: "sub-b"},
}
cfg := &Config{
ProxyGroups: []ProxyGroupRule{
{Name: "HK Group", Type: "select", Match: MatchRule{NameContains: []string{"HK"}}},
},
RateFilters: []RateFilterRule{
{Name: "Cheap", Type: "select", Pattern: `([\d.]+)x`, Operator: "<=", Value: 1.0},
},
Patches: []PatchRule{
{Path: "rules", Op: "append", Value: []interface{}{"MATCH,DIRECT"}},
},
}
finalConfig := map[string]interface{}{
"rules": []interface{}{},
}
errs := Apply(finalConfig, cfg, proxies)
if len(errs) != 0 {
t.Fatalf("unexpected errors: %v", errs)
}
groups, ok := finalConfig["proxy-groups"].([]interface{})
if !ok || len(groups) != 2 {
t.Fatalf("expected 2 proxy-groups, got %#v", finalConfig["proxy-groups"])
}
hkGroup := groups[0].(map[string]interface{})
if hkGroup["name"] != "HK Group" {
t.Fatalf("unexpected first group: %#v", hkGroup)
}
hkProxies := hkGroup["proxies"].([]interface{})
if len(hkProxies) != 2 {
t.Fatalf("expected 2 HK proxies, got %#v", hkProxies)
}
cheapGroup := groups[1].(map[string]interface{})
if cheapGroup["name"] != "Cheap" {
t.Fatalf("unexpected second group: %#v", cheapGroup)
}
cheapProxies := cheapGroup["proxies"].([]interface{})
want := []interface{}{"sub-a | HK 01 | 1.0x", "sub-b | SG 01 | 0.5x"}
if len(cheapProxies) != 2 || cheapProxies[0] != want[0] || cheapProxies[1] != want[1] {
t.Fatalf("unexpected cheap proxies: %#v", cheapProxies)
}
if rules, _ := finalConfig["rules"].([]interface{}); len(rules) != 1 || rules[0] != "MATCH,DIRECT" {
t.Fatalf("unexpected rules after patch: %#v", finalConfig["rules"])
}
}
func TestApply_InvalidRateFilterDoesNotBlockOthers(t *testing.T) {
proxies := []ProxyRef{
{OriginalName: "HK 01", DisplayName: "sub-a | HK 01", Subscription: "sub-a"},
}
cfg := &Config{
ProxyGroups: []ProxyGroupRule{
{Name: "All", Type: "select"},
},
RateFilters: []RateFilterRule{
{Name: "Bad", Pattern: "(", Operator: "<="},
},
}
finalConfig := map[string]interface{}{}
errs := Apply(finalConfig, cfg, proxies)
if len(errs) != 1 {
t.Fatalf("expected exactly 1 error for the bad regexp, got %v", errs)
}
groups, ok := finalConfig["proxy-groups"].([]interface{})
if !ok || len(groups) != 1 {
t.Fatalf("expected the valid proxy-groups rule to still run: %#v", finalConfig["proxy-groups"])
}
}