Files
bubble/test/sync.test.ts
T
trav 1106de26f2 v0.8.0: bubble for Android 0.7.3 — Get info for everyone, with the chat's photos; no more "MMS Received"
Desktop:
- Click a chat's picture or name (or right-click it in the list → Get info…)
  for any chat, not only groups. One person: their picture, name, number
  (and its label), which of your numbers you're texting from, Add to
  contacts… if they aren't one. The number no longer sits in the header.
- Get info lists every photo and video in the chat as thumbnails, oldest
  first like the chat, scrolled to the newest. Each opens full size.
- The full-size view steps through the chat's photos with ← and → (or the
  side buttons) and says "n of N · date", from Get info or from the chat.
  Its glyphs were light gray on the white buttons; now the usual glyph color.

Both: voip.ms puts "MMS Received" as the text of a received picture that
came without words. Nobody wrote it, so it's dropped as messages come in,
and once from the ones already stored (only on messages with pictures, so
someone typing "MMS received" still shows).

Tried in a sandbox with made-up chats and generated pictures, driven over
DevTools: the header, Get info for a person and a group, thumbnails,
←/→ through 14 pictures, Esc closing the picture then the dialog, and the
v11 migration clearing a stored "MMS Received".
2026-11-12 15:01:56 -08:00

121 lines
5.1 KiB
TypeScript

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 getMessages({ 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('toNewMessage (MMS)', () => {
const mms = { ...raw(8, '2024-02-03'), message: '', col_media2: '', col_media3: '' }
it('maps getMMS media to kind mms', () => {
const url = 'https://voip.ms/media/abc/media.jpeg'
expect(toNewMessage({ ...mms, col_media1: url, media: [url] })).toMatchObject({ kind: 'mms', media: [{ url }] })
expect(toNewMessage({ ...mms, col_media1: url })).toMatchObject({ kind: 'mms', media: [{ url }] })
})
it('drops voip.ms\'s "MMS Received" stand-in text from pictures', () => {
const url = 'https://voip.ms/media/abc/media.jpeg'
expect(toNewMessage({ ...mms, message: 'MMS Received', col_media1: url }).body).toBe('')
expect(toNewMessage({ ...mms, message: ' mms received\n', col_media1: url }).body).toBe('')
expect(toNewMessage({ ...mms, message: 'MMS Received, thanks', col_media1: url }).body).toBe('MMS Received, thanks')
// Typed by a person, with no picture: kept.
expect(toNewMessage({ ...mms, message: 'MMS received', col_media1: '' }).body).toBe('MMS received')
})
it('treats rows without media as plain texts', () => {
const m = toNewMessage({ ...mms, message: 'long text', col_media1: '', media: [] })
expect(m.kind).toBeUndefined()
expect(m.media).toBeUndefined()
})
it('labels text too long for one SMS as a text-only MMS', () => {
expect(toNewMessage({ ...mms, message: 'x'.repeat(161) })).toMatchObject({ kind: 'mms' })
expect(toNewMessage({ ...mms, message: '😀'.repeat(160) }).kind).toBeUndefined()
expect(toNewMessage({ ...mms, message: 'x'.repeat(161) }).media).toBeUndefined()
})
})
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)
})
describe('SyncEngine poll', () => {
it('replaces a failed local copy of a text that voip.ms did send', async () => {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'voipms-sync-'))
const db = new MessageDb(path.join(dir, 'm.db'))
db.set('backfill_done', '1')
const sent = { ...raw(42, ymd(Date.now()), '0'), message: 'see you at 8' }
const t = db.insertPending({ did: '5550001111', contact: '5415550142', direction: 'out', body: 'see you at 8', ts: toNewMessage(sent).ts })
db.markFailed(t, "voip.ms didn't respond in time.")
const engine = new SyncEngine(db, fakeApi([sent]) as unknown as VoipMs, { pollSeconds: 3600, dids: [], backfillFloor: '2010-01-01' })
await engine.pollNow()
expect(db.getMessage(t)).toBeUndefined()
expect(db.getConversationMessages('5550001111', '5415550142').map((m) => [m.id, m.status])).toEqual([[42, 'delivered']])
db.close()
fs.rmSync(dir, { recursive: true, force: true })
})
})