|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + formatDayHeading, |
| 6 | + formatShortMonthDayOrdinal, |
| 7 | + formatThreadSummaryLastReplyTime, |
| 8 | +} from "./dateFormatters.ts"; |
| 9 | + |
| 10 | +function localUnixSeconds(year, monthIndex, day) { |
| 11 | + return new Date(year, monthIndex, day, 12).getTime() / 1_000; |
| 12 | +} |
| 13 | + |
| 14 | +function weekday(date) { |
| 15 | + return new Intl.DateTimeFormat("en-US", { weekday: "long" }).format(date); |
| 16 | +} |
| 17 | + |
| 18 | +function month(date) { |
| 19 | + return new Intl.DateTimeFormat("en-US", { month: "long" }).format(date); |
| 20 | +} |
| 21 | + |
| 22 | +test("formatShortMonthDayOrdinal formats month before ordinal day", () => { |
| 23 | + assert.equal( |
| 24 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 19)), |
| 25 | + "May 19th", |
| 26 | + ); |
| 27 | +}); |
| 28 | + |
| 29 | +test("formatShortMonthDayOrdinal handles ordinal suffixes", () => { |
| 30 | + assert.equal( |
| 31 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 1)), |
| 32 | + "May 1st", |
| 33 | + ); |
| 34 | + assert.equal( |
| 35 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 2)), |
| 36 | + "May 2nd", |
| 37 | + ); |
| 38 | + assert.equal( |
| 39 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 3)), |
| 40 | + "May 3rd", |
| 41 | + ); |
| 42 | + assert.equal( |
| 43 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 4)), |
| 44 | + "May 4th", |
| 45 | + ); |
| 46 | + assert.equal( |
| 47 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 11)), |
| 48 | + "May 11th", |
| 49 | + ); |
| 50 | + assert.equal( |
| 51 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 12)), |
| 52 | + "May 12th", |
| 53 | + ); |
| 54 | + assert.equal( |
| 55 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 13)), |
| 56 | + "May 13th", |
| 57 | + ); |
| 58 | + assert.equal( |
| 59 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 21)), |
| 60 | + "May 21st", |
| 61 | + ); |
| 62 | + assert.equal( |
| 63 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 22)), |
| 64 | + "May 22nd", |
| 65 | + ); |
| 66 | + assert.equal( |
| 67 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 23)), |
| 68 | + "May 23rd", |
| 69 | + ); |
| 70 | + assert.equal( |
| 71 | + formatShortMonthDayOrdinal(localUnixSeconds(2026, 4, 31)), |
| 72 | + "May 31st", |
| 73 | + ); |
| 74 | +}); |
| 75 | + |
| 76 | +test("formatThreadSummaryLastReplyTime expands relative units", () => { |
| 77 | + const now = localUnixSeconds(2026, 4, 19); |
| 78 | + |
| 79 | + assert.equal(formatThreadSummaryLastReplyTime(now - 30, now), "just now"); |
| 80 | + assert.equal(formatThreadSummaryLastReplyTime(now - 60, now), "1 minute ago"); |
| 81 | + assert.equal( |
| 82 | + formatThreadSummaryLastReplyTime(now - 180, now), |
| 83 | + "3 minutes ago", |
| 84 | + ); |
| 85 | + assert.equal( |
| 86 | + formatThreadSummaryLastReplyTime(now - 3_600, now), |
| 87 | + "1 hour ago", |
| 88 | + ); |
| 89 | + assert.equal( |
| 90 | + formatThreadSummaryLastReplyTime(now - 10_800, now), |
| 91 | + "3 hours ago", |
| 92 | + ); |
| 93 | + assert.equal( |
| 94 | + formatThreadSummaryLastReplyTime(now - 86_400, now), |
| 95 | + "1 day ago", |
| 96 | + ); |
| 97 | + assert.equal( |
| 98 | + formatThreadSummaryLastReplyTime(now - 345_600, now), |
| 99 | + "4 days ago", |
| 100 | + ); |
| 101 | +}); |
| 102 | + |
| 103 | +test("formatThreadSummaryLastReplyTime uses ordinal dates for older replies", () => { |
| 104 | + const now = localUnixSeconds(2026, 5, 15); |
| 105 | + const replyAt = localUnixSeconds(2026, 4, 19); |
| 106 | + |
| 107 | + assert.equal(formatThreadSummaryLastReplyTime(replyAt, now), "on May 19th"); |
| 108 | +}); |
| 109 | + |
| 110 | +test("formatDayHeading omits the year for current-year dates", () => { |
| 111 | + const now = new Date(); |
| 112 | + const date = new Date(now.getFullYear(), (now.getMonth() + 6) % 12, 19, 12); |
| 113 | + |
| 114 | + assert.equal( |
| 115 | + formatDayHeading(date.getTime() / 1_000), |
| 116 | + `${weekday(date)}, ${month(date)} 19th`, |
| 117 | + ); |
| 118 | +}); |
| 119 | + |
| 120 | +test("formatDayHeading includes the year for other years", () => { |
| 121 | + const year = new Date().getFullYear() - 1; |
| 122 | + const date = new Date(year, 4, 19, 12); |
| 123 | + |
| 124 | + assert.equal( |
| 125 | + formatDayHeading(date.getTime() / 1_000), |
| 126 | + `${weekday(date)}, May 19th, ${year}`, |
| 127 | + ); |
| 128 | +}); |
0 commit comments