;; 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/search.bjo — (bjomarks search), on a vault in the temp ;; directory. ;; ;; bjo run tests/search.bjo (import (std prelude)) (import (std test)) (import (std os)) (import (std datetime)) (import (bjomarks bookmark)) (import (bjomarks keys)) (import (bjomarks store)) (import (bjomarks search)) (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))))) (: (-> Vault string string (Vec string) string Vault)) (defun (add-page v url title tags when) (match (vault-add v url #:title (if (string-empty? title) None (Some title)) #:tags tags #:added (instant (str when "T12:00:00Z"))) ((Ok (Tuple v2 _)) v2) ((Err why) (panic! why)))) ;; Bookmarks, the newest last. (: (-> (Tuple string Vault))) (defun (example) (def root (make-temp-directory #:prefix "bjomarks-search-")) (def v0 (match (create-vault (folder-storage (path-combine root "shared")) (path-combine root "here") "pw" #:cost cheap) ((Ok v) v) ((Err _) (panic! "no vault")))) (def v (vec-fold (fun (p v) (def (Tuple url title tags when) p) (add-page v url title tags when)) v0 [(Tuple "https://doc.rust-lang.org/book/" "The Rust Programming Language" ["rust" "docs"] "2011-03-04") (Tuple "https://www.github.com/rust-lang/rust" "rust-lang/rust" ["code"] "2012-06-30") (Tuple "https://gist.github.com/x/1" "A gist about async" ["rust" "async"] "2012-07-01") (Tuple "https://example.se/kottbullar" "Köttbullar med gräddsås" ["mat"] "2013-01-15") (Tuple "https://example.fr/cafe" "Le café" ["café" "c#"] "2014-02-28") (Tuple "https://notgithub.io/" "" [] "2015-05-05")])) (Tuple root v)) (: (-> Vault string (Vec string))) (defun (found v text) (vec-map (fun (b) (.url b)) (search-bookmarks v text))) (def book "https://doc.rust-lang.org/book/") (def repo "https://www.github.com/rust-lang/rust") (def gist "https://gist.github.com/x/1") (def kott "https://example.se/kottbullar") (def cafe "https://example.fr/cafe") (def notgh "https://notgithub.io/") (deftest "parsing" (check (query-empty? (parse-query " t: tag: d: a: a:: - "))) (def q (parse-query "Rust -t:Docs \"two words\" a:2011:2012-06 a:2012-13")) (check (= (.words q) ["rust"])) (check (= (vec-length (.must q)) 3)) (check (= (vec-length (.must-not q)) 1)) (check (= (query-problem q) "Not a date: 2012-13")) (check (= (query-problem (parse-query "a:2012-02-30")) "Not a date: 2012-02-30")) (check (= (query-problem (parse-query "a:2012:2013-1")) "Not a date: 2012:2013-1")) (check (= (query-problem (parse-query "a:2012-02-29 a::2013 a:2011:")) "")) (check (= (fold-text "Café ÉTÉ") "cafe ete")) (check (= (fold-text "Åsa Ärlig Öl Æble Ø") "åsa ärlig öl æble ø"))) (deftest "typos" (check (= (typos "rust" "rust" 1) 0)) (check (= (typos "rsut" "rust" 1) 1)) (check (= (typos "rst" "rust" 1) 1)) (check (= (typos "programing" "programming" 2) 1)) (check (= (typos "abcdef" "uvwxyz" 2) 3))) (deftest "words" (def (Tuple root v) (example)) (check (= (vec-length (found v "")) 6)) ;; The whole word in a title first, then the newest. (check (= (found v "rust") [repo book gist])) (check (= (found v "RUST lang") [repo book])) (check (= (found v "prog") [book])) (check (= (found v "gramming") [book])) (check (= (found v "programing") [book])) (check (= (found v "rsut") [repo book gist])) (check (= (found v "rsu") [])) (check (= (found v "async") [gist])) (check (= (found v "cafe") [cafe])) (check (= (found v "CAFÉ") [cafe])) ;; ö is not o, but one typo away in a long enough word. (check (= (found v "köttbullar") [kott])) (check (= (found v "gräddsås") [kott])) ;; Two typos in a word of eight letters, but not in a quoted text. (check (= (found v "graddsas") [kott])) (check (= (found v "\"graddsas\"") [])) (check (= (found v "\"kottbullar\"") [kott])) (check (= (found v "\"köttbullar med\"") [kott])) (check (= (found v "\"med köttbullar\"") [])) (directory-delete-tree root)) (deftest "tags, domains and days" (def (Tuple root v) (example)) (check (= (found v "t:rust") [gist book])) (check (= (found v "tag:RUST") [gist book])) (check (= (found v "t:rus") [])) (check (= (found v "t:cafe") [cafe])) (check (= (found v "t:\"c#\"") [cafe])) (check (= (found v "t:rust -t:docs") [gist])) (check (= (found v "d:github") [notgh gist repo])) (check (= (found v "d:github.com") [gist repo])) (check (= (found v "d:www.github.com") [gist repo])) (check (= (found v "d:gist.github.com") [gist])) (check (= (found v "-d:github") [cafe kott book])) (check (= (found v "a:2012") [gist repo])) (check (= (found v "a:2012-06") [repo])) (check (= (found v "a:2012-06-30") [repo])) (check (= (found v "a:2011:2012-06") [repo book])) (check (= (found v "a:2013:") [notgh cafe kott])) (check (= (found v "a::2011-03-04") [book])) (check (= (found v "a:2014-02") [cafe])) (check (= (found v "-a:2011:2014") [notgh])) (check (= (found v "a:2012-13") [])) (check (= (found v "rust a:2012 d:github.com") [repo gist])) (check (= (found v "-rust") [notgh cafe kott])) (directory-delete-tree root)) (run-tests)