2022-03-03 17:30:00 +00:00
|
|
|
(ns more-speech.ui.formatters-spec
|
|
|
|
(:require [speclj.core :refer :all]
|
2022-06-04 17:02:04 +00:00
|
|
|
[more-speech.ui.formatters :refer :all]
|
|
|
|
[more-speech.ui.swing.ui-context :refer :all]
|
|
|
|
[more-speech.nostr.util :as util]))
|
|
|
|
|
|
|
|
(defn hexify [n] (util/num32->hex-string n))
|
2022-03-03 17:30:00 +00:00
|
|
|
|
2022-03-07 14:23:27 +00:00
|
|
|
(describe "Abbreviations."
|
|
|
|
(it "abbreviates pubkeys"
|
|
|
|
(should= "short" (abbreviate "short" 10))
|
|
|
|
(should= "some lo..." (abbreviate "some long string." 10)))
|
|
|
|
|
|
|
|
)
|
|
|
|
|
2022-03-03 17:30:00 +00:00
|
|
|
(describe "reformat article body to fit width."
|
|
|
|
(it "should not wrap nothing"
|
|
|
|
(should= "" (reformat-article "" 1)))
|
|
|
|
|
|
|
|
(it "should not wrap something shorter than the width."
|
|
|
|
(should= "xxx" (reformat-article "xxx" 10)))
|
|
|
|
|
|
|
|
(it "should wrap something longer than the width."
|
|
|
|
(should= "xx\nxx" (reformat-article "xxxx" 2)))
|
|
|
|
|
|
|
|
(it "should repeatedly wrap long strings."
|
|
|
|
(should= "xx\nxx\nxx" (reformat-article "xxxxxx" 2)))
|
|
|
|
|
|
|
|
(it "should break spaces"
|
|
|
|
(should= "x\nx" (reformat-article "x x" 1))
|
|
|
|
(should= "x\nx" (reformat-article "x x" 2))
|
|
|
|
(should= "xx\nxx" (reformat-article "xx xx" 4)))
|
|
|
|
|
|
|
|
(it "should ignore existing single line breaks."
|
|
|
|
(should= "x x" (reformat-article "x\nx" 5)))
|
|
|
|
|
|
|
|
(it "should preserve existing double line breaks."
|
|
|
|
(should= "x\n\nx" (reformat-article "x\n\nx" 7)))
|
|
|
|
|
2022-03-03 18:24:32 +00:00
|
|
|
(it "should preserve leading spaces."
|
|
|
|
(should= "x\n xxx" (reformat-article "x\n xxx" 6)))
|
|
|
|
|
|
|
|
(it "should give priority to leading spaces"
|
|
|
|
(should= "x\n x\n\nx" (reformat-article "x\n x\n\nx" 20)))
|
2022-03-07 13:34:21 +00:00
|
|
|
|
|
|
|
(it "should not break this line. (bug-fix)"
|
|
|
|
(should= "I found you!" (reformat-article "I found you!" 50)))
|
2022-06-08 23:39:19 +00:00
|
|
|
|
|
|
|
(it "always breaks at a reply prefix '>'"
|
|
|
|
(should= ">this is\n>a reply." (reformat-article ">this is\n>a reply." 50)))
|
2022-03-03 17:30:00 +00:00
|
|
|
)
|
2022-05-21 20:14:47 +00:00
|
|
|
|
2022-06-04 17:02:04 +00:00
|
|
|
(describe "format header"
|
2022-05-21 20:14:47 +00:00
|
|
|
(it "formats an empty message"
|
2022-07-05 13:59:45 +00:00
|
|
|
(let [profiles {}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
2022-05-21 20:14:47 +00:00
|
|
|
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
|
|
|
|
:created-at 1
|
|
|
|
:content ""
|
|
|
|
:tags []}
|
2022-06-04 08:02:07 +00:00
|
|
|
timestamp (format-time (event :created-at))
|
2022-07-05 13:59:45 +00:00
|
|
|
header (format-header event)]
|
2022-06-04 08:02:07 +00:00
|
|
|
(should= (str " 11111111111111111... " timestamp " \n") header)))
|
2022-05-21 20:14:47 +00:00
|
|
|
|
|
|
|
(it "formats a simple message"
|
2022-07-05 13:59:45 +00:00
|
|
|
(let [profiles {}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
|
|
|
|
:created-at 1
|
|
|
|
:content "the message"
|
|
|
|
:tags []}
|
|
|
|
timestamp (format-time (event :created-at))
|
2022-07-05 13:59:45 +00:00
|
|
|
header (format-header event)]
|
2022-06-04 17:02:04 +00:00
|
|
|
(should= (str " 11111111111111111... " timestamp " the message\n") header)))
|
2022-05-21 20:14:47 +00:00
|
|
|
|
2022-07-05 13:59:45 +00:00
|
|
|
(it "formats a simple message with a user profile"
|
2022-07-05 14:22:15 +00:00
|
|
|
(let [profiles {1 {:name "user-1"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:pubkey 1
|
|
|
|
:created-at 1
|
|
|
|
:content "the message"
|
|
|
|
:tags []}
|
|
|
|
timestamp (format-time (event :created-at))
|
|
|
|
header (format-header event)]
|
|
|
|
(should= (str " user-1 " timestamp " the message\n") header)))
|
2022-07-05 13:59:45 +00:00
|
|
|
|
2022-05-21 20:14:47 +00:00
|
|
|
(it "formats a long message with line ends."
|
2022-07-05 13:59:45 +00:00
|
|
|
(let [profiles {}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
|
|
|
|
:created-at 1
|
|
|
|
:content "Four score and seven years ago
|
2022-05-21 20:14:47 +00:00
|
|
|
our fathers brought forth upon this continent
|
|
|
|
a new nation concieved in liberty and dedicated to
|
|
|
|
the proposition that all men are created equal."
|
2022-06-04 17:02:04 +00:00
|
|
|
:tags []}
|
|
|
|
timestamp (format-time (event :created-at))
|
2022-07-05 13:59:45 +00:00
|
|
|
header (format-header event)]
|
2022-06-04 17:02:04 +00:00
|
|
|
(should= (str " 11111111111111111... " timestamp " Four score and seven years ago~our fathers brought forth upon this continent~...\n") header)))
|
2022-05-21 20:14:47 +00:00
|
|
|
|
|
|
|
(it "formats a message with a subject"
|
2022-07-05 13:59:45 +00:00
|
|
|
(let [profiles {}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:pubkey 16r1111111111111111111111111111111111111111111111111111111111111111
|
|
|
|
:created-at 1
|
|
|
|
:content "the message"
|
|
|
|
:tags [[:subject "the subject"]]}
|
|
|
|
timestamp (format-time (event :created-at))
|
2022-07-05 13:59:45 +00:00
|
|
|
header (format-header event)]
|
2022-06-04 17:02:04 +00:00
|
|
|
(should= (str " 11111111111111111... " timestamp " the subject|the message\n") header)))
|
2022-05-21 20:14:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
(describe "subject and discussion tags"
|
|
|
|
(context "get-subject"
|
|
|
|
(it "returns null if no tags"
|
|
|
|
(let [tags []
|
|
|
|
subject (get-subject tags)]
|
|
|
|
(should= nil subject)))
|
|
|
|
|
|
|
|
(it "returns null if no subject tag"
|
2022-06-04 17:02:04 +00:00
|
|
|
(let [tags [[:p "hi"]]
|
|
|
|
subject (get-subject tags)]
|
|
|
|
(should= nil subject)))
|
2022-05-21 20:14:47 +00:00
|
|
|
|
|
|
|
(it "returns subject if found"
|
2022-06-04 17:02:04 +00:00
|
|
|
(let [tags [[:p "hi"] [:subject "the subject"]]
|
|
|
|
subject (get-subject tags)]
|
|
|
|
(should= "the subject" subject)))
|
2022-05-21 20:14:47 +00:00
|
|
|
))
|
|
|
|
|
2022-06-04 17:02:04 +00:00
|
|
|
(describe "Replacing References"
|
|
|
|
(context "using #[n] and p tags"
|
|
|
|
(it "replaces nothing if nothing to replace"
|
2022-07-05 14:22:15 +00:00
|
|
|
(let [profiles {0 {:name "x"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
content "content"
|
|
|
|
event {:content content}]
|
|
|
|
(should= "content" (replace-references event))))
|
|
|
|
|
|
|
|
(it "replaces a single p reference"
|
|
|
|
(let [content "the #[0] reference"
|
2022-07-05 14:11:34 +00:00
|
|
|
profiles {0 {:name "x"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:content content :tags [[:p (hexify 0)]]}]
|
|
|
|
(should= "the @x reference" (replace-references event))))
|
|
|
|
|
|
|
|
(it "replaces a single p reference at the start"
|
|
|
|
(let [content "#[0] reference"
|
2022-07-05 14:11:34 +00:00
|
|
|
profiles {0 {:name "x"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:content content :tags [[:p (hexify 0)]]}]
|
|
|
|
(should= "@x reference" (replace-references event))))
|
|
|
|
|
|
|
|
(it "replaces a single p reference at the end"
|
|
|
|
(let [content "the #[0]"
|
2022-07-05 14:11:34 +00:00
|
|
|
profiles {0 {:name "x"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:content content :tags [[:p (hexify 0)]]}]
|
|
|
|
(should= "the @x" (replace-references event))))
|
|
|
|
|
|
|
|
(it "replaces a single p reference alone"
|
|
|
|
(let [content "#[0]"
|
2022-07-05 14:11:34 +00:00
|
|
|
profiles {0 {:name "x"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:content content :tags [[:p (hexify 0)]]}]
|
|
|
|
(should= "@x" (replace-references event))))
|
|
|
|
|
|
|
|
(it "replaces a two p references"
|
|
|
|
(let [content "the #[0] and #[1] reference"
|
2022-07-05 14:11:34 +00:00
|
|
|
profiles {0 {:name "x"}
|
2022-07-05 14:22:15 +00:00
|
|
|
1 {:name "y"}}
|
2022-07-05 14:11:34 +00:00
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:content content :tags [[:p (hexify 0)]
|
|
|
|
[:p (hexify 1)]]}]
|
|
|
|
(should= "the @x and @y reference" (replace-references event))))
|
|
|
|
|
|
|
|
(it "Replaces a p reference with an abbreviated id if not a nickname"
|
|
|
|
(let [content "#[0]"
|
2022-07-05 14:22:15 +00:00
|
|
|
profiles {0 {:name "x"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:content content :tags [[:p "deadbeef"]]}]
|
|
|
|
(should= "@id:deadbeef" (replace-references event))))
|
|
|
|
|
|
|
|
(it "does not replace reference if there is no p tag"
|
|
|
|
(let [content "#[1]"
|
2022-07-05 14:22:15 +00:00
|
|
|
profiles {0 {:name "x"}}
|
|
|
|
event-context (atom {:profiles profiles})
|
2022-06-04 17:02:04 +00:00
|
|
|
_ (reset! ui-context {:event-context event-context})
|
|
|
|
event {:content content :tags [[:p "deadbeef"]]}]
|
2022-06-05 19:10:45 +00:00
|
|
|
(should= "#[1]" (replace-references event))))))
|
|
|
|
|
2022-07-05 13:59:45 +00:00
|
|
|
(describe "format-reply"
|
|
|
|
(it "formats a reply to an event"
|
|
|
|
(let [profiles {1 {:name "user-1"}
|
|
|
|
2 {:name "user-2"}}
|
2022-07-05 14:22:15 +00:00
|
|
|
_ (reset! ui-context {:event-context (atom {:profiles profiles})})
|
2022-07-05 13:59:45 +00:00
|
|
|
created-at (make-date "07/05/2022")
|
|
|
|
relays ["relay-1"]
|
|
|
|
tags [["p" (hexify 1)]]
|
|
|
|
event {:pubkey 1 :created-at created-at :relays relays :tags tags :content "Hello #[0]."}]
|
|
|
|
(should=
|
|
|
|
">From: user-1 at 07/05/22 24:00:00 on relay-1\n>---------------\n>Hello @user-1."
|
|
|
|
(format-reply event)))))
|
|
|
|
|
2022-06-16 20:12:59 +00:00
|
|
|
|