feat(extension): add shared relative-time util with tests
This commit is contained in:
46
extension/src/shared/__tests__/relative-time.test.ts
Normal file
46
extension/src/shared/__tests__/relative-time.test.ts
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||||
|
import { relativeTime, daysUntilPurge } from '../relative-time';
|
||||||
|
|
||||||
|
const NOW_UNIX = 1779552000; // fixed reference instant
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
vi.setSystemTime(new Date(NOW_UNIX * 1000));
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('relativeTime', () => {
|
||||||
|
it('returns "just now" under 60s', () => {
|
||||||
|
expect(relativeTime(NOW_UNIX - 30)).toBe('just now');
|
||||||
|
});
|
||||||
|
it('returns minutes under an hour', () => {
|
||||||
|
expect(relativeTime(NOW_UNIX - 600)).toBe('10m ago');
|
||||||
|
});
|
||||||
|
it('returns hours under a day', () => {
|
||||||
|
expect(relativeTime(NOW_UNIX - 7200)).toBe('2h ago');
|
||||||
|
});
|
||||||
|
it('returns days under a week', () => {
|
||||||
|
expect(relativeTime(NOW_UNIX - 3 * 86400)).toBe('3d ago');
|
||||||
|
});
|
||||||
|
it('returns weeks under a month', () => {
|
||||||
|
expect(relativeTime(NOW_UNIX - 14 * 86400)).toBe('2w ago');
|
||||||
|
});
|
||||||
|
it('returns months above 30 days', () => {
|
||||||
|
expect(relativeTime(NOW_UNIX - 90 * 86400)).toBe('3mo ago');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('daysUntilPurge', () => {
|
||||||
|
it('returns null for forever retention', () => {
|
||||||
|
expect(daysUntilPurge(NOW_UNIX - 5 * 86400, { kind: 'forever' })).toBeNull();
|
||||||
|
});
|
||||||
|
it('returns remaining days for a recent trash', () => {
|
||||||
|
expect(daysUntilPurge(NOW_UNIX - 8 * 86400, { kind: 'days', value: 30 })).toBe(22);
|
||||||
|
});
|
||||||
|
it('clamps to zero when retention already elapsed', () => {
|
||||||
|
expect(daysUntilPurge(NOW_UNIX - 60 * 86400, { kind: 'days', value: 30 })).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
||||||
27
extension/src/shared/relative-time.ts
Normal file
27
extension/src/shared/relative-time.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/// Single source of truth for relative-time formatting and trash-retention math.
|
||||||
|
/// Pulled out of five near-duplicate inline copies (settings-vault, devices,
|
||||||
|
/// trash, field-history, vault/vault).
|
||||||
|
|
||||||
|
import type { TrashRetention } from './types';
|
||||||
|
|
||||||
|
/// Format a past unix timestamp (seconds) as "Nm ago" / "Nh ago" / "Nd ago" /
|
||||||
|
/// "Nw ago" / "Nmo ago" relative to now. Returns "just now" under 60 seconds.
|
||||||
|
export function relativeTime(unixSec: number): string {
|
||||||
|
const now = Math.floor(Date.now() / 1000);
|
||||||
|
const diff = now - unixSec;
|
||||||
|
if (diff < 60) return 'just now';
|
||||||
|
if (diff < 3600) return `${Math.floor(diff / 60)}m ago`;
|
||||||
|
if (diff < 86400) return `${Math.floor(diff / 3600)}h ago`;
|
||||||
|
if (diff < 604800) return `${Math.floor(diff / 86400)}d ago`;
|
||||||
|
if (diff < 2592000) return `${Math.floor(diff / 604800)}w ago`;
|
||||||
|
return `${Math.floor(diff / 2592000)}mo ago`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Days remaining until an item trashed at `trashedAt` (unix seconds) will be
|
||||||
|
/// auto-purged given the vault's retention policy. Returns null for forever
|
||||||
|
/// retention; clamps to 0 when the retention window has already elapsed.
|
||||||
|
export function daysUntilPurge(trashedAt: number, retention: TrashRetention): number | null {
|
||||||
|
if (retention.kind === 'forever') return null;
|
||||||
|
const trashedDaysAgo = Math.floor((Date.now() / 1000 - trashedAt) / 86400);
|
||||||
|
return Math.max(0, retention.value - trashedDaysAgo);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user