224 lines · 12.1 KB · RustRaw
//! Cotton Vault's screens over invented rows: chips, sections, search,//! paging, pictures, and the look's data. No value is sealed or printed here://! these are plain columns only.use std::collections::HashMap;use super::list::{self, fold, initials, items, rank, tint, Ask, Entry};use super::look::{contrast, Look, COLOUR_ROLES};use super::shapes::*;fn entry(id: &str, kind: &str, title: &str) -> Entry { Entry { id: id.into(), kind: kind.into(), title: title.into(), ..Default::default() }}fn ask(filter: &str, q: &str) -> Ask { Ask { filter: filter.into(), q: q.into(), after: None }}fn sample() -> Vec<Entry> { let mut mail = entry("a", "login", "Invented Mail"); mail.site = Some("mail.invented.test".into()); mail.username = Some("ada@invented.test".into()); mail.favourite = true; mail.last_used = Some("2026-11-10T10:00:00Z".into()); mail.passkey = true; let mut bank = entry("b", "login", "Bänk of Nowhere"); bank.site = Some("bank.invented.test".into()); bank.username = Some("ada".into()); bank.last_used = Some("2026-11-12T10:00:00Z".into()); let card = entry("c", "card", "Travel card"); let note = entry("d", "note", "Wi-Fi at the cabin"); let mut gone = entry("e", "login", "Old forum"); gone.in_bin = true; let key = entry("f", "passkey", "invented.test"); vec![mail, bank, card, note, gone, key]}#[test]fn the_list_opens_with_favourites_then_recent_then_everything_a_to_z() { let answer = items(&sample(), &HashMap::new(), &ask("", ""), false); let ids: Vec<&str> = answer.sections.iter().map(|section| section.id.as_str()).collect(); assert_eq!(ids, ["favourites", "recent", "all"]); assert_eq!(answer.sections[0].rows.iter().map(|row| row.id.as_str()).collect::<Vec<_>>(), ["a"]); assert_eq!(answer.sections[1].rows.iter().map(|row| row.id.as_str()).collect::<Vec<_>>(), ["b", "a"], "newest use first"); let all: Vec<&str> = answer.sections[2].rows.iter().map(|row| row.title.as_str()).collect(); assert_eq!(all, ["Bänk of Nowhere", "Invented Mail", "invented.test", "Travel card", "Wi-Fi at the cabin"], "folded A–Z, the bin left out"); assert_eq!(answer.total, 5); assert_eq!(answer.bin.as_ref().map(|act| act.label.as_str()), Some("Recently deleted · 1")); assert!(answer.empty.is_none() && answer.next.is_none());}#[test]fn chips_count_each_group_kept_and_only_groups_with_something() { let answer = items(&sample(), &HashMap::new(), &ask("all", ""), false); let chips: Vec<(&str, u32)> = answer.chips.iter().map(|chip| (chip.id.as_str(), chip.count)).collect(); assert_eq!(chips, [("all", 5), ("favourites", 1), ("passwords", 2), ("passkeys", 2), ("cards", 1), ("notes", 1)], "Cotton Secrets' groups, in their order"); let passwords = items(&sample(), &HashMap::new(), &ask("passwords", ""), false); assert_eq!(passwords.sections[0].rows.len(), 2, "a group lists its kinds alone"); assert!(passwords.sections[0].rows.iter().all(|row| row.group == "passwords")); assert!(answer.chips[0].on); let logins = items(&sample(), &HashMap::new(), &ask("login", ""), false); assert_eq!(logins.sections.len(), 1, "a kind's chip lists that kind alone, no favourites or recent"); assert_eq!(logins.sections[0].rows.len(), 2); let passkeys = items(&sample(), &HashMap::new(), &ask("passkey", ""), false); assert_eq!(passkeys.sections[0].rows.iter().map(|row| row.id.as_str()).collect::<Vec<_>>(), ["a", "f"], "a login with a passkey counts as one"); assert_eq!(passkeys.sections[0].rows[0].badge.as_deref(), Some("Passkey"));}#[test]fn search_matches_word_starts_folds_accents_and_ranks_titles_first() { let all = sample(); let found = |q: &str| -> Vec<String> { items(&all, &HashMap::new(), &ask("", q), false).sections.iter().flat_map(|s| s.rows.iter().map(|r| r.id.clone())).collect() }; assert_eq!(found("bank"), ["b"], "ä folds to a"); assert_eq!(found("ail"), Vec::<String>::new(), "never the middle of a word"); assert_eq!(found("invented"), ["a", "f", "b"], "title starts first, then a site match"); assert_eq!(found("ada"), ["b", "a"], "user names are searched"); assert_eq!(found("old"), Vec::<String>::new(), "the bin is not searched from the list"); let answer = items(&all, &HashMap::new(), &ask("", "zzz"), false); assert_eq!(answer.empty.as_ref().map(|empty| empty.title.as_str()), Some("Nothing matches")); assert!(answer.bin.is_none()); let mail = &all[0]; assert_eq!(rank(mail, &fold("inv mail"), &["inv", "mail"]), Some(1));}#[test]fn the_bin_lists_what_was_deleted_with_put_back() { let answer = items(&sample(), &HashMap::new(), &ask("bin", ""), false); assert_eq!(answer.title, "Recently deleted"); assert_eq!(answer.sections[0].rows[0].id, "e"); assert_eq!(answer.sections[0].rows[0].quick[0].id, "restore"); assert!(answer.chips.iter().any(|chip| chip.id == "bin" && chip.on));}#[test]fn pages_follow_a_cursor_never_a_number() { let many: Vec<Entry> = (0..450).map(|n| entry(&format!("id-{n:04}"), "login", &format!("Invented {n:04}"))).collect(); let first = items(&many, &HashMap::new(), &ask("", ""), false); assert_eq!(first.sections.last().unwrap().rows.len(), ITEMS_PAGE); let mut seen: Vec<String> = first.sections.last().unwrap().rows.iter().map(|row| row.id.clone()).collect(); let mut next = first.next.clone(); while let Some(after) = next { let page = items(&many, &HashMap::new(), &Ask { filter: String::new(), q: String::new(), after: Some(after) }, false); seen.extend(page.sections.last().unwrap().rows.iter().map(|row| row.id.clone())); next = page.next; } assert_eq!(seen.len(), 450); let mut sorted = seen.clone(); sorted.dedup(); assert_eq!(sorted.len(), 450, "no row twice");}#[test]fn rows_carry_marks_pictures_and_quick_acts_with_presence_for_secrets() { let marks: HashMap<String, Vec<String>> = [("a".to_string(), vec!["reused".to_string(), "weak".to_string()])].into(); let answer = items(&sample(), &marks, &ask("login", ""), true); let mail = answer.sections[0].rows.iter().find(|row| row.id == "a").unwrap(); assert_eq!(mail.marks.iter().map(|mark| (mark.id.as_str(), mark.tone)).collect::<Vec<_>>(), [("weak", Tone::Danger), ("reused", Tone::Attention)]); assert_eq!(mail.subtitle, "ada@invented.test"); assert_eq!(mail.picture.initials, "IM"); assert!(mail.picture.symbol.is_none()); let quick: Vec<(&str, bool)> = mail.quick.iter().map(|act| (act.id.as_str(), act.presence)).collect(); assert_eq!(quick, [("copy_username", false), ("copy_password", true), ("unfavourite", false)]); assert_eq!(mail.quick[1].method, "vault.field"); let card = items(&sample(), &HashMap::new(), &ask("card", ""), true).sections[0].rows[0].clone(); assert_eq!(card.picture.symbol.as_deref(), Some("card")); assert_eq!(card.subtitle, "Card"); assert_eq!(initials(" ?? "), "?"); assert_eq!(tint("Invented Mail"), tint("invented mail"), "a tint does not change with case");}#[test]fn an_empty_vault_says_so_and_offers_add() { let answer = items(&[], &HashMap::new(), &ask("", ""), false); let empty = answer.empty.expect("empty"); assert_eq!(empty.title, "Nothing saved yet"); assert_eq!(empty.act.map(|act| act.method), Some("vault.edit".to_string())); assert_eq!(answer.chips.len(), 1); assert_eq!(answer.add.kinds.first().map(|act| act.id.as_str()), Some("login"));}#[test]fn every_line_the_list_says_is_in_the_kit() { let lines = list::lines(); for (group, _) in list::GROUPS { assert!(lines.say(&format!("chips.{group}"), &[]).is_some(), "chips.{group}"); } for line in ["card", "file", "file_named", "lines_one", "lines_many", "keystore", "keystore_one"] { assert!(lines.say(&format!("second.{line}"), &[]).is_some(), "second.{line}"); } for kind in list::CHIP_KINDS.iter().chain(list::ADD_KINDS) { assert!(lines.say(&format!("chips.{kind}"), &[]).is_some(), "chips.{kind}"); assert!(lines.say(&format!("kinds.{kind}"), &[]).is_some(), "kinds.{kind}"); } for mark in ["weak", "reused", "old", "breached"] { assert!(lines.say(&format!("marks.{mark}"), &[]).is_some()); }}#[test]fn the_look_carries_every_role_in_both_modes_with_readable_contrast() { let look = Look::shipped(); assert_eq!(look.fonts["sans"].licence, "SIL OFL 1.1"); assert_eq!(look.fonts["mono"].licence, "SIL OFL 1.1"); for mode in ["light", "dark"] { let colours = &look.colours[mode]; for role in COLOUR_ROLES { assert!(colours.contains_key(*role), "{mode}.{role}"); } let on = |fg: &str, bg: &str| contrast(&colours[fg], &colours[bg]).unwrap(); assert!(on("text", "background") >= 7.0, "{mode}: title text"); assert!(on("secondary", "background") >= 4.5, "{mode}: secondary text"); assert!(on("tertiary", "background") >= 4.5, "{mode}: tertiary text"); assert!(on("on_accent", "accent") >= 4.5, "{mode}: the main action"); for state in ["safe", "attention", "danger"] { assert!(on(state, "background") >= 4.5, "{mode}: {state}"); } assert!(on("on_tint", "tint.0") >= 4.5 && on("on_tint", "tint.7") >= 4.5, "{mode}: initials"); } assert!(Look::parse(&super::look::SHIPPED.replace("\"text\": \"#09090B\"", "\"text\": \"black\"")).is_err());}#[test]fn every_kind_stands_in_one_of_the_five_groups() { use crate::vault::SecretKind; for kind in SecretKind::ALL { let group = list::group_of(kind.as_str()); assert!(list::GROUPS.iter().any(|(id, kinds)| *id == group && kinds.contains(&kind.as_str())), "{kind} is listed in a group"); } assert_eq!(list::group_of("passkey"), "passkeys"); assert_eq!(list::group_of("file"), "developer"); assert_eq!(list::group_of("api_key"), "developer");}#[test]fn a_second_line_says_no_more_of_a_value_than_its_tail() { use crate::vault::SecretPayload; let lines = list::lines(); let card = SecretPayload::Card { number: "4111 1111 1111 4821".into(), holder: None, expiry: None, cvv: Some("123".into()), notes: None }; assert_eq!(list::second_line(lines, &card).as_deref(), Some("Card ending 4821")); let key = SecretPayload::ApiKey { key: "sk_test_INVENTED0000000004f2a".into(), notes: None }; assert_eq!(list::second_line(lines, &key).as_deref(), Some("sk_test_…4f2a")); assert_eq!(list::masked_tail("AKIAINVENTED00001234").as_deref(), Some("…1234"), "no prefix: the tail alone"); assert_eq!(list::masked_tail("ghp-INVENTEDINVENTED9z9z").as_deref(), Some("ghp-…9z9z")); assert_eq!(list::masked_tail("short123").as_deref(), Some("••••"), "eight characters or fewer: nothing shown"); let env = SecretPayload::File { content: "QV9LRVk9MQpCX0tFWT0yCg==".into(), file_name: Some("invented.env".into()), store_password: None, key_alias: None, key_password: None, notes: None }; assert_eq!(list::second_line(lines, &env).as_deref(), Some("invented.env · 2 lines")); let unnamed = SecretPayload::File { content: "QV9LRVk9MQo=".into(), file_name: None, store_password: None, key_alias: None, key_password: None, notes: None }; assert_eq!(list::second_line(lines, &unnamed).as_deref(), Some("File · 1 line")); let keystore = SecretPayload::File { content: "/u3+7QAAAAI=".into(), file_name: Some("upload.jks".into()), store_password: Some("an invented password".into()), key_alias: Some("upload".into()), key_password: Some("another".into()), notes: None }; assert_eq!(list::second_line(lines, &keystore).as_deref(), Some("upload.jks and 2 passwords")); let note = SecretPayload::Note { body: "an invented note".into() }; assert_eq!(list::second_line(lines, ¬e), None, "a note's second line stays the plain columns'");}#[test]fn a_second_line_stands_in_for_the_subtitle_and_tokens_are_mono() { let mut all = sample(); let mut token = entry("t", "api_key", "Invented deploy key"); token.second = Some("sk_test_…4f2a".into()); all.push(token); let answer = items(&all, &HashMap::new(), &ask("developer", ""), true); let row = &answer.sections[0].rows[0]; assert_eq!((row.subtitle.as_str(), row.mono, row.group.as_str()), ("sk_test_…4f2a", true, "developer"));}