15d7f5e873
- notescraft-lint (rules, safe fixes, learned style) and notescraft-blocks (block model and edits); engine lint.* and block.* commands with CLI and MCP parity - Lint tab, underlines, slash menu with preview in Source and Visual, block grip and menu, Settings > Editor and lint, Vim block keys - Visual view: tables, note embeds, %% comments, wrapping property chips - Vault knowledge (Related tab, assistant sources, Settings) and calendar plugin - Sidebar top padding fixed, dot grid follows PaperCraft spacing, right-card tabs no longer cover the collapse button Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
143 lines
5.6 KiB
Rust
143 lines
5.6 KiB
Rust
//! Event file round trips and hostile input.
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
use notescraft_calendar::{Error, Event, MAX_EVENT_BYTES, MAX_ITEMS, Priority, Reminder, Status, parse_event, write_event};
|
|
|
|
fn full() -> Event {
|
|
Event {
|
|
title: "Standup: day #1 'quoted' \"too\"".to_owned(),
|
|
start: "2026-10-19T09:30".parse().unwrap(),
|
|
end: Some("2026-10-19T09:45".parse().unwrap()),
|
|
tz: Some("Europe/Lisbon".to_owned()),
|
|
repeat: Some("weekly".to_owned()),
|
|
exceptions: vec!["2026-10-26T09:30".parse().unwrap()],
|
|
calendar: Some("Work".to_owned()),
|
|
reminders: vec![Reminder { minutes_before: 10 }, Reminder { minutes_before: 0 }],
|
|
tags: vec!["team".to_owned(), "a, b".to_owned()],
|
|
priority: Some(Priority::High),
|
|
status: Some(Status::Tentative),
|
|
link: Some("https://example.com/call?a=1#x".to_owned()),
|
|
note: Some("meetings/standup.md".to_owned()),
|
|
}
|
|
}
|
|
|
|
fn minimal(title: &str) -> Event {
|
|
Event {
|
|
title: title.to_owned(),
|
|
start: "2026-10-19".parse().unwrap(),
|
|
end: None,
|
|
tz: None,
|
|
repeat: None,
|
|
exceptions: vec![],
|
|
calendar: None,
|
|
reminders: vec![],
|
|
tags: vec![],
|
|
priority: None,
|
|
status: None,
|
|
link: None,
|
|
note: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_full_event_round_trips() {
|
|
let text = write_event(None, &full()).unwrap();
|
|
assert!(text.starts_with("---\n"));
|
|
assert_eq!(parse_event(&text).unwrap(), full());
|
|
}
|
|
|
|
#[test]
|
|
fn awkward_titles_stay_text() {
|
|
for title in ["2026", "true", "null", "a: b", "# not a comment", " padded ", "line\nbreak", "tab\there", "ünï 日本 🎉", "'", "\"", "[x]", "- y"] {
|
|
let text = write_event(None, &minimal(title)).unwrap();
|
|
assert_eq!(parse_event(&text).unwrap().title, title, "{title:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn rewrite_keeps_unknown_keys_comments_and_body() {
|
|
let original = "---\n# keep me\naliases: [a]\ntitle: Old\nstart: 2026-01-01\nmystery: {x: 1}\n---\n\nBody text.\n";
|
|
let out = write_event(Some(original), &minimal("New")).unwrap();
|
|
assert!(out.contains("# keep me\n") && out.contains("aliases: [a]\n") && out.contains("mystery: {x: 1}\n"));
|
|
assert!(out.ends_with("---\n\nBody text.\n"));
|
|
let event = parse_event(&out).unwrap();
|
|
assert_eq!((event.title.as_str(), event.start.to_string().as_str()), ("New", "2026-10-19"));
|
|
}
|
|
|
|
#[test]
|
|
fn emptied_fields_are_removed() {
|
|
let with = write_event(None, &full()).unwrap();
|
|
let without = write_event(Some(&with), &minimal("Standup")).unwrap();
|
|
for key in ["end:", "tz:", "repeat:", "exceptions:", "reminders:", "tags:", "priority:", "status:", "link:", "note:", "calendar:"] {
|
|
assert!(!without.contains(key), "{key} left in\n{without}");
|
|
}
|
|
assert_eq!(parse_event(&without).unwrap(), minimal("Standup"));
|
|
}
|
|
|
|
#[test]
|
|
fn missing_or_wrong_fields_are_errors_not_fixes() {
|
|
assert!(matches!(parse_event("no frontmatter"), Err(Error::NoFrontmatter)));
|
|
assert!(matches!(parse_event("---\nstart: 2026-10-19\n---\n"), Err(Error::Field { field: "title", .. })));
|
|
assert!(matches!(parse_event("---\ntitle: x\n---\n"), Err(Error::Field { field: "start", .. })));
|
|
let cases = [
|
|
("start: 2026-02-30", "start"),
|
|
("start: 2026-13-01", "start"),
|
|
("start: 99999-01-01", "start"),
|
|
("start: [2026-10-19]", "start"),
|
|
("start: 12", "start"),
|
|
("start: 2026-10-19\nend: tomorrow", "end"),
|
|
("start: 2026-10-19\npriority: urgent", "priority"),
|
|
("start: 2026-10-19\nstatus: maybe", "status"),
|
|
("start: 2026-10-19\nreminders: [-5]", "reminders"),
|
|
("start: 2026-10-19\nreminders: [1.5]", "reminders"),
|
|
("start: 2026-10-19\nreminders: [99999999999]", "reminders"),
|
|
("start: 2026-10-19\nreminders: soon", "reminders"),
|
|
("start: 2026-10-19\ntags: [[a]]", "tags"),
|
|
("start: 2026-10-19\ntags: one", "tags"),
|
|
("start: 2026-10-19\nexceptions: [nope]", "exceptions"),
|
|
];
|
|
for (body, field) in cases {
|
|
let result = parse_event(&format!("---\ntitle: x\n{body}\n---\n"));
|
|
assert!(matches!(&result, Err(Error::Field { field: f, .. }) if *f == field), "{body}: {result:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn hostile_sizes_are_refused() {
|
|
let huge = format!("---\ntitle: x\nstart: 2026-10-19\nnote: {}\n---\n", "a".repeat(MAX_EVENT_BYTES));
|
|
assert!(matches!(parse_event(&huge), Err(Error::TooLarge(_))));
|
|
let long_title = format!("---\ntitle: {}\nstart: 2026-10-19\n---\n", "t".repeat(5000));
|
|
assert!(parse_event(&long_title).is_err());
|
|
let many = (0..=MAX_ITEMS).map(|i| format!("t{i}")).collect::<Vec<_>>().join(", ");
|
|
assert!(parse_event(&format!("---\ntitle: x\nstart: 2026-10-19\ntags: [{many}]\n---\n")).is_err());
|
|
let mut event = minimal("x");
|
|
event.title = "t".repeat(5000);
|
|
assert!(write_event(None, &event).is_err());
|
|
event = minimal("x");
|
|
event.tags = vec!["t".to_owned(); MAX_ITEMS + 1];
|
|
assert!(write_event(None, &event).is_err());
|
|
assert!(write_event(None, &minimal(" ")).is_err());
|
|
let mut backwards = minimal("x");
|
|
backwards.end = Some("2026-10-18".parse().unwrap());
|
|
assert!(write_event(None, &backwards).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn junk_never_panics() {
|
|
let junk = [
|
|
"",
|
|
"---",
|
|
"---\n",
|
|
"---\n---\n",
|
|
"---\n:\n---\n",
|
|
"---\ntitle:\n---\n",
|
|
"---\n\u{0}\u{1}\n---\n",
|
|
"---\ntitle: [\nstart: {\n---\n",
|
|
"---\ntitle: &a *a\nstart: !!x 1\n---\n",
|
|
];
|
|
for text in junk {
|
|
let _ = parse_event(text);
|
|
let _ = write_event(Some(text), &minimal("x"));
|
|
}
|
|
}
|