; Phase 1 — normalize, stream ops, api. Uses the feed-test harness
; (feed-test name got expected) provided by conformance.sh.

; ---------- normalize ----------

(feed-test
  "normalize default actor"
  (feed/actor (feed/normalize {}))
  "")
(feed-test
  "normalize default verb"
  (feed/verb (feed/normalize {}))
  "post")
(feed-test
  "normalize default at"
  (feed/at (feed/normalize {}))
  0)
(feed-test
  "normalize default object"
  (feed/object (feed/normalize {}))
  nil)
(feed-test
  "normalize default tags"
  (feed/tags (feed/normalize {}))
  (list))
(feed-test
  "normalize keeps actor"
  (feed/actor (feed/normalize {:actor "alice"}))
  "alice")
(feed-test
  "normalize keeps verb"
  (feed/verb (feed/normalize {:verb "like"}))
  "like")
(feed-test
  "normalize scalar tag -> list"
  (feed/tags (feed/normalize {:tags "x"}))
  (list "x"))
(feed-test
  "normalize list tags kept"
  (feed/tags (feed/normalize {:tags (list "a" "b")}))
  (list "a" "b"))
(feed-test
  "activity constructor at"
  (feed/at (feed/activity "a" "post" "o" 5 (list)))
  5)
(feed-test
  "activity? on activity"
  (feed/activity? (feed/normalize {:actor "a"}))
  true)
(feed-test "activity? on number" (feed/activity? 5) false)
(feed-test "activity? on bare dict" (feed/activity? {:foo 1}) false)

; ---------- stream ----------

(define
  S
  (feed/stream
    (list
      (feed/activity "alice" "post" "p1" 30 (list))
      (feed/activity "bob" "like" "p1" 10 (list))
      (feed/activity "alice" "post" "p2" 20 (list)))))

(feed-test "stream count" (feed/count S) 3)
(feed-test "stream items len" (len (feed/items S)) 3)
(feed-test
  "sort-by-at actors asc"
  (map feed/actor (feed/items (feed/sort-by-at S)))
  (list "bob" "alice" "alice"))
(feed-test
  "recent newest first"
  (map feed/at (feed/items (feed/recent S)))
  (list 30 20 10))
(feed-test
  "take 2 of recent"
  (feed/count (feed/take (feed/recent S) 2))
  2)
(feed-test
  "take clamps past end"
  (feed/count (feed/take S 10))
  3)
(feed-test
  "by-actor alice count"
  (feed/count (feed/by-actor S "alice"))
  2)
(feed-test
  "by-verb like actor"
  (map feed/actor (feed/items (feed/by-verb S "like")))
  (list "bob"))
(feed-test
  "by-object p1 count"
  (feed/count (feed/by-object S "p1"))
  2)
(feed-test
  "since 20 count"
  (feed/count (feed/since S 20))
  2)
(feed-test
  "reverse ats"
  (map feed/at (feed/items (feed/reverse S)))
  (list 20 10 30))
(feed-test "empty? on empty" (feed/empty? feed/empty) true)
(feed-test
  "empty? on filtered-out"
  (feed/empty? (feed/by-actor S "zzz"))
  true)

; ---------- api ----------

(feed/reset!)
(feed/post {:actor "x" :at 1 :verb "post"})
(feed/post {:actor "y" :at 2 :verb "like"})
(feed-test "api size after posts" (feed/size) 2)
(feed-test "api all count" (feed/count (feed/all)) 2)
(feed-test
  "post returns normalized verb"
  (feed/verb (feed/post {:actor "z"}))
  "post")
(feed-test "api size after third post" (feed/size) 3)
