bjomarks/tests/import.bjo

182 lines
9.4 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/import.bjo — (bjomarks import): Firefox's JSON backup, and
;; an import into a vault.
;;
;; bjo run tests/import.bjo
;;
;; The backup here is made up, in the shape Firefox writes: the fields of
;; each node, the four top folders with a "root" field, and dates in
;; microseconds.
(import (std prelude))
(import (std test))
(import (std datetime))
(import (std os))
(import (bjomarks bookmark))
(import (bjomarks keys))
(import (bjomarks store))
(import (bjomarks import))
(: (-> long Instant))
(defun (at-seconds s)
(match (unix-microseconds->instant (* s 1000000L))
((Some i) i)
(None (panic! "no instant"))))
;; The instant given to a bookmark with no date.
(def fallback (at-seconds 1800000000L))
(def backup
"""
{"guid":"root________","title":"","index":0,"dateAdded":1500000000000000,"lastModified":1500000000000000,
"id":1,"typeCode":2,"type":"text/x-moz-place-container","root":"placesRoot","children":[
{"guid":"menu________","title":"menu","index":0,"dateAdded":1500000000000000,"lastModified":1500000000000000,
"id":2,"typeCode":2,"type":"text/x-moz-place-container","root":"bookmarksMenuFolder","children":[
{"guid":"folderA00001","title":" Rust Docs ","index":0,"dateAdded":1700000000000000,"lastModified":1700000000000000,
"id":7,"typeCode":2,"type":"text/x-moz-place-container","children":[
{"guid":"bookmark0001","title":"The Book","index":0,"dateAdded":1700000100000000,"lastModified":1700000100000000,
"id":8,"typeCode":1,"type":"text/x-moz-place","uri":"https://doc.rust-lang.org/book/","tags":"Rust,Must Read"},
{"guid":"folderB00001","title":"Async, Await","index":1,"dateAdded":1700000000000000,"lastModified":1700000000000000,
"id":9,"typeCode":2,"type":"text/x-moz-place-container","children":[
{"guid":"bookmark0002","title":"Async book","index":0,"dateAdded":1700000200000000,
"lastModified":1700000200000000,"id":10,"typeCode":1,"type":"text/x-moz-place",
"uri":"https://rust-lang.github.io/async-book/"}]}]},
{"guid":"separator001","title":"","index":1,"dateAdded":1700000000000000,"lastModified":1700000000000000,
"id":11,"typeCode":3,"type":"text/x-moz-place-separator"},
{"guid":"bookmark0003","title":"Most Visited","index":2,"dateAdded":1700000000000000,
"lastModified":1700000000000000,"id":12,"typeCode":1,"type":"text/x-moz-place",
"uri":"place:sort=8&maxResults=10"},
{"guid":"bookmark0004","title":"A bookmarklet","index":3,"dateAdded":1700000000000000,
"lastModified":1700000000000000,"id":13,"typeCode":1,"type":"text/x-moz-place",
"uri":"javascript:alert(1)","keyword":"js"},
{"guid":"bookmark0005","title":"","index":4,"dateAdded":1700000300000000,"lastModified":1700000300000000,
"id":14,"typeCode":1,"type":"text/x-moz-place","uri":" HTTPS://Example.org/Upper "}]},
{"guid":"toolbar_____","title":"toolbar","index":1,"dateAdded":1500000000000000,"lastModified":1500000000000000,
"id":3,"typeCode":2,"type":"text/x-moz-place-container","root":"toolbarFolder","children":[
{"guid":"bookmark0006","title":"The Rust book, again","index":0,"dateAdded":1600000000000000,
"lastModified":1600000000000000,"id":15,"typeCode":1,"type":"text/x-moz-place",
"uri":"https://doc.rust-lang.org/book/","tags":"reading"}]},
{"guid":"unfiled_____","title":"unfiled","index":3,"dateAdded":1500000000000000,"lastModified":1500000000000000,
"id":5,"typeCode":2,"type":"text/x-moz-place-container","root":"unfiledBookmarksFolder","children":[
{"guid":"bookmark0007","title":"No date","index":0,"id":16,"typeCode":1,"type":"text/x-moz-place",
"uri":"http://example.com/"},
{"guid":"bookmark0008","title":"Two hashes","index":1,"dateAdded":1700000400000000,"id":17,"typeCode":1,
"type":"text/x-moz-place","uri":"https://example.org/app#/page#part"}]},
{"guid":"mobile______","title":"mobile","index":4,"dateAdded":1500000000000000,"lastModified":1500000000000000,
"id":6,"typeCode":2,"type":"text/x-moz-place-container","root":"mobileFolder","children":[]}]}
""")
(: (-> FirefoxBackup))
(defun (read-backup)
(match (read-firefox-backup backup #:now fallback)
((Ok b) b)
((Err why) (panic! why))))
(: (-> string Imported))
(defun (imported url)
(def found (vec-filter (fun (i) (= (.url i) url)) (.bookmarks (read-backup))))
(if (vec-empty? found) (panic! (str "not imported: " url)) (vec-ref found 0)))
(: (-> (Result string FirefoxBackup) string))
(defun (error-of r)
(match r
((Ok _) "Ok")
((Err why) why)))
;; ---- Tag names -----------------------------------------------------------------------
(deftest "a tag's name"
#:cases ((text wanted)
("Rust" (Some "rust"))
(" Rust Docs " (Some "rust-docs"))
("Async, Await" (Some "async-await"))
("a\tb\nc" (Some "a-b-c"))
(",lead,and,trail," (Some "lead-and-trail"))
(" , " None)
("" None))
(check (= (tag-name text) wanted)))
;; ---- Reading a backup ----------------------------------------------------------------
(deftest "the bookmarks of a backup, one for each address, in the order found"
(check (= (vec-map #(.url &) (.bookmarks (read-backup)))
["https://doc.rust-lang.org/book/" "https://rust-lang.github.io/async-book/"
"HTTPS://Example.org/Upper" "http://example.com/"])))
(deftest "addresses that are not http or https are counted and left out"
(check (= (.not-web (read-backup)) 2)))
(deftest "an http address that is not a URL is left out, and named with the reason"
(def invalid (.invalid (read-backup)))
(check (= (vec-map fst invalid) ["https://example.org/app#/page#part"]))
(check (vec-fold (fun (p ok) (and ok (string-contains? (snd p) "'#' is not allowed in the fragment"))) #t invalid)))
(deftest "folders of the user's are tags, at any depth; Firefox's top folders are not"
(check (= (.tags (imported "https://rust-lang.github.io/async-book/")) ["async-await" "rust-docs"]))
(check (= (.tags (imported "http://example.com/")) [])))
(deftest "Firefox's tags are tags, named as folders are"
(check (vec-contains? (.tags (imported "https://doc.rust-lang.org/book/")) "must-read")))
(deftest "an address bookmarked twice is one bookmark"
(def book (imported "https://doc.rust-lang.org/book/"))
(check (= (.copies (read-backup)) 1))
(check (= (.tags book) ["must-read" "reading" "rust" "rust-docs"]))
(check (= (.title book) (Some "The Book")))
(check (= (.added book) (at-seconds 1600000000L))))
(deftest "the date is Firefox's, in microseconds, and a bookmark with none gets now"
(check (= (.added (imported "https://rust-lang.github.io/async-book/")) (at-seconds 1700000200L)))
(check (= (.added (imported "http://example.com/")) fallback)))
(deftest "an empty title is no title"
(check (= (.title (imported "HTTPS://Example.org/Upper")) None))
(check (= (.title (imported "https://rust-lang.github.io/async-book/")) (Some "Async book"))))
(deftest "what is not a backup"
#:cases ((text wanted)
("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<TITLE>Bookmarks</TITLE>"
(str "this is Firefox's HTML export, and bjomarks reads the JSON backup: in Firefox's "
"Library window, choose Import and Backup, then Backup…"))
("[1, 2]" "not a Firefox bookmark backup: it is not an object")
("{\"a\": 1}" "not a Firefox bookmark backup: it has no top folder"))
(check (= (error-of (read-firefox-backup text)) wanted)))
(deftest "text that is not JSON"
(check (string-starts-with? (error-of (read-firefox-backup "{\"type\": ")) "not JSON: ")))
;; ---- Into a vault --------------------------------------------------------------------
(deftest "an import adds the addresses that the vault does not have, and changes no other"
(def root (make-temp-directory #:prefix "bjomarks-import-"))
(def made (match (create-vault (folder-storage (path-combine root "shared")) (path-combine root "local") "pw"
#:cost (KeyCost (iterations 1) (memory-kib 8)))
((Ok v) v)
((Err _) (panic! "the vault was not made"))))
(def (Ok (Tuple v0 mine)) (vault-add made "https://doc.rust-lang.org/book/" #:tags ["mine"])
:leave-with (check-fail "vault-add refused"))
(def (Tuple v1 report) (vault-import v0 (.bookmarks (read-backup))))
(check (= (.added report) 3))
(check (= (.already report) 1))
(check (= (.refused report) []))
(check (= (vec-length (vault-bookmarks v1)) 4))
(check (= (match (vault-find v1 (.id mine)) ((Some b) (.tags b)) (None [])) ["mine"]))
(def async (vec-filter (fun (b) (= (.url b) "https://rust-lang.github.io/async-book/")) (vault-bookmarks v1)))
(check (= (vec-map #(.tags &) async) [["async-await" "rust-docs"]]))
(check (= (vec-map #(.title &) async) [(Some "Async book")]))
(check (= (vec-map #(.added &) async) [(at-seconds 1700000200L)]))
;; The same backup again adds nothing.
(def (Tuple v2 again) (vault-import v1 (.bookmarks (read-backup))))
(check (= (.added again) 0))
(check (= (.already again) 4))
(check (= (vec-length (vault-bookmarks v2)) 4))
;; And it is saved as any change is.
(check (vault-dirty? v2))
(vault-close (fst (vault-sync v2)))
(directory-delete-tree root))
(run-tests)