Files
Marcos Paulo 15d7f5e873 feat: editor blocks and lint, vault knowledge, calendar plugin, macOS menu
- 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>
2026-10-18 12:36:00 -03:00

182 lines
7.8 KiB
Rust

//! Recurrence tables, series splits and a randomised bounds check.
#![allow(clippy::unwrap_used, clippy::format_push_string, clippy::cast_possible_truncation, clippy::assert_is_empty)]
use notescraft_calendar::{Error, EventTime, MAX_OCCURRENCES, Rule};
use notescraft_templates::Date;
fn t(text: &str) -> EventTime {
text.parse().unwrap()
}
fn d(text: &str) -> Date {
t(text).date()
}
fn days(rule: &str, start: &str, skip: &[&str], from: &str, to: &str) -> Vec<String> {
let skip: Vec<EventTime> = skip.iter().map(|s| t(s)).collect();
let rule: Rule = rule.parse().unwrap();
rule.occurrences(t(start), &skip, d(from), d(to)).unwrap().iter().map(ToString::to_string).collect()
}
#[test]
fn simple_rules() {
assert_eq!(days("daily", "2026-10-30", &[], "2026-10-01", "2026-11-02"), ["2026-10-30", "2026-10-31", "2026-11-01", "2026-11-02"]);
assert_eq!(
days("FREQ=DAILY;INTERVAL=3;COUNT=3", "2026-01-01T08:00", &[], "2026-01-01", "2026-12-31"),
["2026-01-01T08:00", "2026-01-04T08:00", "2026-01-07T08:00"]
);
assert_eq!(days("weekly", "2026-10-19", &[], "2026-10-01", "2026-11-10"), ["2026-10-19", "2026-10-26", "2026-11-02", "2026-11-09"]);
assert_eq!(days("FREQ=YEARLY;COUNT=2", "2026-03-04", &[], "2000-01-01", "2100-01-01"), ["2026-03-04", "2027-03-04"]);
}
#[test]
fn weekly_by_day_and_interval() {
// Monday start: the first week keeps only days from the start on.
assert_eq!(
days("FREQ=WEEKLY;BYDAY=MO,WE,FR;COUNT=5", "2026-10-21", &[], "2026-01-01", "2026-12-31"),
["2026-10-21", "2026-10-23", "2026-10-26", "2026-10-28", "2026-10-30"]
);
assert_eq!(
days("FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,SU", "2026-10-18", &[], "2026-10-01", "2026-11-15"),
["2026-10-18", "2026-10-27", "2026-11-01", "2026-11-10", "2026-11-15"]
);
}
#[test]
fn month_ends_leap_days_and_missing_days() {
// The 31st skips months without one.
assert_eq!(days("FREQ=MONTHLY;COUNT=4", "2026-01-31", &[], "2026-01-01", "2027-12-31"), ["2026-01-31", "2026-03-31", "2026-05-31", "2026-07-31"]);
// A leap day only comes back in leap years.
assert_eq!(days("FREQ=YEARLY;COUNT=3", "2024-02-29", &[], "2024-01-01", "2100-01-01"), ["2024-02-29", "2028-02-29", "2032-02-29"]);
// Monthly across a year end.
assert_eq!(days("FREQ=MONTHLY;INTERVAL=2", "2026-11-15", &[], "2026-11-01", "2027-03-31"), ["2026-11-15", "2027-01-15", "2027-03-15"]);
}
#[test]
fn wall_clock_time_survives_a_daylight_saving_change() {
// Europe/Lisbon moved its clocks on 2026-10-25 and 2026-03-29: the time of day stays.
let out = days("weekly", "2026-10-19T09:30", &[], "2026-10-19", "2026-11-02");
assert_eq!(out, ["2026-10-19T09:30", "2026-10-26T09:30", "2026-11-02T09:30"]);
}
#[test]
fn skipped_days_still_count_and_are_left_out() {
let out = days("FREQ=DAILY;COUNT=3", "2026-10-01", &["2026-10-02"], "2026-10-01", "2026-12-31");
assert_eq!(out, ["2026-10-01", "2026-10-03"]);
}
#[test]
fn until_is_inclusive_and_the_window_clips() {
assert_eq!(days("FREQ=DAILY;UNTIL=2026-10-03", "2026-10-01", &[], "2026-10-02", "2026-12-31"), ["2026-10-02", "2026-10-03"]);
assert_eq!(days("FREQ=DAILY;UNTIL=2026-10-03", "2026-10-10", &[], "2026-10-01", "2026-12-31"), Vec::<String>::new());
assert_eq!(days("daily", "2026-10-10", &[], "2026-10-01", "2026-10-09"), Vec::<String>::new());
}
#[test]
fn a_far_window_jumps_instead_of_walking() {
let out = days("daily", "1970-01-01", &[], "2026-10-17", "2026-10-18");
assert_eq!(out, ["2026-10-17", "2026-10-18"]);
let weekly = days("FREQ=WEEKLY;INTERVAL=3;BYDAY=MO,TH", "2000-01-03", &[], "2026-10-01", "2026-10-31");
assert!(weekly.iter().all(|s| s.as_str() >= "2026-10-01" && s.as_str() <= "2026-10-31") && !weekly.is_empty());
}
#[test]
fn too_many_results_is_an_error() {
let rule: Rule = "daily".parse().unwrap();
assert!(matches!(rule.occurrences(t("2000-01-01"), &[], d("2000-01-01"), d("2100-01-01")), Err(Error::TooMany(MAX_OCCURRENCES))));
}
#[test]
fn bad_rules_are_refused() {
for text in [
"",
"hourly",
"FREQ=DAILY;FOO=1",
"FREQ=DAILY;COUNT=0",
"FREQ=DAILY;INTERVAL=0",
"FREQ=DAILY;INTERVAL=1001",
"FREQ=DAILY;COUNT=1;UNTIL=2026-01-01",
"FREQ=MONTHLY;BYDAY=MO",
"FREQ=WEEKLY;BYDAY=XX",
"FREQ=WEEKLY;BYDAY=",
"INTERVAL=2",
"FREQ",
"FREQ=DAILY;UNTIL=2026-13-01",
"FREQ=DAILY;COUNT=-1",
&"x".repeat(300),
] {
assert!(text.parse::<Rule>().is_err(), "{text:?}");
}
}
#[test]
fn text_form_round_trips() {
for text in ["FREQ=DAILY", "FREQ=WEEKLY;INTERVAL=2;BYDAY=MO,WE;UNTIL=2026-12-31", "FREQ=MONTHLY;COUNT=12", "FREQ=YEARLY"] {
assert_eq!(text.parse::<Rule>().unwrap().to_string(), text);
}
assert_eq!("Weekly".parse::<Rule>().unwrap().to_string(), "FREQ=WEEKLY");
}
#[test]
fn a_split_series_adds_up_to_the_original() {
let from = d("2026-01-01");
let to = d("2027-12-31");
for (rule, start, at) in [
("FREQ=WEEKLY;BYDAY=MO,WE", "2026-01-05T10:00", "2026-06-10"),
("FREQ=DAILY;COUNT=60", "2026-02-01", "2026-03-01"),
("FREQ=MONTHLY;UNTIL=2027-06-30", "2026-01-31", "2026-08-15"),
("FREQ=YEARLY", "2024-02-29", "2030-01-01"),
("FREQ=WEEKLY;INTERVAL=2", "2026-01-05", "2026-01-19"),
] {
let rule: Rule = rule.parse().unwrap();
let (start, at) = (t(start), d(at));
let whole = rule.occurrences(start, &[], from, to).unwrap();
let split = rule.split(start, at).unwrap();
let mut joined = split.head.occurrences(start, &[], from, to).unwrap();
joined.extend(split.tail.occurrences(split.tail_start, &[], from, to).unwrap());
assert_eq!(joined, whole, "{rule}");
assert!(split.tail_start.date() >= at && joined.iter().all(|o| *o != split.tail_start || o.date() >= at));
}
}
#[test]
fn a_split_needs_a_part_on_each_side() {
let rule: Rule = "daily".parse().unwrap();
assert!(rule.split(t("2026-10-10"), d("2026-10-10")).is_err(), "nothing before");
assert!(rule.split(t("2026-10-10"), d("2026-10-01")).is_err(), "cut before the start");
let capped: Rule = "FREQ=DAILY;COUNT=2".parse().unwrap();
assert!(capped.split(t("2026-10-10"), d("2026-11-01")).is_err(), "nothing after");
}
#[test]
fn random_rules_stay_inside_their_bounds() {
let mut seed = 0x9E37_79B9_7F4A_7C15_u64;
let mut next = |modulo: u64| {
seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407);
(seed >> 33) % modulo
};
for _ in 0..500 {
let freq = ["DAILY", "WEEKLY", "MONTHLY", "YEARLY"][next(4) as usize];
let mut text = format!("FREQ={freq};INTERVAL={}", 1 + next(5));
match next(3) {
0 => text.push_str(&format!(";COUNT={}", 1 + next(40))),
1 => text.push_str(&format!(";UNTIL=20{:02}-{:02}-{:02}", 26 + next(5), 1 + next(12), 1 + next(28))),
_ => {}
}
let rule: Rule = text.parse().unwrap();
let start = t(&format!("20{:02}-{:02}-{:02}T{:02}:{:02}", 20 + next(10), 1 + next(12), 1 + next(28), next(24), next(60)));
let from = start.date().add_days(i32::try_from(next(800)).unwrap() - 200).unwrap();
let to = from.add_days(i32::try_from(next(900)).unwrap()).unwrap();
let out = rule.occurrences(start, &[], from, to).unwrap();
assert!(out.len() <= MAX_OCCURRENCES, "{text}");
assert!(out.windows(2).all(|w| w[0] < w[1]), "{text} not strictly increasing");
assert!(out.iter().all(|o| o.date() >= from && o.date() <= to && o.date() >= start.date()), "{text} outside window");
if let Some(count) = rule.count {
assert!(out.len() <= count as usize, "{text}");
}
if let Some(until) = rule.until {
assert!(out.iter().all(|o| o.date() <= until), "{text}");
}
}
}