405 lines · 12.2 KB · RustRaw
use super::*;fn parse(text: &str) -> ParsedPhrase { parse_phrase( text, "2026-10-01T12:00:00Z", "Europe/Berlin", &["Cotton".into(), "Home office".into()], &["Sam".into(), "Sam Lee".into()], ) .unwrap()}// A wrong guess edits the owner's records instantly: unknown words must survive,// and ambiguous hours must remain alternatives rather than become certainty./// RULE: The phrase parser preserves source spans and unknown words and returns ranked alternatives for ambiguous hours.#[test]fn twenty_phrases_preserve_the_understood_spans_and_unknown_remainder() { for (text, date, time, person, project, verb, rest) in [ ( "call Sam tomorrow at 6", Some("2026-10-02"), Some("18:00"), Some("Sam"), None, None, "call", ), ( "next Tuesday", Some("2026-10-06"), None, None, None, None, "", ), ("3 Oct", Some("2026-10-03"), None, None, None, None, ""), ( "on the 14th", Some("2026-10-14"), None, None, None, None, "", ), ( "Friday 6 pm", Some("2026-10-02"), Some("18:00"), None, None, None, "", ), ("18:30", None, Some("18:30"), None, None, None, ""), ("at noon", None, Some("12:00"), None, None, None, ""), ("at midnight", None, Some("00:00"), None, None, None, ""), ( "done Cotton", None, None, None, Some("Cotton"), Some("done"), "", ), ( "move Home office tomorrow", Some("2026-10-02"), None, None, Some("Home office"), Some("move"), "", ), ( "delete old item", None, None, None, None, Some("delete"), "old item", ), ( "rename Sam Lee", None, None, Some("Sam Lee"), None, Some("rename"), "", ), ("add milk", None, None, None, None, Some("add"), "milk"), ("new task", None, None, None, None, Some("add"), "task"), ( "remind Sam today", Some("2026-10-01"), None, Some("Sam"), None, Some("remind"), "", ), ("yesterday", Some("2026-09-30"), None, None, None, None, ""), ( "SAM at 12 am", None, Some("00:00"), Some("Sam"), None, None, "", ), ( "Sam 12 pm", None, Some("12:00"), Some("Sam"), None, None, "", ), ( "Cotton at 7", None, Some("19:00"), None, Some("Cotton"), None, "", ), ( "buy bread on the 1st", Some("2026-10-01"), None, None, None, None, "buy bread", ), ( "meet Friday at 6:15 pm", Some("2026-10-02"), Some("18:15"), None, None, None, "meet", ), ( "call Sam next Monday", Some("2026-10-05"), None, Some("Sam"), None, None, "call", ), ] { let p = parse(text); assert_eq!( p.date.as_ref().map(|v| v.candidates[0].as_str()), date, "{text}" ); assert_eq!( p.time.as_ref().map(|v| v.candidates[0].as_str()), time, "{text}" ); assert_eq!( p.person.as_ref().map(|v| v.candidates[0].as_str()), person, "{text}" ); assert_eq!( p.project.as_ref().map(|v| v.candidates[0].as_str()), project, "{text}" ); assert_eq!( p.verb.as_ref().map(|v| v.candidates[0].as_str()), verb, "{text}" ); assert_eq!(p.remainder, rest, "{text}"); for v in [&p.date, &p.time, &p.person, &p.project, &p.verb] .into_iter() .flatten() { assert_eq!(&text[v.span.start..v.span.end], v.text); } } assert_eq!(parse("at 6").time.unwrap().candidates, ["18:00", "06:00"]);}/// RULE: The phrase parser leaves unrecognised English and non-English input untouched.#[test]fn five_unknown_phrases_are_untouched() { for text in [ "buy milk", "something someday", "sample cottonwood", "bonjour monde", "42 bananas", ] { let p = parse(text); assert!( p.date.is_none() && p.time.is_none() && p.person.is_none() && p.project.is_none() && p.verb.is_none(), "{text}" ); assert_eq!(p.remainder, text); }}/// RULE: The phrase parser uses only the supplied clock and zone and refuses invalid context.#[test]fn context_names_and_remainders_are_deterministic() { let p = parse_phrase( "☕ call Sám tomorrow", "2026-10-01T23:30:00Z", "Europe/Berlin", &[], &["Sám".into()], ) .unwrap(); assert_eq!(p.date.unwrap().candidates, ["2026-10-03"]); let person = p.person.unwrap(); assert_eq!( &"☕ call Sám tomorrow"[person.span.start..person.span.end], "Sám" ); assert_eq!(p.remainder, "☕ call"); assert_eq!( parse_phrase( "Friday", "2026-10-01T12:00:00Z", "UTC", &["Friday".into()], &[] ) .unwrap() .project .unwrap() .candidates, ["Friday"] ); assert!(parse("please don't delete this").verb.is_none()); // "go to" and "open" are one verb, and only at the start. for (text, end) in [("go to todos", 5), ("Go to Notes", 6), ("open the Kestrel invoice", 4)] { let verb = parse(text).verb.unwrap(); assert_eq!((verb.candidates[0].as_str(), verb.span.end), ("open", end), "{text}"); } assert!(parse("I want to go to Lonavala").verb.is_none()); assert!(parse("opened the mail").verb.is_none()); assert!(parse("31 Apr").date.is_none()); assert!(parse("on the 32nd").date.is_none()); assert!(parse("on the 14st").date.is_none()); for text in [ "at 25", "at 6:99", "25:00", "0 pm", "13 pm", "at 6:30 potatoes", ] { if text == "at 6:30 potatoes" { assert_eq!(parse(text).time.unwrap().candidates, ["18:30", "06:30"]); } else { assert!(parse(text).time.is_none(), "{text}"); } } assert!(parse_phrase("tomorrow", "2026-10-01", "UTC", &[], &[]).is_err()); assert!(parse_phrase("tomorrow", "2026-10-01T12:00:00Z", "invalid", &[], &[]).is_err()); assert_eq!(parse("tomorrow tomorrow").remainder, "tomorrow"); assert_eq!(parse(" buy milk ").remainder, " buy milk "); assert_eq!(parse("at 12").time.unwrap().candidates, ["12:00", "00:00"]);}/// RULE: Phrase dates cross calendar and daylight-saving boundaries in the supplied time zone.#[test]fn calendar_rollovers_and_dst_use_the_supplied_zone() { for (text, now, expected) in [ ("3 Oct", "2026-10-04T00:00:00Z", "2027-10-03"), ("29 Feb", "2026-10-01T00:00:00Z", "2028-02-29"), ("on the 31st", "2026-11-01T00:00:00Z", "2026-12-31"), ("tomorrow", "2026-12-31T12:00:00Z", "2027-01-01"), ("next Thursday", "2026-10-01T12:00:00Z", "2026-10-08"), ("tomorrow", "2026-03-28T23:30:00Z", "2026-03-30"), ] { assert_eq!( parse_phrase(text, now, "Europe/Berlin", &[], &[]) .unwrap() .date .unwrap() .candidates, [expected] ); }}/// RULE: Non-ASCII digits and English lookalikes never panic the phrase parser.#[test]fn non_ascii_digits_and_case_folds_cannot_panic() { // Regex Unicode digits and English lookalikes must never reach numeric // parsing or weekday/month lookup as if they were ASCII English. for text in ["٣ Oct", "at ٦", "thurſday", "3 ſep"] { let p = parse(text); assert!(p.date.is_none() && p.time.is_none(), "{text}"); }}// A clock written with minutes is no less ambiguous than a bare hour; names,// punctuation and script boundaries must not change what the input means./// RULE: Unqualified twelve-hour times retain both ranked alternatives with or without minutes; leading zero and hours above twelve are explicit twenty-four-hour times.#[test]fn minutes_keep_the_same_hour_alternatives() { for (text, expected) in [ ("at 6:30", vec!["18:30", "06:30"]), ("6:30", vec!["18:30", "06:30"]), ("at 12:30", vec!["12:30", "00:30"]), ("06:30", vec!["06:30"]), ("at 06", vec!["06:00"]), ("18:30", vec!["18:30"]), ("6:30 am", vec!["06:30"]), ] { assert_eq!(parse(text).time.unwrap().candidates, expected, "{text}"); }}/// RULE: Next weekday retains the upcoming and following-week interpretations when both differ; month names work on either side of a valid ordinal day.#[test]fn weekday_and_month_names_do_not_hide_date_alternatives() { assert_eq!( parse("next Friday").date.unwrap().candidates, ["2026-10-02", "2026-10-09"] ); assert_eq!( parse("next Thursday").date.unwrap().candidates, ["2026-10-08"] ); for text in ["3 Oct", "Oct 3", "October 3rd", "3rd October"] { assert_eq!( parse(text).date.unwrap().candidates, ["2026-10-03"], "{text}" ); } assert_eq!(parse("14th Oct").date.unwrap().candidates, ["2026-10-14"]); for text in ["Oct 3th", "14st Oct", "31st Apr"] { assert!(parse(text).date.is_none(), "{text}"); }}/// RULE: Remainder source spans exactly partition the unconsumed UTF-8 input, while the display remainder omits punctuation left alone by consumed names and dates.#[test]fn remainder_spans_preserve_punctuation_and_devanagari() { let text = "☕ call Sam, tomorrow"; let p = parse(text); assert_eq!(p.remainder, "☕ call"); assert_eq!( p.remainder_spans .iter() .map(|s| &text[s.start..s.end]) .collect::<Vec<_>>(), ["☕ call ", ", "] ); let text = "कॉल सीमा tomorrow"; let p = parse_phrase(text, "2026-10-01T12:00:00Z", "UTC", &[], &["सीमा".into()]).unwrap(); assert_eq!(p.person.unwrap().text, "सीमा"); assert_eq!(p.remainder, "कॉल"); assert_eq!( p.remainder_spans .iter() .map(|s| &text[s.start..s.end]) .collect::<Vec<_>>(), ["कॉल ", " "] ); assert_eq!(parse("call, tomorrow").remainder, "call,"); assert_eq!(parse("tomorrow # !").remainder, "# !"); assert!(parse("कलtomorrow").date.is_none());}/// RULE: A supplied contact dictionary reuses its compiled matchers across keystrokes, and longest Unicode names still win.#[test]fn contact_matchers_stay_warm_between_keystrokes() { let names = (0..1000) .map(|i| format!("Contact {i}")) .chain(["सीमा".into(), "सीमा शर्मा".into()]) .collect::<Vec<_>>(); let mut cache = Vec::new(); let first = cached_name_patterns(&names, &mut cache).unwrap(); let second = cached_name_patterns(&names, &mut cache).unwrap(); assert!(Arc::ptr_eq(&first, &second)); let p = parse_phrase( "कॉल सीमा शर्मा tomorrow", "2026-10-01T12:00:00Z", "UTC", &[], &names, ) .unwrap(); assert_eq!(p.person.unwrap().candidates, ["सीमा शर्मा"]);}