Files
ScaenaPublic/crates/scaena-core/tests/claims.rs
T

435 lines
12 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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, CanonFile};
use scaena_core::claims::{self, Problem, Ref};
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/
---
- [F1] Tabula keeps notes on your own computer.
- [F2] It syncs between your devices without a cloud account. (assumption)
- [F3] It runs on Windows. More: https://aula.marek-novak.cz/tabula/windows
- [F7] A new note takes 1 click.
";
fn canon() -> Canon {
let mut c = Canon::new();
c.insert(canon::parse("tabula", TABULA).unwrap()).unwrap();
c.insert(CanonFile::adhoc(
"release",
"Tabula",
vec![
(1, "Tabula 1.2 is out.".into()),
(2, "Search is faster.".into()),
],
))
.unwrap();
c
}
fn r(app: &str, fact: u32) -> Ref {
Ref {
app: app.into(),
fact,
}
}
fn unsourced(text: &str) -> bool {
let rep = claims::check(text, &canon());
!rep.ok()
&& rep
.problems
.iter()
.all(|p| matches!(p, Problem::Unsourced { .. }))
}
#[test]
fn sourced_sentences_pass_and_refs_are_collected() {
let c = canon();
let rep = claims::check(
"Tabula keeps your notes on your computer [tabula#F1]. A new note takes 1 click.[tabula#F7][tabula#F1] \
Tabula 1.2 is out! [release#F1]",
&c,
);
assert!(rep.ok(), "{:?}", rep.problems);
assert_eq!(
rep.refs,
vec![r("release", 1), r("tabula", 1), r("tabula", 7)]
);
}
#[test]
fn numbers_prices_platforms_and_features_need_refs() {
for s in [
"Tabula 1.2 is out.",
"A new note takes 1 click.",
"It costs $5.",
"Only 5 € once.",
"It costs five dollars.",
"Pay once, own it forever.",
"It runs on Windows.",
"Now on Linux and macOS.",
"Coming to Android and iOS.",
"Install it as a PWA.",
"Works in your browser.",
"Use it on your phone.",
"A desktop app.",
"Runs on your home server.",
"It syncs between your devices.",
"Tabula keeps notes on your own computer.",
"No account needed",
"The fastest notes app ever made!",
"Search is faster now.",
"Tabula",
"Tabula is free.",
"100% private",
"Your notes never leave your computer?",
"Ten times faster",
"I made this for myself.",
"#tabula",
] {
assert!(unsourced(s), "{s:?} must be rejected as unsourced");
}
}
#[test]
fn plain_sentences_pass_without_refs() {
for s in [
"Here's what's new.",
"Here’s what’s new:",
"What's new",
"Try it today.",
"Try it today! 🎉",
"Try Tabula today.",
"try it now",
"Check it out.",
"Learn more.",
"Read more: https://aula.marek-novak.cz/tabula/",
"Get it here: https://aula.marek-novak.cz/tabula/",
"Thanks for reading.",
"Let me know what you think.",
"Feedback welcome.",
"🎉",
"---",
"",
" \n\n ",
] {
let rep = claims::check(s, &canon());
assert!(rep.ok(), "{s:?} should pass: {:?}", rep.problems);
}
}
#[test]
fn a_plain_phrase_with_anything_extra_needs_a_ref() {
for s in [
"Try it today on Windows.",
"Try it today for $5.",
"Try it today, it's free.",
"Here's what's new in 1.2.",
"Try the fastest notes app today.",
"Try it 2day.",
"#tabula #windows",
] {
assert!(unsourced(s), "{s:?} must be rejected");
}
}
#[test]
fn every_sentence_is_checked_separately() {
let c = canon();
let rep = claims::check(
"Tabula keeps notes local [tabula#F1]. It runs on Linux. Try it today.",
&c,
);
assert_eq!(
rep.problems,
vec![Problem::Unsourced {
sentence: "It runs on Linux.".into()
}]
);
// Lines are sentences too.
let rep = claims::check("Tabula 1.2 [release#F1]\nFaster search", &c);
assert_eq!(
rep.problems,
vec![Problem::Unsourced {
sentence: "Faster search".into()
}]
);
}
#[test]
fn decimals_and_urls_do_not_split_sentences() {
let c = canon();
assert_eq!(
claims::sentences("Tabula 1.2 is out. Try it."),
vec!["Tabula 1.2 is out.", "Try it."]
);
assert_eq!(
claims::sentences("See https://aula.marek-novak.cz/tabula/?a=1. Thanks!"),
vec!["See https://aula.marek-novak.cz/tabula/?a=1.", "Thanks!"]
);
assert_eq!(
claims::sentences("A. [tabula#F1] B"),
vec!["A. [tabula#F1]", "B"]
);
assert!(claims::check("Tabula 1.2 is out. [release#F1]", &c).ok());
}
#[test]
fn bad_refs_are_rejected() {
let c = canon();
let rep = claims::check("Tabula keeps notes local. [tabula#F99]", &c);
assert_eq!(
rep.problems,
vec![Problem::MissingFact { r: r("tabula", 99) }]
);
let rep = claims::check("Notes, done. [notapp#F1]", &c);
assert_eq!(
rep.problems,
vec![Problem::UnknownApp { r: r("notapp", 1) }]
);
let rep = claims::check("It syncs everywhere. [tabula#F2]", &c);
assert_eq!(
rep.problems,
vec![Problem::Unconfirmed { r: r("tabula", 2) }]
);
// A bad ref fails even on a plain sentence.
let rep = claims::check("Try it today. [tabula#F99]", &c);
assert_eq!(
rep.problems,
vec![Problem::MissingFact { r: r("tabula", 99) }]
);
// One good ref does not excuse a bad one.
let rep = claims::check("Local notes. [tabula#F1][tabula#F2]", &c);
assert_eq!(
rep.problems,
vec![Problem::Unconfirmed { r: r("tabula", 2) }]
);
}
#[test]
fn malformed_refs_are_rejected() {
let c = canon();
for bad in [
"[F1]",
"[tabula#f1]",
"[Tabula#F1]",
"[tabula #F1]",
"[tabula#F01]",
"[tabula#F0]",
"[tabula#F10000]",
"[tabula#1]",
"[tabula#F1",
"[#F1]",
"[tabula#]",
"[tabula_x#F1]",
"tabula#F1",
"[tabula#F1 ]",
] {
let text = format!("Tabula keeps notes on your own computer. {bad}");
let rep = claims::check(&text, &c);
assert!(
rep.problems
.iter()
.any(|p| matches!(p, Problem::MalformedRef { .. })),
"{bad:?}: {:?}",
rep.problems
);
assert!(!rep.ok());
}
}
#[test]
fn urls_must_come_from_the_canon() {
let c = canon();
assert!(claims::check("Get it here: https://aula.marek-novak.cz/tabula/", &c).ok());
assert!(claims::check("Get it here: https://aula.marek-novak.cz/tabula", &c).ok());
assert!(claims::check("Read more: https://aula.marek-novak.cz/tabula/windows.", &c).ok());
assert!(
claims::check(
"Read more: (https://aula.marek-novak.cz/tabula/windows)",
&c
)
.ok()
);
for (text, url) in [
(
"Get it here: https://evil.example/tabula",
"https://evil.example/tabula",
),
(
"Get it here: http://aula.marek-novak.cz/tabula/",
"http://aula.marek-novak.cz/tabula/",
),
(
"Get it here: https://aula.marek-novak.cz/tabula/x",
"https://aula.marek-novak.cz/tabula/x",
),
("Get it here: www.evil.example", "www.evil.example"),
("Get it here: evil.com", "evil.com"),
(
"Get it here: evil.example/download",
"evil.example/download",
),
("Get it here: ftp://evil.example", "ftp://evil.example"),
] {
let rep = claims::check(text, &c);
assert!(
rep.problems
.contains(&Problem::UrlNotInCanon { url: url.into() }),
"{text:?}: {:?}",
rep.problems
);
}
// A URL in a sourced sentence is still checked.
let rep = claims::check(
"It runs on Windows, see https://evil.example/ [tabula#F3]",
&c,
);
assert_eq!(
rep.problems,
vec![Problem::UrlNotInCanon {
url: "https://evil.example/".into()
}]
);
// A bare form of an allowed URL is fine.
assert!(claims::check("Get it here: aula.marek-novak.cz/tabula/", &c).ok());
}
#[test]
fn hidden_characters_are_rejected() {
let c = canon();
let rep = claims::check("Try it today.\u{202e}", &c);
assert_eq!(
rep.problems,
vec![Problem::HiddenCharacter { codepoint: 0x202e }]
);
let rep = claims::check("Try it\0 today.", &c);
assert!(
rep.problems
.contains(&Problem::HiddenCharacter { codepoint: 0 })
);
// CRLF and tabs are ordinary whitespace.
assert!(claims::check("Try it today.\r\nThanks!\t", &c).ok());
}
#[test]
fn reasons_are_plain_english() {
let c = canon();
let rep = claims::check(
"It runs on Linux. Local. [tabula#F99] Sync. [x#F1] Sync. [tabula#F2] [F3] https://evil.example/",
&c,
);
let reasons: Vec<String> = rep.problems.iter().map(Problem::reason).collect();
assert!(
reasons
.iter()
.any(|s| s.contains("It runs on Linux.") && s.contains("no source")),
"{reasons:?}"
);
assert!(
reasons
.iter()
.any(|s| s.contains("tabula#F99") && s.contains("does not exist")),
"{reasons:?}"
);
assert!(
reasons
.iter()
.any(|s| s.contains("unknown app") && s.contains("x")),
"{reasons:?}"
);
assert!(
reasons
.iter()
.any(|s| s.contains("unconfirmed fact") && s.contains("tabula#F2")),
"{reasons:?}"
);
assert!(reasons.iter().any(|s| s.contains("[F3]")), "{reasons:?}");
assert!(
reasons
.iter()
.any(|s| s.contains("https://evil.example/") && s.contains("not in the canon")),
"{reasons:?}"
);
assert_eq!(
Problem::HiddenCharacter { codepoint: 0x202e }.reason(),
"Hidden or direction-changing character U+202E."
);
}
#[test]
fn problems_are_not_repeated() {
let c = canon();
let rep = claims::check("A. [tabula#F99] B. [tabula#F99]", &c);
assert_eq!(
rep.problems,
vec![Problem::MissingFact { r: r("tabula", 99) }]
);
}
#[test]
fn strip_refs_for_display() {
assert_eq!(
claims::strip_refs(
"Tabula keeps notes local [tabula#F1]. One click.[tabula#F7][tabula#F1] Done [release#F1]"
),
"Tabula keeps notes local. One click. Done"
);
assert_eq!(
claims::strip_refs("Tabula [tabula#F1] keeps notes."),
"Tabula keeps notes."
);
assert_eq!(claims::strip_refs("A.[tabula#F1]B"), "A. B");
assert_eq!(
claims::strip_refs("Line one [tabula#F1]\nLine two"),
"Line one\nLine two"
);
assert_eq!(
claims::strip_refs("Keep [beta] and [Tabula#F1]."),
"Keep [beta] and [Tabula#F1]."
);
assert_eq!(claims::strip_refs(""), "");
assert_eq!(
claims::strip_refs("Žluťoučký [tabula#F1]kůň"),
"Žluťoučký kůň"
);
}
#[test]
fn stripped_text_of_a_passing_text_has_no_refs() {
let c = canon();
let text = "Local notes [tabula#F1]. Try it today.";
assert!(claims::check(text, &c).ok());
let stripped = claims::strip_refs(text);
assert!(claims::check(&stripped, &c).refs.is_empty());
}
#[test]
fn states_fact_is_fail_closed() {
let c = canon();
assert!(!claims::states_fact("Try it today.", &c));
assert!(!claims::states_fact("Here's what's new [tabula#F1].", &c));
assert!(claims::states_fact("Tabula is great.", &c));
assert!(claims::states_fact("It is what it is.", &c));
}
#[test]
fn a_huge_text_is_checked_without_trouble() {
let c = canon();
let text = "Local notes [tabula#F1]. ".repeat(2000);
let rep = claims::check(&text, &c);
assert!(rep.ok());
assert_eq!(rep.refs, vec![r("tabula", 1)]);
let line = "a".repeat(100_000);
assert!(!claims::check(&line, &c).ok());
}