package agent import ( "testing" "github.com/selfheal-lab/selfheal-lab-apps/ai/internal/jev" ) func f(v float64) *float64 { return &v } func TestEnforceRules(t *testing.T) { a := &Agent{jevEnforce: true} // rule A: best_action disagrees with high confidence -> escalate res := &Result{} res.ProposedAction, res.Target = "net.route_change", "" rev := &ShadowRecord{Gate: "review", Answers: map[string]any{"best_action": jev.Answer{Type: "choice", Choice: "escalate", Confidence: f(0.95)}}} a.Enforce(res, rev) if res.ProposedAction != "escalate" || res.Jev["enforced"].(map[string]any)["rule"] != "best_action_disagrees" { t.Fatalf("rule A: %+v %v", res.Diagnosis, res.Jev) } // rule B: target invalid -> escalate res = &Result{} res.ProposedAction, res.Target = "k8s.rollout_restart", "" rev = &ShadowRecord{Gate: "review", Answers: map[string]any{"best_action": jev.Answer{Type: "choice", Choice: "k8s.rollout_restart", Confidence: f(0.9)}, "target_valid": jev.Answer{Type: "noul", Noul: f(0.2)}}} a.Enforce(res, rev) if res.ProposedAction != "escalate" || res.Jev["enforced"].(map[string]any)["rule"] != "target_invalid" { t.Fatalf("rule B: %+v %v", res.Diagnosis, res.Jev) } // agreement with valid target -> unchanged; none/escalate -> untouched; shadow mode -> untouched res = &Result{} res.ProposedAction, res.Target = "k8s.rollout_restart", "api" rev = &ShadowRecord{Gate: "review", Answers: map[string]any{"best_action": jev.Answer{Choice: "k8s.rollout_restart", Confidence: f(0.99)}, "target_valid": jev.Answer{Noul: f(0.9)}}} a.Enforce(res, rev) if res.ProposedAction != "k8s.rollout_restart" || res.Jev != nil { t.Fatalf("agreement: %+v", res.Diagnosis) } res = &Result{} res.ProposedAction = "none" a.Enforce(res, &ShadowRecord{Answers: map[string]any{"best_action": jev.Answer{Choice: "escalate", Confidence: f(0.99)}}}) if res.ProposedAction != "none" { t.Fatalf("none must not change") } s := &Agent{jevEnforce: false} res = &Result{} res.ProposedAction = "k8s.scale" s.Enforce(res, &ShadowRecord{Answers: map[string]any{"best_action": jev.Answer{Choice: "escalate", Confidence: f(0.99)}}}) if res.ProposedAction != "k8s.scale" { t.Fatalf("shadow must not change") } }