import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { SNIPPET_END, SNIPPET_START } from '@shared/types' import { MessageDb, toFtsQuery, type NewMessage } from '../src/main/db' let dir: string let db: MessageDb const msg = (id: number, contact: string, body: string, ts: number, direction: 'in' | 'out' = 'in'): NewMessage => ({ id, did: '5550001111', contact, direction, body, ts }) beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'voipms-test-')) db = new MessageDb(path.join(dir, 'messages.db')) }) afterEach(() => { db.close() fs.rmSync(dir, { recursive: true, force: true }) }) describe('MessageDb', () => { it('dedupes by voip.ms id', () => { expect(db.upsertMessages([msg(1, 'a', 'hi', 1000), msg(2, 'a', 'yo', 2000)])).toHaveLength(2) expect(db.upsertMessages([msg(2, 'a', 'yo', 2000), msg(3, 'a', 'new', 3000)]).map((m) => m.id)).toEqual([3]) expect(db.count()).toBe(3) }) it('lists conversations newest first with previews and unread counts', () => { db.set('unread_floor', '1500') db.upsertMessages([msg(1, 'a', 'old', 1000), msg(2, 'b', 'hello b', 2000), msg(3, 'a', 'latest a', 3000), msg(4, 'a', 'me', 2500, 'out')]) const convs = db.listConversations() expect(convs.map((c) => c.contact)).toEqual(['a', 'b']) expect(convs[0]).toMatchObject({ lastBody: 'latest a', lastDirection: 'in', unread: 1 }) expect(convs[1]).toMatchObject({ unread: 1 }) db.markRead('5550001111', 'a') expect(db.listConversations()[0].unread).toBe(0) }) it('searches with prefix matching, snippets, and per-chat scope', () => { db.upsertMessages([ msg(1, 'a', 'Want to grab pizza tonight?', 1000), msg(2, 'b', 'The pizzeria on Main is closed', 2000), msg(3, 'a', 'nothing here', 3000) ]) const all = db.search('piz') expect(all.map((h) => h.id)).toEqual([2, 1]) expect(all[1].snippet).toContain(`${SNIPPET_START}pizza${SNIPPET_END}`) expect(db.search('piz', { did: '5550001111', contact: 'a' }).map((h) => h.id)).toEqual([1]) expect(db.search('grab tonight').map((h) => h.id)).toEqual([1]) expect(db.search('"); DROP TABLE messages; --')).toEqual([]) expect(db.search('')).toEqual([]) }) it('handles pending sends: confirm, dedupe against poll, failure', () => { const t1 = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'on my way', ts: 5000 }) expect(t1).toBeLessThan(0) expect(db.getMessage(t1)?.status).toBe('pending') db.confirmPending(t1, 99) expect(db.getMessage(99)).toMatchObject({ status: 'delivered', body: 'on my way' }) // Poll got the real message before sendSMS returned. const t2 = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'x', ts: 6000 }) db.upsertMessages([msg(100, 'a', 'x', 6001, 'out')]) db.confirmPending(t2, 100) expect(db.getMessage(t2)).toBeUndefined() expect(db.count()).toBe(2) const t3 = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'fail', ts: 7000 }) db.markFailed(t3) expect(db.getMessage(t3)?.status).toBe('failed') expect(db.search('fail').map((h) => h.id)).toEqual([t3]) db.deleteMessage(t3) expect(db.search('fail')).toEqual([]) }) it('keeps the FTS index in sync when ids change', () => { const t = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'unique words', ts: 1 }) db.confirmPending(t, 555) expect(db.search('unique').map((h) => h.id)).toEqual([555]) }) it('writes consistent snapshots and prunes to the last N', async () => { db.upsertMessages([msg(1, 'a', 'backup me', 1000)]) const snaps = path.join(dir, 'snapshots') fs.mkdirSync(snaps) for (const d of ['2020-01-01', '2020-01-02', '2020-01-03']) fs.writeFileSync(path.join(snaps, `messages-${d}.db`), '') const file = await db.snapshot(snaps, 2) expect(fs.readdirSync(snaps).sort()).toEqual(['messages-2020-01-03.db', path.basename(file)].sort()) const copy = new MessageDb(file) expect(copy.search('backup').length).toBe(1) copy.close() }) }) describe('toFtsQuery', () => { it('quotes words as prefix terms', () => { expect(toFtsQuery('hey "you" OR')).toBe('"hey"* "you"* "OR"*') expect(toFtsQuery(' ')).toBe('') }) })