date-fns-hijri/test-cjs.cjs
Aric Camarata d12117f000 fix: local-day adapter semantics with exact round-trips on hijri-core's UTC-day contract
Behavior changes (lock-step with hijri-core fix/utc-day-boundary):

- toHijriDate and all field/format/comparison/arithmetic functions now lift input
  Dates through localDayToUtcSlot() before calling coreToHijri(), reading the
  caller's LOCAL calendar day (date-fns convention). Previously passed the raw
  Date which caused off-by-one results in timezones west of UTC against the new
  UTC-day core contract.

- fromHijriDate now returns local-midnight Dates (new Date(y, m, d)) instead of
  UTC midnight. Local field accessors and date-fns format() render the intended
  calendar day on every host timezone. toISOString() is no longer the right API
  for this value.

- addHijriMonths, addHijriYears, startOfHijriMonth, endOfHijriMonth call
  fromHijriDate directly; the utcMidnightToLocalNoon shim is removed.

- Round-trip toHijriDate(fromHijriDate(y, m, d)) is now exact on every timezone.

Verified: 58/58 ESM tests, 10/10 CJS tests, 16/16 vitest assertions across
TZ=UTC, TZ=America/New_York, and TZ=Pacific/Auckland.
2026-06-10 16:38:32 -04:00

76 lines
1.8 KiB
JavaScript

'use strict';
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const {
toHijriDate,
fromHijriDate,
isValidHijriDate,
getHijriMonthName,
formatHijriDate,
getHijriYear,
getHijriMonth,
getHijriDay,
} = require('./dist/index.cjs');
const REF = new Date(2023, 2, 23, 12); // 1 Ramadan 1444
describe('CJS: toHijriDate', () => {
it('returns correct HijriDate', () => {
const h = toHijriDate(REF);
assert.ok(h !== null);
assert.equal(h.hy, 1444);
assert.equal(h.hm, 9);
assert.equal(h.hd, 1);
});
});
describe('CJS: fromHijriDate', () => {
it('converts to correct Gregorian date (local midnight)', () => {
const d = fromHijriDate(1444, 9, 1);
// Returns local midnight — use local accessors, not UTC
assert.equal(d.getFullYear(), 2023);
assert.equal(d.getMonth(), 2);
assert.equal(d.getDate(), 23);
});
});
describe('CJS: isValidHijriDate', () => {
it('true for valid date', () => {
assert.equal(isValidHijriDate(1444, 9, 1), true);
});
it('false for invalid month', () => {
assert.equal(isValidHijriDate(1444, 13, 1), false);
});
});
describe('CJS: getHijriMonthName', () => {
it('long', () => {
assert.equal(getHijriMonthName(9), 'Ramadan');
});
it('short', () => {
assert.equal(getHijriMonthName(9, 'short'), 'Ram');
});
});
describe('CJS: formatHijriDate', () => {
it('iYYYY-iMM-iDD', () => {
assert.equal(formatHijriDate(REF, 'iYYYY-iMM-iDD'), '1444-09-01');
});
});
describe('CJS: field getters', () => {
it('getHijriYear', () => {
assert.equal(getHijriYear(REF), 1444);
});
it('getHijriMonth', () => {
assert.equal(getHijriMonth(REF), 9);
});
it('getHijriDay', () => {
assert.equal(getHijriDay(REF), 1);
});
});