;; lib/mod/tests/activity.sx — Ext 16: ActivityPub-shaped decision export.

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

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

;; ── action → AP verb ──

(mod-ap-test! "remove → Delete" (mod/action->verb "remove") "Delete")
(mod-ap-test! "ban → Block" (mod/action->verb "ban") "Block")
(mod-ap-test! "hide → Flag" (mod/action->verb "hide") "Flag")
(mod-ap-test! "escalate → Flag" (mod/action->verb "escalate") "Flag")
(mod-ap-test! "keep → nil (no activity)" (mod/action->verb "keep") nil)

;; ── single decision → activity ──

(define mod-ap-spam (mod/mk-report "r1" "a" "bob" "this is spam"))
(define
  mod-ap-dec
  (mod/decide-report mod-ap-spam (list mod-ap-spam) mod/default-rules))
(define mod-ap-act (mod/decision->activity mod-ap-dec "instance.example"))

(mod-ap-test! "activity type is Flag (hide)" (get mod-ap-act :type) "Flag")
(mod-ap-test! "activity object is report id" (get mod-ap-act :object) "r1")
(mod-ap-test!
  "activity actor preserved"
  (get mod-ap-act :actor)
  "instance.example")
(mod-ap-test!
  "activity preserves precise action"
  (get mod-ap-act :action)
  "hide")
(mod-ap-test! "activity carries rule" (get mod-ap-act :rule) "spam-hide")
(mod-ap-test!
  "activity summary"
  (get mod-ap-act :summary)
  "moderation/hide via spam-hide")

;; ── keep produces no activity ──

(define mod-ap-clean (mod/mk-report "r2" "a" "b" "a fine post"))
(define
  mod-ap-keep
  (mod/decide-report mod-ap-clean (list mod-ap-clean) mod/default-rules))
(mod-ap-test!
  "keep decision → nil activity"
  (mod/decision->activity mod-ap-keep "x")
  nil)

;; ── abuse → Delete ──

(define mod-ap-abuse (mod/mk-report "r3" "a" "b" "harassment here"))
(define
  mod-ap-abuse-dec
  (mod/decide-report mod-ap-abuse (list mod-ap-abuse) mod/default-rules))
(mod-ap-test!
  "abuse decision → Delete activity"
  (get (mod/decision->activity mod-ap-abuse-dec "x") :type)
  "Delete")

;; ── batch export drops keeps ──

(define mod-ap-decisions (list mod-ap-dec mod-ap-keep mod-ap-abuse-dec))
(define mod-ap-acts (mod/decisions->activities mod-ap-decisions "inst"))
(mod-ap-test! "batch export drops the keep" (len mod-ap-acts) 2)
(mod-ap-test!
  "batch export first is the Flag"
  (get (first mod-ap-acts) :type)
  "Flag")
(mod-ap-test!
  "batch export second is the Delete"
  (get (nth mod-ap-acts 1) :type)
  "Delete")
(mod-ap-test!
  "empty decisions → no activities"
  (mod/decisions->activities (list) "inst")
  (list))

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