import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { MessageDb } from '../src/main/db' import { sendWithRetry, UNCHECKED } from '../src/main/send' import { VoipMsError } from '../src/main/voipms' let dir: string let db: MessageDb let tempId: number const timeout = (): Error => new DOMException('The operation was aborted due to timeout', 'TimeoutError') const offline = (): Error => Object.assign(new TypeError('fetch failed'), { cause: { code: 'ENOTFOUND' } }) const sleep = async (): Promise => undefined // An attempt that plays back `results` in order (errors are thrown). function attempts(...results: Array) { const fn = async (): Promise => { fn.calls++ const r = results.shift()! if (r instanceof Error) throw r return r } fn.calls = 0 return fn } beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'voipms-send-')) db = new MessageDb(path.join(dir, 'messages.db')) tempId = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'breakfast?', ts: 1000 }) }) afterEach(() => { db.close() fs.rmSync(dir, { recursive: true, force: true }) }) describe('sendWithRetry', () => { it('confirms a message that sends first time', async () => { await sendWithRetry(tempId, { db, attempt: attempts(77), checkSent: async () => undefined, sleep }) expect(db.getMessage(77)?.status).toBe('delivered') }) it('does not resend after a timeout when the listing shows it went out', async () => { const attempt = attempts(timeout(), 99) const checkSent = async (): Promise => void db.reconcileSent(db.upsertMessages([{ id: 88, did: '5550001111', contact: 'a', direction: 'out', body: 'breakfast?', ts: 2000 }])) await sendWithRetry(tempId, { db, attempt, checkSent, sleep }) expect(attempt.calls).toBe(1) expect(db.getConversationMessages('5550001111', 'a').map((m) => m.id)).toEqual([88]) }) it('resends after a timeout once the listing shows it did not go out', async () => { const attempt = attempts(timeout(), 99) let checks = 0 await sendWithRetry(tempId, { db, attempt, checkSent: async () => void checks++, sleep }) expect(checks).toBe(2) expect(attempt.calls).toBe(2) expect(db.getConversationMessages('5550001111', 'a').map((m) => m.id)).toEqual([99]) }) it('does not resend a text the laptop slept on, though the listing dropped its emoji', async () => { const t = db.insertPending({ did: '5550001111', contact: 'b', direction: 'out', body: 'see you tonight ❤️', ts: 1000 }) const cut = Object.assign(new TypeError('fetch failed'), { cause: { code: 'ECONNRESET' } }) const attempt = attempts(cut, 99) // Just woken: the first look can't get through, the next one can. const looks = [false, true] const checkSent = async (): Promise => { const ok = looks.shift()! if (ok) db.reconcileSent(db.upsertMessages([{ id: 88, did: '5550001111', contact: 'b', direction: 'out', body: 'see you tonight ', ts: 18_000 }])) return ok } await sendWithRetry(t, { db, attempt, checkSent, sleep }) expect(attempt.calls).toBe(1) expect(db.getConversationMessages('5550001111', 'b').map((m) => m.id)).toEqual([88]) }) it("marks it failed instead of resending when the listing can't be checked", async () => { const attempt = attempts(timeout(), 99) await sendWithRetry(tempId, { db, attempt, checkSent: async () => false, sleep }) expect(attempt.calls).toBe(1) expect(db.getMessage(tempId)).toMatchObject({ status: 'failed', error: UNCHECKED }) }) it('resends right away when the request never left', async () => { const attempt = attempts(offline(), 99) await sendWithRetry(tempId, { db, attempt, checkSent: async () => undefined, sleep }) expect(attempt.calls).toBe(2) expect(db.getMessage(99)?.status).toBe('delivered') }) it('gives up after three attempts and says why', async () => { const attempt = attempts(offline(), offline(), offline(), 99) await sendWithRetry(tempId, { db, attempt, checkSent: async () => undefined, sleep }) expect(attempt.calls).toBe(3) expect(db.getMessage(tempId)).toMatchObject({ status: 'failed', error: expect.stringMatching(/internet connection/) }) }) it('does not retry a rejection', async () => { const attempt = attempts(new VoipMsError('invalid_dst'), 99) await sendWithRetry(tempId, { db, attempt, checkSent: async () => undefined, sleep }) expect(attempt.calls).toBe(1) expect(db.getMessage(tempId)).toMatchObject({ status: 'failed', error: 'Invalid destination number.' }) }) })