Files
karaoke-gen/frontend/lib/__tests__/utils.test.ts
T
Andrew Beveridge ☄️andClaude Opus 4.8 1b6833b182 fix(jobs): show job/log timestamps in the viewer's local timezone (#907)
## Problem

Job dates on the dashboard displayed the raw UTC wall-clock instead of
the viewer's local time. e.g. a job submitted at ~4:10 PM local (viewer
at UTC-2) showed as `8/14/2026, 6:10:25 PM`.

## Root cause

`created_at` (and timeline/log timestamps) were built from naive
`datetime.utcnow()` and serialized via `model_dump(mode='json')` /
`.isoformat()`, producing ISO strings with **no timezone offset**
(`"2026-08-14T18:10:25.123456"`). Firestore stored them as plain strings
and returned them verbatim. The frontend's `new Date(str)` parses an
offset-less ISO date-time as **local** time, so the UTC value was shown
unconverted — off by the viewer's UTC offset.

## Fix

**Frontend (fixes all existing + new data, no migration):**
- New `parseServerDate()` util in `frontend/lib/utils.ts` — treats
offset-less ISO date-times as UTC (appends `Z`); passes through strings
that already carry `Z`/`±HH:MM`, plus `Date`/epoch inputs.
- Applied at every customer-facing server-timestamp display: `JobCard`
created-at, `JobLogs` timestamps, referral payout dates, and
referral-discount expiry (`BuyCreditsDialog` + dashboard).
- 8 unit tests covering
naive/`Z`/`+00:00`/non-UTC-offset/date-only/epoch/`Date` cases.

**Backend (makes stored timestamps unambiguous going forward):**
- `create_job` and timeline events now use `datetime.now(timezone.utc)`,
so serialized ISO strings carry a `+00:00` offset. Arithmetic consumers
(`job_health_service`) already guard for both naive and aware datetimes.

## Testing
- Backend: 210 targeted tests pass (job_manager / firestore / timeline /
job_health / countdown).
- Frontend: full suite 1108 tests pass (85 suites), including the new
util tests.

## Notes
- `/admin/*` pages (English-only, internal) still use `new Date()`
directly; they benefit from the backend change for new jobs but were
left otherwise untouched as out of scope.
- CodeRabbit local review was rate-limited (free OSS quota), so this PR
intentionally omits `@coderabbitai ignore` to let the bot review.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-14 17:40:11 -04:00

49 lines
1.8 KiB
TypeScript

import { parseServerDate } from '../utils'
describe('parseServerDate', () => {
it('treats an offset-less ISO timestamp (naive UTC) as UTC', () => {
// Backend serializes datetime.utcnow() with no timezone designator.
// This should be interpreted as UTC, NOT local time.
const d = parseServerDate('2026-08-14T18:10:25')
expect(d.getTime()).toBe(Date.UTC(2026, 7, 14, 18, 10, 25))
})
it('treats an offset-less timestamp with fractional seconds as UTC', () => {
const d = parseServerDate('2026-08-14T18:10:25.123456')
expect(d.getTime()).toBe(Date.UTC(2026, 7, 14, 18, 10, 25, 123))
})
it('respects an explicit Z (UTC) designator', () => {
const d = parseServerDate('2026-08-14T18:10:25Z')
expect(d.getTime()).toBe(Date.UTC(2026, 7, 14, 18, 10, 25))
})
it('respects an explicit +00:00 offset', () => {
const d = parseServerDate('2026-08-14T18:10:25+00:00')
expect(d.getTime()).toBe(Date.UTC(2026, 7, 14, 18, 10, 25))
})
it('respects a non-UTC offset', () => {
// 18:10 at +02:00 == 16:10 UTC
const d = parseServerDate('2026-08-14T18:10:25+02:00')
expect(d.getTime()).toBe(Date.UTC(2026, 7, 14, 16, 10, 25))
})
it('passes through Date instances unchanged', () => {
const original = new Date(Date.UTC(2026, 7, 14, 18, 10, 25))
expect(parseServerDate(original)).toBe(original)
})
it('accepts epoch milliseconds', () => {
const ms = Date.UTC(2026, 7, 14, 18, 10, 25)
expect(parseServerDate(ms).getTime()).toBe(ms)
})
it('does not append Z to non-ISO-datetime strings (date-only)', () => {
// Date-only strings are already parsed as UTC by the Date constructor;
// appending Z would be invalid. Ensure we leave them alone.
const d = parseServerDate('2026-08-14')
expect(d.getTime()).toBe(Date.UTC(2026, 7, 14))
})
})