bjomarks: bookmarks, their store, search and import
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
9f7718ad8f
25 changed files with 6658 additions and 0 deletions
135
tests/duplicates.bjo
Normal file
135
tests/duplicates.bjo
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
;; 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/duplicates.bjo — (bjomarks duplicates): two devices that
|
||||
;; add one page before they sync, in folders of the temp directory.
|
||||
;;
|
||||
;; bjo run tests/duplicates.bjo
|
||||
|
||||
(import (std prelude))
|
||||
(import (std test))
|
||||
(import (std os))
|
||||
(import (std datetime))
|
||||
(import (std uuid))
|
||||
(import (bjomarks bookmark))
|
||||
(import (bjomarks keys))
|
||||
(import (bjomarks store))
|
||||
(import (bjomarks duplicates))
|
||||
|
||||
(def cheap (KeyCost (iterations 1) (memory-kib 8)))
|
||||
|
||||
(: (-> string Instant))
|
||||
(defun (instant text)
|
||||
(match (iso8601->instant text)
|
||||
((Some i) i)
|
||||
(None (panic! (str "not an instant: " text)))))
|
||||
|
||||
(: (-> (Result StoreError Vault) Vault))
|
||||
(defun (must r)
|
||||
(match r
|
||||
((Ok v) v)
|
||||
((Err _) (panic! "no vault"))))
|
||||
|
||||
;; Two devices of one vault, in a new folder.
|
||||
(: (-> (Tuple string Vault Vault)))
|
||||
(defun (two-devices)
|
||||
(def root (make-temp-directory #:prefix "bjomarks-dup-"))
|
||||
(def shared (path-combine root "shared"))
|
||||
(def one (must (create-vault (folder-storage shared) (path-combine root "one") "pw" #:cost cheap)))
|
||||
(def two (match (join-vault (folder-storage shared) (path-combine root "two") "pw")
|
||||
((Ok (Tuple v _)) v)
|
||||
((Err _) (panic! "no join"))))
|
||||
(Tuple root one two))
|
||||
|
||||
(: (-> Vault string (Option string) (Vec string) string (Option string) Vault))
|
||||
(defun (add-page v url title tags when archive)
|
||||
(match (vault-add v url #:title title #:tags tags #:added (instant when) #:archive archive)
|
||||
((Ok (Tuple v2 _)) v2)
|
||||
((Err why) (panic! why))))
|
||||
|
||||
(: (-> Vault Vault))
|
||||
(defun (synced v) (fst (vault-sync v)))
|
||||
|
||||
;; Both devices see the bookmarks of both.
|
||||
(: (-> Vault Vault (Tuple Vault Vault)))
|
||||
(defun (exchange a b)
|
||||
(def a2 (synced a))
|
||||
(def b2 (synced b))
|
||||
(Tuple (synced a2) b2))
|
||||
|
||||
(: (-> Vault string (Vec Bookmark)))
|
||||
(defun (of-page v url) (vec-filter (fun (b) (= (.url b) url)) (vault-bookmarks v)))
|
||||
|
||||
(deftest "one page from two devices, with one title, becomes one bookmark"
|
||||
(def (Tuple root one0 two0) (two-devices))
|
||||
(def one1 (add-page one0 "https://example.org/a" (Some "A page") ["x" "web"] "2026-01-01T10:00:00Z" None))
|
||||
(def two1 (add-page two0 "https://example.org/a" None ["y" "web"] "2025-06-01T10:00:00Z"
|
||||
(Some "https://web.archive.org/a")))
|
||||
(def two2 (add-page two1 "https://example.org/b" (Some "B") [] "2026-02-01T10:00:00Z" None))
|
||||
(def (Tuple one2 two3) (exchange one1 two2))
|
||||
(check (= (vec-length (of-page one2 "https://example.org/a")) 2))
|
||||
(def (Tuple one3 asked) (vault-join-duplicates one2))
|
||||
(check (vec-empty? asked))
|
||||
(def pages (of-page one3 "https://example.org/a"))
|
||||
(check (= (vec-length pages) 1))
|
||||
(def b (vec-ref pages 0))
|
||||
(check (= (.title b) (Some "A page")))
|
||||
(check (= (.tags b) ["web" "x" "y"]))
|
||||
(check (= (instant->unix-milliseconds (.added b)) (instant->unix-milliseconds (instant "2025-06-01T10:00:00Z"))))
|
||||
(check (= (.archive b) (Some "https://web.archive.org/a")))
|
||||
(check (= (vec-length (vault-bookmarks one3)) 2))
|
||||
;; Joining again changes nothing.
|
||||
(def (Tuple one4 again) (vault-join-duplicates one3))
|
||||
(check (vec-empty? again))
|
||||
(check (not (vault-dirty? (vault-save one4))))
|
||||
(check (= (vault-revision (vault-save one4)) (vault-revision (vault-save one3))))
|
||||
;; The other device joins it too, the same way, and they agree.
|
||||
(def (Tuple two4 _) (vault-join-duplicates two3))
|
||||
(def (Tuple one5 two5) (exchange (vault-save one4) (vault-save two4)))
|
||||
(def a1 (of-page one5 "https://example.org/a"))
|
||||
(def a2 (of-page two5 "https://example.org/a"))
|
||||
(check (= (vec-length a1) 1))
|
||||
(check (= (vec-length a2) 1))
|
||||
(check (= (.id (vec-ref a1 0)) (.id (vec-ref a2 0))))
|
||||
(check (= (.tags (vec-ref a1 0)) (.tags (vec-ref a2 0))))
|
||||
(directory-delete-tree root))
|
||||
|
||||
(deftest "different titles are asked, and the one chosen is kept"
|
||||
(def (Tuple root one0 two0) (two-devices))
|
||||
(def one1 (add-page one0 "https://example.org/a" (Some "First") ["x"] "2026-01-01T10:00:00Z" None))
|
||||
(def two1 (add-page two0 "https://example.org/a" (Some "Second") ["y"] "2026-01-02T10:00:00Z" None))
|
||||
(def (Tuple one2 _) (exchange one1 two1))
|
||||
(def (Tuple one3 asked) (vault-join-duplicates one2))
|
||||
(check (= (vec-length asked) 1))
|
||||
(def d (vec-ref asked 0))
|
||||
(check (= (duplicate-url d) "https://example.org/a"))
|
||||
(check (= (duplicate-titles d) ["First" "Second"]))
|
||||
(check (= (vec-length (duplicate-ids d)) 2))
|
||||
;; Both stay, with the tags of both.
|
||||
(def pages (of-page one3 "https://example.org/a"))
|
||||
(check (= (vec-length pages) 2))
|
||||
(check (= (vec-length (vec-filter (fun (b) (= (.tags b) ["x" "y"])) pages)) 2))
|
||||
;; Asked again, until a title is chosen.
|
||||
(def (Tuple one4 again) (vault-join-duplicates one3))
|
||||
(check (= (vec-length again) 1))
|
||||
(def one5 (vault-choose-title one4 "https://example.org/a" (Some "Second")))
|
||||
(def kept (of-page one5 "https://example.org/a"))
|
||||
(check (= (vec-length kept) 1))
|
||||
(check (= (.title (vec-ref kept 0)) (Some "Second")))
|
||||
(check (= (.tags (vec-ref kept 0)) ["x" "y"]))
|
||||
(def (Tuple _ none) (vault-join-duplicates one5))
|
||||
(check (vec-empty? none))
|
||||
(directory-delete-tree root))
|
||||
|
||||
(deftest "a page with one bookmark is left alone"
|
||||
(def (Tuple root one0 _) (two-devices))
|
||||
(def one1 (vault-save (add-page one0 "https://example.org/a" (Some "A") [] "2026-01-01T10:00:00Z" None)))
|
||||
(def one2 (add-page one1 "https://example.org/a/" (Some "A") [] "2026-01-01T10:00:00Z" None))
|
||||
(def (Tuple one3 asked) (vault-join-duplicates one2))
|
||||
(check (vec-empty? asked))
|
||||
(check (= (vec-length (vault-bookmarks one3)) 2))
|
||||
(check (= (vault-choose-title one3 "https://example.org/a" (Some "B")) one3))
|
||||
(directory-delete-tree root))
|
||||
|
||||
(run-tests)
|
||||
Loading…
Add table
Add a link
Reference in a new issue