mirror of
https://github.com/ksyasuda/SubMiner.git
synced 2026-07-27 04:49:49 -07:00
refactor(renderer): extract modal registry for open/dismiss/active queries (#166)
This commit is contained in:
@@ -3,28 +3,52 @@ import test from 'node:test';
|
||||
|
||||
import { epochMsFromDbTimestamp, formatRelativeDate, formatSessionDayLabel } from './formatters';
|
||||
|
||||
const FIXED_NOW = new Date(2026, 2, 16, 12, 0, 0).getTime();
|
||||
|
||||
function withFixedNow(run: (now: number) => void, now = FIXED_NOW): void {
|
||||
const realNow = Date.now;
|
||||
Date.now = () => now;
|
||||
try {
|
||||
run(now);
|
||||
} finally {
|
||||
Date.now = realNow;
|
||||
}
|
||||
}
|
||||
|
||||
test('formatRelativeDate: future timestamps return "just now"', () => {
|
||||
assert.equal(formatRelativeDate(Date.now() + 60_000), 'just now');
|
||||
withFixedNow((now) => {
|
||||
assert.equal(formatRelativeDate(now + 60_000), 'just now');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: 0ms ago returns "just now"', () => {
|
||||
assert.equal(formatRelativeDate(Date.now()), 'just now');
|
||||
withFixedNow((now) => {
|
||||
assert.equal(formatRelativeDate(now), 'just now');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: 30s ago returns "just now"', () => {
|
||||
assert.equal(formatRelativeDate(Date.now() - 30_000), 'just now');
|
||||
withFixedNow((now) => {
|
||||
assert.equal(formatRelativeDate(now - 30_000), 'just now');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: 5 minutes ago returns "5m ago"', () => {
|
||||
assert.equal(formatRelativeDate(Date.now() - 5 * 60_000), '5m ago');
|
||||
withFixedNow((now) => {
|
||||
assert.equal(formatRelativeDate(now - 5 * 60_000), '5m ago');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: 59 minutes ago returns "59m ago"', () => {
|
||||
assert.equal(formatRelativeDate(Date.now() - 59 * 60_000), '59m ago');
|
||||
withFixedNow((now) => {
|
||||
assert.equal(formatRelativeDate(now - 59 * 60_000), '59m ago');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: 2 hours ago returns "2h ago"', () => {
|
||||
assert.equal(formatRelativeDate(Date.now() - 2 * 3_600_000), '2h ago');
|
||||
withFixedNow((now) => {
|
||||
assert.equal(formatRelativeDate(now - 2 * 3_600_000), '2h ago');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: same calendar day can return "23h ago"', () => {
|
||||
@@ -52,12 +76,16 @@ test('formatRelativeDate: two calendar days ago returns "2d ago"', () => {
|
||||
});
|
||||
|
||||
test('formatRelativeDate: 5 days ago returns "5d ago"', () => {
|
||||
assert.equal(formatRelativeDate(Date.now() - 5 * 86_400_000), '5d ago');
|
||||
withFixedNow((now) => {
|
||||
assert.equal(formatRelativeDate(now - 5 * 86_400_000), '5d ago');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: 10 days ago returns locale date string', () => {
|
||||
const ts = Date.now() - 10 * 86_400_000;
|
||||
assert.equal(formatRelativeDate(ts), new Date(ts).toLocaleDateString());
|
||||
withFixedNow((now) => {
|
||||
const ts = now - 10 * 86_400_000;
|
||||
assert.equal(formatRelativeDate(ts), new Date(ts).toLocaleDateString());
|
||||
});
|
||||
});
|
||||
|
||||
test('formatRelativeDate: prior calendar day under 24h returns "Yesterday"', () => {
|
||||
@@ -81,21 +109,29 @@ test('epochMsFromDbTimestamp keeps ms timestamps as-is', () => {
|
||||
});
|
||||
|
||||
test('formatSessionDayLabel formats today and yesterday', () => {
|
||||
const now = Date.now();
|
||||
const oneDayMs = 24 * 60 * 60_000;
|
||||
assert.equal(formatSessionDayLabel(now), 'Today');
|
||||
assert.equal(formatSessionDayLabel(now - oneDayMs), 'Yesterday');
|
||||
withFixedNow((now) => {
|
||||
const oneDayMs = 24 * 60 * 60_000;
|
||||
assert.equal(formatSessionDayLabel(now), 'Today');
|
||||
assert.equal(formatSessionDayLabel(now - oneDayMs), 'Yesterday');
|
||||
});
|
||||
});
|
||||
|
||||
test('formatSessionDayLabel includes year for past-year dates', () => {
|
||||
const now = new Date();
|
||||
const sameDayLastYear = new Date(now.getFullYear() - 1, now.getMonth(), now.getDate()).getTime();
|
||||
const label = formatSessionDayLabel(sameDayLastYear);
|
||||
const year = new Date(sameDayLastYear).getFullYear();
|
||||
assert.ok(label.includes(String(year)));
|
||||
const withoutYear = new Date(sameDayLastYear).toLocaleDateString(undefined, {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
});
|
||||
assert.notEqual(label, withoutYear);
|
||||
const fixedNow = new Date(2027, 2, 16, 12, 0, 0).getTime();
|
||||
withFixedNow((now) => {
|
||||
const current = new Date(now);
|
||||
const sameDayLastYear = new Date(
|
||||
current.getFullYear() - 1,
|
||||
current.getMonth(),
|
||||
current.getDate(),
|
||||
).getTime();
|
||||
const label = formatSessionDayLabel(sameDayLastYear);
|
||||
const year = new Date(sameDayLastYear).getFullYear();
|
||||
assert.ok(label.includes(String(year)));
|
||||
const withoutYear = new Date(sameDayLastYear).toLocaleDateString(undefined, {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
});
|
||||
assert.notEqual(label, withoutYear);
|
||||
}, fixedNow);
|
||||
});
|
||||
|
||||
@@ -66,7 +66,7 @@ export function formatSessionDayLabel(sessionStartedAtMs: number): string {
|
||||
if (day === today - 1) return 'Yesterday';
|
||||
|
||||
const date = new Date(sessionStartedAtMs);
|
||||
const includeYear = date.getFullYear() !== new Date().getFullYear();
|
||||
const includeYear = date.getFullYear() !== new Date(Date.now()).getFullYear();
|
||||
return date.toLocaleDateString(undefined, {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
|
||||
Reference in New Issue
Block a user