- Extract computeNextPeriodicInform to app/utils/periodic_inform.ts - 11 unit tests for the periodic computation logic - DeviceDetail: 3 date columns on same row (created, last seen, next estimated)
56 lines
1.9 KiB
TypeScript
56 lines
1.9 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { computeNextPeriodicInform } from '../../../app/utils/periodic_inform.js'
|
|
|
|
describe('computeNextPeriodicInform', () => {
|
|
it('computes next date from last periodic + interval', () => {
|
|
const last = new Date('2026-06-02T10:00:00Z')
|
|
const result = computeNextPeriodicInform(last, 30)
|
|
expect(result).toEqual(new Date('2026-06-02T10:00:30Z'))
|
|
})
|
|
|
|
it('works with string interval', () => {
|
|
const last = new Date('2026-06-02T12:00:00Z')
|
|
const result = computeNextPeriodicInform(last, '60')
|
|
expect(result).toEqual(new Date('2026-06-02T12:01:00Z'))
|
|
})
|
|
|
|
it('works with ISO string date', () => {
|
|
const result = computeNextPeriodicInform('2026-06-02T08:00:00Z', 300)
|
|
expect(result).toEqual(new Date('2026-06-02T08:05:00Z'))
|
|
})
|
|
|
|
it('returns null if lastPeriodicInformAt is null', () => {
|
|
expect(computeNextPeriodicInform(null, 30)).toBeNull()
|
|
})
|
|
|
|
it('returns null if lastPeriodicInformAt is undefined', () => {
|
|
expect(computeNextPeriodicInform(undefined, 30)).toBeNull()
|
|
})
|
|
|
|
it('returns null if interval is 0', () => {
|
|
expect(computeNextPeriodicInform(new Date(), 0)).toBeNull()
|
|
})
|
|
|
|
it('returns null if interval is negative', () => {
|
|
expect(computeNextPeriodicInform(new Date(), -10)).toBeNull()
|
|
})
|
|
|
|
it('returns null if interval is null', () => {
|
|
expect(computeNextPeriodicInform(new Date(), null)).toBeNull()
|
|
})
|
|
|
|
it('returns null if interval is empty string', () => {
|
|
expect(computeNextPeriodicInform(new Date(), '')).toBeNull()
|
|
})
|
|
|
|
it('returns null if date string is invalid', () => {
|
|
expect(computeNextPeriodicInform('not-a-date', 30)).toBeNull()
|
|
})
|
|
|
|
it('handles large intervals (1 hour)', () => {
|
|
const last = new Date('2026-06-02T10:00:00Z')
|
|
const result = computeNextPeriodicInform(last, 3600)
|
|
expect(result).toEqual(new Date('2026-06-02T11:00:00Z'))
|
|
})
|
|
})
|