refactor(routing): 暴露 system_locked_domains + 服务端 follow-up 清理(FK/Validate/dedup/exclude单源/共用Store/Builtin保留)

This commit is contained in:
wangjia
2026-07-29 06:38:52 +08:00
parent 52912268d0
commit 3159dd75c1
11 changed files with 285 additions and 36 deletions
+37 -2
View File
@@ -1,13 +1,32 @@
package routing
import "testing"
import (
"encoding/json"
"testing"
)
// TestValidateEmptyErrorsSerializeAsArray guards the JSON shape API clients
// depend on: a valid profile's Validate() must marshal to `[]`, not `null`.
func TestValidateEmptyErrorsSerializeAsArray(t *testing.T) {
raw, err := json.Marshal(Default().Validate())
if err != nil {
t.Fatal(err)
}
if string(raw) != "[]" {
t.Fatalf("want `[]`, got %s", raw)
}
}
func TestValidate(t *testing.T) {
ok := Default()
ok.Rules = []Rule{{Type: "domain_suffix", Value: "example.com", Action: "direct", Enabled: true}}
if e := ok.Validate(); len(e) != 0 {
e := ok.Validate()
if len(e) != 0 {
t.Fatalf("valid profile got errors %v", e)
}
if e == nil {
t.Fatal("Validate must return a non-nil empty slice (serializes to [] not null)")
}
bad := Default()
bad.Mode = "weird" // 非法 mode
@@ -48,3 +67,19 @@ func TestNormalizeDedupAndTrim(t *testing.T) {
t.Fatalf("want first-occurrence order preserved, got %v", p.Rules)
}
}
// TestNormalizeDedupNoSeparatorCollision guards against the historical
// string-concatenation dedup key ("type|value|action"): two distinct rules
// whose Value contains "|" could concatenate to the same string even though
// (type, value, action) differ. The struct-keyed dedup must tell them apart.
func TestNormalizeDedupNoSeparatorCollision(t *testing.T) {
p := Default()
p.Rules = []Rule{
{Type: "domain", Value: "a|b", Action: "proxy", Enabled: true},
{Type: "domain", Value: "a", Action: "b|proxy", Enabled: true},
}
p.Normalize()
if len(p.Rules) != 2 {
t.Fatalf("want 2 distinct rules preserved (no false collision), got %d: %v", len(p.Rules), p.Rules)
}
}