Skip to content

Commit 4f56db9

Browse files
committed
fix(webview): keep two-digit years in McpErrorRow and freeze the spec clock
What: differenceInCalendarDays now copies each input and zeroes it with setHours(0,0,0,0) instead of rebuilding it through new Date(year, month, day). The McpErrorRow spec freezes the system clock and gains two cases: the year-99/100 boundary and a just-after-midnight render. Why: two review findings, both real and both reproduced before fixing. 1. new Date(year, month, day) maps years 0-99 onto 1900-1999. A date in year 99 was read as 1999, so the year-99/100 boundary produced a delta of 693,959 days instead of 1 and the output degraded to a short date. Verified: the pre-fix helper returns "12/31/99" where "yesterday at 12:00 PM" is correct. 2. The component formats against the live new Date() while the fixture was built from the live Date.now(). In the first five minutes after local midnight the two land on different calendar days, so the /^today at/ assertion failed. Verified by freezing the clock at 00:02 local: the old assertion fails, and it now passes deterministically. Impact: no behaviour change outside those two cases. Re-ran the exhaustive equivalence sweep against date-fns formatRelative: 19,201 hourly samples across 5 timezones, mismatches=0, same as before the fix. Full webview suite 174 files / 2,249 tests passed.
1 parent 121494d commit 4f56db9

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎webview-ui/src/components/mcp/McpErrorRow.tsx‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ const MILLISECONDS_IN_DAY = 86_400_000
2020
* Calendar days, not elapsed milliseconds: 23:59 and 00:01 on consecutive days
2121
* are one day apart even though only two minutes elapsed. Local midnights are
2222
* compared directly, so DST shifts (23h or 25h days) still round to one day.
23+
*
24+
* The inputs are copied and zeroed with `setHours` rather than rebuilt through
25+
* `new Date(year, month, day)`, because that constructor maps years 0-99 onto
26+
* 1900-1999: a date in year 99 would be silently read as 1999 and produce a
27+
* delta of centuries instead of a day.
2328
*/
2429
const differenceInCalendarDays = (date: Date, baseDate: Date): number => {
25-
const dateMidnight = new Date(date.getFullYear(), date.getMonth(), date.getDate())
26-
const baseMidnight = new Date(baseDate.getFullYear(), baseDate.getMonth(), baseDate.getDate())
30+
const dateMidnight = new Date(date)
31+
dateMidnight.setHours(0, 0, 0, 0)
32+
33+
const baseMidnight = new Date(baseDate)
34+
baseMidnight.setHours(0, 0, 0, 0)
2735

2836
return Math.round((dateMidnight.getTime() - baseMidnight.getTime()) / MILLISECONDS_IN_DAY)
2937
}

‎webview-ui/src/components/mcp/__tests__/McpErrorRow.spec.tsx‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,51 @@ describe("formatRelativeTimestamp", () => {
4242
it("throws on an invalid date", () => {
4343
expect(() => formatRelativeTimestamp(new Date(Number.NaN), BASE)).toThrow(RangeError)
4444
})
45+
46+
it("keeps two-digit years instead of remapping them to 1900s", () => {
47+
// `new Date(99, 11, 31)` would mean 1999. These are real year-99 dates, and
48+
// the day delta across the year-99/100 boundary must stay one day.
49+
const year99 = new Date("0099-12-31T12:00:00")
50+
const year100 = new Date("0100-01-01T12:00:00")
51+
52+
expect(year99.getFullYear()).toBe(99)
53+
expect(formatRelativeTimestamp(year99, year100)).toBe("yesterday at 12:00 PM")
54+
expect(formatRelativeTimestamp(year100, year99)).toBe("tomorrow at 12:00 PM")
55+
expect(formatRelativeTimestamp(year100, year100)).toBe("today at 12:00 PM")
56+
})
4557
})
4658

4759
describe("McpErrorRow", () => {
60+
// Freeze the clock at a stable midday instant. Without this the component
61+
// formats against the live `new Date()` while the fixture is built from the
62+
// live `Date.now()`; in the first five minutes after local midnight the two
63+
// land on different calendar days and the "today at" assertion fails.
64+
beforeEach(() => {
65+
vi.useFakeTimers()
66+
vi.setSystemTime(new Date(2026, 9, 29, 12, 0, 0))
67+
})
68+
69+
afterEach(() => {
70+
vi.useRealTimers()
71+
})
72+
4873
it("renders the error message and a relative timestamp", () => {
4974
const timestamp = Date.now() - 5 * 60 * 1000
5075

5176
render(<McpErrorRow error={{ message: "Connection refused", timestamp, level: "error" }} />)
5277

5378
expect(screen.getByText("Connection refused")).toBeInTheDocument()
54-
expect(screen.getByText(/^today at \d{1,2}:\d{2} (AM|PM)$/)).toBeInTheDocument()
79+
expect(screen.getByText("today at 11:55 AM")).toBeInTheDocument()
80+
})
81+
82+
it("still renders a same-day timestamp just after local midnight", () => {
83+
// The exact window the flake appeared in: 00:02 local, five minutes ago
84+
// lands on the previous calendar day, so the phrasing must be "yesterday".
85+
vi.setSystemTime(new Date(2026, 9, 29, 0, 2, 0))
86+
87+
render(<McpErrorRow error={{ message: "late", timestamp: Date.now() - 5 * 60 * 1000, level: "warn" }} />)
88+
89+
expect(screen.getByText("yesterday at 11:57 PM")).toBeInTheDocument()
5590
})
5691

5792
it.each([

0 commit comments

Comments
 (0)