120 lines
4.1 KiB
TypeScript
120 lines
4.1 KiB
TypeScript
import { describe, it, expect } from 'vitest';
|
||
import {
|
||
describeDataDates,
|
||
describeKandangScope,
|
||
describeReportPeriod,
|
||
describeTimeframePeriod,
|
||
parseDayRangeLabel,
|
||
buildPdfFilename,
|
||
isoDataDates,
|
||
reportPeriodSlug,
|
||
} from '../PdfReportHeader.tsx';
|
||
|
||
const START = '2026-05-21T17:00:00.000Z'; // cycle CYCLE-JBW-2026-05-22
|
||
|
||
describe('PdfReportHeader helpers', () => {
|
||
it('maps cycle day 0 to the start date', () => {
|
||
expect(describeDataDates(START, 0)).toBe('22 Mei 2026');
|
||
expect(describeDataDates(START, 0, 0)).toBe('22 Mei 2026');
|
||
});
|
||
it('maps cycle day 1 to the day after start', () => {
|
||
expect(describeDataDates(START, 1)).toBe('23 Mei 2026');
|
||
});
|
||
it('maps day 48 to the right calendar date', () => {
|
||
expect(describeDataDates(START, 48)).toBe('9 Jul 2026');
|
||
});
|
||
it('renders a range for a week window', () => {
|
||
expect(describeDataDates(START, 42, 48)).toBe('3 Jul 2026 – 9 Jul 2026');
|
||
});
|
||
it('omits the date when the cycle start is unknown', () => {
|
||
expect(describeDataDates(null, 12)).toBeNull();
|
||
});
|
||
it('reads day numbers out of a range label', () => {
|
||
expect(parseDayRangeLabel('hari ke-42 s/d ke-48')).toEqual({ fromDay: 42, toDay: 48 });
|
||
expect(parseDayRangeLabel('hari ke-48')).toEqual({ fromDay: 48, toDay: null });
|
||
expect(parseDayRangeLabel('Minggu 6')).toEqual({ fromDay: 6, toDay: null });
|
||
});
|
||
it('names the combined scope when no coop is selected', () => {
|
||
const kandangs = [
|
||
{ id: 1, name: 'Kandang Atas' },
|
||
{ id: 2, name: 'Kandang Bawah' },
|
||
];
|
||
expect(describeKandangScope(kandangs, null)).toBe('Semua Kandang (Atas + Bawah)');
|
||
expect(describeKandangScope(kandangs, 1)).toBe('Kandang Atas');
|
||
expect(describeKandangScope(kandangs, '2')).toBe('Kandang Bawah');
|
||
});
|
||
it('labels each report scope', () => {
|
||
expect(describeReportPeriod('daily')).toBe('Laporan Harian');
|
||
expect(describeReportPeriod('weekly', 'Minggu 6')).toBe('Laporan Mingguan — Minggu 6');
|
||
expect(describeReportPeriod('end_cycle')).toBe('Laporan Akhir Siklus');
|
||
expect(describeTimeframePeriod('mingguan', 'hari ke-42 s/d ke-48')).toBe(
|
||
'Laporan Mingguan — hari ke-42 s/d ke-48'
|
||
);
|
||
});
|
||
});
|
||
|
||
describe('buildPdfFilename', () => {
|
||
it('carries coop, period and date range', () => {
|
||
expect(
|
||
buildPdfFilename({
|
||
reportName: 'Dashboard',
|
||
area: 'Sukawarna',
|
||
kandangLabel: 'Kandang Atas',
|
||
periodSlug: reportPeriodSlug('weekly', 'Minggu 6'),
|
||
isoDates: isoDataDates(START, 42, 48),
|
||
})
|
||
).toBe(
|
||
'AI-Insight_Dashboard_Sukawarna_Kandang-Atas_Mingguan-Minggu-6_2026-07-03_sd_2026-07-09.pdf'
|
||
);
|
||
});
|
||
|
||
it('flattens the combined-coop label safely', () => {
|
||
expect(
|
||
buildPdfFilename({
|
||
reportName: 'Hitung-Ayam',
|
||
area: 'Sukawarna',
|
||
kandangLabel: 'Semua Kandang (Atas + Bawah)',
|
||
periodSlug: 'Harian',
|
||
isoDates: isoDataDates(START, 48),
|
||
})
|
||
).toBe('AI-Insight_Hitung-Ayam_Sukawarna_Semua-Kandang-Atas-Bawah_Harian_2026-07-09.pdf');
|
||
});
|
||
|
||
it('omits the date segment when the cycle start is unknown', () => {
|
||
expect(
|
||
buildPdfFilename({
|
||
reportName: 'fcr',
|
||
area: 'Sukawarna',
|
||
kandangLabel: 'Kandang Bawah',
|
||
periodSlug: reportPeriodSlug('end_cycle'),
|
||
isoDates: isoDataDates(null, 1, 48),
|
||
})
|
||
).toBe('AI-Insight_fcr_Sukawarna_Kandang-Bawah_Akhir-Siklus.pdf');
|
||
});
|
||
|
||
it('uses a Laporan prefix instead of AI-Insight when asked', () => {
|
||
expect(
|
||
buildPdfFilename({
|
||
prefix: 'Laporan',
|
||
reportName: 'Harian',
|
||
area: 'Sukawarna',
|
||
kandangLabel: 'Kandang Atas',
|
||
periodSlug: 'Harian-Hari-0',
|
||
isoDates: isoDataDates(START, 0),
|
||
})
|
||
).toBe('Laporan_Harian_Sukawarna_Kandang-Atas_Harian-Hari-0_2026-05-22.pdf');
|
||
});
|
||
|
||
it('two coops on the same day no longer collide', () => {
|
||
const base = {
|
||
reportName: 'Hitung-Ayam',
|
||
area: 'Sukawarna',
|
||
periodSlug: 'Harian',
|
||
isoDates: isoDataDates(START, 48),
|
||
};
|
||
expect(buildPdfFilename({ ...base, kandangLabel: 'Kandang Atas' })).not.toBe(
|
||
buildPdfFilename({ ...base, kandangLabel: 'Kandang Bawah' })
|
||
);
|
||
});
|
||
});
|