Files
dashboard/backend/utils/__tests__/dateUtils.test.js
T
2026-08-27 14:26:19 +07:00

77 lines
2.6 KiB
JavaScript

import { describe, it, expect, vi, afterEach } from 'vitest';
/**
* Batas 17:00 WIB adalah aturan yang menentukan "hari data" seluruh aplikasi:
* data operasional hanya diperbarui sekali sehari pada pukul 17:00, jadi
* sebelum jam itu hari yang datanya sudah lengkap masih D-1.
*
* `CUTOFF_HOUR` dibaca sekali saat modul dimuat, jadi tiap kasus di bawah
* me-reset module registry dan mengimpor ulang supaya override-nya terpakai.
* Dengan begitu tesnya deterministik — tidak ikut berubah tergantung jam
* berapa suite ini dijalankan.
*/
const loadWithCutoff = async (hour) => {
vi.resetModules();
process.env.CUTOFF_HOUR_OVERRIDE = String(hour);
const mod = await import('../dateUtils.js');
return mod.default ?? mod;
};
afterEach(() => {
delete process.env.CUTOFF_HOUR_OVERRIDE;
vi.resetModules();
});
describe('batas 17:00 (D-1 logic)', () => {
it('cutoff 24 berarti jam berapa pun masih sebelum batas — hari efektif selalu D-1', async () => {
const { isBeforeCutoffTime, getEffectiveQueryDate, getCurrentWibDate } =
await loadWithCutoff(24);
expect(isBeforeCutoffTime()).toBe(true);
const today = getCurrentWibDate();
const effective = getEffectiveQueryDate();
today.setHours(0, 0, 0, 0);
effective.setHours(0, 0, 0, 0);
const selisihHari = Math.round((today - effective) / (1000 * 60 * 60 * 24));
expect(selisihHari).toBe(1);
});
it('cutoff 0 berarti batas sudah lewat — hari efektif adalah hari ini', async () => {
const { isBeforeCutoffTime, getEffectiveQueryDate, getCurrentWibDate } = await loadWithCutoff(0);
expect(isBeforeCutoffTime()).toBe(false);
const today = getCurrentWibDate();
const effective = getEffectiveQueryDate();
today.setHours(0, 0, 0, 0);
effective.setHours(0, 0, 0, 0);
expect(effective.getTime()).toBe(today.getTime());
});
it('nilai bawaannya 17, bukan tengah malam', async () => {
vi.resetModules();
delete process.env.CUTOFF_HOUR_OVERRIDE;
const mod = await import('../dateUtils.js');
const { CUTOFF_HOUR } = mod.default ?? mod;
expect(CUTOFF_HOUR).toBe(17);
});
it('hari efektif tidak pernah melompat lebih dari satu hari dari hari ini', async () => {
for (const hour of [0, 8, 17, 23, 24]) {
const { getEffectiveQueryDate, getCurrentWibDate } = await loadWithCutoff(hour);
const today = getCurrentWibDate();
const effective = getEffectiveQueryDate();
today.setHours(0, 0, 0, 0);
effective.setHours(0, 0, 0, 0);
const selisihHari = Math.round((today - effective) / (1000 * 60 * 60 * 24));
expect(selisihHari === 0 || selisihHari === 1).toBe(true);
}
});
});