[core] Tests: canon parser, claim checker, hook templates, property tests
This commit is contained in:
@@ -0,0 +1,341 @@
|
||||
// SPDX-License-Identifier: CPAL-1.0
|
||||
// Copyright (c) 2026 Marek Novák. Exhibit A notice: LICENSE at the repository root.
|
||||
|
||||
use scaena_core::canon::{self, Canon, CanonCode, CanonFile, Status};
|
||||
|
||||
const TABULA: &str = "---
|
||||
id: tabula
|
||||
name: Tabula
|
||||
one_liner: Notes that stay on your own computer.
|
||||
audience: People who want notes without an account.
|
||||
price: (assumption) one-time purchase
|
||||
clicks: New note in 1 click.
|
||||
status: released
|
||||
aula: https://aula.marek-novak.cz/tabula/
|
||||
---
|
||||
<!-- sources: Mark, 2026-11-23 -->
|
||||
|
||||
# Tabula
|
||||
|
||||
## What it does
|
||||
- [F1] Tabula keeps notes on your own computer.
|
||||
- [F2] It syncs between your devices without a cloud account. (assumption)
|
||||
|
||||
## Honest limits
|
||||
- [F3] There is no phone app yet. Docs: https://aula.marek-novak.cz/tabula/limits.
|
||||
";
|
||||
|
||||
fn code(stem: &str, text: &str) -> (usize, CanonCode) {
|
||||
let e = canon::parse(stem, text).expect_err("must be rejected");
|
||||
(e.line, e.code)
|
||||
}
|
||||
|
||||
fn with_front(front: &str, body: &str) -> String {
|
||||
format!("---\n{front}---\n{body}")
|
||||
}
|
||||
|
||||
const FRONT: &str = "id: tabula
|
||||
name: Tabula
|
||||
one_liner: Notes.
|
||||
audience: Everyone.
|
||||
price: free
|
||||
clicks: 1 click.
|
||||
status: beta
|
||||
";
|
||||
|
||||
#[test]
|
||||
fn parses_the_sample_file() {
|
||||
let f = canon::parse("tabula", TABULA).unwrap();
|
||||
assert_eq!(f.id, "tabula");
|
||||
assert_eq!(f.front.name, "Tabula");
|
||||
assert_eq!(f.front.one_liner, "Notes that stay on your own computer.");
|
||||
assert_eq!(f.front.audience, "People who want notes without an account.");
|
||||
assert_eq!(f.front.price, "(assumption) one-time purchase");
|
||||
assert!(canon::is_assumption_value(&f.front.price));
|
||||
assert!(!canon::is_assumption_value(&f.front.name));
|
||||
assert_eq!(f.front.clicks, "New note in 1 click.");
|
||||
assert_eq!(f.front.status, Status::Released);
|
||||
assert_eq!(f.front.aula.as_deref(), Some("https://aula.marek-novak.cz/tabula/"));
|
||||
assert_eq!(f.front.site, None);
|
||||
assert_eq!(f.facts.len(), 3);
|
||||
let f1 = f.fact(1).unwrap();
|
||||
assert_eq!(f1.text, "Tabula keeps notes on your own computer.");
|
||||
assert!(!f1.assumption);
|
||||
assert_eq!(f1.line, 16);
|
||||
let f2 = f.fact(2).unwrap();
|
||||
assert_eq!(f2.text, "It syncs between your devices without a cloud account.");
|
||||
assert!(f2.assumption);
|
||||
assert_eq!(f2.line, 17);
|
||||
assert!(f.fact(4).is_none());
|
||||
assert!(f.fact(0).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn urls_are_front_matter_plus_https_in_facts() {
|
||||
let f = canon::parse("tabula", TABULA).unwrap();
|
||||
assert_eq!(
|
||||
f.urls(),
|
||||
vec![
|
||||
"https://aula.marek-novak.cz/tabula/".to_string(),
|
||||
"https://aula.marek-novak.cz/tabula/limits".to_string(),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_urls_in_facts_are_not_allowed() {
|
||||
let text = with_front(FRONT, "- [F1] See http://example.com/x and https://example.com/y.\n");
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
assert_eq!(f.urls(), vec!["https://example.com/y".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn site_and_status_values() {
|
||||
for (s, st) in [("released", Status::Released), ("beta", Status::Beta), ("planned", Status::Planned)] {
|
||||
let front = FRONT.replace("status: beta", &format!("status: {s}"));
|
||||
let text = with_front(&format!("{front}site: https://tabula.example/\n"), "");
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
assert_eq!(f.front.status, st);
|
||||
assert_eq!(f.front.site.as_deref(), Some("https://tabula.example/"));
|
||||
assert!(f.facts.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_status() {
|
||||
let front = FRONT.replace("status: beta", "status: Released");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (8, CanonCode::BadStatus("Released".into())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_unknown_duplicate_and_empty_keys() {
|
||||
let front = FRONT.replace("price: free\n", "");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")).1, CanonCode::MissingKey("price".into()));
|
||||
|
||||
let front = format!("{FRONT}colour: brass\n");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (9, CanonCode::UnknownKey("colour".into())));
|
||||
|
||||
let front = format!("{FRONT}name: Other\n");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (9, CanonCode::DuplicateKey("name".into())));
|
||||
|
||||
let front = FRONT.replace("price: free", "price:");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")).1, CanonCode::EmptyValue("price".into()));
|
||||
|
||||
let front = FRONT.replace("price: free", "price: ");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")).1, CanonCode::EmptyValue("price".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_non_flat_front_matter() {
|
||||
for bad in ["just text", " indented: yes", "list:\n - a", "", "# comment", "Name: Tabula", "key:value"] {
|
||||
let front = format!("{FRONT}{bad}\n");
|
||||
let (line, c) = code("tabula", &with_front(&front, ""));
|
||||
assert_eq!(line, 9, "{bad:?}");
|
||||
assert!(
|
||||
matches!(c, CanonCode::FrontMatterLine | CanonCode::UnknownKey(_)),
|
||||
"{bad:?} gave {c:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_or_unclosed_front_matter() {
|
||||
assert_eq!(code("tabula", "# Tabula\n- [F1] x\n"), (1, CanonCode::NoFrontMatter));
|
||||
assert_eq!(code("tabula", ""), (1, CanonCode::NoFrontMatter));
|
||||
assert_eq!(code("tabula", " ---\n"), (1, CanonCode::NoFrontMatter));
|
||||
assert_eq!(code("tabula", &format!("---\n{FRONT}")).1, CanonCode::UnclosedFrontMatter);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_must_match_stem_and_be_valid() {
|
||||
assert_eq!(
|
||||
code("other", &with_front(FRONT, "")),
|
||||
(1, CanonCode::IdMismatch { id: "tabula".into(), stem: "other".into() })
|
||||
);
|
||||
for bad in ["Tabula", "-tabula", "tabula-", "tab_ula", "tab ula", "a234567890123456789012345678901234"] {
|
||||
let front = FRONT.replace("id: tabula", &format!("id: {bad}"));
|
||||
let c = code(bad, &with_front(&front, "")).1;
|
||||
assert_eq!(c, CanonCode::BadId(bad.trim().into()), "{bad:?}");
|
||||
}
|
||||
assert!(canon::is_valid_app_id("a"));
|
||||
assert!(canon::is_valid_app_id("my-app-2"));
|
||||
assert!(canon::is_valid_app_id("a2345678901234567890123456789012"));
|
||||
assert!(!canon::is_valid_app_id(""));
|
||||
assert!(!canon::is_valid_app_id("a23456789012345678901234567890123"));
|
||||
assert!(!canon::is_valid_app_id("é"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn urls_in_front_matter_must_be_https() {
|
||||
for bad in ["http://aula.example/", "aula.example", "https://a b", "https://"] {
|
||||
let text = with_front(&format!("{FRONT}aula: {bad}\n"), "");
|
||||
assert_eq!(code("tabula", &text).1, CanonCode::BadUrl(bad.into()), "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_malformed_fact_lines() {
|
||||
for bad in [
|
||||
"- [F] no number",
|
||||
"- [Fx] letters",
|
||||
"- [F1]no space",
|
||||
"- [F1 missing bracket",
|
||||
"- [F1]",
|
||||
"- [f1] lowercase",
|
||||
"- [F1] two spaces",
|
||||
"* [F1] star bullet",
|
||||
" - [F1] indented",
|
||||
"- [F1a] suffix",
|
||||
"- [F-1] negative",
|
||||
] {
|
||||
let text = with_front(FRONT, &format!("\n{bad}\n"));
|
||||
let (line, c) = code("tabula", &text);
|
||||
assert_eq!(line, 10, "{bad:?}");
|
||||
assert!(
|
||||
matches!(c, CanonCode::MalformedFact | CanonCode::EmptyFact),
|
||||
"{bad:?} gave {c:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fact_ids_in_range_without_leading_zeros() {
|
||||
let ok = with_front(FRONT, "- [F1] a\n- [F9999] b\n");
|
||||
let f = canon::parse("tabula", &ok).unwrap();
|
||||
assert_eq!(f.facts.iter().map(|x| x.id).collect::<Vec<_>>(), vec![1, 9999]);
|
||||
for bad in ["- [F0] zero", "- [F10000] big", "- [F01] lead", "- [F99999999999999999999] huge"] {
|
||||
let text = with_front(FRONT, &format!("{bad}\n"));
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::FactIdOutOfRange), "{bad:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_duplicate_facts_and_empty_facts() {
|
||||
let text = with_front(FRONT, "- [F1] a\n## Limits\n- [F1] b\n");
|
||||
assert_eq!(code("tabula", &text), (11, CanonCode::DuplicateFact(1)));
|
||||
let text = with_front(FRONT, "- [F1] \n");
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::EmptyFact));
|
||||
let text = with_front(FRONT, "- [F1] (assumption)\n");
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::EmptyFact));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn assumption_marker() {
|
||||
let text = with_front(
|
||||
FRONT,
|
||||
"- [F1] Plain.\n- [F2] Unsure. (assumption)\n- [F3] Odd case (Assumption)\n\
|
||||
- [F4] Inside (assumption) the text.\n- [F5] Trailing spaces (assumption) \n",
|
||||
);
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
let got: Vec<(&str, bool)> = f.facts.iter().map(|x| (x.text.as_str(), x.assumption)).collect();
|
||||
assert_eq!(
|
||||
got,
|
||||
vec![
|
||||
("Plain.", false),
|
||||
("Unsure.", true),
|
||||
("Odd case", true),
|
||||
("Inside (assumption) the text.", true),
|
||||
("Trailing spaces", true),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn other_body_lines_are_ignored() {
|
||||
let text = with_front(FRONT, "# T\n\nSome prose [F7] here.\n- plain bullet\n> - [F9] quoted\n- [F1] fact\n");
|
||||
let f = canon::parse("tabula", &text).unwrap();
|
||||
assert_eq!(f.facts.len(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn size_limit_is_64_kib() {
|
||||
let mut text = with_front(FRONT, "");
|
||||
while text.len() < canon::MAX_FILE_BYTES {
|
||||
text.push('\n');
|
||||
}
|
||||
assert_eq!(text.len(), canon::MAX_FILE_BYTES);
|
||||
assert!(canon::parse("tabula", &text).is_ok());
|
||||
text.push('\n');
|
||||
assert_eq!(code("tabula", &text), (0, CanonCode::TooLarge { bytes: canon::MAX_FILE_BYTES + 1 }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_huge_line_is_fine_under_the_limit_and_rejected_over_it() {
|
||||
let long = "a".repeat(60 * 1024);
|
||||
let text = with_front(FRONT, &format!("- [F1] {long}\n"));
|
||||
assert_eq!(canon::parse("tabula", &text).unwrap().facts[0].text.len(), long.len());
|
||||
let long = "a".repeat(70 * 1024);
|
||||
let text = with_front(FRONT, &format!("- [F1] {long}\n"));
|
||||
assert!(matches!(code("tabula", &text).1, CanonCode::TooLarge { .. }));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bom_and_crlf_are_tolerated() {
|
||||
let crlf = TABULA.replace('\n', "\r\n");
|
||||
let a = canon::parse("tabula", TABULA).unwrap();
|
||||
let b = canon::parse("tabula", &crlf).unwrap();
|
||||
assert_eq!(a, b);
|
||||
let bom = format!("\u{feff}{TABULA}");
|
||||
assert_eq!(canon::parse("tabula", &bom).unwrap(), a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lone_cr_nul_and_hidden_characters_are_rejected() {
|
||||
let text = with_front(FRONT, "- [F1] a\rb\n");
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::LoneCarriageReturn));
|
||||
for (ch, cp) in [
|
||||
('\0', 0u32),
|
||||
('\u{7}', 7),
|
||||
('\u{7f}', 0x7f),
|
||||
('\u{85}', 0x85),
|
||||
('\u{202e}', 0x202e),
|
||||
('\u{2066}', 0x2066),
|
||||
('\u{200f}', 0x200f),
|
||||
('\u{061c}', 0x061c),
|
||||
('\u{200b}', 0x200b),
|
||||
('\u{feff}', 0xfeff),
|
||||
('\u{e0041}', 0xe0041),
|
||||
] {
|
||||
let text = with_front(FRONT, &format!("- [F1] a{ch}b\n"));
|
||||
assert_eq!(code("tabula", &text), (9, CanonCode::ForbiddenCharacter { codepoint: cp }), "{cp:X}");
|
||||
}
|
||||
// In the front matter too.
|
||||
let front = FRONT.replace("name: Tabula", "name: Tab\u{202e}ula");
|
||||
assert_eq!(code("tabula", &with_front(&front, "")), (3, CanonCode::ForbiddenCharacter { codepoint: 0x202e }));
|
||||
// Tabs and ordinary non-ASCII text are fine.
|
||||
let text = with_front(FRONT, "- [F1] Poznámky\tžluťoučký kůň 📝\n");
|
||||
assert!(canon::parse("tabula", &text).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canon_set() {
|
||||
let mut c = Canon::new();
|
||||
c.insert(canon::parse("tabula", TABULA).unwrap()).unwrap();
|
||||
let err = c.insert(canon::parse("tabula", TABULA).unwrap()).unwrap_err();
|
||||
assert_eq!(err.code, CanonCode::DuplicateApp("tabula".into()));
|
||||
c.insert(CanonFile::adhoc("release", "Tabula", vec![(1, "Tabula 1.2 is out.".into())])).unwrap();
|
||||
assert_eq!(c.apps().map(|f| f.id.as_str()).collect::<Vec<_>>(), vec!["release", "tabula"]);
|
||||
assert!(c.get("tabula").is_some());
|
||||
assert!(c.get("nope").is_none());
|
||||
assert_eq!(c.fact("tabula", 1).unwrap().text, "Tabula keeps notes on your own computer.");
|
||||
assert!(c.fact("tabula", 9).is_none());
|
||||
assert!(c.fact("nope", 1).is_none());
|
||||
let urls = c.allowed_urls();
|
||||
assert!(urls.contains("https://aula.marek-novak.cz/tabula/"));
|
||||
assert!(urls.contains("https://aula.marek-novak.cz/tabula/limits"));
|
||||
assert_eq!(urls.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adhoc_release_file() {
|
||||
let f = CanonFile::adhoc(
|
||||
"release",
|
||||
"Tabula",
|
||||
vec![(1, "Tabula 1.2 is out.".into()), (2, "Faster search. (assumption)".into())],
|
||||
);
|
||||
assert_eq!(f.id, "release");
|
||||
assert_eq!(f.front.name, "Tabula");
|
||||
assert_eq!(f.facts.len(), 2);
|
||||
assert!(f.facts.iter().all(|x| !x.assumption), "release facts count as confirmed");
|
||||
assert_eq!(f.fact(2).unwrap().text, "Faster search. (assumption)");
|
||||
}
|
||||
Reference in New Issue
Block a user