| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // 🤖 Parity tests for the native parseReminder port — the SAME cases the web |
| 5 | // suite pins (test/apps.test.mjs "remind: parseReminder …"), so the two parsers |
| 6 | // accept and reject identically. `now` is injected, so everything relative is |
| 7 | // exact; clock/date shapes assert on local-time components, like the web tests. |
| 8 | final class ReminderParseTests: XCTestCase { |
| 9 | func testRelativeTimes() { |
| 10 | XCTAssertEqual(ReminderParse.parse("Call mom in 2h", now: 0), |
| 11 | .init(text: "Call mom", when: 2 * 3_600_000)) |
| 12 | XCTAssertEqual(ReminderParse.parse("stretch in 45m", now: 1000), |
| 13 | .init(text: "stretch", when: 1000 + 45 * 60000)) |
| 14 | } |
| 15 | |
| 16 | func testClockTimeIsNextOccurrenceWithinADay() throws { |
| 17 | let now = Date().timeIntervalSince1970 * 1000 |
| 18 | let at = try XCTUnwrap(ReminderParse.parse("Standup at 9:00", now: now)) |
| 19 | XCTAssertEqual(at.text, "Standup") |
| 20 | XCTAssertGreaterThan(at.when, now) |
| 21 | XCTAssertLessThanOrEqual(at.when, now + 24 * 3_600_000) |
| 22 | let comps = Calendar.current.dateComponents([.hour, .minute], |
| 23 | from: Date(timeIntervalSince1970: at.when / 1000)) |
| 24 | XCTAssertEqual(comps.hour, 9) |
| 25 | XCTAssertEqual(comps.minute, 0) |
| 26 | } |
| 27 | |
| 28 | func testExplicitDateAfterBar() throws { |
| 29 | let abs = try XCTUnwrap(ReminderParse.parse("Renew passport | 2026-08-01", now: 0)) |
| 30 | XCTAssertEqual(abs.text, "Renew passport") |
| 31 | let d = Calendar.current.dateComponents([.year, .month, .day, .hour], |
| 32 | from: Date(timeIntervalSince1970: abs.when / 1000)) |
| 33 | XCTAssertEqual(d.year, 2026) |
| 34 | XCTAssertEqual(d.month, 8) |
| 35 | XCTAssertEqual(d.day, 1) |
| 36 | XCTAssertEqual(d.hour, 9, "a bare date defaults to 09:00") |
| 37 | } |
| 38 | |
| 39 | func testSlackPhrasing() { |
| 40 | // "me in 10 seconds to do a thing" — leading "me" dropped, time leads. |
| 41 | XCTAssertEqual(ReminderParse.parse("me in 10 seconds to do a thing", now: 0), |
| 42 | .init(text: "do a thing", when: 10000)) |
| 43 | XCTAssertEqual(ReminderParse.parse("in 5 minutes to stretch", now: 1000), |
| 44 | .init(text: "stretch", when: 1000 + 5 * 60000)) |
| 45 | XCTAssertEqual(ReminderParse.parse("me to water plants in 1 h", now: 0), |
| 46 | .init(text: "water plants", when: 3_600_000)) |
| 47 | // "meeting" must NOT be read as the "me" keyword. |
| 48 | XCTAssertEqual(ReminderParse.parse("meeting in 10m", now: 0), |
| 49 | .init(text: "meeting", when: 10 * 60000)) |
| 50 | } |
| 51 | |
| 52 | // Port-hardening edges from review: each of these crashed or silently |
| 53 | // diverged from the web before being pinned here. |
| 54 | func testNonASCIIDigitsAreRejectedNotCrashed() { |
| 55 | // ICU \d matches Unicode digits ("٥") that Swift's numeric inits then |
| 56 | // reject — the patterns must be [0-9] and the outcome a calm nil. |
| 57 | XCTAssertNil(ReminderParse.parse("pray in ٥ minutes", now: 0)) |
| 58 | XCTAssertNil(ReminderParse.parse("bad | ٢٠٢٦-08-01", now: 0)) |
| 59 | XCTAssertNil(ReminderParse.parse("standup at ٩:00", now: 0)) |
| 60 | } |
| 61 | |
| 62 | func testSubCenturyYearsAreRejected() { |
| 63 | // JS's new Date(26,…) means 1926 and fails its round-trip; 0026 must |
| 64 | // not become 26 AD natively (it would file a permanently-due reminder). |
| 65 | XCTAssertNil(ReminderParse.parse("renew passport | 0026-08-01", now: 0)) |
| 66 | XCTAssertNil(ReminderParse.parse("renew passport | 0099-01-01", now: 0)) |
| 67 | } |
| 68 | |
| 69 | func testNewlinesTrimLikeTheWeb() { |
| 70 | // JS .trim() strips newlines; multi-line drafts must split identically. |
| 71 | XCTAssertEqual(ReminderParse.parse("call mum \n| 2026-08-01", now: 0)?.text, "call mum") |
| 72 | XCTAssertEqual(ReminderParse.parse("me \n in 10 s \n to stretch", now: 0)?.text, "stretch") |
| 73 | } |
| 74 | |
| 75 | func testAbsurdRelativeAmountsDoNotProduceInfinity() { |
| 76 | // A digit string big enough to overflow Double must reject (the web's |
| 77 | // equivalent add is dropped in the fold), never propagate ±inf into |
| 78 | // scheduling arithmetic. |
| 79 | let huge = String(repeating: "9", count: 400) |
| 80 | XCTAssertNil(ReminderParse.parse("call mum in \(huge) weeks", now: 0)) |
| 81 | // A large-but-finite amount still parses — the SERVER is the range |
| 82 | // authority (postSchedule's horizon check), same as the web. |
| 83 | XCTAssertNotNil(ReminderParse.parse("call mum in 999999999 weeks", now: 0)) |
| 84 | } |
| 85 | |
| 86 | func testRejections() { |
| 87 | XCTAssertNil(ReminderParse.parse("just a note", now: 0)) |
| 88 | XCTAssertNil(ReminderParse.parse("", now: 0)) |
| 89 | XCTAssertNil(ReminderParse.parse("do it in 3x", now: 0), "unknown unit") |
| 90 | XCTAssertNil(ReminderParse.parse("in 2h", now: 0), "no text before the time") |
| 91 | // Explicit dates/times are rejected, never rolled over, when out of range. |
| 92 | XCTAssertNil(ReminderParse.parse("bad | 2026-06-31", now: 0), "June has 30 days") |
| 93 | XCTAssertNil(ReminderParse.parse("bad | 2026-02-29", now: 0), "2026 isn't a leap year") |
| 94 | XCTAssertNil(ReminderParse.parse("bad | 2026-13-01", now: 0), "month > 12") |
| 95 | XCTAssertNil(ReminderParse.parse("bad | 2026-08-01 24:00", now: 0), "hour > 23") |
| 96 | XCTAssertNil(ReminderParse.parse("bad | 2026-08-01 12:60", now: 0), "minute > 59") |
| 97 | } |
| 98 | } |
| 99 | |