WC Tests all pass again after fiddling around with the live system.

This commit is contained in:
Robert C. Martin 2023-05-23 08:53:53 -05:00
parent a0ead65e21
commit 139ca2459c
2 changed files with 54 additions and 50 deletions

View File

@ -1,10 +1,12 @@
(ns more-speech.nostr.event-handlers-spec (ns more-speech.nostr.event-handlers-spec
(:require [speclj.core :refer :all] (:require
[more-speech.nostr.event-dispatcher :as handlers] [more-speech.nostr.util :as util]
[more-speech.db.gateway :as gateway] [speclj.core :refer :all]
[more-speech.db.in-memory :as in-memory] [more-speech.nostr.event-dispatcher :as handlers]
[more-speech.config :as config] [more-speech.db.gateway :as gateway]
[more-speech.mem :refer :all])) [more-speech.db.in-memory :as in-memory]
[more-speech.config :as config]
[more-speech.mem :refer :all]))
(declare db) (declare db)
(describe "event-handlers" (describe "event-handlers"
@ -52,7 +54,7 @@
(with-redefs (with-redefs
[handlers/handle-text-event (stub :handle-text-event) [handlers/handle-text-event (stub :handle-text-event)
handlers/process-event (stub :process-event) handlers/process-event (stub :process-event)
handlers/translate-event (stub :translate-event {:return event}) util/translate-event (stub :translate-event {:return event})
handlers/decrypt-dm-event (stub :decrypt-dm-event {:return event}) handlers/decrypt-dm-event (stub :decrypt-dm-event {:return event})
handlers/compute-id (stub :compute-id {:return 1})] handlers/compute-id (stub :compute-id {:return 1})]
(set-mem [:processed-event-ids] {}) (set-mem [:processed-event-ids] {})
@ -68,7 +70,7 @@
(with-redefs (with-redefs
[handlers/handle-text-event (stub :handle-text-event) [handlers/handle-text-event (stub :handle-text-event)
handlers/process-event (stub :process-event) handlers/process-event (stub :process-event)
handlers/translate-event (stub :translate-event {:return event}) util/translate-event (stub :translate-event {:return event})
handlers/decrypt-dm-event (stub :decrypt-dm-event {:return event}) handlers/decrypt-dm-event (stub :decrypt-dm-event {:return event})
handlers/compute-id (stub :compute-id {:return 1})] handlers/compute-id (stub :compute-id {:return 1})]
(set-mem [:processed-event-ids] {1 #{:first-url}}) (set-mem [:processed-event-ids] {1 #{:first-url}})

View File

@ -1,5 +1,6 @@
(ns more-speech.nostr.zaps-spec (ns more-speech.nostr.zaps-spec
(:require (:require
[clojure.core.async :as async]
[more-speech.bech32 :as bech32] [more-speech.bech32 :as bech32]
[more-speech.config :as config] [more-speech.config :as config]
[more-speech.db.gateway :as gateway] [more-speech.db.gateway :as gateway]
@ -12,7 +13,6 @@
[more-speech.relay :as relay] [more-speech.relay :as relay]
[more-speech.util.fortune-messages :as fortune] [more-speech.util.fortune-messages :as fortune]
[more-speech.websocket-relay :as ws-relay] [more-speech.websocket-relay :as ws-relay]
[org.bovinegenius.exploding-fish :as uri]
[speclj.core :refer :all]) [speclj.core :refer :all])
(:import (ecdhJava SECP256K1))) (:import (ecdhJava SECP256K1)))
@ -111,6 +111,7 @@
my-pubkey (util/bytes->num (es/get-pub-key (util/num->bytes 32 my-privkey))) my-pubkey (util/bytes->num (es/get-pub-key (util/num->bytes 32 my-privkey)))
_ (set-mem :pubkey my-pubkey) _ (set-mem :pubkey my-pubkey)
_ (set-mem [:keys :private-key] (util/hexify my-privkey)) _ (set-mem [:keys :private-key] (util/hexify my-privkey))
_ (set-mem [:keys :public-key] (util/hexify my-pubkey))
_ (reset! relays {"relay-r1" {:read :read-all} _ (reset! relays {"relay-r1" {:read :read-all}
"relay-nr" {:read :read-none} "relay-nr" {:read :read-none}
"relay-r2" {:read :read-all}}) "relay-r2" {:read :read-all}})
@ -231,50 +232,51 @@
(context "wallet-connect" (context "wallet-connect"
(it "executes wallet-connect payment" (it "executes wallet-connect payment"
(set-mem [:keys :wallet-connect] "nostrwalletconnect://info-id?relay=wc-relay-url") (let [event-index (atom -1)
(with-redefs [ws-relay/make (stub :relay-make {:return "some-relay"}) events [{:content "pay_invoice"} {:kind 23195 :content "{\"result_type\": \"pay_invoice\"}"}]]
relay/open (stub :relay-open {:return "open-relay"}) (set-mem [:keys :wallet-connect] "nostrwalletconnect://beef?relay=wc-relay-url&secret=dead")
relay/send (stub :relay-send) (with-redefs [ws-relay/make (stub :relay-make {:return "some-relay"})
relay/close (stub :relay-close) relay/open (stub :relay-open {:return "open-relay"})
zaps/get-wc-request-event (stub :request-event {:return "request-event"})] relay/send (stub :relay-send)
(let [info-promise (promise)] relay/close (stub :relay-close)
(deliver info-promise "pay_invoice") zaps/get-wc-request-event (stub :request-event {:return "request-event"})
(zaps/zap-by-wallet-connect "event-to-zap" info-promise) async/<!! (stub :read-chan {:invoke (fn [_x] (get events (swap! event-index inc)))})]
(should-have-invoked :relay-make {:with ["wc-relay-url" :*]}) (let [event-chan :some-channel]
(should-have-invoked :relay-open {:with ["some-relay"]}) (zaps/zap-by-wallet-connect "event-to-zap" event-chan)
(should-have-invoked :relay-send {:with ["open-relay" ["REQ" "ms-info" {"kinds" [13194], "authors" ["info-id"]}]]}) (should-have-invoked :relay-make {:with ["wc-relay-url" :*]})
(should-have-invoked :request-event {:with ["event-to-zap" :*]}) (should-have-invoked :relay-open {:with ["some-relay"]})
(should-have-invoked :relay-send {:with ["open-relay" "request-event"]}) (should-have-invoked :relay-send {:with ["open-relay" ["REQ" "ms-info" {"kinds" [13194], "authors" ["beef"]}]]})
(should-have-invoked :relay-close {:with ["open-relay"]}) (should-have-invoked :request-event {:with ["event-to-zap" :*]})
(should-have-invoked :relay-send {:with ["open-relay" "request-event"]})
(should-have-invoked :relay-close {:with ["open-relay"]})
)
) )
) )
)
(it "generates wc request" (it "generates wc request"
(should= "{\"method\":\"pay_invoice\",\"params\":{\"invoice\":\"invoice\"}}" (should= "{\"method\":\"pay_invoice\",\"params\":{\"invoice\":\"invoice\"}}"
(zaps/make-wc-json-request "invoice"))) (zaps/make-wc-json-request "invoice")))
(it "composes a wc request event" (it "composes a wc request event"
(with-redefs [config/proof-of-work-default 0] (with-redefs [config/proof-of-work-default 0]
(let [sender-private-key-bytes (util/make-private-key) (let [sender-private-key-bytes (util/make-private-key)
sender-private-key (util/bytes->num sender-private-key-bytes) sender-private-key (util/bytes->num sender-private-key-bytes)
sender-private-key-hex (util/hexify sender-private-key) sender-private-key-hex (util/hexify sender-private-key)
sender-public-key-bytes (es/get-pub-key sender-private-key-bytes) sender-public-key-bytes (es/get-pub-key sender-private-key-bytes)
sender-public-key (util/bytes->num sender-public-key-bytes) sender-public-key (util/bytes->num sender-public-key-bytes)
recipient-private-key-bytes (util/make-private-key) recipient-private-key-bytes (util/make-private-key)
recipient-private-key (util/bytes->num recipient-private-key-bytes) recipient-private-key (util/bytes->num recipient-private-key-bytes)
recipient-public-key-bytes (es/get-pub-key recipient-private-key-bytes) recipient-public-key-bytes (es/get-pub-key recipient-private-key-bytes)
recipient-public-key (util/bytes->num recipient-public-key-bytes) recipient-public-key (util/bytes->num recipient-public-key-bytes)
recipient-public-key-hex (util/hexify recipient-public-key) recipient-public-key-hex (util/hexify recipient-public-key)
inbound-shared-secret (SECP256K1/calculateKeyAgreement inbound-shared-secret (SECP256K1/calculateKeyAgreement
recipient-private-key recipient-private-key
sender-public-key) sender-public-key)
request "request" request "request"
_ (set-mem :pubkey 1) _ (set-mem :pubkey 1)
[_ event] (zaps/compose-wc-request-event recipient-public-key-hex sender-private-key-hex request)] [_ event] (zaps/compose-wc-request-event recipient-public-key-hex sender-private-key-hex request)]
(should= 23194 (:kind event)) (should= 23194 (:kind event))
(should= [[:p recipient-public-key-hex]] (filter #(= :p (first %)) (:tags event))) (should= [[:p recipient-public-key-hex]] (filter #(= :p (first %)) (:tags event)))
(should= request (SECP256K1/decrypt inbound-shared-secret (:content event))) (should= request (SECP256K1/decrypt inbound-shared-secret (:content event)))))))
)))
) )
) )