; Phase 1 — kv facet: get/put/delete/has?/keys, get-or, update.

(persist-test "absent key reads nil" (persist/kv-get (persist/open) "x") nil)
(persist-test
  "has? false when absent"
  (persist/kv-has? (persist/open) "x")
  false)
(persist-test
  "put then get"
  (let
    ((b (persist/open)))
    (begin (persist/kv-put b "x" 7) (persist/kv-get b "x")))
  7)
(persist-test
  "put returns value"
  (let ((b (persist/open))) (persist/kv-put b "x" 9))
  9)
(persist-test
  "has? true after put"
  (let
    ((b (persist/open)))
    (begin (persist/kv-put b "x" 1) (persist/kv-has? b "x")))
  true)
(persist-test
  "put overwrites"
  (let
    ((b (persist/open)))
    (begin
      (persist/kv-put b "x" 1)
      (persist/kv-put b "x" 2)
      (persist/kv-get b "x")))
  2)
(persist-test
  "delete removes key"
  (let
    ((b (persist/open)))
    (begin
      (persist/kv-put b "x" 1)
      (persist/kv-delete b "x")
      (persist/kv-has? b "x")))
  false)
(persist-test
  "delete then get is nil"
  (let
    ((b (persist/open)))
    (begin
      (persist/kv-put b "x" 1)
      (persist/kv-delete b "x")
      (persist/kv-get b "x")))
  nil)
(persist-test
  "keys lists stored keys"
  (let
    ((b (persist/open)))
    (begin
      (persist/kv-put b "a" 1)
      (persist/kv-put b "b" 2)
      (len (persist/kv-keys b))))
  2)
(persist-test
  "get-or returns default when absent"
  (persist/kv-get-or (persist/open) "x" 99)
  99)
(persist-test
  "get-or returns value when present"
  (let
    ((b (persist/open)))
    (begin
      (persist/kv-put b "x" 5)
      (persist/kv-get-or b "x" 99)))
  5)
(persist-test
  "kv-update applies fn over default"
  (let
    ((b (persist/open)))
    (begin
      (persist/kv-update b "n" 0 (fn (v) (+ v 1)))
      (persist/kv-update b "n" 0 (fn (v) (+ v 1)))
      (persist/kv-get b "n")))
  2)
(persist-test
  "kv facet does not touch log"
  (let
    ((b (persist/open)))
    (begin (persist/kv-put b "x" 1) (persist/count b "x")))
  0)
