; Follow-up — pagination (offset + cursor). (feed-test name got expected)

; ---------- offset / limit ----------

(define
  O
  (feed/stream
    (list
      (feed/activity "u" "post" "o1" 1 (list))
      (feed/activity "u" "post" "o2" 2 (list))
      (feed/activity "u" "post" "o3" 3 (list))
      (feed/activity "u" "post" "o4" 4 (list))
      (feed/activity "u" "post" "o5" 5 (list)))))

(feed-test
  "page 1"
  (map
    (fn (a) (get a :object))
    (feed/items (feed/page O 0 2)))
  (list "o1" "o2"))
(feed-test
  "page 2"
  (map
    (fn (a) (get a :object))
    (feed/items (feed/page O 2 2)))
  (list "o3" "o4"))
(feed-test
  "page 3 (partial)"
  (map
    (fn (a) (get a :object))
    (feed/items (feed/page O 4 2)))
  (list "o5"))
(feed-test
  "page past end empty"
  (feed/count (feed/page O 10 2))
  0)
(feed-test "page-count 5/2 = 3" (feed/page-count O 2) 3)
(feed-test "page-count 5/5 = 1" (feed/page-count O 5) 1)

; ---------- cursor (recency) ----------

(define
  R
  (feed/stream
    (list
      (feed/activity "u" "post" "a" 50 (list))
      (feed/activity "u" "post" "b" 40 (list))
      (feed/activity "u" "post" "c" 30 (list))
      (feed/activity "u" "post" "d" 20 (list))
      (feed/activity "u" "post" "e" 10 (list)))))

(define p1 (feed/page-before R 100 2))
(feed-test
  "cursor page 1 newest first"
  (map (fn (a) (get a :object)) (feed/items p1))
  (list "a" "b"))
(feed-test "next cursor after page 1" (feed/next-cursor p1) 40)

(define p2 (feed/page-before R (feed/next-cursor p1) 2))
(feed-test
  "cursor page 2"
  (map (fn (a) (get a :object)) (feed/items p2))
  (list "c" "d"))
(feed-test "next cursor after page 2" (feed/next-cursor p2) 20)

(define p3 (feed/page-before R (feed/next-cursor p2) 2))
(feed-test
  "cursor page 3 (partial)"
  (map (fn (a) (get a :object)) (feed/items p3))
  (list "e"))

(feed-test
  "empty page nil cursor"
  (feed/next-cursor (feed/page-before R 5 2))
  nil)

(feed-test
  "after cursor loads newer"
  (map
    (fn (a) (get a :object))
    (feed/items (feed/recent (feed/after R 30))))
  (list "a" "b"))
(feed-test
  "before cursor count"
  (feed/count (feed/before R 30))
  2)
