From 08a899cf0108268b826d794f3d93c75c1782ea59 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 24 Oct 2026 23:36:19 +0300 Subject: [PATCH] Pin the colour of a chat's avatar with a test that fails when it falls back to black The ui-vm gate's minor "server_tint falls back to black" was closed by a fallback in App::conversation_item, `.unwrap_or_else(|| vm::avatar::tint(&title))`, that no test could fail: replacing it with `.unwrap_or_default()` (opaque black) left cargo test -p voelin-ui -p voelin-ui-vm green. The choice of (kind, session) -> colour is now a pure function in voelin-ui-vm, servers::logic::conversation_tint(state, kind, session, title), and messages.rs calls it. A test pins the three cases: a server chat of a server in the list takes the rail's colour, a server chat whose server is gone takes the colour of its title, and a channel's or a person's chat (with or without a known server) takes the colour of its title. The titles are chosen so that the title's colour differs from the server's, and the test asserts that. App::server_tint, whose only caller this was, is removed. Co-Authored-By: Claude Sonnet 5.5 --- crates/voelin-ui-vm/src/servers/logic.rs | 45 ++++++++++++++++++++++++ crates/voelin-ui/src/messages.rs | 8 +---- crates/voelin-ui/src/servers/bind.rs | 7 ---- 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/crates/voelin-ui-vm/src/servers/logic.rs b/crates/voelin-ui-vm/src/servers/logic.rs index 5085385..d184fb7 100644 --- a/crates/voelin-ui-vm/src/servers/logic.rs +++ b/crates/voelin-ui-vm/src/servers/logic.rs @@ -410,6 +410,24 @@ pub fn server_tint(state: &AppState, id: i64) -> Option Some(tints(&state.bookmarks)[position]) } +/// The colour of a chat's avatar in a list: the server's own colour, as the +/// rail shows it, for the chat of a server (`kind` 0 of a +/// [`ConversationItem`](crate::bridge::ConversationItem)) that is still in +/// the list; for every other chat, and for a server chat that outlived its +/// bookmark, the colour of the `title`, never black. +pub fn conversation_tint( + state: &AppState, + kind: i32, + session: Option, + title: &str, +) -> crate::color::Rgb { + match (kind, session) { + (0, Some(session)) => server_tint(state, session), + _ => None, + } + .unwrap_or_else(|| crate::avatar::tint(title)) +} + /// The server card, the voice controls and who we are. pub struct ToolbarView { /// The server's icon. @@ -845,6 +863,33 @@ mod tests { assert!(st.sessions[&1].collapsed.is_empty()); } + /// A chat takes its server's colour only while it is a server chat of a + /// server in the list: a chat whose server is gone, and a chat that is a + /// channel's or a person's, take the colour of their own title. Black + /// is what a missing colour would be (`Rgb::default()`). + #[test] + fn a_chat_takes_its_servers_colour_or_that_of_its_title() { + let st = state(); + let server = tints(&st.bookmarks)[0]; + let titled = |title: &str| crate::avatar::tint(title); + // Titles whose own colour is not the server's, so that the chat + // below can tell the two apart. + assert_ne!(titled("Old Server"), server); + assert_ne!(titled("Alice"), server); + assert_ne!(titled("Alice"), crate::color::Rgb::default()); + // The server's chat, the server in the list: its colour, not the + // title's. + assert_eq!(conversation_tint(&st, 0, Some(1), "Old Server"), server); + // The server is gone from the list: the title's colour. + assert_eq!(conversation_tint(&st, 0, Some(9), "Old Server"), titled("Old Server")); + // Not a server chat of a known server: the title's colour, whether + // the server is in the list or not, or not known at all. + assert_eq!(conversation_tint(&st, 1, Some(1), "Alice"), titled("Alice")); + assert_eq!(conversation_tint(&st, 2, Some(1), "Alice"), titled("Alice")); + assert_eq!(conversation_tint(&st, 2, Some(9), "Alice"), titled("Alice")); + assert_eq!(conversation_tint(&st, 0, None, "Alice"), titled("Alice")); + } + /// The Invite button's link and note; the voice view's chat. #[test] fn invite_and_voice_view() { diff --git a/crates/voelin-ui/src/messages.rs b/crates/voelin-ui/src/messages.rs index 4f133e7..02bc717 100644 --- a/crates/voelin-ui/src/messages.rs +++ b/crates/voelin-ui/src/messages.rs @@ -201,13 +201,7 @@ impl App { ago: ago(ts).into(), unread: chat.unread, initials: vm::avatar::initials(&title).into(), - // A server's chat in the server's colour; a name's own colour - // when the server is gone. - tint: match (kind, session) { - (0, Some(s)) => self.server_tint(s), - _ => None, - } - .unwrap_or_else(|| vm::avatar::tint(&title)), + tint: vm::servers::logic::conversation_tint(&self.state, kind, session, &title), avatar, online, selected: open.is_some_and(|o| { diff --git a/crates/voelin-ui/src/servers/bind.rs b/crates/voelin-ui/src/servers/bind.rs index b6d6625..7d18067 100644 --- a/crates/voelin-ui/src/servers/bind.rs +++ b/crates/voelin-ui/src/servers/bind.rs @@ -286,13 +286,6 @@ impl App { } } - /// A server's colour, as the rail shows it ([`logic::server_tint`]); - /// none for a server that is not in the list (a chat that outlived its - /// bookmark), where the caller picks a colour of its own. - pub(crate) fn server_tint(&self, id: i64) -> Option { - logic::server_tint(&self.state, id) - } - /// The rail: servers with their state, unread count (without private /// chats) and streams; what the chat strip and the private chats have /// unread.