; Phase 1 — log facet: append/read/read-from, sequential seq, stream isolation.
; Note: map returns an array-backed list not equal? to a (list ...) literal,
; so assertions build their compared list with list/nth, not map.

(persist-test
  "empty stream reads empty"
  (len (persist/read (persist/open) "orders"))
  0)
(persist-test
  "last-seq empty is 0"
  (persist/last-seq (persist/open) "orders")
  0)
(persist-test
  "append returns event with seq 1"
  (persist/event-seq
    (persist/append (persist/open) "orders" "placed" 0 {:id 1}))
  1)
(persist-test
  "append assigns sequential seqs"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "orders" "placed" 0 {})
      (persist/append b "orders" "placed" 1 {})
      (persist/event-seq
        (persist/append b "orders" "placed" 2 {}))))
  3)
(persist-test
  "read returns events oldest-first"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "s" "a" 0 {:n 1})
      (persist/append b "s" "b" 0 {:n 2})
      (let
        ((es (persist/read b "s")))
        (list
          (get (persist/event-data (nth es 0)) :n)
          (get (persist/event-data (nth es 1)) :n)))))
  (list 1 2))
(persist-test
  "count tracks appends"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "s" "a" 0 {})
      (persist/append b "s" "a" 0 {})
      (persist/count b "s")))
  2)
(persist-test
  "streams are isolated"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "s1" "a" 0 {})
      (persist/append b "s2" "a" 0 {})
      (persist/append b "s2" "a" 0 {})
      (list (persist/count b "s1") (persist/count b "s2"))))
  (list 1 2))
(persist-test
  "read-from filters by seq"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "s" "a" 0 {})
      (persist/append b "s" "a" 0 {})
      (persist/append b "s" "a" 0 {})
      (let
        ((es (persist/read-from b "s" 2)))
        (list
          (persist/event-seq (nth es 0))
          (persist/event-seq (nth es 1))))))
  (list 2 3))
(persist-test
  "read-from past end is empty"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "s" "a" 0 {})
      (len (persist/read-from b "s" 5))))
  0)
