//! Theme switching over a real TCP socket: `settings.set appearance.theme` //! and `theme.systemPreference` stream `themeChanged` with the theme's //! tokens to `events.subscribe` clients, the way the desktop window gets //! them. #![allow( clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing, clippy::panic )] use std::io::{BufRead, BufReader, Write}; use std::net::TcpStream; use std::sync::Arc; use std::time::Duration; use lutra_engine::theme; use lutra_testkit::EngineBackend; use serde_json::{Value, json}; /// A client that collects event lines and returns responses. struct Client { reader: BufReader, writer: TcpStream, events: Vec, } impl Client { fn connect(addr: std::net::SocketAddr) -> Self { let stream = TcpStream::connect(addr).unwrap(); stream .set_read_timeout(Some(Duration::from_secs(5))) .unwrap(); Self { writer: stream.try_clone().unwrap(), reader: BufReader::new(stream), events: Vec::new(), } } fn line(&mut self) -> Value { let mut line = String::new(); self.reader.read_line(&mut line).expect("line within 5 s"); serde_json::from_str(&line).unwrap() } /// Sends a request and returns its response, collecting event lines that /// arrive before it. fn call(&mut self, request: Value) -> Value { self.writer .write_all(format!("{request}\n").as_bytes()) .unwrap(); loop { let line = self.line(); if line.get("event").is_some() { self.events.push(line["event"].clone()); } else { return line; } } } /// Waits for the next `themeChanged` event (events arrive from a /// subscriber queue, possibly after the response) and drops the events /// before it. fn next_theme(&mut self) -> Value { loop { if let Some(at) = self.events.iter().position(|e| e["kind"] == "themeChanged") { let theme = self.events.remove(at); self.events.drain(..at); return theme; } let line = self.line(); if line.get("event").is_some() { self.events.push(line["event"].clone()); } } } } fn execute(command: &str, params: Value) -> Value { json!({"id": 2, "method": "engine.execute", "params": {"command": command, "params": params}}) } fn tokens(theme: &theme::Theme) -> Value { serde_json::to_value(theme::token_map(theme)).unwrap() } #[test] fn switching_streams_theme_changed_with_tokens() { let dir = tempfile::tempdir().unwrap(); let backend = Arc::new(EngineBackend::with_config(lutra_testkit::engine_config( dir.path(), ))); let server = lutra_control::start(0, backend.clone()).unwrap(); let mut client = Client::connect(server.local_addr()); client.call(json!({"id": 1, "method": "events.subscribe"})); let r = client.call(execute("theme.list", Value::Null)); assert_eq!(r["result"][1]["id"], "lutra-light", "{r}"); let r = client.call(execute( "settings.set", json!({"key": "appearance.theme", "value": "lutra-light"}), )); assert_eq!(r["result"]["value"], "lutra-light", "{r}"); let changed = client.next_theme(); assert_eq!(changed["id"], "lutra-light"); assert_eq!(changed["dark"], false); assert_eq!(changed["tokens"], tokens(&theme::LIGHT)); client.call(execute( "settings.set", json!({"key": "appearance.theme", "value": "lutra-dark"}), )); let changed = client.next_theme(); assert_eq!(changed["id"], "lutra-dark"); assert_eq!(changed["dark"], true); assert_eq!(changed["tokens"], tokens(&theme::DARK)); client.call(execute( "settings.set", json!({"key": "appearance.theme", "value": "system"}), )); let r = client.call(execute("theme.systemPreference", json!({"dark": false}))); assert_eq!(r["result"]["id"], "lutra-light", "{r}"); let changed = client.next_theme(); assert_eq!( changed["id"], "lutra-light", "switching from dark to system on an unreported (dark) OS must not emit" ); assert_eq!(changed["tokens"]["--lt-editor-selection-bg"], "#ecd6dc"); }