Lines
96.15 %
Functions
100 %
Branches
//! The per-user theme keys `tui-theme`, `tui-theme-overrides` and `tui-colors`, and the five-key read.
use super::report_chart::{echoed, pending};
use crate::app::ui_config::READ;
use crate::app::{App, CliConfig};
use crate::draw::draw;
use crate::route::RouteCtx;
use crate::tabs::nms_eval::ConsoleEval;
use crate::test_support::dn_theme;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::style::Color;
use rpc::{ScriptCtx, Session};
use server::lossless::seed;
use sqlx::PgPool;
use sqlx::types::Uuid;
use tvui::{ColorDepth, Role, Theme};
pub(super) fn attached() -> App {
let mut app = crate::test_support::app();
app.attach_console(ConsoleEval::recording().0);
app
}
pub(super) fn reads(app: &App) -> Vec<i64> {
pending(app, |ctx| matches!(ctx, RouteCtx::UiConfig { .. }))
pub(super) fn item(text: Option<&str>) -> String {
match text {
Some(text) => format!(r#""(:config-value \"{text}\")""#),
None => r#""(:config-value nil)""#.to_string(),
/// The reply to a read: the theme triple, then the keymap pair left unset.
fn wire(id: i64, items: [String; 3]) -> String {
let unset = [item(None), item(None)];
let items: Vec<_> = items.into_iter().chain(unset).collect();
format!(r#"(:id {id} :value ({}))"#, items.join(" "))
/// Answers the one read with the preset, override and colour values.
fn answer(app: &mut App, [preset, overrides, colors]: [Option<&str>; 3]) {
let [id] = reads(app)[..] else {
panic!("one config read");
};
let items = [preset, overrides, colors].map(item);
app.route_reply(id, &wire(id, items));
fn preset(name: &str) -> Theme {
Theme::preset(name).expect("preset")
#[tokio::test]
async fn attaching_the_console_asks_for_the_five_keys_in_one_request() {
let (eval, sent) = ConsoleEval::recording();
app.attach_console(eval);
assert_eq!(reads(&app).len(), 1);
let echoed = echoed(&mut app, &sent).await;
let keys = [
"tui-theme",
"tui-theme-overrides",
"tui-colors",
"tui-keymap",
"tui-keymap-overrides",
];
for key in keys {
assert!(
echoed.contains(&format!(r#"(get-config "{key}")"#)),
"{echoed}"
);
async fn the_replies_choose_preset_overrides_and_depth() {
let mut app = attached();
answer(
&mut app,
[Some("turbo"), Some("status = white on red"), Some("16")],
assert_eq!(app.theme.depth(), ColorDepth::Ansi16);
let panel = app.theme.style(Role::Panel);
assert_eq!(panel.fg, Some(Color::LightYellow));
assert_eq!(panel.bg, Some(Color::Blue));
let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
terminal
.draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
.expect("draw");
let cell = &terminal.backend().buffer()[(79, 23)];
assert_eq!(cell.bg, Color::Red);
assert_eq!(cell.fg, Color::White);
async fn missing_keys_keep_the_defaults() {
answer(&mut app, [None, None, None]);
assert_eq!(app.theme.style(Role::Panel), dn_theme().style(Role::Panel));
assert!(app.status.is_empty(), "{}", app.status);
async fn auto_depth_is_the_depth_the_app_started_with() {
let Ok(mut app) = App::new(Uuid::nil(), CliConfig::default(), ColorDepth::Indexed256) else {
panic!("the default theme builds");
answer(&mut app, [None, None, Some("auto")]);
assert_eq!(app.theme.depth(), ColorDepth::Indexed256);
async fn command_line_choices_beat_the_config() {
let cli = CliConfig {
preset: Some("dn".to_string()),
depth: Some(ColorDepth::Rgb),
..CliConfig::default()
let Ok(mut app) = App::new(Uuid::nil(), cli, ColorDepth::Ansi16) else {
panic!("the command-line theme builds");
answer(&mut app, [Some("turbo"), None, Some("16")]);
assert_eq!(app.theme.depth(), ColorDepth::Rgb);
assert_eq!(
app.theme.style(Role::Panel),
preset("dn").style(Role::Panel)
async fn a_bad_override_keeps_the_preset_and_depth_and_shows_the_error() {
[Some("turbo"), Some("status = pink on red"), Some("16")],
assert_eq!(app.status, "theme: line 1: unknown colour `pink`");
let turbo = preset("turbo").with_depth(ColorDepth::Ansi16);
assert_eq!(app.theme.style(Role::Panel), turbo.style(Role::Panel));
assert_eq!(app.theme.style(Role::Status), turbo.style(Role::Status));
async fn an_unknown_preset_or_depth_shows_the_error() {
answer(&mut app, [Some("bogus"), None, None]);
app.status,
"theme: unknown preset `bogus`; known: dn, turbo"
answer(&mut app, [Some("turbo"), None, Some("8")]);
"theme: unknown colour depth `8`; known: rgb, 256, 16, auto"
assert_eq!(app.theme.depth(), dn_theme().depth());
preset("turbo").style(Role::Panel)
async fn a_bad_depth_in_config_is_ignored_when_the_command_line_sets_one() {
depth: Some(ColorDepth::Ansi16),
let Ok(mut app) = App::new(Uuid::nil(), cli, ColorDepth::Rgb) else {
app.attach_echo();
app.read_ui_config();
answer(&mut app, [None, None, Some("8")]);
#[test]
fn a_bad_command_line_preset_is_an_error() {
preset: Some("bogus".to_string()),
let built = App::new(Uuid::nil(), cli, ColorDepth::Rgb);
built.err().as_deref(),
Some("unknown preset `bogus`; known: dn, turbo")
async fn a_stale_reply_is_ignored() {
let [old, new] = reads(&app)[..] else {
panic!("two config reads");
let turbo = [Some("turbo"), None, None].map(item);
app.route_reply(old, &wire(old, turbo));
let current = [Some("turbo"), None, Some("16")].map(item);
app.route_reply(new, &wire(new, current));
async fn a_read_error_is_shown_and_keeps_the_theme() {
let [id] = reads(&app)[..] else {
app.route_reply(
id,
&format!(r#"(:id {id} :error (:code eval :message "boom"))"#),
assert_eq!(app.status, "config: wire error from server: [eval] boom");
async fn a_non_text_value_is_shown_not_taken_as_unset() {
let items = [
item(None),
r#""(:config-value #u8(1 2))""#.to_string(),
assert_eq!(app.status, "config: the config value is not text");
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn a_real_session_answers_the_config_read(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let fixture = seed(&pool).await?;
let mut session = Session::new(ScriptCtx::new(fixture.user))?;
let set = r#"(:id 1 :form (set-config "tui-theme" "turbo"))"#;
session.handle_form(set).await;
let set = r#"(:id 1 :form (set-config "tui-colors" "16"))"#;
anyhow::bail!("one config read");
let wire = session
.handle_form(&format!("(:id {id} :form {READ})"))
.await;
app.route_reply(id, &wire);
app.theme.style(Role::Panel).bg,
preset("turbo")
.with_depth(ColorDepth::Ansi16)
.style(Role::Panel)
.bg
Ok(())