55 lines
2.1 KiB
TypeScript
55 lines
2.1 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import test from 'node:test';
|
|
|
|
import { endOfLocalDay, formatDateParam, parseLocalDateInput, rangeForDay, rangeForLastDays, rangeForPreviousDay, startOfLocalDay } from './dateRanges.ts';
|
|
|
|
const assertRange = (range: { start: Date; end: Date }, start: string, end: string) => {
|
|
assert.equal(formatDateParam(range.start), start);
|
|
assert.equal(formatDateParam(range.end), end);
|
|
assert.equal(range.start.getHours(), 0);
|
|
assert.equal(range.start.getMinutes(), 0);
|
|
assert.equal(range.start.getSeconds(), 0);
|
|
assert.equal(range.start.getMilliseconds(), 0);
|
|
assert.equal(range.end.getHours(), 23);
|
|
assert.equal(range.end.getMinutes(), 59);
|
|
assert.equal(range.end.getSeconds(), 59);
|
|
assert.equal(range.end.getMilliseconds(), 999);
|
|
};
|
|
|
|
test('Hoje uses full local calendar-day boundaries', () => {
|
|
const today = new Date(2026, 5, 15, 14, 30, 10, 50);
|
|
|
|
assertRange(rangeForDay(today), '2026-06-15', '2026-06-15');
|
|
});
|
|
|
|
test('Ontem uses the full previous local calendar day', () => {
|
|
const today = new Date(2026, 5, 15, 0, 5, 0, 0);
|
|
|
|
assertRange(rangeForPreviousDay(today), '2026-06-14', '2026-06-14');
|
|
});
|
|
|
|
test('Ultimos 7 dias includes today plus the previous 6 calendar days', () => {
|
|
const today = new Date(2026, 5, 15, 22, 10, 0, 0);
|
|
|
|
assertRange(rangeForLastDays(7, today), '2026-06-09', '2026-06-15');
|
|
});
|
|
|
|
test('Ultimos 30 and 90 dias use inclusive calendar-day ranges', () => {
|
|
const today = new Date(2026, 5, 15, 22, 10, 0, 0);
|
|
|
|
assertRange(rangeForLastDays(30, today), '2026-05-17', '2026-06-15');
|
|
assertRange(rangeForLastDays(90, today), '2026-03-18', '2026-06-15');
|
|
});
|
|
|
|
test('custom single-day input parses as local date and can build a full-day range', () => {
|
|
const customDate = parseLocalDateInput('2026-06-14');
|
|
|
|
assert.ok(customDate);
|
|
assertRange({ start: startOfLocalDay(customDate), end: endOfLocalDay(customDate) }, '2026-06-14', '2026-06-14');
|
|
});
|
|
|
|
test('API date params are stable YYYY-MM-DD strings', () => {
|
|
assert.equal(formatDateParam(new Date(2026, 5, 14, 23, 59, 59, 999)), '2026-06-14');
|
|
assert.equal(parseLocalDateInput('14/06/2026'), null);
|
|
});
|