Lutra/crates/control/tests/theme.rs
danny 9801a346b6 H5: banner-aligned themes, diagnostic and VCS tokens
Both themes follow the README banner without taking it over: calm,
near-neutral surfaces (cream-tinted light, faintly plum-tinted dark),
the raspberry only as the accent, retuned syntax colours and the
terminal on the editor surface. Approved by the owner from before/after
screenshots.

New tokens `--lt-diag-error|warning|info|hint` (lint underlines, gutter,
tooltips, Problems icons) and `--lt-vcs-added|modified|deleted|untracked|
ignored|conflicted` (for M4.4), listed in `theme::DIAGNOSTIC_TOKENS` and
`theme::VCS_TOKENS`. Completion kind icons take the syntax colour of
their kind. The contrast test checks every new colour (4.5:1 text,
3:1 icons) in both themes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 13:58:38 +01:00

134 lines
4.3 KiB
Rust

//! 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<TcpStream>,
writer: TcpStream,
events: Vec<Value>,
}
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");
}