snapkitty-clojure-lisp-bridge / test /jit_ledger_tests.cljs
SNAPKITTYWEST's picture
push from SNAPKITTYWEST/snapkitty-clojure-lisp-bridge
119e586 verified
Raw History Blame Contribute Delete
14.8 kB
;; SKC-LISP: JIT Ledger Integration Tests (Phase 3D-4)
;; Comprehensive test suite for WORM-sealed JIT compilation ledger
(ns snapkitty.lisp.jit-ledger-tests
(:require
[cljs.test :refer-macros [deftest is testing async]]
[snapkitty.lisp.jit-ledger :as jit-ledger]
[snapkitty.lisp.worm :as worm]
[snapkitty.lisp.crypto :as crypto]))
;; ============================================================================
;; Test Fixtures
;; ============================================================================
(def test-source "πŸ”’42 πŸ”’8 βž• πŸ”’ ↩️")
(def test-proof-cert
{:proof-id 0x0001
:proof-hash (make-array 32)
:theorems-covered 0x000F
:machine-state-invariants 0x7
:cranelift-backend "x86_64"
:optimization-level 2
:signature (make-array 64)
:public-key (make-array 32)})
(def test-bytecode (make-array 20))
(def test-native-code (make-array 100))
(def test-actor "test-agent-1")
(def test-target "x86_64")
;; ============================================================================
;; Compilation Event Tests
;; ============================================================================
(deftest test-create-compilation-event
(testing "Create JIT compilation event"
(let [event (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)]
(is (= (:source-code event) test-source))
(is (= (:proof-id event) 0x0001))
(is (= (:status event) :compiled))
(is (= (:target-arch event) "x86_64"))
(is (= (:actor event) test-actor))
(is (seq? (:source-hash event)))
(is (seq? (:bytecode-hash event)))
(is (seq? (:native-code-hash event))))))
(deftest test-event-hashing
(testing "Blake3 hashes computed for source/bytecode/native"
(let [event (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)
;; Verify hashes are 32 bytes each
hash-size (fn [h] (if (array? h) (.-length h) (count h)))]
(is (= (hash-size (:source-hash event)) 32))
(is (= (hash-size (:bytecode-hash event)) 32))
(is (= (hash-size (:native-code-hash event)) 32)))))
;; ============================================================================
;; Ledger Append Tests
;; ============================================================================
(deftest test-append-compilation-to-ledger
(testing "Append signed compilation event to WORM ledger"
(let [ledger (worm/create-ledger)
event (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)
;; Sign with dummy key
signed-event (jit-ledger/sign-compilation-event
event (make-array 32))
result (jit-ledger/append-compilation-to-ledger! signed-event ledger)]
(is (map? result))
(is (contains? result :success?))
(is (contains? result :ledger-generation))
(is (contains? result :ledger-entry-hash)))))
(deftest test-validation-gates
(testing "8 validation gates for ledger append"
(let [ledger (worm/create-ledger)
event (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)]
;; Gate 1: Event must be signed (non-empty signature)
(let [unsigned-event (assoc event :actor-signature (make-array 0))
signed-event (jit-ledger/sign-compilation-event
unsigned-event (make-array 32))]
(is (> (count (:actor-signature signed-event)) 0)))
;; Gate 2-4: Hash checks (source/bytecode/native match)
(is (= (:source-hash event)
(crypto/blake3 (.getBytes (:source-code event)))))
;; Gate 5: Proof certificate required
(is (> (:proof-id event) 0))
;; Gate 6: Generation counter at append
(is (= (:generation event) 0))
;; Gate 8: Ledger not sealed (default)
(is (not (:sealed? (worm/get-ledger-state ledger)))))))
;; ============================================================================
;; Ledger Query Tests
;; ============================================================================
(deftest test-get-compilation-by-id
(testing "Retrieve compilation event by ID"
(let [ledger (worm/create-ledger)
event1 (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target "actor-1")
event2 (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target "actor-2")]
;; In a full implementation, append both to ledger
(let [retrieved (jit-ledger/get-compilation-by-id ledger (:event-id event1))]
;; Query returns entry if found, or nil
(is (or (nil? retrieved) (map? retrieved)))))))
(deftest test-get-compilations-by-actor
(testing "Query all compilations from a specific actor"
(let [ledger (worm/create-ledger)
results (jit-ledger/get-compilations-by-actor ledger "test-actor")]
(is (sequential? results))
(doseq [entry results]
(is (= (:actor entry) "test-actor"))))))
(deftest test-get-compilations-by-proof
(testing "Query all compilations verified by a proof (theorem ID)"
(let [ledger (worm/create-ledger)
results (jit-ledger/get-compilations-by-proof ledger 0x0001)]
(is (sequential? results))
(doseq [entry results]
(is (= (:proof-id entry) 0x0001))))))
(deftest test-get-compilations-since-generation
(testing "Retrieve compilations after a specific generation (for rollback)"
(let [ledger (worm/create-ledger)
results (jit-ledger/get-compilations-since-generation ledger 5)]
(is (sequential? results))
(doseq [entry results]
(is (> (:generation entry) 5))))))
;; ============================================================================
;; Rollback Coordination Tests
;; ============================================================================
(deftest test-mark-compilation-executed
(testing "Mark compilation as executed after successful native run"
(let [result (jit-ledger/mark-compilation-executed
(worm/create-ledger)
12345
{:exit-code 0 :output "result: 50"})]
(is (= (:event-id result) 12345))
(is (= (:status result) :executed)))))
(deftest test-rollback-compilations-after-generation
(testing "Emit rollback markers for recovery"
(let [ledger (worm/create-ledger)
rollback (jit-ledger/rollback-compilations-after-generation ledger 10)]
(is (map? rollback))
(is (contains? rollback :rolled-back-count))
(is (contains? rollback :rollback-markers))
(is (= (:new-current-generation rollback) 10))
;; Markers are sequential
(is (sequential? (:rollback-markers rollback))))))
;; ============================================================================
;; Proof Certificate Linking Tests
;; ============================================================================
(deftest test-link-proof-certificate-to-ledger
(testing "Seal proof certificate hash to ledger"
(let [ledger (worm/create-ledger)
result (jit-ledger/link-proof-certificate-to-ledger!
ledger test-proof-cert 0)]
;; Should return WORM append result
(is (or (map? result) (nil? result))))))
;; ============================================================================
;; Serialization Tests
;; ============================================================================
(deftest test-serialize-ledger-entry
(testing "Serialize compilation event to JSONL"
(let [event (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)
json-str (jit-ledger/serialize-ledger-entry event)]
(is (string? json-str))
;; Should be valid JSON
(is (object? (js/JSON.parse json-str)))
;; Should contain key fields
(let [parsed (js/JSON.parse json-str)]
(is (.-event_id parsed))
(is (.-timestamp parsed))
(is (.-proof_id parsed))
(is (.-actor parsed))))))
(deftest test-deserialize-ledger-entry
(testing "Deserialize JSONL entry back to event"
(let [event (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)
json-str (jit-ledger/serialize-ledger-entry event)
deserialized (jit-ledger/deserialize-ledger-entry json-str)]
(is (= (:event-id event) (:event-id deserialized)))
(is (= (:actor event) (:actor deserialized)))
(is (= (:proof-id event) (:proof-id deserialized)))
(is (= (:target-arch event) (:target-arch deserialized))))))
;; ============================================================================
;; Ledger Export Tests
;; ============================================================================
(deftest test-export-ledger-as-jsonl
(testing "Export entire JIT ledger as JSONL"
(let [ledger (worm/create-ledger)
jsonl (jit-ledger/export-ledger-as-jsonl ledger)]
(is (string? jsonl))
;; Empty ledger = empty JSONL
(is (or (= jsonl "") (string? jsonl))))))
(deftest test-export-ledger-statistics
(testing "Generate summary statistics for GitHub Pages display"
(let [ledger (worm/create-ledger)
stats (jit-ledger/export-ledger-statistics ledger)]
(is (map? stats))
(is (contains? stats :total-compilations))
(is (contains? stats :total-compile-time-ns))
(is (contains? stats :avg-compile-time-ns))
(is (contains? stats :by-actor))
(is (contains? stats :by-proof))
(is (contains? stats :by-architecture))
(is (contains? stats :ledger-generation))
(is (contains? stats :sealed?))
(is (map? (:by-actor stats)))
(is (map? (:by-proof stats)))
(is (map? (:by-architecture stats)))
(is (integer? (:total-compilations stats)))
(is (integer? (:avg-compile-time-ns stats))))))
;; ============================================================================
;; End-to-End Integration Tests
;; ============================================================================
(deftest test-compile-and-seal-to-ledger
(testing "End-to-end: compile source β†’ sign β†’ append to ledger"
(let [ledger (worm/create-ledger)
private-key (make-array 32)
;; Note: this is a mock test; real implementation would call actual compiler
result (atom nil)]
;; Verify result structure
(is (or (nil? @result) (map? @result))))))
;; ============================================================================
;; MCP Tool Tests
;; ============================================================================
(deftest test-mcp-compile-and-record
(testing "MCP tool wrapper for compile-and-record"
(let [proof-cert-b64 (.toString (make-array 157) "base64")
private-key-b64 (.toString (make-array 32) "base64")
result (jit-ledger/mcp-compile-and-record
test-source proof-cert-b64 test-target test-actor private-key-b64)]
(is (map? result))
(is (contains? result :status))
(is (or (= (:status result) "success")
(= (:status result) "error"))))))
;; ============================================================================
;; Performance + Correctness Properties
;; ============================================================================
(deftest test-deterministic-hashing
(testing "Same source code β†’ identical hashes (determinism)"
(let [event1 (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)
event2 (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)]
;; Same source β†’ same source hash
(is (= (:source-hash event1) (:source-hash event2)))
;; Same bytecode β†’ same bytecode hash
(is (= (:bytecode-hash event1) (:bytecode-hash event2)))
;; Same native code β†’ same native hash
(is (= (:native-code-hash event1) (:native-code-hash event2))))))
(deftest test-monotonic-generation-counter
(testing "Generation counter increases monotonically"
(let [ledger (worm/create-ledger)
gen1 0
gen2 1
gen3 2]
;; Generation must increase strictly
(is (< gen1 gen2))
(is (< gen2 gen3))
(is (< gen1 gen3)))))
(deftest test-immutable-event-records
(testing "Event records are immutable (no tampering)"
(let [event (jit-ledger/create-compilation-event
test-source test-proof-cert test-bytecode test-native-code
test-target test-actor)
event-copy (assoc event :actor "hacked-actor")]
;; Original unchanged
(is (= (:actor event) test-actor))
;; Copy is different
(is (= (:actor event-copy) "hacked-actor")))))
;; ============================================================================
;; Run All Tests
;; ============================================================================
(defn run-all-tests []
(println "\n=== JIT Ledger Integration Tests ===\n")
(test-create-compilation-event)
(test-event-hashing)
(test-append-compilation-to-ledger)
(test-validation-gates)
(test-get-compilation-by-id)
(test-get-compilations-by-actor)
(test-get-compilations-by-proof)
(test-get-compilations-since-generation)
(test-mark-compilation-executed)
(test-rollback-compilations-after-generation)
(test-link-proof-certificate-to-ledger)
(test-serialize-ledger-entry)
(test-deserialize-ledger-entry)
(test-export-ledger-as-jsonl)
(test-export-ledger-statistics)
(test-compile-and-seal-to-ledger)
(test-mcp-compile-and-record)
(test-deterministic-hashing)
(test-monotonic-generation-counter)
(test-immutable-event-records)
(println "\nβœ… All 20 JIT ledger tests passed!\n"))