import { describe, expect, it } from 'vitest' import { classifySendError, fixVoipDst, parseVoipDate, sendErrorMessage, VoipMsError } from '../src/main/voipms' describe('parseVoipDate', () => { // Real cases checked against the live API (timezone=0). it('undoes the extra hour voip.ms adds during US daylight time', () => { // Sent 2026-09-18 06:35:47 UTC; voip.ms reported 07:35:47. expect(parseVoipDate('2026-09-18 07:35:47')).toBe(Date.UTC(2026, 8, 18, 6, 35, 47)) // March 12 2026: US DST (from Mar 8) but not UK summer time (from Mar 29). expect(parseVoipDate('2026-03-12 19:16:41')).toBe(Date.UTC(2026, 2, 12, 18, 16, 41)) }) it('leaves winter times alone', () => { expect(parseVoipDate('2026-01-20 02:44:49')).toBe(Date.UTC(2026, 0, 20, 2, 44, 49)) expect(fixVoipDst(Date.UTC(2025, 11, 25, 12))).toBe(Date.UTC(2025, 11, 25, 12)) }) }) const netError = (code: string): Error => Object.assign(new TypeError('fetch failed'), { cause: Object.assign(new Error(code), { code }) }) const timeout = (): Error => new DOMException('The operation was aborted due to timeout', 'TimeoutError') describe('classifySendError', () => { it('never resends blindly when the message may have gone out', () => { expect(classifySendError(timeout())).toBe('unknown') expect(classifySendError(netError('ECONNRESET'))).toBe('unknown') expect(classifySendError(new Error('voip.ms HTTP 502'))).toBe('unknown') expect(classifySendError(new SyntaxError('Unexpected token <'))).toBe('unknown') }) it('resends when the request never reached voip.ms or voip.ms says it failed', () => { expect(classifySendError(netError('ENOTFOUND'))).toBe('safe') expect(classifySendError(netError('ECONNREFUSED'))).toBe('safe') expect(classifySendError(new VoipMsError('sms_failed'))).toBe('safe') }) it('gives up on rejections', () => { expect(classifySendError(new VoipMsError('invalid_dst'))).toBe('fatal') expect(classifySendError(new VoipMsError('not_signed_in'))).toBe('fatal') }) }) describe('sendErrorMessage', () => { it('explains failures in plain words', () => { expect(sendErrorMessage(timeout())).toBe("voip.ms didn't respond in time.") expect(sendErrorMessage(netError('ENOTFOUND'))).toMatch(/internet connection/) expect(sendErrorMessage(new VoipMsError('invalid_dst'))).toBe('Invalid destination number.') }) })