Files
Marcos Paulo 5036ec2dbb feat(editor): wave 7
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-13 20:47:40 -03:00

119 lines
4.4 KiB
Rust

//! Highlight tests: deterministic adversarial coverage invariant, fixture
//! colours per kind, and the oversized-document fallback.
use notescraft_editor::MAX_HIGHLIGHT_BYTES;
use notescraft_editor::colors::EditorColors;
use notescraft_editor::highlight::layout_job;
fn resolver(target: &str) -> bool {
!target.contains("missing")
}
/// Tiny deterministic xorshift generator (no rand dependency).
struct Rng(u32);
impl Rng {
fn next(&mut self) -> u32 {
let mut x = self.0;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
self.0 = x;
x
}
}
#[test]
fn coverage_invariant_on_random_adversarial_strings() {
const ALPHABET: [&str; 15] = ["[", "]", "!", "#", "^", "=", "%", "$", ">", "-", "*", "`", "```", "é", "a"];
let colors = EditorColors::fallback_dark();
let mut rng = Rng(0x5EED_1234);
for case in 0..200 {
let len = (rng.next() % 120) as usize + 1;
let mut text = String::new();
for _ in 0..len {
let pick = (rng.next() % 15) as usize;
let Some(chunk) = ALPHABET.get(pick) else { continue };
text.push_str(chunk);
}
let job = layout_job(&text, &colors, 16.0, f32::INFINITY, &resolver);
assert_eq!(job.text, text, "case {case}: job text must be the source");
// Sections are ordered, contiguous and cover the whole string.
let mut next = 0usize;
for section in &job.sections {
assert_eq!(section.byte_range.start.0, next, "case {case}: gap at {next}");
next = section.byte_range.end.0;
}
assert_eq!(next, text.len(), "case {case}: coverage incomplete");
}
}
#[test]
fn fixture_colours_every_kind() {
let colors = EditorColors::fallback_dark();
let text = "---\ntags: [x]\n---\n\
# Big one\n\
body [[Existing]] [[missing-note]] #tag ==hi== `code` %%c%%\n\
- [ ] task\n\
> [!info] Title\n\
```rust\nfence\n```\n\
[link](dest.md) ^blockid";
let job = layout_job(text, &colors, 16.0, 600.0, &resolver);
let color_at = |needle: &str| -> egui::Color32 {
let start = text.find(needle).unwrap();
job.format_at_byte(egui::text::ByteIndex(start)).color
};
let bg_at = |needle: &str| -> egui::Color32 {
let start = text.find(needle).unwrap();
job.format_at_byte(egui::text::ByteIndex(start)).background
};
let size_at = |needle: &str| -> f32 {
let start = text.find(needle).unwrap();
job.format_at_byte(egui::text::ByteIndex(start)).font_id.size
};
assert_eq!(color_at("---"), colors.muted, "frontmatter muted");
assert_eq!(size_at("# Big one"), 32.0, "h1 is double the base size");
assert_eq!(color_at("# Big one"), colors.heading);
assert_eq!(color_at("[[Existing]]"), colors.link);
assert_eq!(color_at("[[missing-note]]"), colors.link_unresolved);
assert_eq!(color_at("#tag"), colors.tag);
assert_eq!(bg_at("==hi=="), colors.highlight);
assert_eq!(bg_at("`code`"), colors.code_background);
assert_eq!(color_at("`code`"), colors.code);
assert_eq!(color_at("%%c%%"), colors.muted);
assert_eq!(color_at("[ ] task"), colors.accent, "task checkbox accent");
assert_eq!(color_at("[!info]"), colors.accent, "callout header accent");
assert_eq!(bg_at("fence"), colors.code_background, "fence body monospace bg");
assert_eq!(color_at("[link](dest.md)"), colors.link, "internal-ish link via resolver");
assert_eq!(color_at("^blockid"), colors.muted);
}
#[test]
fn oversized_text_falls_back_to_plain() {
let colors = EditorColors::fallback_dark();
let text = "# heading\n".repeat((3 * 1024 * 1024) / 10 + 1);
assert!(text.len() > MAX_HIGHLIGHT_BYTES, "fixture must exceed the parse cutoff");
let job = layout_job(&text, &colors, 16.0, f32::INFINITY, &resolver);
assert_eq!(job.sections.len(), 1);
assert_eq!(job.text.len(), text.len());
}
#[test]
fn empty_and_tiny_inputs() {
let colors = EditorColors::fallback_dark();
let job = layout_job("", &colors, 16.0, 100.0, &resolver);
assert_eq!(job.text, "");
let job = layout_job("word", &colors, 16.0, 100.0, &resolver);
assert_eq!(job.text, "word");
}
#[test]
fn never_panics_on_degenerate_input() {
let colors = EditorColors::fallback_dark();
let nasty = ["[[[[]]]]", "## ## ", "```", "===", "`_`~", "*a*b*c*", "> [!", "[!^#-"];
for text in nasty {
let job = layout_job(text, &colors, 16.0, 100.0, &resolver);
assert_eq!(job.text, text);
}
}