bjomarks-android: the Android client of bjomarks

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Linus Björnstam 2026-11-21 21:06:11 +01:00
commit bf114513a5
11 changed files with 2526 additions and 0 deletions

414
tests/app.bjo Normal file
View file

@ -0,0 +1,414 @@
;; 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-android/tests/app.bjo — the phone app without a phone.
;;
;; bjo run tests/app.bjo
;;
;; - The shared folder is a storage of functions over a map in memory.
;; - The test is the host: it posts shared texts, as the Android app's C#
;; does, and keeps the password as the phone's unlock does. No window is
;; opened.
(import (std prelude))
(import (std simpletest))
(import (std os))
(import (std uuid))
(import (bjogui view))
(import (except (bjogui app) Live))
(import (bjogui platform))
(import (bjomarks bookmark))
(import (bjomarks store))
(import (bjomarks import))
(import (bjomarks draft))
(import (bjomarks duplicates))
(import (bjomarks-android app))
(import (bjomarks-android settings))
(import/class
(ClrString (: System.String)))
(import/extern
(host-post (: Bjolang.Runtime.Host.Post (-> string string void)))
(host-offer (: Bjolang.Runtime.Host.SetHandler (-> string (-> ClrString bool) void)))
(sleep-ms (: System.Threading.Thread.Sleep (-> int void) #:blocking)))
(: (-> (Box (Map string (Array byte))) Storage))
(defun (memory-storage files)
(: (-> string (Option (Array byte))))
(defun (read name) (map-try-ref (box-ref files) name))
(: (-> string (Array byte) void))
(defun (write name bytes) (box-set! files (map-set (box-ref files) name bytes)))
(: (-> string (Vec string)))
(defun (list-names dir)
(def prefix (str dir "/"))
(vec-map (fun (k) (string-drop k (string-count prefix)))
(vec-filter (fun (k) (string-starts-with? k prefix)) (->vec (map-keys (box-ref files))))))
(function-storage "memory" read write list-names))
;; The phone's unlock, as the test plays it: what it keeps, its name, a
;; newline and the secret.
(: (Box string))
(def phone-kept (make-box ""))
(: phone-can (-> ClrString bool) #:sync)
(defun (phone-can s) #t)
(: phone-has (-> ClrString bool) #:sync)
(defun (phone-has name) (not (string-empty? (box-ref phone-kept))))
(: phone-keep (-> ClrString bool) #:sync)
(defun (phone-keep s) (box-set! phone-kept (.ToString s)) #t)
(: phone-forget (-> ClrString bool) #:sync)
(defun (phone-forget name) (box-set! phone-kept "") #t)
(: (-> Model (Vec string)))
(defun (urls m)
(match (.vault m)
((Some v) (vec-sort (vec-map (fun (b) (.url b)) (vault-bookmarks v))))
(None [])))
(: (-> Model (Option Draft)))
(defun (draft-in m) (.draft m))
;; A Firefox backup with two bookmarks in a folder "Reading", and one that
;; the vault has already.
(: string)
(def backup-json
"""
{"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":"Reading","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/"},
{"guid":"bookmark0002","title":"Old","index":1,"dateAdded":1700000100000000,"lastModified":1700000100000000,
"id":9,"typeCode":1,"type":"text/x-moz-place","uri":"https://example.org/b"}]}]}]}
""")
(defun (main)
;; shared-url, without an app.
(expect "an address after words" (shared-url "A page\nhttps://example.org/a b") (Some "https://example.org/a"))
(expect "http too" (shared-url "see http://example.org") (Some "http://example.org"))
(expect "no address" (shared-url "just words") None)
(def root (make-temp-directory #:prefix "bjomarks-android-test-"))
(def files (make-box (: (map-empty) (Map string (Array byte)))))
(def storage (memory-storage files))
;; Shared before the vault is open: it waits for it.
(start-headless!)
(host-post "share-text" "A page\nhttps://example.org/a")
(def app (start-app (initial-model (path-combine root "one") (path-combine root "settings") None
#:storage (Some storage) #:start CreateNew
;; No server listens there: a lookup fails at once.
#:wayback "http://127.0.0.1:9/wayback")
gui-update view (fun (c) unit)
#:windows windows #:subscriptions subscriptions))
;; Runs what the fibers posted until `done?` holds, for 30 seconds at most:
;; Argon2id is slow.
(: (-> (-> Model bool) bool))
(defun (wait-for done?)
(let go ((tries 0))
(app-run-pending! app)
(cond ((done? (app-model app)) #t)
((> tries 3000) #f)
(else (sleep-ms 10) (go (+ tries 1))))))
(defun (send msg)
(app-dispatch! app msg)
(app-run-pending! app))
(expect "the share waits"
(wait-for (fun (m) (some? (.shared m)))) #t)
(expect "a storage given is locked at once" (= (.phase (app-model app)) Locked) #t)
(expect "the label of the storage" (.folder-label (app-model app)) "memory")
;; The phone can keep the password, and the user chooses it.
(host-offer "device-secret-available" phone-can)
(host-offer "device-secret-stored" phone-has)
(host-offer "device-secret-keep" phone-keep)
(host-offer "device-secret-unlock" phone-has)
(host-offer "device-secret-forget" phone-forget)
(send (DeviceChecked (Ok (Tuple #t #f))))
(expect "the phone can keep it" (Tuple (.device (app-model app)) (.kept (app-model app))) (Tuple #t #f))
(send (KeepWithDevice #t))
(send Unlock)
(expect "no password" (.problem (app-model app)) "Type the password.")
(send (Password "hunter2"))
(send (Again "hunter3"))
(send Unlock)
(expect "two passwords" (.problem (app-model app)) "The two passwords are not the same.")
(send (Again "hunter2"))
(send Unlock)
(expect "unlocking" (= (.phase (app-model app)) Unlocking) #t)
(expect "the vault is made" (wait-for (fun (m) (= (.phase m) Open))) #t)
(expect "keys.bin is in the folder" (some? (map-try-ref (box-ref files) "keys.bin")) #t)
(expect "the phone keeps the password" (wait-for (fun (m) (.kept m))) #t)
(expect "under its name" (box-ref phone-kept) (str password-secret "\nhunter2"))
;; The share that waited is the dialog of a new bookmark.
(match (draft-in (app-model app))
((Some d)
(expect "the shared address" (.url d) "https://example.org/a")
(expect "the subject is the title" (.title d) "A page")
(expect "a new bookmark" (none? (.editing d)) #t))
(None (expect "a dialog after the share" #f #t)))
(expect "the share is used" (none? (.shared (app-model app))) #t)
(send (DraftTags "web, test"))
(send SaveDraft)
(expect "saved" (urls (app-model app)) ["https://example.org/a"])
(expect "what Share sends"
(share-text-of (vec-ref (visible-bookmarks (app-model app)) 0))
"A page\nhttps://example.org/a\nTags: test web")
(expect "the dialog is closed" (none? (draft-in (app-model app))) #t)
(expect "the sync ends" (wait-for (fun (m) (not (.saving m)))) #t)
(expect "no sync failed" (.failed (app-model app)) #f)
(expect "the device's file is in the folder"
(vec-length (vec-filter (fun (k) (string-starts-with? k "devices/")) (->vec (map-keys (box-ref files))))) 1)
;; The same address again: the dialog of its bookmark.
(host-post "share-text" "Again\nhttps://example.org/a")
(expect "a share while open"
(wait-for (fun (m) (some? (draft-in m)))) #t)
(match (draft-in (app-model app))
((Some d)
(expect "the bookmark there" (some? (.editing d)) #t)
(expect "with a problem" (string-empty? (.problem d)) #f)
(expect "its tags, sorted" (.tags d) "test web"))
(None unit))
(send CancelDraft)
;; A new bookmark that is not an address, and one with an address that a
;; bookmark has: it loads that bookmark, and another address brings back
;; what was typed.
(send New)
(send (DraftUrl "not an address"))
(send SaveDraft)
(expect "not an address"
(match (draft-in (app-model app)) ((Some d) (string-empty? (.problem d))) (None #t)) #f)
(send (DraftTitle "Bee"))
(send (DraftUrl "https://example.org/a"))
(expect "an address there already loads its bookmark"
(match (draft-in (app-model app)) ((Some d) (Tuple (.problem d) (.title d))) (None (Tuple "" "")))
(Tuple address-here "A page"))
(send (DraftUrl "https://example.org/b"))
(expect "another address brings back what was typed"
(match (draft-in (app-model app)) ((Some d) (Tuple (none? (.editing d)) (.title d))) (None (Tuple #f "")))
(Tuple #t "Bee"))
;; The archive link: a lookup that fails says so; a link typed is kept.
(send DraftFetchArchive)
(expect "an archive lookup that fails"
(wait-for (fun (m) (match (draft-in m)
((Some d) (string-starts-with? (.problem d) "No archive"))
(None #f))))
#t)
(send (DraftArchive "https://web.archive.org/web/2026/https://example.org/b"))
(send SaveDraft)
(expect "two bookmarks" (urls (app-model app)) ["https://example.org/a" "https://example.org/b"])
(expect "with the archive link"
(vec-map (fun (b) (.archive b))
(vec-filter (fun (b) (= (.url b) "https://example.org/b"))
(match (.vault (app-model app)) ((Some v) (vault-bookmarks v)) (None []))))
[(Some "https://web.archive.org/web/2026/https://example.org/b")])
;; Searching.
(send (Search "tag:web"))
(expect "a search by tag" (vec-map (fun (b) (.url b)) (visible-bookmarks (app-model app)))
["https://example.org/a"])
(send (Search "bee"))
(expect "a search by title" (vec-map (fun (b) (.url b)) (visible-bookmarks (app-model app)))
["https://example.org/b"])
;; Back clears the search, and with none it is not taken.
(expect "Back with a search" (some? (back-message (app-model app))) #t)
(send (match (back-message (app-model app)) ((Some msg) msg) (None (Search "not taken"))))
(expect "clears it" (.query (app-model app)) "")
(expect "and shows all" (vec-length (visible-bookmarks (app-model app))) 2)
(expect "Back without a search is not taken" (none? (back-message (app-model app))) #t)
;; Copy address: the note says what was copied.
(send (CopyAddress (.id (vec-ref (visible-bookmarks (app-model app)) 0))))
(expect "Copy address" (string-starts-with? (.note (app-model app)) "Copied https://example.org/") #t)
;; Open in private window, with no host that can: the address is copied.
(def first-id (.id (vec-ref (visible-bookmarks (app-model app)) 0)))
(send (OpenPrivately first-id))
(expect "no private window without a host"
(wait-for (fun (m) (string-contains? (.note m) "The address is copied"))) #t)
(send (OpenBookmark first-id))
(expect "Open asks the system" (string-starts-with? (.note (app-model app)) "Opening https://example.org/") #t)
(expect "and without a window, it cannot"
(wait-for (fun (m) (string-starts-with? (.note m) "Could not open"))) #t)
;; Edit, remove and undo.
(def a-id (.id (vec-ref (vec-filter (fun (b) (= (.url b) "https://example.org/a"))
(visible-bookmarks (app-model app))) 0)))
(send (Edit a-id))
(send (DraftTitle "Ay"))
(send (DraftTags "x"))
(send SaveDraft)
(def edited (vec-filter (fun (b) (= (.id b) a-id)) (visible-bookmarks (app-model app))))
(expect "the title edited" (.title (vec-ref edited 0)) (Some "Ay"))
(expect "the tags edited" (.tags (vec-ref edited 0)) ["x"])
(send (Remove a-id))
(expect "removed" (urls (app-model app)) ["https://example.org/b"])
(expect "the note" (.note (app-model app)) "Removed Ay.")
(send Undo)
(expect "brought back" (urls (app-model app)) ["https://example.org/a" "https://example.org/b"])
(expect "nothing more to undo" (empty? (.removed (app-model app))) #t)
(expect "the syncs end" (wait-for (fun (m) (not (.saving m)))) #t)
;; More, Help, and an import: the file's text is read as the picker's
;; file would be.
(send ShowMore)
(expect "the More layer" (app-windows app) (list "more"))
(send ShowHelp)
(expect "Help closes More" (Tuple (.more (app-model app)) (.help (app-model app))) (Tuple #f #t))
(send HideHelp)
(expect "no layers" (app-windows app) (list))
(send (BackupRead "bookmarks.json" (Ok (read-firefox-backup backup-json))))
(expect "imported" (urls (app-model app))
["https://doc.rust-lang.org/book/" "https://example.org/a" "https://example.org/b"])
(expect "the note of the import" (.note (app-model app))
"Imported 1 bookmark from bookmarks.json. 1 was here already.")
(send (BackupRead "bad.json" (Ok (read-firefox-backup "not json"))))
(expect "a file that is not a backup"
(string-starts-with? (.note (app-model app)) "Could not import bad.json") #t)
(expect "the syncs end again" (wait-for (fun (m) (not (.saving m)))) #t)
;; "Other bookmarks…": saved, closed, and back to the password screen.
(send ShowMore)
(send SwitchVault)
(expect "back to the password screen"
(wait-for (fun (m) (and (= (.phase m) Locked) (none? (.vault m))))) #t)
(expect "with nothing of the vault"
(Tuple (.query (app-model app)) (list-length (.removed (app-model app))) (.start (app-model app)))
(Tuple "" 0 OpenExisting))
(app-stop! app)
;; This device again: the phone gives the password after its unlock.
(def again (start-app (initial-model (path-combine root "one") (path-combine root "settings") None
#:storage (Some storage) #:start OpenExisting)
gui-update view (fun (c) unit)
#:windows windows #:subscriptions subscriptions))
(app-dispatch! again (DeviceChecked (Ok (Tuple #t #t))))
(app-run-pending! again)
(expect "the phone asks at once" (.asking (app-model again)) #t)
(host-post "device-secret" "cancelled\n")
(let go ((tries 0))
(app-run-pending! again)
(when (and (.asking (app-model again)) (< tries 300)) (sleep-ms 10) (go (+ tries 1))))
(expect "cancelled: the password screen" (Tuple (.asking (app-model again)) (= (.phase (app-model again)) Locked))
(Tuple #f #t))
(app-dispatch! again AskDevice)
(host-post "device-secret" "ok\nhunter2")
(let go ((tries 0))
(app-run-pending! again)
(unless (or (= (.phase (app-model again)) Open) (> tries 3000)) (sleep-ms 10) (go (+ tries 1))))
(expect "the phone's password opens it" (= (.phase (app-model again)) Open) #t)
(app-stop! again)
;; A kept password that is wrong is forgotten.
(def wrong (start-app (initial-model (path-combine root "one") (path-combine root "settings") None
#:storage (Some storage) #:start OpenExisting)
gui-update view (fun (c) unit)
#:windows windows #:subscriptions subscriptions))
(app-dispatch! wrong (DeviceChecked (Ok (Tuple #t #t))))
(host-post "device-secret" "ok\nhunter3")
(let go ((tries 0))
(app-run-pending! wrong)
(unless (or (string-empty? (box-ref phone-kept)) (> tries 3000)) (sleep-ms 10) (go (+ tries 1))))
(expect "a wrong kept password is forgotten"
(Tuple (box-ref phone-kept) (.kept (app-model wrong)) (= (.phase (app-model wrong)) Locked))
(Tuple "" #f #t))
(app-stop! wrong)
;; Another device joins through the same folder, and sees both.
(def two (start-app (initial-model (path-combine root "two") (path-combine root "settings") None
#:storage (Some storage) #:start JoinShared)
gui-update view (fun (c) unit)
#:windows windows #:subscriptions subscriptions))
(app-dispatch! two (Password "hunter2"))
(app-dispatch! two Unlock)
(let go ((tries 0))
(app-run-pending! two)
(unless (or (= (.phase (app-model two)) Open) (> tries 3000))
(sleep-ms 10)
(go (+ tries 1))))
(expect "the other device opens" (= (.phase (app-model two)) Open) #t)
(expect "and has all" (urls (app-model two))
["https://doc.rust-lang.org/book/" "https://example.org/a" "https://example.org/b"])
;; A third device, not yet synced, bookmarked example.org/a with a title
;; of its own: after a sync, the app asks which title to keep.
(def three (match (join-vault storage (path-combine root "three") "hunter2")
((Ok (Tuple v _)) v)
((Err _) (panic! "the third device did not join"))))
(def three2 (match (vault-add three "https://example.org/a" #:title (Some "Another title") #:tags ["other"])
((Ok (Tuple v _)) v)
((Err why) (panic! why))))
(ignore (vault-sync three2))
(defun (sync-two)
(app-dispatch! two SyncTick)
(let go ((tries 0))
(app-run-pending! two)
(when (and (.saving (app-model two)) (< tries 3000)) (sleep-ms 10) (go (+ tries 1)))))
(sync-two)
(def asked (.duplicates (app-model two)))
(expect "a page bookmarked twice" (vec-length asked) 1)
(expect "its titles" (vec-length (duplicate-titles (vec-ref asked 0))) 2)
(expect "the question shows" (list-length (windows (app-model two))) 1)
(app-dispatch! two (ChooseTitle "https://example.org/a" "Another title"))
(sync-two)
(def kept (vec-filter (fun (b) (= (.url b) "https://example.org/a"))
(vault-bookmarks (option-ref-or (.vault (app-model two)) three2))))
(expect "one bookmark of it" (vec-length kept) 1)
(expect "with the title chosen" (.title (vec-ref kept 0)) (Some "Another title"))
(expect "and the tags of both" (vec-contains? (.tags (vec-ref kept 0)) "other") #t)
(expect "no question" (Tuple (vec-length (.duplicates (app-model two))) (list-length (windows (app-model two))))
(Tuple 0 0))
(app-stop! two)
;; The settings keep the folder's bookmark, and other fields.
(def settings (path-combine root "settings"))
(expect "no settings yet" (read-folder-mark settings) None)
(save-folder-mark! settings "mark-1")
(save-folder-mark! settings "mark-2")
(expect "the last mark" (read-folder-mark settings) (Some "mark-2"))
;; The list of folders. Settings of an earlier version have their one
;; folder, with no name yet.
(expect "the folder of an earlier version" (vec-map vault-entry-mark (read-vaults settings)) ["mark-2"])
(def home (VaultEntry (name "Home") (mark "mark-a")))
(def work (VaultEntry (name "Work") (mark "mark-b")))
(save-vaults! settings [home work] (Some "mark-b"))
(expect "the list is kept" (vec-map vault-entry-name (read-vaults settings)) ["Home" "Work"])
(expect "and the one used last" (read-folder-mark settings) (Some "mark-b"))
(save-vaults! settings [home] None)
(expect "none used last" (read-folder-mark settings) None)
(expect "a shorter list" (vec-map vault-entry-name (read-vaults settings)) ["Home"])
;; Choosing among them.
(def listed (initial-model (path-combine root "four") settings (Some "mark-a") #:vaults [home work]))
(expect "the one used last opens at the start" (Tuple (.phase listed) (.current listed))
(Tuple Checking (Some "mark-a")))
(def locked (record-set listed (phase Locked)))
(def (Tuple chose _) (gui-update (ChooseVault 1) locked))
(expect "another one is opened" (Tuple (.phase chose) (.mark chose) (.current chose))
(Tuple Checking (Some "mark-b") (Some "mark-b")))
(def (Tuple same _) (gui-update (ChooseVault 0) locked))
(expect "the one in use again changes nothing" (.phase same) Locked)
(def (Tuple adding _) (gui-update (ChooseVault 2) locked))
(expect "Add new asks for a folder, and keeps the one in use"
(Tuple (.phase adding) (.current adding)) (Tuple Locked (Some "mark-a")))
(def (Tuple forgot _) (gui-update ForgetVault locked))
(expect "Remove from list" (Tuple (vec-map vault-entry-name (.vaults forgot)) (.current forgot) (.phase forgot))
(Tuple ["Work"] None NoFolder))
(directory-delete-tree root)
(println "bjomarks-android app ok"))