bjomarks/tests/draft.bjo

104 lines
4.3 KiB
Text
Raw Normal View History

;; 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/draft.bjo — (bjomarks draft): the dialog of a bookmark,
;; and the search.
;;
;; bjo run tests/draft.bjo
(import (std prelude))
(import (std test))
(import (std os))
(import (bjomarks bookmark))
(import (bjomarks keys))
(import (bjomarks store))
(import (bjomarks draft))
(def cheap (KeyCost (iterations 1) (memory-kib 8)))
;; A vault in a new folder, with the bookmarks of `urls`. Returns the vault
;; and the folder, which the test deletes.
(: (-> (Vec string) (Tuple Vault string)))
(defun (vault-with urls)
(def root (make-temp-directory #:prefix "bjomarks-draft-"))
(def v0 (match (create-vault (folder-storage (path-combine root "shared")) (path-combine root "one") "pw" #:cost cheap)
((Ok v) v)
((Err _) (panic! "no vault"))))
(Tuple (vec-fold (fun (url v)
(match (vault-add v url #:tags ["web"])
((Ok (Tuple v2 _)) v2)
((Err why) (panic! why))))
v0 urls)
root))
(: (-> Vault Draft Vault))
(defun (applied v d)
(match (apply-draft v d)
((Ok (Tuple v2 _)) v2)
((Err why) (panic! why))))
(deftest "a new bookmark"
(def (Tuple v root) (vault-with []))
(def d (record-set empty-draft (url " https://example.org/a ") (title "A") (tags "x, y")))
(def v2 (applied v d))
(def b (vec-ref (vault-bookmarks v2) 0))
(check (= (.url b) "https://example.org/a"))
(check (= (.title b) (Some "A")))
(check (= (.tags b) ["x" "y"]))
(check (= (.archive b) None))
(check-match (apply-draft v (record-set empty-draft (url "not an address"))) (Err _))
(directory-delete-tree root))
(deftest "typing an address that a bookmark has loads it, and another address brings back what was typed"
(def (Tuple v root) (vault-with ["https://example.org/a"]))
(def typed (record-set empty-draft (title "Mine") (tags "t")))
(def d1 (with-address v typed "https://example.org/a"))
(check (some? (.editing d1)))
(check (= (.problem d1) address-here))
(check (= (.tags d1) "web"))
(def d2 (with-address v d1 "https://example.org/b"))
(check (none? (.editing d2)))
(check (= (.title d2) "Mine"))
(check (= (.tags d2) "t"))
(check (= (.problem d2) ""))
;; A dialog opened to edit a bookmark does not load another.
(def edit (draft-of (vec-ref (vault-bookmarks v) 0)))
(check (= (.editing (with-address v edit "https://example.org/a")) (.editing edit)))
(directory-delete-tree root))
(deftest "an edit keeps the id, and checks the archive link"
(def (Tuple v root) (vault-with ["https://example.org/a"]))
(def b (vec-ref (vault-bookmarks v) 0))
(def edit (draft-of b))
(def d (record-set edit (title "Ay") (archive "https://web.archive.org/web/1/https://example.org/a")))
(def v2 (applied v d))
(def b2 (vec-ref (vault-bookmarks v2) 0))
(check (= (.id b2) (.id b)))
(check (= (.title b2) (Some "Ay")))
(check (some? (.archive b2)))
(check-match (apply-draft v (record-set edit (archive "no link"))) (Err _))
(directory-delete-tree root))
(deftest "fetches: started, and their results taken only when the draft waits for them"
(check (= (fetch-address (record-set empty-draft (url " example.org "))) (Some "https://example.org")))
(check (= (fetch-address empty-draft) None))
(check-match (start-title-fetch empty-draft 1) (Some (Err _)))
(def (Some (Ok (Tuple d url))) (start-title-fetch (record-set empty-draft (url "example.org")) 3)
:leave-with (check #f))
(check (= url "https://example.org"))
(check (= (.fetching d) (Some 3)))
(check (none? (start-title-fetch d 4)))
(check (= (.title (with-title-fetched d 3 (Ok (Ok "Example")))) "Example"))
(check (= (.title (with-title-fetched d 2 (Ok (Ok "Old")))) ""))
(check (= (.problem (with-title-fetched d 3 (Ok (Err "timed out")))) "No title: timed out."))
(def (Some (Ok (Tuple a _))) (start-archive-fetch d 5)
:leave-with (check #f))
(check (= (.archive (with-archive-fetched a 5 (Ok (Ok "https://web.archive.org/x")))) "https://web.archive.org/x"))
(check (= (.archive (with-archive-fetched a 6 (Ok (Ok "https://web.archive.org/y")))) "")))
(deftest "tags as typed"
(check (= (split-tags "a, b,,c d") ["a" "b" "c" "d"])))
(run-tests)