| (ns snapkitty.lisp.test.emojiscript-tests
|
| "EmojiScript compiler + executor tests"
|
| (:require [cljs.test :refer [deftest is testing]]
|
| [snapkitty.lisp.emojiscript :as emoji]))
|
|
|
|
|
|
|
|
|
|
|
| (deftest test-compile-simple-arithmetic
|
| "Compile: π’40 π’2 β β©οΈ"
|
| (let [source "π’40 π’2 β β©οΈ"
|
| result (emoji/compile-emojiscript source)]
|
| (is (= true (:valid? result)))
|
| (is (= 4 (:instructions-count result)))
|
| (is (contains? #{:Push :Add :Ret}
|
| (map :op (:bytecode result))))))
|
|
|
| (deftest test-compile-cap-gate
|
| "Compile: π’3 π’5 π β©οΈ (capability check)"
|
| (let [source "π’3 π’5 π β©οΈ"
|
| result (emoji/compile-emojiscript source)]
|
| (is (= true (:valid? result)))
|
| (is (some #(= :CapGate (:op %)) (:bytecode result)))))
|
|
|
| (deftest test-compile-memory-ops
|
| "Compile: π’64 π’9 ποΈ π’0 π€ β©οΈ (alloc + load)"
|
| (let [source "π’64 π’9 ποΈ π’0 π€ β©οΈ"
|
| result (emoji/compile-emojiscript source)]
|
| (is (= true (:valid? result)))
|
| (is (some #(= :Alloc (:op %)) (:bytecode result)))
|
| (is (some #(= :Load (:op %)) (:bytecode result)))))
|
|
|
| (deftest test-compile-jump
|
| "Compile: π’10 β‘οΈ (unconditional jump)"
|
| (let [source "π’10 β‘οΈ β©οΈ"
|
| result (emoji/compile-emojiscript source)]
|
| (is (= true (:valid? result)))
|
| (is (some #(= :Jump (:op %)) (:bytecode result)))))
|
|
|
| (deftest test-compile-conditional-jump
|
| "Compile: π’1 β (conditional jump)"
|
| (let [source "π’1 β β©οΈ"
|
| result (emoji/compile-emojiscript source)]
|
| (is (= true (:valid? result)))
|
| (is (some #(= :JumpIf (:op %)) (:bytecode result)))))
|
|
|
| (deftest test-compile-bitwise-ops
|
| "Compile: π’7 π’3 π€ β©οΈ (bitwise AND)"
|
| (let [source "π’7 π’3 π€ β©οΈ"
|
| result (emoji/compile-emojiscript source)]
|
| (is (= true (:valid? result)))
|
| (is (some #(= :And (:op %)) (:bytecode result)))))
|
|
|
| (deftest test-compile-rejects-empty
|
| "Reject empty program"
|
| (is (thrown-with-msg?
|
| js/Error
|
| #"Empty program"
|
| (emoji/compile-emojiscript ""))))
|
|
|
| (deftest test-compile-rejects-whitespace-only
|
| "Reject whitespace-only program"
|
| (is (thrown-with-msg?
|
| js/Error
|
| #"Empty program"
|
| (emoji/compile-emojiscript " \n\t "))))
|
|
|
| (deftest test-compile-rejects-unknown-emoji
|
| "Reject unknown emoji"
|
| (is (thrown-with-msg?
|
| js/Error
|
| #"Unknown emoji"
|
| (emoji/compile-emojiscript "π₯·"))))
|
|
|
|
|
|
|
|
|
|
|
| (deftest test-execute-simple-addition
|
| "Execute: π’40 π’2 β β©οΈ = 42"
|
| (let [source "π’40 π’2 β β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (= true (:halted? result)))
|
| (is (= 42 (:result result)))
|
| (is (= [42] (:stack result)))))
|
|
|
| (deftest test-execute-subtraction
|
| "Execute: π’100 π’40 β β©οΈ = 60"
|
| (let [source "π’100 π’40 β β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (= 60 (:result result)))))
|
|
|
| (deftest test-execute-multiplication
|
| "Execute: π’6 π’7 βοΈ β©οΈ = 42"
|
| (let [source "π’6 π’7 βοΈ β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (= 42 (:result result)))))
|
|
|
| (deftest test-execute-division
|
| "Execute: π’84 π’2 β β©οΈ = 42"
|
| (let [source "π’84 π’2 β β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (= 42 (:result result)))))
|
|
|
| (deftest test-execute-bitwise-and
|
| "Execute: π’15 π’7 π€ β©οΈ = 7 (bitwise AND)"
|
| (let [source "π’15 π’7 π€ β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (= 7 (:result result)))))
|
|
|
| (deftest test-execute-bitwise-or
|
| "Execute: π’12 π’5 π β©οΈ = 13 (bitwise OR)"
|
| (let [source "π’12 π’5 π β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (= 13 (:result result)))))
|
|
|
| (deftest test-execute-bitwise-xor
|
| "Execute: π’12 π’5 π β©οΈ = 9 (bitwise XOR)"
|
| (let [source "π’12 π’5 π β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (= 9 (:result result)))))
|
|
|
| (deftest test-execute-division-by-zero
|
| "Reject division by zero"
|
| (let [source "π’10 π’0 β β©οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (contains? result :error))
|
| (is (clojure.string/includes? (:error result) "Division by zero"))))
|
|
|
| (deftest test-execute-max-steps
|
| "Enforce step limit (prevent infinite loops)"
|
| (let [source "π’0 β‘οΈ"
|
| compiled (emoji/compile-emojiscript source)
|
| result (emoji/execute-emojiscript (:bytecode compiled) :max-steps 100)]
|
| (is (= 100 (:steps result)))
|
| (is (= false (:halted? result)))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| (deftest test-semantic-pass-stream
|
| "π Stream β telemetry-bus integration"
|
| (let [compiled (emoji/compile-emojiscript "π’42 π β©οΈ")
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (:halted? result))
|
| (is (= 42 (:result result)))
|
| (is (some #(= (:type %) :telemetry) (:events result)))
|
| (is (>= (count (filter #(= (:event %) :stream-push) (:events result))) 1))))
|
|
|
| (deftest test-semantic-pass-policy-check
|
| "π§ PolicyCheck β policy-immune routing"
|
| (let [compiled (emoji/compile-emojiscript "π’100 π§ β©οΈ")
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (:halted? result))
|
| (is (= 100 (:result result)))
|
| (is (some #(= (:type %) :policy-route) (:events result)))))
|
|
|
| (deftest test-semantic-pass-seal
|
| "π Seal β Bifrost WORM signing"
|
| (let [compiled (emoji/compile-emojiscript "π’777 π β©οΈ")
|
| result (emoji/execute-emojiscript (:bytecode compiled) :worm-ledger {})
|
| seal-event (first (filter #(= (:type %) :bifrost-seal) (:events result)))]
|
| (is (:halted? result))
|
| (is (= 777 (:result result)))
|
| (is seal-event)
|
| (is (= 777 (:value seal-event)))))
|
|
|
| (deftest test-semantic-pass-readonly
|
| "π ReadOnly β capability downgrade"
|
| (let [compiled (emoji/compile-emojiscript "π’255 π β©οΈ")
|
| result (emoji/execute-emojiscript (:bytecode compiled))]
|
| (is (:halted? result))
|
| (is (< (:result result) 255))
|
| (is (some #(= (:type %) :capability-downgrade) (:events result)))))
|
|
|
| (deftest test-mcp-compile-tool
|
| "MCP compile_emojiscript tool"
|
| (let [source "π’40 π’2 β β©οΈ"]
|
|
|
|
|
| (is (truthy? (emoji/compile-emojiscript source)))))
|
|
|
| (deftest test-mcp-execute-tool
|
| "MCP execute_emojiscript tool"
|
| (let [source "π’40 π’2 β β©οΈ"
|
| compiled (emoji/compile-emojiscript source)]
|
|
|
| (is (= 42 (:result (emoji/execute-emojiscript (:bytecode compiled)))))))
|
|
|