bjomarks/tests/bookmark.bjo
Linus Björnstam 9f7718ad8f bjomarks: bookmarks, their store, search and import
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 21:06:11 +01:00

412 lines
20 KiB
Text

;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;; bjomarks/tests/bookmark.bjo — (bjomarks bookmark): stamps, the JSON of
;; entries and documents, and the properties of the merge.
;;
;; bjo run tests/bookmark.bjo
(import (std prelude))
(import (std test))
(import (std test gen))
(import (std test prop))
(import (std datetime))
(import (std uuid))
(import (text json))
(import (bjomarks bookmark))
(: (-> string Uuid))
(defun (uuid text)
(match (string->uuid text)
((Some u) u)
(None (panic! (str "not a uuid: " text)))))
(: (-> string Instant))
(defun (at text)
(match (iso8601->instant text)
((Some i) i)
(None (panic! (str "not an instant: " text)))))
(def id-a (uuid "0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f"))
(def id-b (uuid "1c8b3e1f-7b4d-4e8f-a021-2b3c4d5e6f70"))
(def id-c (uuid "2d7c4f20-8c5e-4f90-b132-3c4d5e6f7081"))
(def phone (uuid "3f9c0d2a-1111-4222-8333-444455556666"))
(def laptop (uuid "77b2e40e-aaaa-4bbb-8ccc-ddddeeeeffff"))
(: (-> long int Uuid Stamp))
(defun (stamp-at ms counter device) (Stamp (ms ms) (counter counter) (device device)))
(def example-bookmark
(Bookmark (id id-a) (url "https://example.org/") (title (Some "Example Domain"))
(added (at "2026-11-11T09:12:00Z"))
(tags ["async" "rust"])
(archive (Some "https://web.archive.org/web/20261111091200/https://example.org/"))))
(def example-entry (live-entry (stamp-at 1762852320000L 2 phone) example-bookmark))
(: (-> string (Result string Entry)))
(defun (parse-entry text)
(match (parse-json text #:objects 'fields)
((Ok j) (json->entry j))
((Err why) (Err why))))
(: (-> (Result string Entry) string))
(defun (error-of r)
(match r
((Ok _) "Ok")
((Err why) why)))
;; 1762852320000 is 0x019a722fb300.
(def stamp-a-text "019a722fb300-0002-3f9c0d2a-1111-4222-8333-444455556666")
;; ---- Stamps -------------------------------------------------------------------
(deftest "a stamp's text has fixed widths"
(check (= (stamp->string (stamp-at 1762852320000L 2 phone)) stamp-a-text))
(check (= (stamp->string (stamp-at 0L 0 laptop)) "000000000000-0000-77b2e40e-aaaa-4bbb-8ccc-ddddeeeeffff")))
(deftest "a stamp's text reads back"
(check (= (string->stamp stamp-a-text) (Some (stamp-at 1762852320000L 2 phone))))
(check (= (string->stamp "019A722FB300-0002-3f9c0d2a-1111-4222-8333-444455556666") None))
(check (= (string->stamp "019a722fb300-0002-3F9C0D2A-1111-4222-8333-444455556666") None))
(check (= (string->stamp "019a722fb300_0002-3f9c0d2a-1111-4222-8333-444455556666") None))
(check (= (string->stamp "19a722fb300-0002-3f9c0d2a-1111-4222-8333-444455556666") None))
(check (= (string->stamp "") None)))
(def gen-stamp
(gen-let ((ms (gen-long-range 0L 281474976710655L))
(counter (gen-int-range 0 65535))
(device (gen-element (list phone laptop))))
(stamp-at ms counter device)))
(deftest "the order of stamp texts is the order of stamps"
(for-all ((a gen-stamp) (b gen-stamp))
(check (= (string->stamp (stamp->string a)) (Some a)))
(def by-text (compare (stamp->string a) (stamp->string b)))
(def by-stamp (stamp-compare a b))
(check (= (compare by-text 0) (compare by-stamp 0)))))
(deftest "the clock uses the wall clock when it is ahead"
(def (Tuple c s) (clock-next clock-zero 1000L phone))
(check (= s (stamp-at 1000L 0 phone)))
(check (= (.last-ms c) 1000L)))
(deftest "the clock counts while the wall clock is behind"
(def seen (clock-observe clock-zero (stamp-at 5000L 7 laptop)))
(def (Tuple c s) (clock-next seen 1000L phone))
(check (= s (stamp-at 5000L 8 phone)))
(def (Tuple _c2 s2) (clock-next c 1000L phone))
(check (= s2 (stamp-at 5000L 9 phone)))
(check (> (stamp-compare s2 s) 0)))
(deftest "the clock moves a millisecond on when the counter is full"
(def full (clock-observe clock-zero (stamp-at 5000L 65535 laptop)))
(def (Tuple _c s) (clock-next full 5000L phone))
(check (= s (stamp-at 5001L 0 phone))))
(deftest "a new stamp is later than all that the clock saw"
(for-all ((seen gen-stamp) (wall (gen-long-range 0L 281474976710655L)))
(def (Tuple _c s) (clock-next (clock-observe clock-zero seen) wall phone))
(check (> (compare (Tuple (.ms s) (.counter s)) (Tuple (.ms seen) (.counter seen))) 0))))
;; ---- Bookmarks ----------------------------------------------------------------
(deftest "make-bookmark checks and cleans"
(def (Ok b) (make-bookmark " https://example.org/a " #:tags ["b" " a" "" "b"]
#:added (at "2026-11-11T09:12:00Z"))
:leave-with (check-fail "make-bookmark refused a good address"))
(check (= (.url b) "https://example.org/a"))
(check (= (.tags b) ["a" "b"]))
(check (= (.archive b) None))
(check (= (.added b) (at "2026-11-11T09:12:00Z")))
(check-match (make-bookmark "example.org") (Err _))
(check-match (make-bookmark "http://exa mple.org") (Err _))
(check-match (make-bookmark "https://example.org" #:archive (Some "web.archive.org/x")) (Err _))
(check-match (make-bookmark "https://example.org"
#:archive (Some "https://web.archive.org/web/2026/https://example.org"))
(Ok _))
(check (not (= (.id b) (match (make-bookmark "https://example.org/a") ((Ok b2) (.id b2)) ((Err _) (.id b)))))))
(deftest "changing tags and the archive link"
(check (= (.tags (bookmark-with-tags example-bookmark ["z" "a" "z"])) ["a" "z"]))
(check-match (bookmark-with-archive example-bookmark (Some "nonsense")) (Err _))
(check (= (match (bookmark-with-archive example-bookmark None) ((Ok b) (.archive b)) ((Err _) (Some "x")))
None)))
;; `n` copies of `c`.
(: (-> char int string))
(defun (repeat c n) (string-pad-left "" n #:with c))
(deftest "a title is one line, trimmed, and not empty"
#:cases ((text wanted)
("Example Domain" (Some "Example Domain"))
(" Example \n\t Domain \r\n" (Some "Example Domain"))
;; A no-break space.
((list->string (list #\R #\x00A0 #\s)) (Some "R s"))
("" None)
(" \n\t " None))
(check (= (clean-title text) wanted)))
(deftest "a long title is cut at 300 characters"
(check (= (clean-title (repeat #\a 400)) (Some (repeat #\a 300))))
;; Characters, not UTF-16 units: each of these is two units.
(def (Some cut) (clean-title (repeat #\x1F600 301)) :leave-with (check-fail "no title"))
(check (= (string-count cut) 300))
(check (= cut (repeat #\x1F600 300)))
;; The space at the cut is trimmed.
(check (= (clean-title (str (repeat #\a 299) " b")) (Some (repeat #\a 299)))))
(deftest "make-bookmark and bookmark-with-title clean the title"
(def (Ok b) (make-bookmark "https://example.org/" #:title (Some " Example\nDomain "))
:leave-with (check-fail "make-bookmark refused a good address"))
(check (= (.title b) (Some "Example Domain")))
(def (Ok untitled) (make-bookmark "https://example.org/") :leave-with (check-fail "refused"))
(check (= (.title untitled) None))
(def (Ok blank) (make-bookmark "https://example.org/" #:title (Some " ")) :leave-with (check-fail "refused"))
(check (= (.title blank) None))
(check (= (.title (bookmark-with-title example-bookmark (Some "\tNew title"))) (Some "New title")))
(check (= (.title (bookmark-with-title example-bookmark (Some ""))) None))
(check (= (.title (bookmark-with-title example-bookmark None)) None)))
;; ---- JSON of an entry ------------------------------------------------------------
(def example-json
(str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\","
"\"stamp\":\"" stamp-a-text "\","
"\"url\":\"https://example.org/\","
"\"title\":\"Example Domain\","
"\"added\":\"2026-11-11T09:12:00Z\","
"\"tags\":[\"async\",\"rust\"],"
"\"archive\":\"https://web.archive.org/web/20261111091200/https://example.org/\"}"))
(deftest "an entry as JSON"
(check (= (json->string (entry->json example-entry)) example-json)))
(deftest "an entry read from JSON"
(check (= (parse-entry example-json) (Ok example-entry))))
(deftest "a deleted entry"
(def gone (deleted-entry (stamp-at 1762852320000L 2 phone) id-a))
(def text (json->string (entry->json gone)))
(check (= text (str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"" stamp-a-text
"\",\"deleted\":true}")))
(check (= (parse-entry text) (Ok gone)))
;; The fields of a deleted bookmark are not read.
(check (= (parse-entry (str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"" stamp-a-text
"\",\"deleted\":true,\"url\":3}"))
(Ok gone))))
(deftest "tags and the archive link can be left out"
(def r (parse-entry (str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"" stamp-a-text
"\",\"url\":\"https://example.org/\",\"added\":\"2026-11-11T10:12:00+01:00\","
"\"archive\":null,\"deleted\":false}")))
(def (Ok e) r :leave-with (check-fail (error-of r)))
(def (Live b) (.state e) :leave-with (check-fail "not live"))
(check (= (.tags b) []))
(check (= (.archive b) None))
(check (= (.added b) (at "2026-11-11T09:12:00Z"))))
(deftest "tags are read as a set"
(def r (parse-entry (str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"" stamp-a-text
"\",\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\","
"\"tags\":[\"rust\",\"async\",\"rust\"]}")))
(def (Ok e) r :leave-with (check-fail (error-of r)))
(def (Live b) (.state e) :leave-with (check-fail "not live"))
(check (= (.tags b) ["async" "rust"])))
(deftest "fields from a newer version are kept, in order, after the known ones"
(def text (str "{\"rating\":4,\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\","
"\"stamp\":\"" stamp-a-text "\",\"url\":\"https://example.org/\","
"\"added\":\"2026-11-11T09:12:00Z\",\"x-visits\":[1,2],\"note\":{\"text\":\"hi\"}}"))
(def r (parse-entry text))
(def (Ok e) r :leave-with (check-fail (error-of r)))
(check (= (vec-map fst (.extra e)) ["rating" "x-visits" "note"]))
(check (= (json->string (entry->json e))
(str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"" stamp-a-text "\","
"\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\",\"tags\":[],"
"\"rating\":4,\"x-visits\":[1,2],\"note\":{\"text\":\"hi\"}}"))))
(deftest "a title can be left out, or null, and is read cleaned"
(: (-> string (Option string)))
(defun (title-of field)
(match (parse-entry (str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"" stamp-a-text
"\",\"url\":\"https://example.org/\"," field "\"added\":\"2026-11-11T09:12:00Z\"}"))
((Ok e) (match (.state e) ((Live b) (.title b)) ((Deleted _) (Some "deleted"))))
((Err why) (Some (str "Err: " why)))))
(check (= (title-of "") None))
(check (= (title-of "\"title\":null,") None))
(check (= (title-of "\"title\":\" \",") None))
(check (= (title-of "\"title\":\" Example\\n Domain \",") (Some "Example Domain"))))
(deftest "a bookmark with no title writes no title field"
(def untitled (live-entry (stamp-at 1762852320000L 2 phone) (bookmark-with-title example-bookmark None)))
(def text (json->string (entry->json untitled)))
(check (not (string-contains? text "title")))
(check (= (parse-entry text) (Ok untitled))))
(: (-> string string))
(defun (with-field field)
(str "{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"" stamp-a-text "\"," field "}"))
(deftest "what an entry must not be"
#:cases ((text wanted)
("[1]" "expected an object")
("{\"stamp\":\"x\"}" "id: missing")
("{\"id\":7}" "id: expected a string")
("{\"id\":\"nope\"}" "id: not a uuid: nope")
("{\"id\":\"0b9a2f0e-6a3c-4d7e-9f10-1a2b3c4d5e6f\",\"stamp\":\"soon\"}" "stamp: not a stamp: soon")
((with-field "\"added\":\"2026-11-11T09:12:00Z\"") "url: missing")
((with-field "\"url\":\"example.org\",\"added\":\"2026-11-11T09:12:00Z\"")
"url: not an absolute URL: example.org")
((with-field "\"url\":\"https://example.org/\"") "added: missing")
((with-field "\"url\":\"https://example.org/\",\"added\":\"2026-11-11\"")
"added: not an ISO 8601 date and time with an offset: 2026-11-11")
((with-field "\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\",\"tags\":\"rust\"")
"tags: expected an array of strings")
((with-field "\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\",\"tags\":[\"a\",2]")
"tags[1]: expected a string")
((with-field "\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\",\"tags\":[\"\"]")
"tags[0]: a tag cannot be empty")
((with-field "\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\",\"archive\":1")
"archive: expected a string or null")
((with-field "\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\",\"archive\":\"x\"")
"archive: not an absolute URL: x")
((with-field "\"url\":\"https://example.org/\",\"added\":\"2026-11-11T09:12:00Z\",\"title\":[\"a\"]")
"title: expected a string or null")
((with-field "\"deleted\":\"yes\"") "deleted: expected true or false"))
(check (= (error-of (parse-entry text)) wanted)))
;; ---- The document of a device -------------------------------------------------------
(def example-doc
(DeviceDoc (device phone)
(seen (map-set (map-empty) (uuid->string laptop) 41L))
(entries [example-entry
(deleted-entry (stamp-at 1762852321000L 0 laptop) id-b)])))
(deftest "a document reads back"
(check (= (parse-device-doc (device-doc->string example-doc)) (Ok example-doc))))
(deftest "a document's records are in the order of their ids"
(def reversed (record-set example-doc (entries (vec-reverse (.entries example-doc)))))
(check (= (device-doc->string reversed) (device-doc->string example-doc))))
(deftest "a document as text"
(check (= (device-doc->string (DeviceDoc (device phone) (seen (map-empty)) (entries [])))
"{\"format\":1,\"device\":\"3f9c0d2a-1111-4222-8333-444455556666\",\"seen\":{},\"records\":[]}")))
(: (-> (Result string DeviceDoc) string))
(defun (doc-error r)
(match r
((Ok _) "Ok")
((Err why) why)))
(def doc-head "{\"format\":1,\"device\":\"3f9c0d2a-1111-4222-8333-444455556666\",")
(deftest "what a document must not be"
#:cases ((text wanted)
;; The message that the docs of parse-json give for this text.
("[1, 2,]" "line 1, column 7: ']' does not begin a value")
("[]" "expected an object")
("{\"device\":\"x\"}" "format: missing")
("{\"format\":2,\"records\":[]}" "format 2 is newer than this version reads (1)")
("{\"format\":0}" "format 0 is not a format")
("{\"format\":1,\"records\":[]}" "device: missing")
((str doc-head "\"seen\":{}}") "records: missing")
((str doc-head "\"records\":{}}") "records: expected an array")
((str doc-head "\"seen\":[],\"records\":[]}") "seen: expected an object")
((str doc-head "\"seen\":{\"x\":1},\"records\":[]}") "seen: not a uuid: x")
((str doc-head "\"seen\":{\"77b2e40e-aaaa-4bbb-8ccc-ddddeeeeffff\":-1},\"records\":[]}")
"seen.77b2e40e-aaaa-4bbb-8ccc-ddddeeeeffff: a revision cannot be negative")
((str doc-head "\"records\":[" example-json ",{\"id\":3}]}") "records[1]: id: expected a string"))
(check (= (doc-error (parse-device-doc text)) wanted)))
(deftest "the fields a document does not know are left out"
(check (= (parse-device-doc (str doc-head "\"records\":[],\"later\":true}"))
(Ok (DeviceDoc (device phone) (seen (map-empty)) (entries []))))))
;; ---- Merging --------------------------------------------------------------------
(deftest "the later stamp wins"
(def older (live-entry (stamp-at 1000L 0 phone) example-bookmark))
(def newer (live-entry (stamp-at 1000L 1 laptop) (bookmark-with-tags example-bookmark ["new"])))
(check (= (map-try-ref (entries->map [older newer]) (uuid->string id-a)) (Some newer)))
(check (= (map-try-ref (entries->map [newer older]) (uuid->string id-a)) (Some newer))))
(deftest "a delete wins over an older change, and a newer change wins over a delete"
(def changed (live-entry (stamp-at 1000L 0 phone) example-bookmark))
(def gone (deleted-entry (stamp-at 2000L 0 laptop) id-a))
(def again (live-entry (stamp-at 3000L 0 phone) example-bookmark))
(check (= (live-bookmarks (entries->map [changed gone])) []))
(check (= (live-bookmarks (entries->map [gone changed])) []))
(check (= (live-bookmarks (entries->map [gone again changed])) [example-bookmark])))
(deftest "the bookmarks, the newest first"
(def b1 (record-set example-bookmark (id id-b) (added (at "2026-01-01T00:00:00Z"))))
(def b2 (record-set example-bookmark (id id-c) (added (at "2026-06-01T00:00:00Z"))))
(def es [(live-entry (stamp-at 1L 0 phone) b1) (live-entry (stamp-at 2L 0 phone) example-bookmark)
(live-entry (stamp-at 3L 0 phone) b2)])
(check (= (vec-map #(.id &) (live-bookmarks (entries->map es))) [id-a id-c id-b])))
(deftest "the clock of the entries is their latest stamp"
(def es (entries->map [(live-entry (stamp-at 5L 3 phone) example-bookmark)
(deleted-entry (stamp-at 9L 1 laptop) id-b)]))
(check (= (entries-clock es) (Clock (last-ms 9L) (last-counter 1)))))
;; Few ids, devices and times, so that the generated entries collide often:
;; the same id, the same time, and now and then the same stamp.
(def gen-entry
(gen-let ((id (gen-element (list id-a id-b id-c)))
(ms (gen-long-range 0L 3L))
(counter (gen-int-range 0 2))
(device (gen-element (list phone laptop)))
(deleted gen-bool)
(tags (gen-element (list [] ["a"] ["a" "b"]))))
(def s (stamp-at ms counter device))
(if deleted
(deleted-entry s id)
(live-entry s (record-set example-bookmark (id id) (tags tags))))))
(def gen-entries (gen-vec gen-entry))
(deftest "merging is commutative"
(for-all ((xs gen-entries) (ys gen-entries))
(def a (entries->map xs))
(def b (entries->map ys))
(check (= (merge-maps a b) (merge-maps b a)))))
(deftest "merging is associative"
(for-all ((xs gen-entries) (ys gen-entries) (zs gen-entries))
(def a (entries->map xs))
(def b (entries->map ys))
(def c (entries->map zs))
(check (= (merge-maps (merge-maps a b) c) (merge-maps a (merge-maps b c))))))
(deftest "merging twice changes nothing"
(for-all ((xs gen-entries) (ys gen-entries))
(def a (merge-maps (entries->map xs) (entries->map ys)))
(check (= (merge-maps a a) a))
(check (= (merge-maps a (entries->map ys)) a))))
(deftest "the order of the entries does not matter"
(for-all ((xs gen-entries))
(check (= (entries->map xs) (entries->map (vec-reverse xs))))))
(deftest "each id keeps its latest stamp, so a delete is never undone by an older change"
(for-all ((xs gen-entries))
(def merged (entries->map xs))
(loop (:for e xs)
(:do (match (map-try-ref merged (uuid->string (entry-id e)))
((Some kept) (check (>= (stamp-compare (.stamp kept) (.stamp e)) 0)))
(None (check-fail "an id was lost")))))))
(deftest "a merged document reads back as the same entries"
(for-all ((xs gen-entries))
(def entries (map-fold (fun (k e acc) (vec-add acc e)) [] (entries->map xs)))
(def doc (DeviceDoc (device phone) (seen (map-empty)) (entries entries)))
(match (parse-device-doc (device-doc->string doc))
((Ok back) (check (= (entries->map (.entries back)) (entries->map entries))))
((Err why) (check-fail why)))))
(run-tests)