;; lib/commerce/tests/api.sx — public commerce session surface.
;; Uses (commerce-test name got expected) provided by conformance.sh.

(define
  acat
  (make-catalog
    (list
      (list "widget" 1000 :standard)
      (list "book" 800 :zero-rated))
    (list (list "widget" :small -200))
    (list)))

(define
  arules
  (list
    (list :uk :standard :guest 2000)
    (list :uk :zero-rated :guest 0)))

(define actx (make-pricing-context acat arules :uk :guest))
(define sess0 (commerce-session actx))

;; --- empty session ---

(commerce-test "new-session-empty" (commerce-cart sess0) empty-cart)
(commerce-test "new-count" (commerce-count sess0) 0)
(commerce-test "new-total" (commerce-total sess0) {:subtotal 0 :discounts 0 :total 0 :tax 0})

;; --- add + total ---

(define
  sess1
  (commerce-add
    (commerce-add sess0 "widget" :small 2)
    "book"
    :none 1))

(commerce-test "add-count" (commerce-count sess1) 3)
(commerce-test
  "add-lines"
  (commerce-lines sess1)
  (list (list "widget" :small 2) (list "book" :none 1)))
(commerce-test "add-total" (commerce-total sess1) {:subtotal 2400 :discounts 0 :total 2720 :tax 320})

;; --- mutate ---

(commerce-test
  "set-qty"
  (commerce-lines (commerce-set-qty sess1 "widget" :small 1))
  (list (list "widget" :small 1) (list "book" :none 1)))

(commerce-test
  "remove"
  (commerce-lines (commerce-remove sess1 "book" :none))
  (list (list "widget" :small 2)))

;; --- validation ---

(commerce-test "can-add-yes" (commerce-can-add? sess0 "widget") true)
(commerce-test "can-add-no" (commerce-can-add? sess0 "ghost") false)

;; --- audit breakdown ---

(commerce-test
  "explain"
  (commerce-explain sess1)
  (list {:sku "widget" :unit 800 :qty 2 :variant :small :extended 1600 :tax 320} {:sku "book" :unit 800 :qty 1 :variant :none :extended 800 :tax 0}))

;; --- checkout stub ---

(commerce-test
  "checkout-stub"
  (get (commerce-checkout sess1) :status)
  :not-implemented)
