import { describe, it, expect } from 'vitest' import { buildJkkp8Rows, jkkp8Csv, type Jkkp8Incident } from '@/lib/reports/jkkp8' function makeIncident(overrides: Partial = {}): Jkkp8Incident { return { reference_no: 'SETIA-202607-0001', incident_type: 'injury', description: 'Fell from ladder', reported_at: '2026-07-01T08:00:00Z', medical_status: 'lti', lost_days: 5, is_fatality: false, is_serious_bodily_injury: false, is_dangerous_occurrence: false, is_occupational_disease: false, sites: { name: 'Setia Alam' }, zones: { name: 'Dock 3' }, reporter: { name: 'Ali' }, dosh_reports: [], ...overrides, } } describe('buildJkkp8Rows', () => { it('includes incidents with lost_days >= 4', () => { const rows = buildJkkp8Rows([makeIncident()]) expect(rows).toHaveLength(1) expect(rows[0].obligation).toContain('Lost-time injury') expect(rows[0].filing_status).toBe('pending') }) it('excludes non-reportable incidents', () => { const rows = buildJkkp8Rows([ makeIncident({ lost_days: 1, medical_status: 'first_aid' }), ]) expect(rows).toHaveLength(0) }) it('includes occupational disease (JKKP 7/8 path)', () => { const rows = buildJkkp8Rows([ makeIncident({ lost_days: 0, is_occupational_disease: true }), ]) expect(rows).toHaveLength(1) expect(rows[0].obligation).toContain('Occupational disease') }) it('summarises dosh filing status when reports exist', () => { const rows = buildJkkp8Rows([ makeIncident({ dosh_reports: [ { form_type: 'jkkp6', status: 'submitted', submitted_at: '2026-07-03T10:00:00Z' }, ], }), ]) expect(rows[0].filing_status).toBe('JKKP6: submitted (2026-07-03)') }) }) describe('jkkp8Csv', () => { it('escapes commas and quotes in descriptions', () => { const rows = buildJkkp8Rows([ makeIncident({ description: 'Slip, near "dock" area' }), ]) const csv = jkkp8Csv(rows) expect(csv).toContain('"Slip, near ""dock"" area"') expect(csv.split('\r\n')).toHaveLength(2) }) })