(ns auto-ap.ledger-test
  (:require
   [auto-ap.integration.util :refer [wrap-setup]]
   [clojure.test :as t]))

(t/use-fixtures :each wrap-setup)


(t/deftest entity-change->ledger
  #_(t/testing "Should code an expected deposit"
    (let [{:strs [ed ccp receipts-split client]}
          (:tempids  @(d/transact conn [#:expected-deposit {:status :expected-deposit-status/pending
                                                            :client {:db/id "client"
                                                                     :client/code "BRYCE"
                                                                     :client/locations ["M"]}
                                                            :total 4.0
                                                            :fee 1.0
                                                            :date #inst "2021-01-01T00:00:00-08:00"
                                                            :location "M"
                                                            :db/id "ed"}]))
          result (sut/entity-change->ledger (d/db conn) [:expected-deposit ed])]
      (t/is (= #:journal-entry
               {:source "expected-deposit"
                :client {:db/id client}
                :date #inst "2021-01-01T00:00:00-08:00"
                :original-entity ed
                :vendor :vendor/ccp-square
                :amount 4.0
                }
               (dissoc result :journal-entry/line-items)))

      (t/testing "should debit ccp"
        (t/is (= [#:journal-entry-line
                  {:debit 4.0
                   :location "A"
                   :account :account/ccp}]
                 (filter :journal-entry-line/debit (:journal-entry/line-items result))))
        )
      (t/testing "should credit receipts split ccp"
        (t/is (= [#:journal-entry-line
                  {:credit 4.0
                   :location "A"
                   :account :account/receipts-split}]
                 (filter :journal-entry-line/credit (:journal-entry/line-items result))))))))
