; Extension — stream catalog: enumerate streams, count, existence, totals.

(persist-test
  "empty backend has no streams"
  (persist/stream-count (persist/open))
  0)
(persist-test
  "stream-exists? false when absent"
  (persist/stream-exists? (persist/open) "orders")
  false)
(persist-test
  "append registers a stream"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "orders" "x" 0 {})
      (persist/stream-exists? b "orders")))
  true)
(persist-test
  "stream-count counts distinct streams"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "a" "x" 0 {})
      (persist/append b "b" "x" 0 {})
      (persist/append b "a" "x" 0 {})
      (persist/stream-count b)))
  2)
(persist-test
  "compacted-away stream still lists"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "a" "x" 0 {})
      (persist/checkpoint b "a" "snap" (fn (acc e) acc) 0)
      (persist/truncate b "a" 1)
      (list (persist/count b "a") (persist/stream-exists? b "a"))))
  (list 0 true))
(persist-test
  "kv-only backend lists no streams"
  (let
    ((b (persist/open)))
    (begin (persist/kv-put b "k" 1) (persist/stream-count b)))
  0)
(persist-test
  "total-events sums high-water marks"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "a" "x" 0 {})
      (persist/append b "a" "x" 0 {})
      (persist/append b "b" "x" 0 {})
      (persist/total-events b)))
  3)
(persist-test
  "total-events counts compacted events too"
  (let
    ((b (persist/open)))
    (begin
      (persist/append b "a" "x" 0 {})
      (persist/append b "a" "x" 0 {})
      (persist/checkpoint b "a" "snap" (fn (acc e) acc) 0)
      (persist/truncate b "a" 2)
      (persist/total-events b)))
  2)
(persist-test
  "catalog works on the durable backend"
  (let
    ((db (persist/mock-durable (persist/mem-backend))))
    (begin
      (persist/append db "a" "x" 0 {})
      (persist/append db "b" "x" 0 {})
      (persist/stream-count db)))
  2)
(persist-test
  "catalog survives restart"
  (let
    ((disk (persist/mem-backend)))
    (begin
      (let
        ((db (persist/mock-durable disk)))
        (begin
          (persist/append db "a" "x" 0 {})
          (persist/append db "b" "x" 0 {})))
      (persist/stream-count (persist/mock-durable disk))))
  2)
