;; lib/mod/tests/policies.sx — Ext 17: per-domain policy registry.

(define mod-pol-count 0)
(define mod-pol-pass 0)
(define mod-pol-fail 0)
(define mod-pol-failures (list))

(define
  mod-pol-test!
  (fn
    (name got expected)
    (begin
      (set! mod-pol-count (+ mod-pol-count 1))
      (if
        (= got expected)
        (set! mod-pol-pass (+ mod-pol-pass 1))
        (begin
          (set! mod-pol-fail (+ mod-pol-fail 1))
          (append!
            mod-pol-failures
            (str name "\n    expected: " expected "\n    got:      " got)))))))

(mod/policies-reset!)

;; market is strict: spam is removed outright, not just hidden
(define
  mod-pol-market-rules
  (list
    (mod/mk-rule
      "market-spam-remove"
      :remove (list (list :classification "spam")))
    (mod/mk-rule "default-keep" :keep (list))))

(mod-pol-test!
  "unregistered domain falls back to default"
  (mod/policy-registered? "market")
  false)
(mod/register-policy! "market" mod-pol-market-rules)
(mod-pol-test!
  "domain registered after register!"
  (mod/policy-registered? "market")
  true)

(define mod-pol-spam (mod/mk-report "r1" "a" "b" "this is spam"))

;; ── same report, different domain → different action ──

(mod-pol-test!
  "market policy removes spam"
  (get (mod/decide-in "market" mod-pol-spam (list mod-pol-spam)) :action)
  "remove")
(mod-pol-test!
  "market decision uses market rule"
  (get (mod/decide-in "market" mod-pol-spam (list mod-pol-spam)) :rule)
  "market-spam-remove")
(mod-pol-test!
  "blog (unregistered) uses default → hide"
  (get (mod/decide-in "blog" mod-pol-spam (list mod-pol-spam)) :action)
  "hide")
(mod-pol-test!
  "blog decision uses default rule"
  (get (mod/decide-in "blog" mod-pol-spam (list mod-pol-spam)) :rule)
  "spam-hide")

;; ── policy-for resolution ──

(mod-pol-test!
  "policy-for market returns market rules"
  (mod/policy-for "market")
  mod-pol-market-rules)
(mod-pol-test!
  "policy-for unknown returns default"
  (mod/policy-for "events")
  mod/default-rules)
(mod-pol-test!
  "registered-domains lists market"
  (mod/registered-domains)
  (list "market"))

;; ── a second domain ──

(define
  mod-pol-events-rules
  (list (mod/mk-rule "events-keep-all" :keep (list))))

(mod/register-policy! "events" mod-pol-events-rules)
(mod-pol-test!
  "events policy keeps everything (even spam)"
  (get (mod/decide-in "events" mod-pol-spam (list mod-pol-spam)) :action)
  "keep")
(mod-pol-test!
  "two domains registered"
  (len (mod/registered-domains))
  2)
(mod-pol-test!
  "market still removes after second registration"
  (get (mod/decide-in "market" mod-pol-spam (list mod-pol-spam)) :action)
  "remove")

;; ── clean report is keep everywhere ──

(define mod-pol-clean (mod/mk-report "r2" "a" "b" "a fine post"))
(mod-pol-test!
  "clean report keep in market"
  (get (mod/decide-in "market" mod-pol-clean (list mod-pol-clean)) :action)
  "keep")
(mod-pol-test!
  "clean report keep in blog"
  (get (mod/decide-in "blog" mod-pol-clean (list mod-pol-clean)) :action)
  "keep")

(define mod-policies-tests-run! (fn () {:failures mod-pol-failures :total mod-pol-count :passed mod-pol-pass :failed mod-pol-fail}))
