timing.test.js
1 import { describe, expect, it } from 'vitest'; 2 import { validateTiming, toUnixSeconds } from './timing.js'; 3 describe('validateTiming', () => { 4 const now = () => Math.floor(Date.now() / 1000); 5 it('accepts a time 3 hours from now', () => { 6 expect(() => validateTiming(now() + 3 * 3600)).not.toThrow(); 7 }); 8 it('rejects a time less than 2 hours from now', () => { 9 expect(() => validateTiming(now() + 3600)).toThrow('至少 2 小时后'); 10 }); 11 it('rejects a time more than 14 days from now', () => { 12 expect(() => validateTiming(now() + 15 * 86400)).toThrow('不能超过 14 天'); 13 }); 14 }); 15 describe('toUnixSeconds', () => { 16 it('passes through a numeric unix timestamp', () => { 17 expect(toUnixSeconds(1744070400)).toBe(1744070400); 18 }); 19 it('parses a numeric unix timestamp string', () => { 20 expect(toUnixSeconds('1744070400')).toBe(1744070400); 21 }); 22 it('parses ISO8601 string', () => { 23 expect(toUnixSeconds('2026-04-08T12:00:00Z')).toBe(Math.floor(new Date('2026-04-08T12:00:00Z').getTime() / 1000)); 24 }); 25 it('throws on invalid input', () => { 26 expect(() => toUnixSeconds('not-a-date')).toThrow(); 27 }); 28 });