Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-message

Source of release 0.5.0

0.5.0 / tests / main.luc

## luced-message's headless tests: accounts, the store as the window reads it, folder
## roles and the composer's reply text. Run by tests/run.py with a scratch directory.
from accounts import Account, AccountStore, account_id, escape
import mail_index
import clock
from luce_std import files, paths
import sources_tests
import composer_tests
import list_tests
import fixtures
import db_tests
from luce_prism import prism
import sidebar_tests

func check(condition: bool, what: str):
    assert(condition, what)

func accounts_round_trip(directory: str) -> unit!:
    let store = AccountStore(directory, service = "com.luciaos.luced-message.test")
    let work = Account("work")
    work.name = "Work"
    work.address = "alice@example.com"
    work.imap_host = "imap.example.com"
    work.smtp_host = "smtp.example.com"
    work.user = "alice@example.com"
    work.password = "pa\"ss\\word"
    let local = Account("local")
    local.address = "bob@example.test"
    local.imap_host = "127.0.0.1"
    local.imap_port = 3143
    local.imap_security = "plain"
    store.save([work, local])
    # CI's Linux has no Secret Service: the accounts still save, without passwords.
    let keychain_here = store.warning == ""
    if not keychain_here:
        print(f"keychain unavailable here: {store.warning}")
    let loaded = store.load()
    check(not keychain_here or store.warning == "", f"accounts read back: {store.warning}")
    check(loaded.length == 2, "two accounts")
    check(loaded[0].id == "work" and loaded[0].password == ("pa\"ss\\word" if keychain_here else ""), "the password survives quoting")
    check(loaded[1].imap_port == 3143 and loaded[1].imap_security == "plain", "ports and security")
    check(loaded[1].title() == "bob@example.test", "a title falls back to the address")
    check(account_id("Alice.Smith@Example.com", ["alice_smith_example_com"]) == "alice_smith_example_com_2", "ids are unique")
    check(escape("a\tb\\c\nd") == "a\\tb\\\\c\\nd", "engine fields are escaped")
    let line = work.engine_line()
    check(line.split("\t").length == 11, "the engine line has eleven fields")
    let written = files.read_text(store.path)
    check(not ("password" in written), "no password in accounts.toml")
    if keychain_here:
        store.forget("work")
        store.forget("local")
        check(store.load()[0].password == "", "a forgotten password reads as empty")

## A keychain that refuses the password still leaves the account saved.
func accounts_without_keychain(directory: str) -> unit!:
    let folder = paths.join(directory, "refusing")
    files.ensure_directory(folder)
    let store = AccountStore(folder, service = "")
    let work = Account("work")
    work.address = "alice@example.com"
    work.password = "secret"
    store.save([work])
    check(store.warning != "", "the refused password is reported")
    check(store.load().length == 1, "the account is saved anyway")

func index_reading() -> unit!:
    let store = prism.Store.memory()
    fixtures.folder_with(store, "acct.k1", ["3\t1\t1000\t10\t0\tAlice\tOld\tid3\t\t", "5\t0\t3000\t20\t1\tBob\tNew\\tTabbed\tid5\tid3\tHello",
                                           "9\t4\t2000\t30\t0\tCarol\tMiddle\tid9\t\tA\\nB"])
    let index = mail_index.read_index(store, "acct.k1")
    check(index.count() == 3, "three rows")
    check(index.uid(index.newest(0)) == 5 and index.uid(index.newest(1)) == 9 and index.uid(index.newest(2)) == 3, "newest first by date")
    let bob = index.position(5)
    check(index.subject(bob) == "New\tTabbed" and index.in_reply_to(bob) == "id3" and index.attachment(bob), "fields unescaped")
    check(index.preview(index.position(9)) == "A\nB", "newlines unescaped")
    check(mail_index.has(index.flags(index.position(9)), 4) and not mail_index.has(index.flags(bob), 1), "flag bits")
    check(index.position(4) == -1, "a missing UID")
    let none_here = mail_index.read_index(store, "acct.missing")
    check(none_here.count() == 0, "a folder never synced reads as empty")
    fixtures.account_with(store, "acct", ["INBOX\tINBOX\t47\t0\tk1\t5\t2", "Archive/2026\tArchive/2026\t47\t512\tk2\t9\t0"])
    let folders = mail_index.read_folders(store, "acct")
    check(folders.count() == 2 and folders.unseen(0) == 2, "folders read")
    check(folders.depth(1) == 1 and folders.leaf(1) == "2026" and folders.key(1) == "k2", "depth and leaf")

func conversations() -> unit!:
    # uid flags date size attachment sender subject message-id in-reply-to preview
    let rows = ["1\t1\t100\t1\t0\tCarol\tPlan\tp1\t\t", "2\t0\t200\t1\t0\tDave\tRe: Plan\tp2\tp1\t",
                "3\t1\t150\t1\t1\tBob\tLunch\tl1\t\t", "4\t5\t300\t1\t0\tEve\tRE: Re: Plan\tp3\tp2\t",
                "5\t1\t50\t1\t0\tAnn\tRe: Old topic\to2\tgone\t", "6\t1\t60\t1\t0\tAl\tRe: Old topic\to3\tgone\t",
                "7\t1\t70\t1\t0\tZed\tHello\th1\t\t", "8\t1\t80\t1\t0\tYan\tHello\th2\t\t",
                "9\t1\t400\t1\t0\tKim\tFwd: Lunch\tf1\t\t"]
    let store = prism.Store.memory()
    fixtures.folder_with(store, "acct.k1", rows)
    let index = mail_index.read_index(store, "acct.k1")
    # Lunch+Fwd (newest 400), Plan chain (300), Hello h2 (80), Hello h1 (70), Old topic pair (60).
    check(index.thread_count() == 5, f"five conversations, got {index.thread_count()}")
    check(index.uid(index.thread_member(0, 0)) == 9 and index.thread_size(0) == 2, "a forward joins its subject")
    check(index.thread_size(1) == 3 and index.uid(index.thread_member(1, 0)) == 4 and index.uid(index.thread_member(1, 2)) == 1, "a reply chain, newest first")
    check(index.thread_any(1, 1, false), "the chain has an unread message")
    check(index.thread_any(1, 4, true), "the chain has a flagged message")
    check(index.thread_size(2) == 1 and index.thread_size(3) == 1, "two unrelated Hellos stay apart")
    check(index.thread_size(4) == 2, "replies to a missing message join")
    check(index.thread_attachment(0), "the Lunch thread has an attachment")

pub func main(arguments: list[str]) -> int!:
    let directory = arguments[0] if arguments.length > 0 else "/tmp/luced-message-tests"
    files.ensure_directory(directory)
    accounts_round_trip(directory)
    accounts_without_keychain(directory)
    index_reading()
    conversations()
    db_tests.run(prism.Store.memory())
    sources_tests.run()
    composer_tests.run()
    list_tests.run()
    sidebar_tests.run()
    check(clock.list_date(0) == "", "no date")
    print("PASS luced-message")
    return 0