teqo-backup/tests/unit/campaignTimeCivilDate.unit.spec.ts

97 lines
3.3 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import {
civilDateDaysBetween,
civilDateToUtcMidnightIso,
floorToMinuteStep,
formatBahiaCalendarDateTime,
formatBahiaCivilDate,
formatBahiaCivilDateTimeLabel,
formatBahiaEventDateLabel,
hourOptions,
minuteOptionsForStep,
subtractBahiaCivilDays,
timeStepMinutes,
} from '@/lib/campaignTime'
describe('Bahia civil date helpers (B57)', () => {
it('converts civil dates to UTC midnight ISO anchors', () => {
expect(civilDateToUtcMidnightIso('2026-08-01')).toBe('2026-08-01T00:00:00.000Z')
})
it('subtracts whole civil days', () => {
expect(subtractBahiaCivilDays('2026-08-08', 7)).toBe('2026-08-01')
})
it('counts calendar days between civil dates', () => {
expect(civilDateDaysBetween('2026-08-01', '2026-08-08')).toBe(7)
})
it('round-trips formatBahiaCivilDate through subtract', () => {
const today = formatBahiaCivilDate(new Date('2026-08-08T15:00:00.000Z'))
expect(today).toBe('2026-08-08')
expect(subtractBahiaCivilDays(today, 7)).toBe('2026-08-01')
})
it('formats a calendar instant as Bahia local time with seconds', () => {
expect(formatBahiaCalendarDateTime(new Date('2026-10-03T22:00:00.000Z'))).toBe(
'20261003T190000',
)
})
})
describe('Bahia civil datetime picker helpers (C97)', () => {
it('labels civil datetimes as dd/mm/aaaa às hh:mm — 24h, no meridiem', () => {
expect(formatBahiaCivilDateTimeLabel('2026-08-07T14:30')).toBe('07/08/2026 às 14:30')
expect(formatBahiaCivilDateTimeLabel('2026-08-07T09:45')).toBe('07/08/2026 às 09:45')
expect(formatBahiaCivilDateTimeLabel('2026-08-07T23:05')).toBe('07/08/2026 às 23:05')
expect(formatBahiaCivilDateTimeLabel('2026-08-07T00:00')).toBe('07/08/2026 às 00:00')
})
it('exposes 24 hours and the 15-min step grid', () => {
expect(hourOptions).toHaveLength(24)
expect(hourOptions[0]).toBe('00')
expect(hourOptions[23]).toBe('23')
expect(timeStepMinutes).toBe(15)
expect(minuteOptionsForStep()).toEqual(['00', '15', '30', '45'])
expect(minuteOptionsForStep(5)).toEqual([
'00',
'05',
'10',
'15',
'20',
'25',
'30',
'35',
'40',
'45',
'50',
'55',
])
})
it('floors minutes to the step, leaving the date and hour untouched', () => {
expect(floorToMinuteStep('2026-08-07T14:47')).toBe('2026-08-07T14:45')
expect(floorToMinuteStep('2026-08-07T14:00')).toBe('2026-08-07T14:00')
expect(floorToMinuteStep('2026-08-07T14:59')).toBe('2026-08-07T14:45')
expect(floorToMinuteStep('2026-08-07T14:07', 5)).toBe('2026-08-07T14:05')
})
it('passes malformed input through untouched', () => {
expect(formatBahiaCivilDateTimeLabel('lixo')).toBe('lixo')
expect(floorToMinuteStep('lixo')).toBe('lixo')
})
})
describe('Bahia event date label (S29)', () => {
it('labels the announcement headline in Bahia time, minutes only when needed', () => {
expect(formatBahiaEventDateLabel('2026-10-03T22:00:00.000Z')).toBe('Sábado, 3 de outubro · 19h')
expect(formatBahiaEventDateLabel('2026-10-03T22:30:00.000Z')).toBe(
'Sábado, 3 de outubro · 19h30',
)
})
it('returns an empty string for an unparsable instant', () => {
expect(formatBahiaEventDateLabel('lixo')).toBe('')
})
})