orchestr_internal_test.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package httpapi
  2. import (
  3. "encoding/json"
  4. "testing"
  5. "github.com/gnacho/netpulse/server-go/internal/orchestr"
  6. )
  7. // TestInvertDesiredAdGuard: el inverso de enabled=true es enabled=false (y
  8. // viceversa); el resto de campos se conservan.
  9. func TestInvertDesiredAdGuard(t *testing.T) {
  10. cases := []struct {
  11. name string
  12. in bool
  13. want bool
  14. }{
  15. {"enabled_to_disabled", true, false},
  16. {"disabled_to_enabled", false, true},
  17. }
  18. for _, c := range cases {
  19. t.Run(c.name, func(t *testing.T) {
  20. in, _ := json.Marshal(orchestr.AdGuardDesired{
  21. Enabled: c.in, Port: "3000", UpstreamDNS: "1.1.1.1",
  22. })
  23. out, err := invertDesired("adguard", in)
  24. if err != nil {
  25. t.Fatalf("invertDesired err: %v", err)
  26. }
  27. var got orchestr.AdGuardDesired
  28. if err := json.Unmarshal(out, &got); err != nil {
  29. t.Fatalf("unmarshal err: %v", err)
  30. }
  31. if got.Enabled != c.want {
  32. t.Errorf("Enabled: in=%v want=%v got=%v", c.in, c.want, got.Enabled)
  33. }
  34. // El resto de campos se conservan (no se pierden al invertir).
  35. if got.Port != "3000" || got.UpstreamDNS != "1.1.1.1" {
  36. t.Errorf("campos no conservados: port=%q dns=%q", got.Port, got.UpstreamDNS)
  37. }
  38. })
  39. }
  40. }
  41. // TestInvertDesiredUnsupported: un módulo que no sabe invertirse devuelve error.
  42. func TestInvertDesiredUnsupported(t *testing.T) {
  43. _, err := invertDesired("wireguard", json.RawMessage(`{}`))
  44. if err == nil {
  45. t.Error("esperaba error para módulo no soportado")
  46. }
  47. }
  48. // TestInvertDesiredInvalidJSON: JSON malformado devuelve error (no panic).
  49. func TestInvertDesiredInvalidJSON(t *testing.T) {
  50. _, err := invertDesired("adguard", json.RawMessage(`{bad json`))
  51. if err == nil {
  52. t.Error("esperaba error para JSON inválido")
  53. }
  54. }