pray-calc/test-cjs.cjs
Aric Camarata c02f197ece v2.0.0 — TypeScript rewrite, dual ESM/CJS, 14 methods + PCD dynamic algorithm
Complete rewrite from plain JavaScript to TypeScript with dual CJS/ESM output
via tsup. Removes all legacy .js source files and the old CommonJS-only index.

Key changes:
- Full TypeScript source in src/ with strict mode and declaration maps
- tsup build: dist/index.cjs + dist/index.mjs + dual .d.ts / .d.mts types
- 14 traditional fixed-angle methods (UOIF through MUIS) + MSC seasonal method
- PCD dynamic algorithm: MSC seasonal base + Earth-Sun distance correction +
  ecliptic geometry + atmospheric refraction + observer elevation
- getTimesAll() batches all 14x2 zenith angles into a single SPA call
- getMscFajr() / getMscIsha() expose MSC seasonal reference directly
- getAngles() returns the PCD-computed fajrAngle and ishaAngle
- High-latitude bounds: angles clipped to [10, 20] above 55N
- 106 tests across ESM and CJS (test.mjs + test-cjs.cjs)
- CI matrix: Node 20/22/24, typecheck, pack-check
- Wiki: 12 reference pages + 6-page research section with global accuracy study,
  home-territory comparison, observational evidence, and field observation matrix
- Moon functions removed (migrated to moon-sighting package)
- pnpm-only, Node >=20, sideEffects: false
2026-02-25 18:11:20 -05:00

122 lines
3.3 KiB
JavaScript

/**
* pray-calc v2 — CJS smoke test.
*
* Verifies that the CommonJS build loads and the primary API works.
*/
'use strict';
const assert = require('assert');
const {
getTimes,
calcTimes,
getTimesAll,
calcTimesAll,
getAngles,
getAsr,
getQiyam,
getMscFajr,
getMscIsha,
solarEphemeris,
toJulianDate,
METHODS,
} = require('./dist/index.cjs');
let passed = 0;
let failed = 0;
function test(name, fn) {
try {
fn();
console.log(` ${name}... PASS`);
passed++;
} catch (err) {
console.error(` ${name}... FAIL: ${err.message}`);
failed++;
}
}
console.log('\n[CJS] Core exports');
test('METHODS exported and has 14 entries', () => {
assert(Array.isArray(METHODS));
assert.strictEqual(METHODS.length, 14);
});
test('getTimes returns valid structure', () => {
const t = getTimes(new Date('2024-06-21'), 40.7128, -74.0060, -4);
assert(isFinite(t.Fajr), `Fajr=${t.Fajr}`);
assert(isFinite(t.Sunrise), `Sunrise=${t.Sunrise}`);
assert(isFinite(t.Maghrib), `Maghrib=${t.Maghrib}`);
assert(isFinite(t.Isha), `Isha=${t.Isha}`);
assert(typeof t.angles.fajrAngle === 'number');
});
test('calcTimes returns HH:MM:SS strings', () => {
const t = calcTimes(new Date('2024-06-21'), 40.7128, -74.0060, -4);
assert(/^\d{2}:\d{2}:\d{2}$/.test(t.Fajr), `Fajr="${t.Fajr}"`);
assert(/^\d{2}:\d{2}:\d{2}$/.test(t.Sunrise), `Sunrise="${t.Sunrise}"`);
assert(/^\d{2}:\d{2}:\d{2}$/.test(t.Maghrib), `Maghrib="${t.Maghrib}"`);
});
test('getTimesAll returns 14 methods', () => {
const t = getTimesAll(new Date('2024-06-21'), 40.7128, -74.0060, -4);
assert.strictEqual(Object.keys(t.Methods).length, 14);
});
test('calcTimesAll Methods are string pairs', () => {
const t = calcTimesAll(new Date('2024-06-21'), 40.7128, -74.0060, -4);
for (const [fajr, isha] of Object.values(t.Methods)) {
assert(typeof fajr === 'string');
assert(typeof isha === 'string');
}
});
test('getAngles returns bounded angles', () => {
const a = getAngles(new Date('2024-06-21'), 40.7128, -74.0060);
assert(a.fajrAngle >= 10 && a.fajrAngle <= 22);
assert(a.ishaAngle >= 10 && a.ishaAngle <= 22);
});
test('getAsr Hanafi later than Shafii', () => {
const s = getAsr(12.0, 40.7, 20.0, false);
const h = getAsr(12.0, 40.7, 20.0, true);
assert(h > s);
});
test('getQiyam returns a number', () => {
const q = getQiyam(4.0, 22.0);
assert(typeof q === 'number');
});
test('getMscFajr returns positive minutes', () => {
const m = getMscFajr(new Date('2024-06-21'), 40.7);
assert(m > 0);
});
test('getMscIsha returns positive minutes', () => {
const m = getMscIsha(new Date('2024-06-21'), 40.7);
assert(m > 0);
});
test('toJulianDate and solarEphemeris work', () => {
const jd = toJulianDate(new Date(Date.UTC(2024, 5, 21, 12, 0, 0)));
const e = solarEphemeris(jd);
assert(typeof e.decl === 'number');
assert(typeof e.r === 'number');
assert(typeof e.eclLon === 'number');
});
test('Makkah all-methods comparison — UAQ Isha = Maghrib + 90min', () => {
const t = getTimesAll(new Date('2024-06-21'), 21.4225, 39.8262, 3);
const diff = (t.Methods.UAQ[1] - t.Maghrib) * 60;
assert(Math.abs(diff - 90) < 2, `UAQ isha diff=${diff}`);
});
const total = passed + failed;
console.log(`\n${'─'.repeat(50)}`);
console.log(`${passed}/${total} CJS tests passed`);
if (failed > 0) {
process.exit(1);
}