;; 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) ("\n