;; lib/commerce/tests/federation.sx — federated catalog (out-of-scope stub).
;; Uses (commerce-test name got expected) provided by conformance.sh.

(define
  cat-a
  (make-catalog
    (list
      (list "widget" 1000 :standard)
      (list "book" 800 :zero-rated))
    (list)
    (list)))

(define
  cat-b
  (make-catalog
    (list
      (list "widget" 900 :standard)
      (list "tea" 1200 :reduced))
    (list)
    (list)))

(define
  cat-c
  (make-catalog (list (list "widget" 1100 :standard)) (list) (list)))

(define
  fed
  (federation-add
    (federation-add (make-federation :alpha cat-a) :beta cat-b)
    :gamma cat-c))

;; --- structure ---

(commerce-test "is-stub" federation-stub? true)
(commerce-test
  "instances"
  (federation-instances fed)
  (list :alpha :beta :gamma))
(commerce-test "product-count" (len (fed-products fed)) 5)

;; --- forward query ---

(commerce-test
  "price-at-instance"
  (run* p (fed-priceo fed :beta "widget" p))
  (list 900))

;; --- backward queries (the showcase) ---

(commerce-test
  "instances-with-widget"
  (instances-with-sku fed "widget")
  (list :alpha :beta :gamma))

(commerce-test
  "instances-with-book"
  (instances-with-sku fed "book")
  (list :alpha))

(commerce-test
  "instances-with-tea"
  (instances-with-sku fed "tea")
  (list :beta))

(commerce-test
  "instance-by-price-backward"
  (run* inst (fresh (c) (fed-producto fed inst "widget" 1100 c)))
  (list :gamma))

;; --- offers + cheapest (deterministic selection) ---

(commerce-test
  "widget-offers"
  (sku-offers fed "widget")
  (list
    (list 1000 :alpha)
    (list 900 :beta)
    (list 1100 :gamma)))

(commerce-test
  "cheapest-widget"
  (cheapest-offer fed "widget")
  (list 900 :beta))
(commerce-test
  "cheapest-book"
  (cheapest-offer fed "book")
  (list 800 :alpha))
(commerce-test "cheapest-missing" (cheapest-offer fed "ghost") nil)
