(ns emptyhead.test.idea.protocol
  (:require [cljs.test :as t :include-macros true]
            [emptyhead.idea.crud :as crud]
            [emptyhead.idea.protocol :as prtc]
            [emptyhead.test.utils :refer [expect-error]]
            [emptyhead.test.fixtures :as fx]))

(t/use-fixtures :once fx/temporary-state)
(t/use-fixtures :each fx/pre-reset)

(t/deftest protocol
  (t/testing "Value/reference semantics"
    (let [idea (crud/have-idea!)]
      ;; Repeatedly going between value and reference shouldn't mangle idea
      (t/is (= idea (-> idea prtc/value prtc/reference)))
      (t/is (= (prtc/value idea) (-> idea prtc/reference prtc/value)))

      ;; Attempting to get a reference to a copy should be an error
      (expect-error :stale-reference #(prtc/reference (prtc/copy idea)))

      ;; Attempting to get a reference to an invalid idea should be an error
      (expect-error :invalid-reference #(prtc/reference {})))))
