voip.ms Messages: Signal-style desktop SMS client
Electron + React app that imports the full voip.ms SMS history into a local SQLite/FTS5 database, polls for new messages, sends SMS, resolves names/photos from GNOME EDS address books, and supports global and per-chat search with jump-to-message. Data lives in ~/Documents/voipms-desktop-data (covered by Deja Dup) with daily consistent snapshots. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
// Reads the real GNOME/EDS address books (read-only). Run with: LIVE=1 npm test
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { ContactDirectory, listSources } from '../src/main/contacts'
|
||||
|
||||
describe.skipIf(!process.env.LIVE)('live EDS contacts', () => {
|
||||
it('lists enabled address books', async () => {
|
||||
const sources = await listSources()
|
||||
console.log(sources.map((s) => `${s.name} (${s.account})`).join('\n'))
|
||||
expect(sources.length).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('loads contacts with numbers and photos', async () => {
|
||||
const dir = new ContactDirectory()
|
||||
await dir.load()
|
||||
const all = dir.list()
|
||||
const withPhoto = all.filter((c) => c.photo)
|
||||
const byBook = new Map<string, number>()
|
||||
for (const c of all) byBook.set(c.book, (byBook.get(c.book) ?? 0) + 1)
|
||||
console.log(`numbers: ${all.length}, with photo: ${withPhoto.length}`, Object.fromEntries(byBook))
|
||||
const kinds = new Map<string, number>()
|
||||
for (const c of withPhoto) {
|
||||
const k = dir.photoFor(c.number)!.kind
|
||||
kinds.set(k, (kinds.get(k) ?? 0) + 1)
|
||||
}
|
||||
console.log('photo kinds', Object.fromEntries(kinds))
|
||||
expect(all.length).toBeGreaterThan(50)
|
||||
dir.close()
|
||||
})
|
||||
})
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
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('')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { formatNumber, normalizeNumber, splitMessage } from '@shared/phone'
|
||||
|
||||
describe('phone numbers', () => {
|
||||
it('normalizes NANP numbers to 10 digits', () => {
|
||||
expect(normalizeNumber('+1 (541) 555-0142')).toBe('5415550142')
|
||||
expect(normalizeNumber('15415550142')).toBe('5415550142')
|
||||
expect(normalizeNumber('541.555.0142')).toBe('5415550142')
|
||||
expect(normalizeNumber('tel:+15415550142')).toBe('5415550142')
|
||||
})
|
||||
it('leaves short codes alone', () => {
|
||||
expect(normalizeNumber('72975')).toBe('72975')
|
||||
})
|
||||
it('formats', () => {
|
||||
expect(formatNumber('5415550142')).toBe('(541) 555-0142')
|
||||
expect(formatNumber('72975')).toBe('72975')
|
||||
})
|
||||
})
|
||||
|
||||
describe('splitMessage', () => {
|
||||
it('keeps short messages whole', () => {
|
||||
expect(splitMessage('hi there')).toEqual(['hi there'])
|
||||
})
|
||||
it('splits long messages on word boundaries under 160 chars', () => {
|
||||
const text = Array.from({ length: 80 }, (_, i) => `word${i}`).join(' ')
|
||||
const parts = splitMessage(text)
|
||||
expect(parts.length).toBeGreaterThan(1)
|
||||
for (const p of parts) expect([...p].length).toBeLessThanOrEqual(160)
|
||||
expect(parts.join(' ')).toBe(text)
|
||||
})
|
||||
it('hard-splits a long unbroken string', () => {
|
||||
const parts = splitMessage('x'.repeat(400))
|
||||
expect(parts.map((p) => p.length)).toEqual([160, 160, 80])
|
||||
})
|
||||
it('counts emoji as single characters', () => {
|
||||
const parts = splitMessage('😀'.repeat(200))
|
||||
expect(parts.map((p) => [...p].length)).toEqual([160, 40])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,76 @@
|
||||
import fs from 'node:fs'
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { MessageDb } from '../src/main/db'
|
||||
import { fetchRange, SyncEngine, toNewMessage, ymd } from '../src/main/sync'
|
||||
import type { RawSms, VoipMs } from '../src/main/voipms'
|
||||
|
||||
const DAY = 86_400_000
|
||||
|
||||
function fakeApi(all: RawSms[]) {
|
||||
const calls: Array<{ from: string; to: string }> = []
|
||||
return {
|
||||
calls,
|
||||
async getSms({ from, to, limit = 10000 }: { from: string; to: string; limit?: number }) {
|
||||
calls.push({ from, to })
|
||||
return all.filter((m) => m.date.slice(0, 10) >= from && m.date.slice(0, 10) <= to).slice(0, limit)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const raw = (id: number, date: string, type = '1'): RawSms => ({
|
||||
id: String(id),
|
||||
date: `${date} 12:00:00`,
|
||||
type,
|
||||
did: '15550001111',
|
||||
contact: '5415550142',
|
||||
message: `m${id}`
|
||||
})
|
||||
|
||||
describe('toNewMessage', () => {
|
||||
it('maps voip.ms rows', () => {
|
||||
expect(toNewMessage(raw(7, '2024-02-03', '0'))).toEqual({
|
||||
id: 7,
|
||||
did: '5550001111',
|
||||
contact: '5415550142',
|
||||
direction: 'out',
|
||||
body: 'm7',
|
||||
ts: Date.UTC(2024, 1, 3, 12)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('fetchRange', () => {
|
||||
it('splits a window that hits the page limit so nothing is truncated', async () => {
|
||||
const all = Array.from({ length: 10 }, (_, i) => raw(i, ymd(Date.UTC(2024, 0, 1) + i * DAY)))
|
||||
const api = fakeApi(all)
|
||||
const got = await fetchRange(api, Date.UTC(2024, 0, 1), Date.UTC(2024, 0, 10), 4)
|
||||
expect(got.map((m) => Number(m.id)).sort((a, b) => a - b)).toEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
|
||||
})
|
||||
})
|
||||
|
||||
describe('SyncEngine backfill', () => {
|
||||
it('walks back through history and stops at the floor', async () => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'voipms-sync-'))
|
||||
const db = new MessageDb(path.join(dir, 'm.db'))
|
||||
const now = Date.now()
|
||||
const all = [raw(1, ymd(now - DAY)), raw(2, ymd(now - 200 * DAY)), raw(3, ymd(now - 700 * DAY))]
|
||||
const api = fakeApi(all)
|
||||
const engine = new SyncEngine(db, api as unknown as VoipMs, {
|
||||
pollSeconds: 3600,
|
||||
dids: [],
|
||||
backfillFloor: ymd(now - 800 * DAY)
|
||||
})
|
||||
const done = new Promise<void>((resolve) => engine.on('status', (s) => s.backfillDone && resolve()))
|
||||
engine.start()
|
||||
await done
|
||||
engine.stop()
|
||||
expect(db.count()).toBe(3)
|
||||
expect(db.get('backfill_done')).toBe('1')
|
||||
// Everything imported during the first sync counts as already read.
|
||||
expect(db.listConversations()[0].unread).toBe(0)
|
||||
db.close()
|
||||
fs.rmSync(dir, { recursive: true, force: true })
|
||||
}, 20_000)
|
||||
})
|
||||
@@ -0,0 +1,47 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { parseVCard } from '../src/main/contacts'
|
||||
|
||||
describe('parseVCard', () => {
|
||||
it('parses EDS-style cards with E164 params and remote photos', () => {
|
||||
const card = [
|
||||
'BEGIN:VCARD',
|
||||
'VERSION:3.0',
|
||||
'FN:Alex Example',
|
||||
'N:Example;Alex;;;',
|
||||
'TEL;X-EVOLUTION-E164=4435550143,;VALUE=UNKNOWN;TYPE=CELL:(443) 555-0143',
|
||||
'TEL;X-EVOLUTION-E164=5415550142,"+1";X-NC-SCOPE="v2-local";TYPE=VOICE:+1541',
|
||||
' 5550142',
|
||||
'PHOTO;VALUE=BINARY:https://lh5.googleusercontent.com/-abc/photo.jpg',
|
||||
'END:VCARD'
|
||||
].join('\r\n')
|
||||
const c = parseVCard(card)
|
||||
expect(c.name).toBe('Alex Example')
|
||||
expect(c.tels).toEqual([
|
||||
{ number: '4435550143', label: 'cell' },
|
||||
{ number: '5415550142', label: undefined }
|
||||
])
|
||||
expect(c.photo).toEqual({ kind: 'url', url: 'https://lh5.googleusercontent.com/-abc/photo.jpg' })
|
||||
})
|
||||
|
||||
it('parses file photos and falls back to N when FN is missing', () => {
|
||||
const c = parseVCard(
|
||||
'BEGIN:VCARD\nN:Doe;Jane;;;\nTEL;TYPE=work:555-123-4567\nPHOTO;VALUE=uri:file:///home/x/.cache/evolution/addressbook/abc/PHOTO-1.image%2Fpng\nEND:VCARD'
|
||||
)
|
||||
expect(c.name).toBe('Jane Doe')
|
||||
expect(c.tels).toEqual([{ number: '5551234567', label: 'work' }])
|
||||
expect(c.photo).toEqual({ kind: 'file', path: '/home/x/.cache/evolution/addressbook/abc/PHOTO-1.image/png' })
|
||||
})
|
||||
|
||||
it('parses inline base64 photos (vCard 3 and 4)', () => {
|
||||
const v3 = parseVCard('BEGIN:VCARD\nFN:A\nTEL:5551234567\nPHOTO;ENCODING=b;TYPE=PNG:iVBORw0KGgo\n AAAA\nEND:VCARD')
|
||||
expect(v3.photo).toEqual({ kind: 'inline', mime: 'image/png', data: 'iVBORw0KGgoAAAA' })
|
||||
const v4 = parseVCard('BEGIN:VCARD\nFN:A\nTEL:5551234567\nPHOTO:data:image/jpeg;base64,/9j/4AAQ\nEND:VCARD')
|
||||
expect(v4.photo).toEqual({ kind: 'inline', mime: 'image/jpeg', data: '/9j/4AAQ' })
|
||||
})
|
||||
|
||||
it('handles grouped properties and escaped text', () => {
|
||||
const c = parseVCard('BEGIN:VCARD\nitem1.FN:Smith\\, John\nitem1.TEL;type=CELL:+1 541 555 0142\nEND:VCARD')
|
||||
expect(c.name).toBe('Smith, John')
|
||||
expect(c.tels[0]).toEqual({ number: '5415550142', label: 'cell' })
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user