Lutra/crates/engine/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

154 lines
4.5 KiB
Rust

//! Theme commands and `themeChanged`: switching through `settings.set`, the
//! "system" mode following the reported OS preference, and the stored theme
//! surviving a restart.
#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing
)]
use lutra_engine::{Engine, EngineConfig, Event, theme};
use serde_json::{Value, json};
use tempfile::TempDir;
fn themes(events: &[Event]) -> Vec<(String, bool)> {
events
.iter()
.filter_map(|e| match e {
Event::ThemeChanged { id, dark, .. } => Some((id.clone(), *dark)),
_ => None,
})
.collect()
}
fn set_theme(engine: &mut Engine, value: &str) -> Vec<(String, bool)> {
let outcome = engine
.execute(
"settings.set",
json!({"key": "appearance.theme", "value": value}),
)
.unwrap();
themes(&outcome.events)
}
fn report(engine: &mut Engine, dark: bool) -> (Value, Vec<(String, bool)>) {
let outcome = engine
.execute("theme.systemPreference", json!({"dark": dark}))
.unwrap();
let events = themes(&outcome.events);
(outcome.value, events)
}
#[test]
fn list_names_the_builtin_themes_by_catalog_key() {
let mut e = Engine::new().unwrap();
let list = e.execute("theme.list", Value::Null).unwrap().value;
assert_eq!(
list,
json!([
{"id": "lutra-dark", "label": "theme.lutraDark", "dark": true},
{"id": "lutra-light", "label": "theme.lutraLight", "dark": false},
])
);
}
#[test]
fn switching_the_setting_emits_the_new_tokens() {
let mut e = Engine::new().unwrap();
let outcome = e
.execute(
"settings.set",
json!({"key": "appearance.theme", "value": "lutra-light"}),
)
.unwrap();
let Some(Event::ThemeChanged { id, dark, tokens }) = outcome
.events
.iter()
.find(|e| matches!(e, Event::ThemeChanged { .. }))
else {
panic!("no themeChanged in {:?}", outcome.events);
};
assert_eq!(id, "lutra-light");
assert!(!dark);
assert_eq!(tokens, &theme::token_map(&theme::LIGHT));
assert_eq!(tokens["--lt-bg"], "#f3efe3");
assert_eq!(tokens["--lt-font-size-editor"], "14px");
assert!(matches!(
outcome.events.first(),
Some(Event::SettingsChanged { .. })
));
assert_eq!(set_theme(&mut e, "lutra-light"), []);
assert_eq!(
set_theme(&mut e, "lutra-dark"),
[("lutra-dark".into(), true)]
);
let reset = e
.execute("settings.reset", json!({"key": "appearance.theme"}))
.unwrap();
assert_eq!(themes(&reset.events), []);
let other = e
.execute(
"settings.set",
json!({"key": "editor.fontSize", "value": 18}),
)
.unwrap();
assert_eq!(themes(&other.events), []);
}
#[test]
fn system_mode_follows_the_reported_preference() {
let mut e = Engine::new().unwrap();
let (value, events) = report(&mut e, true);
assert_eq!(events, []);
assert_eq!(value["id"], "lutra-dark");
assert_eq!(value["dark"], true);
assert_eq!(value["tokens"]["--lt-bg"], "#1e1b1e");
assert!(value.get("kind").is_none());
let (value, events) = report(&mut e, false);
assert_eq!(events, [("lutra-light".into(), false)]);
assert_eq!(value["id"], "lutra-light");
assert_eq!(report(&mut e, false).1, []);
assert_eq!(
set_theme(&mut e, "lutra-dark"),
[("lutra-dark".into(), true)]
);
let (value, events) = report(&mut e, true);
assert_eq!(events, []);
assert_eq!(report(&mut e, false).1, []);
assert_eq!(value["id"], "lutra-dark");
assert_eq!(set_theme(&mut e, "system"), [("lutra-light".into(), false)]);
}
#[test]
fn rejects_bad_preference_params() {
let mut e = Engine::new().unwrap();
for params in [
json!({}),
json!({"dark": "yes"}),
json!({"dark": true, "x": 1}),
] {
let err = e.execute("theme.systemPreference", params).unwrap_err();
assert_eq!(err.code(), "invalid_params");
}
}
#[test]
fn the_stored_theme_survives_a_restart() {
let dir = TempDir::new().unwrap();
let config = EngineConfig {
config_dir: dir.path().join("config"),
data_dir: dir.path().join("data"),
};
let mut e = Engine::with_config(config.clone()).unwrap();
set_theme(&mut e, "lutra-light");
drop(e);
let mut e = Engine::with_config(config).unwrap();
let (value, _) = report(&mut e, true);
assert_eq!(value["id"], "lutra-light");
}