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('stores MMS media and previews it in the conversation list', () => { const url = 'https://voip.ms/media/x/media.jpeg' db.upsertMessages([{ ...msg(1, 'a', '', 1000), kind: 'mms', media: [{ url }] }]) expect(db.getMessage(1)).toMatchObject({ kind: 'mms', media: [{ url }] }) expect(db.getMessage(1)).not.toHaveProperty('media_json') expect(db.listConversations()[0]).toMatchObject({ lastKind: 'mms', lastBody: '', lastMedia: { url } }) db.setMediaFile(1, 0, { file: 'abc.jpg', mime: 'image/jpeg' }) expect(db.getMessage(1)?.media).toEqual([{ url, file: 'abc.jpg', mime: 'image/jpeg' }]) db.upsertMessages([msg(2, 'a', 'plain', 2000)]) expect(db.getMessage(2)).not.toHaveProperty('media') expect(db.listConversations()[0]).not.toHaveProperty('lastMedia') }) it('keeps local attachment files when a sent MMS was already synced', () => { const t = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: '', ts: 1, kind: 'mms', media: [{ file: 'local.jpg', mime: 'image/jpeg' }] }) db.upsertMessages([{ ...msg(50, 'a', '', 2, 'out'), kind: 'mms', media: [{ url: 'https://x/m.jpeg' }] }]) db.confirmPending(t, 50) expect(db.getMessage(t)).toBeUndefined() expect(db.getMessage(50)?.media).toEqual([{ url: 'https://x/m.jpeg', file: 'local.jpg', mime: 'image/jpeg' }]) }) it('migrates v1 databases by restarting the history import', () => { db.set('backfill_done', '1') db.set('backfill_cursor', '2010-01-01') db.set('unread_floor', '123') // Recreate read_state as v1 had it (before the archive columns). db.db.exec(`DROP TABLE read_state; CREATE TABLE read_state (did TEXT NOT NULL, contact TEXT NOT NULL, last_read_ts INTEGER NOT NULL, PRIMARY KEY (did, contact))`) db.db.pragma('user_version = 1') db.close() db = new MessageDb(path.join(dir, 'messages.db')) expect(db.get('backfill_done')).toBeUndefined() expect(db.get('backfill_cursor')).toBeUndefined() expect(db.get('unread_floor')).toBe('123') db.upsertMessages([msg(1, 'a', 'hi', 1000)]) db.setArchived('5550001111', 'a', true) expect(db.listConversations()[0].archived).toBe(true) }) it('archives conversations until a newer message arrives', () => { db.upsertMessages([msg(1, 'a', 'hi', 1000), msg(2, 'b', 'yo', 2000)]) db.setArchived('5550001111', 'a', true, 5000) expect(db.listConversations().find((c) => c.contact === 'a')).toMatchObject({ archived: true }) expect(db.listConversations().find((c) => c.contact === 'b')).toMatchObject({ archived: false }) // Older history (e.g. from the backfill) doesn't unarchive. db.upsertMessages([msg(3, 'a', 'old', 4000)]) expect(db.listConversations().find((c) => c.contact === 'a')?.archived).toBe(true) // A new message (incoming, or one we send) does. db.upsertMessages([msg(4, 'a', 'new', 6000)]) expect(db.listConversations().find((c) => c.contact === 'a')?.archived).toBe(false) db.setArchived('5550001111', 'a', true, 7000) db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'sent', ts: 8000 }) expect(db.listConversations().find((c) => c.contact === 'a')?.archived).toBe(false) db.setArchived('5550001111', 'b', true) db.setArchived('5550001111', 'b', false) expect(db.listConversations().find((c) => c.contact === 'b')?.archived).toBe(false) }) it('marks conversations unread until opened', () => { db.upsertMessages([msg(1, 'a', 'hi', 1000)]) db.markRead('5550001111', 'a') expect(db.listConversations()[0]).toMatchObject({ unread: 0, markedUnread: false }) db.markUnread('5550001111', 'a') expect(db.listConversations()[0]).toMatchObject({ unread: 0, markedUnread: true }) db.markRead('5550001111', 'a') expect(db.listConversations()[0].markedUnread).toBe(false) }) it('migrates to v4: shifts voip.ms times out of the DST hour, keeps local send times', () => { const summer = Date.UTC(2026, 8, 18, 7, 35, 47) // as voip.ms reported it const winter = Date.UTC(2026, 0, 20, 2, 44, 49) const local = Date.UTC(2026, 8, 18, 6, 35, 34) + 465 // stamped on send db.upsertMessages([ { ...msg(1, 'a', 'summer', summer), kind: 'mms' }, msg(2, 'a', 'winter', winter), msg(3, 'a', 'sent here', local, 'out'), { ...msg(4, 'a', '', summer), kind: 'mms', media: [{ url: 'u' }] } ]) db.db.pragma('user_version = 3') db.close() db = new MessageDb(path.join(dir, 'messages.db')) expect(db.getMessage(1)).toMatchObject({ ts: summer - 3_600_000, kind: 'sms' }) expect(db.getMessage(2)?.ts).toBe(winter) expect(db.getMessage(3)?.ts).toBe(local) expect(db.getMessage(4)).toMatchObject({ ts: summer - 3_600_000, kind: 'mms' }) }) it('migrates to v5: labels texts too long for one SMS as MMS', () => { db.upsertMessages([msg(1, 'a', 'x'.repeat(161), 1000), msg(2, 'a', 'x'.repeat(160), 2000), msg(3, 'a', '😀'.repeat(160), 3000)]) db.db.pragma('user_version = 4') db.close() db = new MessageDb(path.join(dir, 'messages.db')) expect(db.getMessage(1)?.kind).toBe('mms') expect(db.getMessage(2)?.kind).toBe('sms') expect(db.getMessage(3)?.kind).toBe('sms') }) it('keeps a sent text-only MMS labelled MMS when the poll got there first', () => { const t = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'short mms', ts: 1, kind: 'mms' }) db.upsertMessages([msg(60, 'a', 'short mms', 2, 'out')]) db.confirmPending(t, 60) expect(db.getMessage(t)).toBeUndefined() expect(db.getMessage(60)?.kind).toBe('mms') }) it('records why a send failed', () => { const t = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'x', ts: 1 }) expect(db.getMessage(t)?.error).toBeUndefined() db.markFailed(t, "voip.ms didn't respond in time.") expect(db.getMessage(t)).toMatchObject({ status: 'failed', error: "voip.ms didn't respond in time." }) }) it('migrates to v6: adds the send error column', () => { db.db.exec('ALTER TABLE messages DROP COLUMN error') db.db.pragma('user_version = 5') db.close() db = new MessageDb(path.join(dir, 'messages.db')) const t = db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body: 'x', ts: 1 }) db.markFailed(t, 'nope') expect(db.getMessage(t)?.error).toBe('nope') }) describe('reconcileSent', () => { const MIN = 60_000 const pending = (body: string, ts: number): number => db.insertPending({ did: '5550001111', contact: 'a', direction: 'out', body, ts }) const poll = (...msgs: NewMessage[]): number[] => db.reconcileSent(db.upsertMessages(msgs)) it('replaces a failed copy that actually went out', () => { const t = pending('breakfast?', 10 * MIN) db.markFailed(t, 'timeout') expect(poll(msg(500, 'a', 'breakfast?', 11 * MIN, 'out'))).toEqual([t]) expect(db.getMessage(t)).toBeUndefined() expect(db.getMessage(500)?.status).toBe('delivered') expect(db.getConversationMessages('5550001111', 'a')).toHaveLength(1) }) it('replaces a pending copy, and the late send reply is then a no-op', () => { const t = pending('hi', 10 * MIN) expect(poll(msg(501, 'a', 'hi', 10 * MIN, 'out'))).toEqual([t]) db.confirmPending(t, 501) expect(db.getConversationMessages('5550001111', 'a').map((m) => m.id)).toEqual([501]) }) it('ignores other texts, other chats, incoming messages, and far-off times', () => { const t = pending('hi', 100 * MIN) db.markFailed(t) expect( poll( msg(1, 'a', 'hello', 100 * MIN, 'out'), msg(2, 'b', 'hi', 100 * MIN, 'out'), msg(3, 'a', 'hi', 100 * MIN, 'in'), msg(4, 'a', 'hi', 80 * MIN, 'out'), msg(5, 'a', 'hi', 120 * MIN, 'out') ) ).toEqual([]) expect(db.getMessage(t)?.status).toBe('failed') }) it('matches identical texts one to one, oldest first', () => { const t1 = pending('ok', 10 * MIN) const t2 = pending('ok', 10 * MIN + 1) expect(poll(msg(600, 'a', 'ok', 10 * MIN, 'out'))).toEqual([t1]) expect(db.getMessage(t2)?.status).toBe('pending') expect(poll(msg(601, 'a', 'ok', 10 * MIN + 2000, 'out'))).toEqual([t2]) expect(db.getConversationMessages('5550001111', 'a').map((m) => m.id)).toEqual([600, 601]) }) }) 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('') }) }) describe('conversation names', () => { it('names a conversation locally and clears it', () => { db.upsertMessages([msg(1, '5550002222,5550003333', 'hi', 10)]) db.setName('5550001111', '5550002222,5550003333', ' Hike crew ') expect(db.listConversations()[0].name).toBe('Hike crew') expect(db.getName('5550001111', '5550002222,5550003333')).toBe('Hike crew') db.setName('5550001111', '5550002222,5550003333', '') expect(db.listConversations()[0].name).toBeUndefined() }) }) describe('name merging', () => { it('keeps the newest rename from either side', () => { db.setName('5550001111', 'a,b', 'Desk name', 100) db.setName('5550001111', 'c,d', 'Old', 100) const n = db.namesImport([ { did: '5550001111', contact: 'a,b', name: 'Older rabbit name', ts: 50 }, { did: '5550001111', contact: 'c,d', name: 'Rabbit name', ts: 200 }, { did: '5550001111', contact: 'e,f', name: null, ts: 300 } ]) expect(n).toBe(2) expect(db.getName('5550001111', 'a,b')).toBe('Desk name') expect(db.getName('5550001111', 'c,d')).toBe('Rabbit name') expect(db.namesExport()).toHaveLength(3) }) }) describe('read state merging', () => { it('takes the later read point and the newer unread/archived change', () => { db.upsertMessages([msg(1, 'a', 'hi', 10), msg(2, 'b', 'yo', 20), msg(3, 'c', 'sup', 30)]) db.markRead('5550001111', 'a') // read up to 10, here db.setArchived('5550001111', 'b', true, 1000) const n = db.readStateImport([ { did: '5550001111', contact: 'a', lastRead: 5, markedUnread: true, unreadTs: 1, archivedAt: null, archivedTs: null }, { did: '5550001111', contact: 'b', lastRead: 20, markedUnread: false, unreadTs: null, archivedAt: null, archivedTs: Date.now() + 1000 }, { did: '5550001111', contact: 'c', lastRead: 30, markedUnread: false, unreadTs: Date.now(), archivedAt: null, archivedTs: null } ]) expect(n).toBe(2) const conv = (c: string) => db.listConversations().find((x) => x.contact === c)! expect(conv('a').markedUnread).toBe(false) // our mark-read is newer than their mark-unread expect(conv('b').archived).toBe(false) // their unarchive is newer expect(conv('c').unread).toBe(0) // read on the other device expect(db.readStateExport()).toHaveLength(3) }) }) describe('drafts', () => { it('keeps unsent text per contact until cleared, across reopening', () => { db.setDraft('5551230001', 'see you at ') db.setDraft('5551230002', 'ok') db.setDraft('5551230002', ' ') db.set('backfill_cursor', 'x') // other kv state isn't a draft db.close() db = new MessageDb(path.join(dir, 'messages.db')) expect(db.getDrafts()).toEqual({ '5551230001': 'see you at ' }) }) })