import XCTest @testable import LChatCore // 🤖 Parity tests for the native parseReminder port — the SAME cases the web // suite pins (test/apps.test.mjs "remind: parseReminder …"), so the two parsers // accept and reject identically. `now` is injected, so everything relative is // exact; clock/date shapes assert on local-time components, like the web tests. final class ReminderParseTests: XCTestCase { func testRelativeTimes() { XCTAssertEqual(ReminderParse.parse("Call mom in 2h", now: 0), .init(text: "Call mom", when: 2 * 3_600_000)) XCTAssertEqual(ReminderParse.parse("stretch in 45m", now: 1000), .init(text: "stretch", when: 1000 + 45 * 60000)) } func testClockTimeIsNextOccurrenceWithinADay() throws { let now = Date().timeIntervalSince1970 * 1000 let at = try XCTUnwrap(ReminderParse.parse("Standup at 9:00", now: now)) XCTAssertEqual(at.text, "Standup") XCTAssertGreaterThan(at.when, now) XCTAssertLessThanOrEqual(at.when, now + 24 * 3_600_000) let comps = Calendar.current.dateComponents([.hour, .minute], from: Date(timeIntervalSince1970: at.when / 1000)) XCTAssertEqual(comps.hour, 9) XCTAssertEqual(comps.minute, 0) } func testExplicitDateAfterBar() throws { let abs = try XCTUnwrap(ReminderParse.parse("Renew passport | 2026-08-01", now: 0)) XCTAssertEqual(abs.text, "Renew passport") let d = Calendar.current.dateComponents([.year, .month, .day, .hour], from: Date(timeIntervalSince1970: abs.when / 1000)) XCTAssertEqual(d.year, 2026) XCTAssertEqual(d.month, 8) XCTAssertEqual(d.day, 1) XCTAssertEqual(d.hour, 9, "a bare date defaults to 09:00") } func testSlackPhrasing() { // "me in 10 seconds to do a thing" — leading "me" dropped, time leads. XCTAssertEqual(ReminderParse.parse("me in 10 seconds to do a thing", now: 0), .init(text: "do a thing", when: 10000)) XCTAssertEqual(ReminderParse.parse("in 5 minutes to stretch", now: 1000), .init(text: "stretch", when: 1000 + 5 * 60000)) XCTAssertEqual(ReminderParse.parse("me to water plants in 1 h", now: 0), .init(text: "water plants", when: 3_600_000)) // "meeting" must NOT be read as the "me" keyword. XCTAssertEqual(ReminderParse.parse("meeting in 10m", now: 0), .init(text: "meeting", when: 10 * 60000)) } // Port-hardening edges from review: each of these crashed or silently // diverged from the web before being pinned here. func testNonASCIIDigitsAreRejectedNotCrashed() { // ICU \d matches Unicode digits ("٥") that Swift's numeric inits then // reject — the patterns must be [0-9] and the outcome a calm nil. XCTAssertNil(ReminderParse.parse("pray in ٥ minutes", now: 0)) XCTAssertNil(ReminderParse.parse("bad | ٢٠٢٦-08-01", now: 0)) XCTAssertNil(ReminderParse.parse("standup at ٩:00", now: 0)) } func testSubCenturyYearsAreRejected() { // JS's new Date(26,…) means 1926 and fails its round-trip; 0026 must // not become 26 AD natively (it would file a permanently-due reminder). XCTAssertNil(ReminderParse.parse("renew passport | 0026-08-01", now: 0)) XCTAssertNil(ReminderParse.parse("renew passport | 0099-01-01", now: 0)) } func testNewlinesTrimLikeTheWeb() { // JS .trim() strips newlines; multi-line drafts must split identically. XCTAssertEqual(ReminderParse.parse("call mum \n| 2026-08-01", now: 0)?.text, "call mum") XCTAssertEqual(ReminderParse.parse("me \n in 10 s \n to stretch", now: 0)?.text, "stretch") } func testAbsurdRelativeAmountsDoNotProduceInfinity() { // A digit string big enough to overflow Double must reject (the web's // equivalent add is dropped in the fold), never propagate ±inf into // scheduling arithmetic. let huge = String(repeating: "9", count: 400) XCTAssertNil(ReminderParse.parse("call mum in \(huge) weeks", now: 0)) // A large-but-finite amount still parses — the SERVER is the range // authority (postSchedule's horizon check), same as the web. XCTAssertNotNil(ReminderParse.parse("call mum in 999999999 weeks", now: 0)) } func testRejections() { XCTAssertNil(ReminderParse.parse("just a note", now: 0)) XCTAssertNil(ReminderParse.parse("", now: 0)) XCTAssertNil(ReminderParse.parse("do it in 3x", now: 0), "unknown unit") XCTAssertNil(ReminderParse.parse("in 2h", now: 0), "no text before the time") // Explicit dates/times are rejected, never rolled over, when out of range. XCTAssertNil(ReminderParse.parse("bad | 2026-06-31", now: 0), "June has 30 days") XCTAssertNil(ReminderParse.parse("bad | 2026-02-29", now: 0), "2026 isn't a leap year") XCTAssertNil(ReminderParse.parse("bad | 2026-13-01", now: 0), "month > 12") XCTAssertNil(ReminderParse.parse("bad | 2026-08-01 24:00", now: 0), "hour > 23") XCTAssertNil(ReminderParse.parse("bad | 2026-08-01 12:60", now: 0), "minute > 59") } }