Files
3x-rule-sync/main_test.go
T

72 lines
2.6 KiB
Go

package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestParseTemplateUnwrapsPanelResponse(t *testing.T) {
template, err := parseTemplate(json.RawMessage(`"{\"xraySetting\":{\"outbounds\":[{\"tag\":\"direct\"}]},\"subscriptionOutboundTags\":[\"sub\"]}"`))
if err != nil || template.config["outbounds"] == nil || len(template.subscriptionOutboundTags) != 1 {
t.Fatalf("template=%#v err=%v", template, err)
}
}
func TestUpdateConfigAcceptsEmptySuccessResponse(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/panel/api/xray/update" {
t.Fatalf("path: %s", r.URL.Path)
}
w.WriteHeader(http.StatusOK)
}))
defer server.Close()
client := panelClient{baseURL: server.URL, token: "test", http: server.Client()}
if err := client.updateConfig(t.Context(), map[string]any{}, ""); err != nil {
t.Fatal(err)
}
}
func TestReconcilePreservesManualObjects(t *testing.T) {
config := map[string]any{
"outbounds": []any{map[string]any{"tag": "proxy-a"}, map[string]any{"tag": "proxy-b"}},
"observatory": map[string]any{},
"routing": map[string]any{
"rules": []any{
map[string]any{"ruleTag": "manual", "outboundTag": "direct"},
map[string]any{"ruleTag": "3x-rule-sync:removed", "outboundTag": "proxy-a"},
},
"balancers": []any{
map[string]any{"tag": "manual-balancer"},
map[string]any{"tag": "3x-rule-sync:balancer:removed"},
},
},
}
updated, err := reconcile(config, []Rule{{Name: "test", Match: map[string]any{"domain": []any{"example.com"}}, Outbound: Outbound{TagRegex: "^proxy-"}}}, "last")
if err != nil {
t.Fatal(err)
}
routing := updated["routing"].(map[string]any)
rules, _ := objects(routing["rules"])
if len(rules) != 2 || rules[0]["ruleTag"] != "manual" || rules[1]["balancerTag"] != "3x-rule-sync:balancer:test" {
t.Fatalf("unexpected rules: %#v", rules)
}
balancers, _ := objects(routing["balancers"])
if len(balancers) != 2 || balancers[0]["tag"] != "manual-balancer" {
t.Fatalf("manual balancer lost: %#v", balancers)
}
}
func TestDisabledRuleWritesEnabledFalse(t *testing.T) {
config := map[string]any{"outbounds": []any{map[string]any{"tag": "proxy-a"}}, "routing": map[string]any{"rules": []any{map[string]any{"ruleTag": "3x-rule-sync:test", "outboundTag": "proxy-a"}}}}
updated, err := reconcile(config, []Rule{{Name: "test", Disabled: true, Outbound: Outbound{TagRegex: "proxy-a"}}}, "last")
if err != nil {
t.Fatal(err)
}
rules, _ := objects(updated["routing"].(map[string]any)["rules"])
if len(rules) != 1 || rules[0]["enabled"] != false {
t.Fatalf("disabled rule: %#v", rules)
}
}