Files
TORCH/test/utils.test.mjs
T
google-labs-jules[bot]andPR0M3TH3AN e787247c6f test(utils): add unit tests for todayDateStr and nowUnix
Add comprehensive unit tests for `todayDateStr` and `nowUnix` in `src/utils.mjs`.
These tests use `node:test`'s mock timers to ensure deterministic execution, verifying
that the date formatting and timestamp generation behave as expected.

- `todayDateStr`: verified correct YYYY-MM-DD format
- `nowUnix`: verified correct integer timestamp generation

Co-authored-by: PR0M3TH3AN <[email protected]>
2026-02-18 03:27:26 +00:00

59 lines
2.0 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert';
import { getIsoWeekStr, todayDateStr, nowUnix } from '../src/utils.mjs';
describe('Date Utilities', () => {
describe('todayDateStr', () => {
it('returns current date in YYYY-MM-DD format', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
// 2023-10-25 12:00:00 UTC
const date = new Date('2023-10-25T12:00:00Z');
t.mock.timers.setTime(date.getTime());
assert.strictEqual(todayDateStr(), '2023-10-25');
});
});
describe('nowUnix', () => {
it('returns current unix timestamp', (t) => {
t.mock.timers.enable({ apis: ['Date'] });
const timeMs = 1700000000000;
t.mock.timers.setTime(timeMs);
// 1700000000000 / 1000 = 1700000000
assert.strictEqual(nowUnix(), 1700000000);
});
});
describe('getIsoWeekStr', () => {
it('returns correct ISO week for simple cases', () => {
assert.strictEqual(getIsoWeekStr('2023-01-02'), '2023-W01'); // Mon
assert.strictEqual(getIsoWeekStr('2023-01-08'), '2023-W01'); // Sun
assert.strictEqual(getIsoWeekStr('2023-01-09'), '2023-W02'); // Mon
});
it('handles year boundaries correctly', () => {
// 2023-01-01 is Sunday. Belong to last week of 2022.
assert.strictEqual(getIsoWeekStr('2023-01-01'), '2022-W52');
// 2024-01-01 is Monday. Week 1 of 2024.
assert.strictEqual(getIsoWeekStr('2024-01-01'), '2024-W01');
// 2023-12-31 is Sunday. Week 52 of 2023.
assert.strictEqual(getIsoWeekStr('2023-12-31'), '2023-W52');
});
it('handles leap years', () => {
assert.strictEqual(getIsoWeekStr('2024-02-29'), '2024-W09');
});
it('handles empty input (defaults to today)', () => {
// Just check it returns a string in correct format
const result = getIsoWeekStr();
assert.match(result, /^\d{4}-W\d{2}$/);
});
it('handles invalid input gracefully', () => {
assert.strictEqual(getIsoWeekStr('invalid-date'), '');
});
});
});