From 1103548acb28de17a05619b4a9a00d61f2764673 Mon Sep 17 00:00:00 2001 From: "Robert C. Martin" Date: Sun, 29 Jan 2023 08:32:56 -0600 Subject: [PATCH] get rid of some ui-context references. More to come. --- spec/more_speech/nostr/trust_updater_spec.clj | 47 +++++++++++-------- .../ui/swing/article_tree_spec.clj | 13 +++-- src/more_speech/ui/swing/ui_context.clj | 10 ++-- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/spec/more_speech/nostr/trust_updater_spec.clj b/spec/more_speech/nostr/trust_updater_spec.clj index 06add3b..858ccae 100644 --- a/spec/more_speech/nostr/trust_updater_spec.clj +++ b/spec/more_speech/nostr/trust_updater_spec.clj @@ -1,34 +1,41 @@ (ns more-speech.nostr.trust-updater-spec (:require [speclj.core :refer :all] [more-speech.nostr.trust-updater :refer :all] - [more-speech.ui.swing.ui-context :refer :all])) + [more-speech.ui.swing.ui-context :refer :all] + [more-speech.db.in-memory :as in-memory] + [more-speech.db.gateway :as gateway])) +(declare db) (describe "Setting Trust" (with-stubs) + (with db (in-memory/get-db)) + (before (in-memory/clear-db @db)) + (it "establishes new trust with a petname." - (let [my-pubkey 1 - contact-lists {1 [{:pubkey 2}]} - event-state {:pubkey my-pubkey :contact-lists contact-lists}] - (reset! ui-context {:event-context (atom event-state)})) - (entrust 3 "the-petname") - (let [my-contacts (get (:contact-lists @(:event-context @ui-context)) 1)] - (should= (set [{:pubkey 2} - {:pubkey 3 :petname "the-petname"}]) - (set my-contacts))) - ) + (let [my-pubkey 1] + (set-mem :pubkey my-pubkey) + (gateway/add-contacts @db my-pubkey [{:pubkey 2}]) + (entrust 3 "the-petname") + (let [my-contacts (gateway/get-contacts @db my-pubkey)] + (should= (set [{:pubkey 2} + {:pubkey 3 :petname "the-petname"}]) + (set my-contacts))) + )) (it "restablishes trust with a new petname." - (let [my-pubkey 1 - contact-lists {1 [{:pubkey 1} - {:pubkey 3 :petname "the-old-petname"} - {:pubkey 2}]} - event-state {:pubkey my-pubkey :contact-lists contact-lists}] - (reset! ui-context {:event-context (atom event-state)})) + (let [my-pubkey 1 + contacts [{:pubkey 1} + {:pubkey 3 :petname "the-old-petname"} + {:pubkey 2}] + ] + (set-mem :pubkey my-pubkey) + (gateway/add-contacts @db my-pubkey contacts) (entrust 3 "the-new-petname") - (let [my-contacts (get (:contact-lists @(:event-context @ui-context)) 1)] + (let [my-contacts + (gateway/get-contacts @db my-pubkey) + ] (should= (set [{:pubkey 1} {:pubkey 2} {:pubkey 3 :petname "the-new-petname"}]) - (set my-contacts))) - ) + (set my-contacts))))) ) diff --git a/spec/more_speech/ui/swing/article_tree_spec.clj b/spec/more_speech/ui/swing/article_tree_spec.clj index df706a1..a1a7cdf 100644 --- a/spec/more_speech/ui/swing/article_tree_spec.clj +++ b/spec/more_speech/ui/swing/article_tree_spec.clj @@ -17,7 +17,9 @@ (describe "header tree" (with db (in-memory/get-db)) - (before (reset! ui-context {:event-context (atom nil)})) + (before (in-memory/clear-db @db) + (clear-mem) + (swap! ui-context assoc :node-map {} :orphaned-references {})) (context "finding chronological insertion point" (it "returns zero if empty tree" @@ -149,8 +151,8 @@ (let [event-id 1N node-map {} orphaned-references {} - _ (reset! ui-context {:node-map node-map - :orphaned-references orphaned-references})] + _ (swap! ui-context assoc :node-map node-map + :orphaned-references orphaned-references)] (resolve-any-orphans event-id) (should= {} (:node-map @ui-context)) (should= {} (:orphaned-references @ui-context))) @@ -164,8 +166,9 @@ node-map {orphan-id [original-orphan-node] parent-id [parent-node]} orphaned-references {parent-id #{orphan-id}} - _ (reset! ui-context {:node-map node-map - :orphaned-references orphaned-references}) + _ (swap! ui-context assoc + :node-map node-map + :orphaned-references orphaned-references) _ (resolve-any-orphans parent-id) orphan-nodes (get-in @ui-context [:node-map orphan-id]) new-orphan-node (second orphan-nodes) diff --git a/src/more_speech/ui/swing/ui_context.clj b/src/more_speech/ui/swing/ui_context.clj index 1005e22..f942df1 100644 --- a/src/more_speech/ui/swing/ui_context.clj +++ b/src/more_speech/ui/swing/ui_context.clj @@ -1,9 +1,13 @@ (ns more-speech.ui.swing.ui-context (:require [clojure.spec.alpha :as s])) -(def ui-context (atom {:event-context (atom nil) - :node-map {} - :orphaned-references {}})) + +(defn make-ui-context [] + (atom {:event-context (atom nil) + :node-map {} + :orphaned-references {}})) + +(def ui-context (make-ui-context)) (s/def ::id number?) (s/def ::orphaned-references (s/map-of ::id (s/coll-of ::id :kind set?)))