// SPDX-License-Identifier: MIT package engine import ( "context" "os" "path/filepath" "strings" "testing" "prx/internal/config" "prx/internal/execx" ) func TestIsActive(t *testing.T) { active := &execx.Fake{Handler: func(_ string, _ []string, _ []byte) (execx.Result, error) { return execx.Result{Stdout: []byte("active\n")}, nil }} got, err := IsActive(context.Background(), active, "sing-box") if err != nil || !got { t.Fatalf("IsActive = %v, %v", got, err) } inactive := &execx.Fake{Handler: func(_ string, _ []string, _ []byte) (execx.Result, error) { return execx.Result{Stdout: []byte("inactive\n"), Code: 3}, &execx.ExitError{Name: "systemctl", Code: 3, Stderr: []byte("inactive")} }} got, err = IsActive(context.Background(), inactive, "sing-box") if err != nil || got { t.Fatalf("IsActive(inactive) = %v, %v", got, err) } } func TestValidateConfig(t *testing.T) { dir := t.TempDir() withTun := filepath.Join(dir, "with-tun.json") if err := os.WriteFile(withTun, []byte(`{"inbounds":[{"type":"tun"}]}`), 0o644); err != nil { t.Fatal(err) } warnings, err := ValidateConfig(withTun) if err != nil { t.Fatalf("ValidateConfig: %v", err) } if len(warnings) != 0 { t.Fatalf("unexpected warnings: %v", warnings) } noTun := filepath.Join(dir, "no-tun.json") if err := os.WriteFile(noTun, []byte(`{"inbounds":[{"type":"mixed"}]}`), 0o644); err != nil { t.Fatal(err) } warnings, err = ValidateConfig(noTun) if err != nil { t.Fatalf("ValidateConfig: %v", err) } if len(warnings) == 0 { t.Fatal("expected a warning for missing tun inbound") } } func TestValidateConfigWarnsOnAutoRouteDisabled(t *testing.T) { path := filepath.Join(t.TempDir(), "auto-route-off.json") body := `{"inbounds":[{"type":"tun"}],"route":{"auto_route":false}}` if err := os.WriteFile(path, []byte(body), 0o644); err != nil { t.Fatal(err) } warnings, err := ValidateConfig(path) if err != nil { t.Fatalf("ValidateConfig: %v", err) } found := false for _, w := range warnings { if strings.Contains(w, "auto_route") { found = true } } if !found { t.Fatalf("expected an auto_route warning, got %v", warnings) } } func TestTemplateContainsTunAndBypass(t *testing.T) { cfg := config.Default() body, err := Template(cfg) if err != nil { t.Fatalf("Template: %v", err) } s := string(body) for _, needle := range []string{`"type": "tun"`, `"auto_route": true`, "10.0.0.0/8", "192.168.0.0/16"} { if !strings.Contains(s, needle) { t.Errorf("template missing %q", needle) } } }