Files
bubble/test/sync.test.ts
T
trav 8c84feaa91 v0.2.0: bubble for Android 0.2.0 — chat search, swipe for unread, faster chat opening
Desktop:
- Search lists chats whose own name matches first, so a named group
  isn't pushed out of the top 8 by chats that merely include a member.

Android (Rabbit R1) app, 0.2.0:
- Pull the chat list past its top for a search box (name, person,
  number, then group members; archived chats too; Back clears it).
- Swipe right to left marks a chat unread / read over a magenta band;
  left to right still archives (SwipeActions replaces SwipeArchive).
- Google Voice chats don't offer voice notes in the paperclip bubble.
- Chats open with their newest 60 messages, then the full history;
  marking read (and the Google Voice network call) waits until they're
  on screen. Cached pictures show at once, and one slow download no
  longer blocks the other images. Fixed a contacts reload race.
- Sandbox data gains filler chats, a named group and a 4,000-message chat.

Also: the Android app, Google Voice support, media, export, emoji and
the other work since the first commit, and a CLAUDE.md carrying the
privacy rule.
2026-11-06 02:19:56 -08:00

113 lines
4.5 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('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 })
})
})