754 lines
29 KiB
Rust
754 lines
29 KiB
Rust
use std::collections::BTreeSet;
|
|
use std::path::Path;
|
|
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use ratatui::buffer::Buffer;
|
|
use ratatui::style::{Color, Modifier};
|
|
use rcom::app::AppState;
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::ui::{
|
|
FileTypePalette, Palette, Theme, ThemeId, ThemeMode, ThemeOrigin, contrast_ratio, render,
|
|
};
|
|
|
|
const KITTY_REPOSITORY: &str = "https://github.com/kovidgoyal/kitty-themes";
|
|
const KITTY_COMMIT: &str = "a35d12290c3d6a74b1f94debc81281cb637f3568";
|
|
const MANIFEST: &str = include_str!("../themes/manifest.json");
|
|
const NOTICES: &str = include_str!("../THIRD_PARTY_NOTICES.md");
|
|
|
|
const CATALOG: [(&str, &str, ThemeMode); 20] = [
|
|
("catppuccin-latte", "Catppuccin Latte", ThemeMode::Light),
|
|
("catppuccin-mocha", "Catppuccin Mocha", ThemeMode::Dark),
|
|
("dracula", "Dracula", ThemeMode::Dark),
|
|
(
|
|
"everforest-dark-medium",
|
|
"Everforest Dark Medium",
|
|
ThemeMode::Dark,
|
|
),
|
|
(
|
|
"everforest-light-medium",
|
|
"Everforest Light Medium",
|
|
ThemeMode::Light,
|
|
),
|
|
("flexoki-dark", "Flexoki Dark", ThemeMode::Dark),
|
|
("flexoki-light", "Flexoki Light", ThemeMode::Light),
|
|
("github-dark", "GitHub Dark", ThemeMode::Dark),
|
|
("github-light", "GitHub Light", ThemeMode::Light),
|
|
("gruvbox-dark", "Gruvbox Dark", ThemeMode::Dark),
|
|
("gruvbox-light", "Gruvbox Light", ThemeMode::Light),
|
|
("night-ops", "Night Ops", ThemeMode::Dark),
|
|
("one-half-dark", "One Half Dark", ThemeMode::Dark),
|
|
("one-half-light", "One Half Light", ThemeMode::Light),
|
|
("rose-pine", "Rosé Pine", ThemeMode::Dark),
|
|
("rose-pine-dawn", "Rosé Pine Dawn", ThemeMode::Light),
|
|
("solarized-dark", "Solarized Dark", ThemeMode::Dark),
|
|
("solarized-light", "Solarized Light", ThemeMode::Light),
|
|
("tokyo-night", "Tokyo Night", ThemeMode::Dark),
|
|
("tokyo-night-day", "Tokyo Night Day", ThemeMode::Light),
|
|
];
|
|
|
|
#[derive(Debug)]
|
|
struct ImportExpectation {
|
|
id: &'static str,
|
|
source_file: &'static str,
|
|
sha256: &'static str,
|
|
author: &'static str,
|
|
upstream_url: &'static str,
|
|
license_repository: &'static str,
|
|
license_commit: &'static str,
|
|
upstream_license: Option<&'static str>,
|
|
notice: &'static str,
|
|
effective_license: &'static str,
|
|
}
|
|
|
|
const IMPORTS: [ImportExpectation; 19] = [
|
|
ImportExpectation {
|
|
id: "dracula",
|
|
source_file: "themes/Dracula.conf",
|
|
sha256: "a4075ea3798795960e0205638ff55f6a506f60437938d94dc2a1aab62140d3ae",
|
|
author: "Keegan Carruthers-Smith",
|
|
upstream_url: "https://raw.githubusercontent.com/dracula/kitty/master/dracula.conf",
|
|
license_repository: "https://github.com/dracula/kitty",
|
|
license_commit: "87717a3f00e3dff0fc10c93f5ff535ea4092de70",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/dracula__kitty.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "catppuccin-mocha",
|
|
source_file: "themes/Catppuccin-Mocha.conf",
|
|
sha256: "9d3f69cdb6d14d651782f7265ad9f00a687344fe8508b171b16688da9ae2bfa0",
|
|
author: "Pocco81 (https://github.com/Pocco81)",
|
|
upstream_url: "https://github.com/catppuccin/kitty/blob/main/themes/mocha.conf",
|
|
license_repository: "https://github.com/catppuccin/kitty",
|
|
license_commit: "177d447f0c3626959ef001b4605f5d844541fd63",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/catppuccin__kitty.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "catppuccin-latte",
|
|
source_file: "themes/Catppuccin-Latte.conf",
|
|
sha256: "3e32d197b44d817db2e2c5835e698184c66b43b85fd78dce1e1b235b0d014974",
|
|
author: "Pocco81 (https://github.com/Pocco81)",
|
|
upstream_url: "https://github.com/catppuccin/kitty/blob/main/themes/latte.conf",
|
|
license_repository: "https://github.com/catppuccin/kitty",
|
|
license_commit: "177d447f0c3626959ef001b4605f5d844541fd63",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/catppuccin__kitty.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "gruvbox-dark",
|
|
source_file: "themes/gruvbox-dark.conf",
|
|
sha256: "3c7162ea53fde5e0437d025563689d9868970839e28a7bbd35bd52bfd5f30afa",
|
|
author: "Pavel Pertsev",
|
|
upstream_url: "https://raw.githubusercontent.com/gruvbox-community/gruvbox-contrib/master/kitty/gruvbox-dark.conf",
|
|
license_repository: "https://github.com/gruvbox-community/gruvbox",
|
|
license_commit: "180ad85971343df68be3422a5630fa84e45a9ab2",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/gruvbox-community__gruvbox.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "gruvbox-light",
|
|
source_file: "themes/gruvbox-light.conf",
|
|
sha256: "7304f0b97be113af68d3a8ae85cbeca7a24346ce5847bd3710c837e6107e1073",
|
|
author: "Pavel Pertsev",
|
|
upstream_url: "https://raw.githubusercontent.com/gruvbox-community/gruvbox-contrib/master/kitty/gruvbox-light.conf",
|
|
license_repository: "https://github.com/gruvbox-community/gruvbox",
|
|
license_commit: "180ad85971343df68be3422a5630fa84e45a9ab2",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/gruvbox-community__gruvbox.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "solarized-dark",
|
|
source_file: "themes/Solarized_Dark.conf",
|
|
sha256: "fc23242afc7cbbc7b6b97ac45a4607e1e9181d4823b42054dc3d9099fd9e1743",
|
|
author: "Ethan Schoonover",
|
|
upstream_url: "https://github.com/altercation/solarized",
|
|
license_repository: "https://github.com/altercation/solarized",
|
|
license_commit: "62f656a02f93c5190a8753159e34b385588d5ff3",
|
|
upstream_license: None,
|
|
notice: "LICENSES/themes/altercation__solarized.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "solarized-light",
|
|
source_file: "themes/Solarized_Light.conf",
|
|
sha256: "3c5064623dbe2deb76d1efa2f304893182486d511567893a9653fef65ae8c9d4",
|
|
author: "Ethan Schoonover",
|
|
upstream_url: "https://github.com/altercation/solarized",
|
|
license_repository: "https://github.com/altercation/solarized",
|
|
license_commit: "62f656a02f93c5190a8753159e34b385588d5ff3",
|
|
upstream_license: None,
|
|
notice: "LICENSES/themes/altercation__solarized.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "tokyo-night",
|
|
source_file: "themes/tokyo_night_night.conf",
|
|
sha256: "e92085fb339ca9be916e3bc47fa6813843c6007b155d4f9bcb9fb5af545adc5c",
|
|
author: "Folke Lemaitre",
|
|
upstream_url: "https://github.com/folke/tokyonight.nvim/raw/main/extras/kitty/tokyonight_night.conf",
|
|
license_repository: "https://github.com/folke/tokyonight.nvim",
|
|
license_commit: "d8e9ca64c0b8e574051f14bf1486ecd12b81de89",
|
|
upstream_license: Some("Apache-2.0"),
|
|
notice: "LICENSES/themes/folke__tokyonight.nvim.txt",
|
|
effective_license: "Apache-2.0",
|
|
},
|
|
ImportExpectation {
|
|
id: "tokyo-night-day",
|
|
source_file: "themes/tokyo_night_day.conf",
|
|
sha256: "3f6bb553ce120c20ce04c9f71eb811d4a4b3c3298370c1a04aa933ea244ec5bd",
|
|
author: "Folke Lemaitre",
|
|
upstream_url: "https://github.com/folke/tokyonight.nvim/raw/main/extras/kitty/tokyonight_day.conf",
|
|
license_repository: "https://github.com/folke/tokyonight.nvim",
|
|
license_commit: "d8e9ca64c0b8e574051f14bf1486ecd12b81de89",
|
|
upstream_license: Some("Apache-2.0"),
|
|
notice: "LICENSES/themes/folke__tokyonight.nvim.txt",
|
|
effective_license: "Apache-2.0",
|
|
},
|
|
ImportExpectation {
|
|
id: "one-half-dark",
|
|
source_file: "themes/OneHalfDark.conf",
|
|
sha256: "187e3e44bb61305f8d94bfa1513d5c19253672879e5429c96dba0add22f26120",
|
|
author: "Son A. Pham",
|
|
upstream_url: "https://raw.githubusercontent.com/sonph/onehalf/master/kitty/onehalf-dark.conf",
|
|
license_repository: "https://github.com/sonph/onehalf",
|
|
license_commit: "75eb2e97acd74660779fed8380989ee7891eec56",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/sonph__onehalf.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "one-half-light",
|
|
source_file: "themes/OneHalfLight.conf",
|
|
sha256: "5329fccb5d0ba8cbd86292756bd7aa7cc1fca9098b51466cf4a419ae8eb3d797",
|
|
author: "Son A. Pham",
|
|
upstream_url: "https://raw.githubusercontent.com/sonph/onehalf/master/kitty/onehalf-light.conf",
|
|
license_repository: "https://github.com/sonph/onehalf",
|
|
license_commit: "75eb2e97acd74660779fed8380989ee7891eec56",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/sonph__onehalf.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "rose-pine",
|
|
source_file: "themes/rose-pine.conf",
|
|
sha256: "0e8885538373c1e279553d2a6ce1ee2bf6a8302beaa9ccded9e39818225971ce",
|
|
author: "mvllow",
|
|
upstream_url: "https://github.com/rose-pine/kitty/blob/main/dist/rose-pine.conf",
|
|
license_repository: "https://github.com/rose-pine/kitty",
|
|
license_commit: "efd4f01cb9887feaa7114ff21a887464295d0205",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/rose-pine__kitty.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "rose-pine-dawn",
|
|
source_file: "themes/rose-pine-dawn.conf",
|
|
sha256: "8a7a2de3d2e3018c02f6ab16e80b9600f151a8891da8ad8c2764e145adbd6f7a",
|
|
author: "mvllow",
|
|
upstream_url: "https://github.com/rose-pine/kitty/blob/main/dist/rose-pine-dawn.conf",
|
|
license_repository: "https://github.com/rose-pine/kitty",
|
|
license_commit: "efd4f01cb9887feaa7114ff21a887464295d0205",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/rose-pine__kitty.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "everforest-dark-medium",
|
|
source_file: "themes/everforest_dark_medium.conf",
|
|
sha256: "038bf240b421f1430507f4432b7704403e917fbe69866b3bcaaeb55ec1a4be60",
|
|
author: "Sainnhe Park",
|
|
upstream_url: "https://github.com/ewal/kitty-everforest/blob/master/themes/everforest_dark_medium.conf",
|
|
license_repository: "https://github.com/sainnhe/everforest",
|
|
license_commit: "85a86eb62409e3ec88713bff3d1b9d7374e112e4",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/sainnhe__everforest.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "everforest-light-medium",
|
|
source_file: "themes/everforest_light_medium.conf",
|
|
sha256: "5009e7e9f24d0e5ea42ddd0fd481d74317bdd04bd53384b2c8fbe2bd8cab246d",
|
|
author: "Sainnhe Park",
|
|
upstream_url: "https://github.com/ewal/kitty-everforest/blob/master/themes/everforest_light_medium.conf",
|
|
license_repository: "https://github.com/sainnhe/everforest",
|
|
license_commit: "85a86eb62409e3ec88713bff3d1b9d7374e112e4",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/sainnhe__everforest.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "github-dark",
|
|
source_file: "themes/GitHub_Dark.conf",
|
|
sha256: "db70f8f1b4204098dc464ea47c3de5071697bd6cc2c4f9d36068c30dd96eb824",
|
|
author: "GitHub",
|
|
upstream_url: "https://github.com/primer/github-vscode-theme",
|
|
license_repository: "https://github.com/primer/github-vscode-theme",
|
|
license_commit: "738b0ac78f6a7cc16c8d541a71fa103d8637a64a",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/primer__github-vscode-theme.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "github-light",
|
|
source_file: "themes/GitHub_Light.conf",
|
|
sha256: "02dc60b8e33709fcaf728daec85882039ece71569bfc1e5c8757181b249dce9c",
|
|
author: "GitHub",
|
|
upstream_url: "https://github.com/primer/github-vscode-theme",
|
|
license_repository: "https://github.com/primer/github-vscode-theme",
|
|
license_commit: "738b0ac78f6a7cc16c8d541a71fa103d8637a64a",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/primer__github-vscode-theme.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "flexoki-dark",
|
|
source_file: "themes/flexoki_dark.conf",
|
|
sha256: "126a74cd3d403611104aeaeb53d55a4b92f0a47ea1745f75b412e59aebc60975",
|
|
author: "Kepano",
|
|
upstream_url: "https://raw.githubusercontent.com/kepano/flexoki/main/kitty/flexoki_dark.conf",
|
|
license_repository: "https://github.com/kepano/flexoki",
|
|
license_commit: "8d723bac4a9ac46adfdf99d42155286977aac72a",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/kepano__flexoki.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
ImportExpectation {
|
|
id: "flexoki-light",
|
|
source_file: "themes/flexoki_light.conf",
|
|
sha256: "10c914e13591d500704750599728b35b9277635f33e76af7b52fc27039cbb284",
|
|
author: "Kepano",
|
|
upstream_url: "https://raw.githubusercontent.com/kepano/flexoki/main/kitty/flexoki_light.conf",
|
|
license_repository: "https://github.com/kepano/flexoki",
|
|
license_commit: "8d723bac4a9ac46adfdf99d42155286977aac72a",
|
|
upstream_license: Some("MIT"),
|
|
notice: "LICENSES/themes/kepano__flexoki.txt",
|
|
effective_license: "MIT",
|
|
},
|
|
];
|
|
|
|
const fn rgb(red: u8, green: u8, blue: u8) -> Color {
|
|
Color::Rgb(red, green, blue)
|
|
}
|
|
|
|
#[test]
|
|
fn catalog_has_exact_stable_ids_display_name_order_and_mode_counts() {
|
|
let actual = ThemeId::ALL
|
|
.iter()
|
|
.map(|id| (id.as_str(), id.display_name(), id.mode()))
|
|
.collect::<Vec<_>>();
|
|
assert_eq!(actual, CATALOG);
|
|
|
|
let unique = actual.iter().map(|entry| entry.0).collect::<BTreeSet<_>>();
|
|
assert_eq!(unique.len(), 20);
|
|
assert_eq!(
|
|
actual
|
|
.iter()
|
|
.filter(|entry| entry.2 == ThemeMode::Dark)
|
|
.count(),
|
|
11
|
|
);
|
|
assert_eq!(
|
|
actual
|
|
.iter()
|
|
.filter(|entry| entry.2 == ThemeMode::Light)
|
|
.count(),
|
|
9
|
|
);
|
|
assert!(actual.windows(2).all(|pair| pair[0].1 <= pair[1].1));
|
|
|
|
let expected_ids = CATALOG
|
|
.iter()
|
|
.map(|entry| entry.0)
|
|
.collect::<Vec<_>>()
|
|
.join(", ");
|
|
assert_eq!(ThemeId::available_ids(), expected_ids);
|
|
for id in ThemeId::ALL {
|
|
assert_eq!(id.to_string(), id.as_str());
|
|
assert_eq!(id.as_str().parse::<ThemeId>().unwrap(), id);
|
|
}
|
|
assert!("not-a-theme".parse::<ThemeId>().is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn definitions_match_the_catalog_and_native_import_boundaries() {
|
|
for id in ThemeId::ALL {
|
|
let definition = id.definition();
|
|
assert_eq!(definition.id, id.as_str());
|
|
assert_eq!(definition.display_name, id.display_name());
|
|
assert_eq!(definition.mode, id.mode());
|
|
if id == ThemeId::NightOps {
|
|
assert_eq!(definition.origin, ThemeOrigin::Native);
|
|
assert_eq!(definition.native_palette, Some(Palette::NIGHT_OPS));
|
|
assert!(definition.seed.is_none());
|
|
} else {
|
|
assert_eq!(definition.origin, ThemeOrigin::Kitty);
|
|
assert!(definition.native_palette.is_none());
|
|
assert!(definition.seed.is_some());
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn night_ops_default_keeps_base_palette_and_pins_accessibility_corrections() {
|
|
let expected_files = FileTypePalette {
|
|
directory: rgb(0x82, 0xaa, 0xff),
|
|
regular: rgb(0xd8, 0xe0, 0xf0),
|
|
executable: rgb(0x9e, 0xce, 0x6a),
|
|
symlink: rgb(0x65, 0xd6, 0xe8),
|
|
image: rgb(0xe0, 0xaf, 0x68),
|
|
media: rgb(0xbb, 0x9a, 0xf7),
|
|
document: rgb(0x7d, 0xcf, 0xff),
|
|
archive: rgb(0xff, 0x7a, 0x90),
|
|
special: rgb(0xe0, 0xaf, 0x68),
|
|
rust_icon: rgb(0xde, 0xa5, 0x84),
|
|
};
|
|
let expected = Palette {
|
|
background: rgb(0x0b, 0x10, 0x20),
|
|
panel: rgb(0x11, 0x18, 0x2b),
|
|
text: rgb(0xd8, 0xe0, 0xf0),
|
|
muted: rgb(0x78, 0x86, 0xa0),
|
|
active: rgb(0x65, 0xd6, 0xe8),
|
|
secondary: rgb(0xa8, 0x9b, 0xff),
|
|
error: rgb(0xff, 0x7a, 0x90),
|
|
selection: rgb(0x62, 0x6e, 0x80),
|
|
selection_text: rgb(0xff, 0xff, 0xff),
|
|
file_types: expected_files,
|
|
};
|
|
assert_eq!(ThemeId::default(), ThemeId::NightOps);
|
|
assert_eq!(Theme::default(), Theme::night_ops());
|
|
assert_eq!(Theme::default().palette, expected);
|
|
assert_eq!(Palette::NIGHT_OPS, expected);
|
|
assert_eq!(FileTypePalette::NIGHT_OPS, expected_files);
|
|
}
|
|
|
|
fn draw_night_ops_selected(theme: Theme) -> Buffer {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
app.active_pane_mut().entries = vec![Entry::new("main.rs", EntryKind::File)];
|
|
app.active_pane_mut().selection.set(Some(0), 1);
|
|
let backend = TestBackend::new(120, 24);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, &app, None, &theme))
|
|
.unwrap();
|
|
terminal.backend().buffer().clone()
|
|
}
|
|
|
|
fn night_ops_selected_cells(buffer: &Buffer) -> [(u16, u16); 2] {
|
|
let row = (2..buffer.area.height)
|
|
.find(|&y| {
|
|
let text = (0..buffer.area.width)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect::<String>();
|
|
text.contains('▸') && text.contains("main.rs")
|
|
})
|
|
.expect("selected Night Ops row must retain its textual marker and filename");
|
|
let find = |symbol: &str| {
|
|
(0..buffer.area.width)
|
|
.map(|x| (x, row))
|
|
.find(|&position| buffer.cell(position).unwrap().symbol() == symbol)
|
|
.unwrap_or_else(|| panic!("missing {symbol} in the selected Night Ops row"))
|
|
};
|
|
[find("🦀"), find("m")]
|
|
}
|
|
|
|
#[test]
|
|
fn night_ops_selection_keeps_structural_rich_and_monochrome_contracts() {
|
|
let rich = draw_night_ops_selected(Theme::night_ops());
|
|
for position in night_ops_selected_cells(&rich) {
|
|
let style = rich.cell(position).unwrap().style();
|
|
assert_eq!(style.fg, Some(Palette::NIGHT_OPS.selection_text));
|
|
assert_eq!(style.bg, Some(Palette::NIGHT_OPS.selection));
|
|
assert!(style.add_modifier.contains(Modifier::BOLD));
|
|
assert!(!style.add_modifier.contains(Modifier::REVERSED));
|
|
}
|
|
|
|
let monochrome = draw_night_ops_selected(Theme::with_capabilities_for(
|
|
ThemeId::NightOps,
|
|
false,
|
|
false,
|
|
));
|
|
for position in night_ops_selected_cells(&monochrome) {
|
|
let style = monochrome.cell(position).unwrap().style();
|
|
assert_eq!(style.fg, Some(Color::Reset));
|
|
assert!(style.add_modifier.contains(Modifier::BOLD));
|
|
assert!(style.add_modifier.contains(Modifier::REVERSED));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn contrast_ratio_matches_known_wcag_pairs_and_is_symmetric() {
|
|
let black = rgb(0x00, 0x00, 0x00);
|
|
let white = rgb(0xff, 0xff, 0xff);
|
|
let gray_77 = rgb(0x77, 0x77, 0x77);
|
|
let gray_76 = rgb(0x76, 0x76, 0x76);
|
|
assert!((contrast_ratio(black, white) - 21.0).abs() < 1e-9);
|
|
assert!((contrast_ratio(white, gray_77) - 4.478_089).abs() < 0.000_01);
|
|
assert!((contrast_ratio(white, gray_76) - 4.542_224_959_6).abs() < 0.000_01);
|
|
assert_eq!(contrast_ratio(black, black), 1.0);
|
|
assert_eq!(
|
|
contrast_ratio(gray_77, white),
|
|
contrast_ratio(white, gray_77)
|
|
);
|
|
}
|
|
|
|
fn check_contrast(
|
|
failures: &mut Vec<String>,
|
|
id: ThemeId,
|
|
role: &str,
|
|
foreground: Color,
|
|
surface_name: &str,
|
|
surface: Color,
|
|
target: f64,
|
|
) {
|
|
let ratio = contrast_ratio(foreground, surface);
|
|
if ratio < target {
|
|
failures.push(format!(
|
|
"{}: {role} {foreground:?} on {surface_name} {surface:?} is {ratio:.6}:1, expected at least {target}:1",
|
|
id.as_str()
|
|
));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_derived_semantic_unselected_file_and_selected_text_color_meets_contrast() {
|
|
let mut failures = Vec::new();
|
|
for id in ThemeId::ALL {
|
|
let palette = Theme::with_capabilities_for(id, true, false).palette;
|
|
for (role, foreground) in [
|
|
("text", palette.text),
|
|
("muted", palette.muted),
|
|
("active", palette.active),
|
|
("secondary", palette.secondary),
|
|
("error", palette.error),
|
|
] {
|
|
check_contrast(
|
|
&mut failures,
|
|
id,
|
|
role,
|
|
foreground,
|
|
"background",
|
|
palette.background,
|
|
4.5,
|
|
);
|
|
check_contrast(
|
|
&mut failures,
|
|
id,
|
|
role,
|
|
foreground,
|
|
"panel",
|
|
palette.panel,
|
|
4.5,
|
|
);
|
|
}
|
|
check_contrast(
|
|
&mut failures,
|
|
id,
|
|
"selection_text",
|
|
palette.selection_text,
|
|
"selection",
|
|
palette.selection,
|
|
4.5,
|
|
);
|
|
|
|
let files = palette.file_types;
|
|
for (role, foreground) in [
|
|
("file.regular", files.regular),
|
|
("file.directory", files.directory),
|
|
("file.executable", files.executable),
|
|
("file.symlink", files.symlink),
|
|
("file.image", files.image),
|
|
("file.media", files.media),
|
|
("file.document", files.document),
|
|
("file.archive", files.archive),
|
|
("file.special", files.special),
|
|
("icon.rust", files.rust_icon),
|
|
] {
|
|
for (surface_name, surface) in
|
|
[("background", palette.background), ("panel", palette.panel)]
|
|
{
|
|
check_contrast(
|
|
&mut failures,
|
|
id,
|
|
role,
|
|
foreground,
|
|
surface_name,
|
|
surface,
|
|
4.5,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
assert!(failures.is_empty(), "{}", failures.join("\n"));
|
|
}
|
|
|
|
#[test]
|
|
fn every_derived_selection_boundary_has_three_to_one_contrast_with_background_and_panel() {
|
|
let mut failures = Vec::new();
|
|
for id in ThemeId::ALL {
|
|
let palette = Theme::with_capabilities_for(id, true, false).palette;
|
|
for (surface_name, surface) in
|
|
[("background", palette.background), ("panel", palette.panel)]
|
|
{
|
|
check_contrast(
|
|
&mut failures,
|
|
id,
|
|
"selection boundary",
|
|
palette.selection,
|
|
surface_name,
|
|
surface,
|
|
3.0,
|
|
);
|
|
}
|
|
}
|
|
assert!(failures.is_empty(), "{}", failures.join("\n"));
|
|
}
|
|
|
|
fn manifest_objects(source: &str) -> Vec<&str> {
|
|
let body = source
|
|
.strip_prefix("[\n")
|
|
.and_then(|body| body.strip_suffix("\n]\n"))
|
|
.expect("manifest must be a pretty-printed JSON array");
|
|
let mut objects = body.split("\n },\n").collect::<Vec<_>>();
|
|
let last = objects
|
|
.pop()
|
|
.expect("manifest must not be empty")
|
|
.strip_suffix("\n }")
|
|
.expect("last manifest object must be closed");
|
|
objects.push(last);
|
|
objects
|
|
.into_iter()
|
|
.map(|object| {
|
|
object
|
|
.strip_prefix(" {\n")
|
|
.expect("manifest object must use the importer's stable indentation")
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn raw_field<'a>(object: &'a str, indentation: usize, key: &str) -> &'a str {
|
|
let prefix = format!("{}\"{key}\": ", " ".repeat(indentation));
|
|
let value = object
|
|
.lines()
|
|
.find_map(|line| line.strip_prefix(&prefix))
|
|
.unwrap_or_else(|| panic!("missing manifest field {key} at indentation {indentation}"));
|
|
value.strip_suffix(',').unwrap_or(value)
|
|
}
|
|
|
|
fn string_field<'a>(object: &'a str, indentation: usize, key: &str) -> &'a str {
|
|
raw_field(object, indentation, key)
|
|
.strip_prefix('"')
|
|
.and_then(|value| value.strip_suffix('"'))
|
|
.unwrap_or_else(|| panic!("manifest field {key} must be a JSON string"))
|
|
}
|
|
|
|
fn assert_hex(value: &str, length: usize, field: &str) {
|
|
assert_eq!(value.len(), length, "{field} has the wrong length");
|
|
assert!(
|
|
value.bytes().all(|byte| byte.is_ascii_hexdigit()),
|
|
"{field} must be hexadecimal: {value}"
|
|
);
|
|
assert_eq!(
|
|
value,
|
|
value.to_ascii_lowercase(),
|
|
"{field} must be lowercase"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn manifest_pins_every_import_and_retains_complete_license_provenance() {
|
|
let objects = manifest_objects(MANIFEST);
|
|
assert_eq!(objects.len(), IMPORTS.len());
|
|
let manifest_ids = objects
|
|
.iter()
|
|
.map(|object| string_field(object, 4, "id"))
|
|
.collect::<BTreeSet<_>>();
|
|
assert_eq!(manifest_ids.len(), IMPORTS.len());
|
|
assert!(!manifest_ids.contains("night-ops"));
|
|
|
|
for (object, expected) in objects.into_iter().zip(&IMPORTS) {
|
|
let id = expected.id.parse::<ThemeId>().unwrap();
|
|
assert_eq!(string_field(object, 4, "id"), expected.id);
|
|
assert_eq!(string_field(object, 4, "display_name"), id.display_name());
|
|
assert_eq!(string_field(object, 4, "mode"), id.mode().as_str());
|
|
assert_eq!(string_field(object, 4, "origin"), "kitty");
|
|
assert_eq!(string_field(object, 4, "author"), expected.author);
|
|
assert_eq!(string_field(object, 4, "license"), "MIT");
|
|
assert_eq!(raw_field(object, 4, "derivation_version"), "1");
|
|
|
|
assert_eq!(string_field(object, 6, "repository"), KITTY_REPOSITORY);
|
|
assert_eq!(string_field(object, 6, "commit"), KITTY_COMMIT);
|
|
assert_hex(KITTY_COMMIT, 40, "kitty source commit");
|
|
assert_eq!(string_field(object, 6, "file"), expected.source_file);
|
|
assert_eq!(string_field(object, 6, "sha256"), expected.sha256);
|
|
assert_hex(expected.sha256, 64, "source SHA-256");
|
|
|
|
assert_eq!(string_field(object, 6, "url"), expected.upstream_url);
|
|
assert_eq!(
|
|
string_field(object, 6, "license_repository"),
|
|
expected.license_repository
|
|
);
|
|
assert_eq!(
|
|
string_field(object, 6, "license_commit"),
|
|
expected.license_commit
|
|
);
|
|
assert_hex(expected.license_commit, 40, "upstream license commit");
|
|
let upstream_license = match expected.upstream_license {
|
|
Some(license) => format!("\"{license}\""),
|
|
None => "null".to_owned(),
|
|
};
|
|
assert_eq!(raw_field(object, 6, "license"), upstream_license);
|
|
assert_eq!(string_field(object, 6, "license_file"), expected.notice);
|
|
|
|
let notice_path = Path::new(env!("CARGO_MANIFEST_DIR")).join(expected.notice);
|
|
let notice = std::fs::read_to_string(¬ice_path)
|
|
.unwrap_or_else(|error| panic!("cannot read {}: {error}", notice_path.display()));
|
|
match expected.effective_license {
|
|
"MIT" => {
|
|
assert!(notice.contains("Copyright"), "{}", expected.notice);
|
|
assert!(
|
|
notice.contains("Permission is hereby granted"),
|
|
"{}",
|
|
expected.notice
|
|
);
|
|
assert!(
|
|
notice.contains("THE SOFTWARE IS PROVIDED"),
|
|
"{}",
|
|
expected.notice
|
|
);
|
|
}
|
|
"Apache-2.0" => {
|
|
assert!(notice.contains("Apache License"), "{}", expected.notice);
|
|
assert!(
|
|
notice.contains("Version 2.0, January 2004"),
|
|
"{}",
|
|
expected.notice
|
|
);
|
|
assert!(
|
|
notice.contains("END OF TERMS AND CONDITIONS"),
|
|
"{}",
|
|
expected.notice
|
|
);
|
|
}
|
|
other => panic!("unexpected test license {other}"),
|
|
}
|
|
}
|
|
|
|
for id in ["solarized-dark", "solarized-light"] {
|
|
assert!(manifest_ids.contains(id));
|
|
}
|
|
let tokyo = IMPORTS
|
|
.iter()
|
|
.filter(|entry| entry.id.starts_with("tokyo-night"));
|
|
assert_eq!(tokyo.count(), 2);
|
|
assert!(
|
|
IMPORTS
|
|
.iter()
|
|
.filter(|entry| entry.id.starts_with("tokyo-night"))
|
|
.all(|entry| entry.upstream_license == Some("Apache-2.0"))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn third_party_notices_cover_every_manifest_entry_and_apache_tokyo_night() {
|
|
assert!(NOTICES.contains("## Curated kitty color themes"));
|
|
assert!(NOTICES.contains(KITTY_REPOSITORY));
|
|
assert!(NOTICES.contains(KITTY_COMMIT));
|
|
assert!(NOTICES.contains("without network access"));
|
|
assert_eq!(NOTICES.matches("\n### ").count(), IMPORTS.len());
|
|
|
|
for expected in &IMPORTS {
|
|
let id = expected.id.parse::<ThemeId>().unwrap();
|
|
let heading = format!("### {} (`{}`)", id.display_name(), expected.id);
|
|
let start = NOTICES
|
|
.find(&heading)
|
|
.unwrap_or_else(|| panic!("missing notices heading {heading}"));
|
|
let after_heading = &NOTICES[start + heading.len()..];
|
|
let end = after_heading.find("\n### ").unwrap_or(after_heading.len());
|
|
let section = &after_heading[..end];
|
|
for required in [
|
|
expected.author,
|
|
expected.source_file,
|
|
expected.sha256,
|
|
expected.upstream_url,
|
|
expected.license_repository,
|
|
expected.license_commit,
|
|
expected.notice,
|
|
expected.effective_license,
|
|
] {
|
|
assert!(
|
|
section.contains(required),
|
|
"{heading} does not mention {required}"
|
|
);
|
|
}
|
|
if expected.id.starts_with("tokyo-night") {
|
|
assert!(section.contains("Apache-2.0"));
|
|
assert!(section.contains("kitty metadata: MIT"));
|
|
}
|
|
}
|
|
}
|