import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { afterEach, beforeEach, describe, expect, it } from 'vitest' import { MessageDb } from '../src/main/db' import { sendWithRetry } from '../src/main/send' import { classifyGvError, decodeBody, GVoice, GVoiceError, gvErrorMessage, readBody, sapisidHash, toE164, type Transport } from '../src/main/gvoice/client' import { decode, encode, fromPblite, toPblite, type Schema } from '../src/main/gvoice/proto' import * as S from '../src/main/gvoice/schema' import { gvMessageId, GvSyncEngine, isGvMessageId, threadContact, toNewMessage } from '../src/main/gvoice/sync' const SELF = '5415550100' const sms = (id: string, ts: number, type: number, text: string, extra: Partial = {}): S.GvMessage => ({ id, timestamp: ts, type, text, transactionId: 0, ...extra }) const thread = (id: string, phoneNumbers: string[], messages: S.GvMessage[], paginationToken = ''): S.GvThread => ({ id, read: true, messages, paginationToken, contacts: [], isText: true, phoneNumbers }) // A protobuf HTTP response, the way Google sends it (base64 in a length-prefixed text chunk). function protoResponse(schema: Schema, msg: Record): Response { const b64 = Buffer.from(encode(schema, msg)).toString('base64') return new Response(`${b64.length}\n${b64}`, { headers: { 'content-type': 'text/plain; charset=utf-8', 'x-goog-safety-content-type': 'application/x-protobuf' } }) } describe('protobuf codec', () => { it('round-trips nested and repeated fields through binary', () => { const t = thread('t.+15415550199', ['+15415550199'], [ sms('a', 1_700_000_000_123, S.MessageType.SMS_IN, 'hi 👋', { mms: { text: 'pic', subject: '', senderPhoneNumber: '+15415550199', participants: [], attachments: [{ id: 'm1', mimeType: 'image/jpeg', status: 1, metadata: [{ size: 1, width: 640, height: 480 }] }] } }) ]) const back = decode(S.Thread, encode(S.Thread, t as unknown as Record)) as unknown as S.GvThread expect(back.id).toBe('t.+15415550199') expect(back.phoneNumbers).toEqual(['+15415550199']) expect(back.messages[0].timestamp).toBe(1_700_000_000_123) expect(back.messages[0].text).toBe('hi 👋') expect(back.messages[0].mms!.attachments[0]).toMatchObject({ id: 'm1', metadata: [{ width: 640, height: 480 }] }) // Missing fields come back as defaults. expect(back.paginationToken).toBe('') expect(back.contacts).toEqual([]) }) it('skips unknown fields and reads packed repeated numbers', () => { // field 99 (unknown varint), then field 1 = "x"; then a packed field 3 [1,2] against a repeated int schema const buf = new Uint8Array([0x98, 0x06, 0x05, 0x0a, 0x01, 0x78, 0x1a, 0x02, 0x01, 0x02]) expect(decode({ s: { n: 1, t: 'string' }, nums: { n: 3, t: 'int', rep: true } }, buf)).toEqual({ s: 'x', nums: [1, 2] }) }) it('writes pblite the way the web client does', () => { const out = toPblite(S.ReqSendSMS, { text: 'yo', threadId: 't.+15415550199', recipients: [], transactionId: { id: 42 }, media: { type: 2, data: new Uint8Array([1, 2, 3]) }, trackingData: { data: '!' } }) expect(out).toEqual([null, null, null, null, 'yo', 't.+15415550199', null, null, [42], [2, 'AQID'], ['!']]) }) it('reads pblite with string int64s and a trailing sparse-field object', () => { const r = fromPblite(S.RespSendSMS, [null, 'thread', null, { '4': '1700000000123', '3': 'item' }]) expect(r).toEqual({ threadId: 'thread', threadItemId: 'item', timestampMs: 1_700_000_000_123 }) }) }) describe('Google Voice client', () => { it('computes SAPISIDHASH', () => { expect(sapisidHash('abc123', 1_700_000_000_999)).toBe('SAPISIDHASH 1700000000_0af60f2c8319974dffd04c264464cd61b24a1ffd') }) it('formats numbers as E.164', () => { expect(toE164('5415550199')).toBe('+15415550199') expect(toE164('447700900123')).toBe('+447700900123') }) it('unwraps base64 protobuf in a text chunk', async () => { const res = protoResponse(S.RespSendSMS, { threadId: 't.x', threadItemId: 'i', timestampMs: 5 }) expect(decodeBody(S.RespSendSMS, await readBody(res))).toEqual({ threadId: 't.x', threadItemId: 'i', timestampMs: 5 }) }) function fakeTransport(respond: (url: URL, init: RequestInit) => Response, cookie: string | undefined = 'sapi') { const calls: Array<{ url: URL; init: RequestInit }> = [] const t: Transport = { fetch: async (url, init) => { const u = new URL(url) calls.push({ url: u, init }) return respond(u, init) }, cookie: async (name) => (name === 'SAPISID' ? cookie : undefined) } return { t, calls } } it('reads the GV number from the account', async () => { const { t, calls } = fakeTransport(() => protoResponse(S.RespGetAccount, { account: { primaryDestinationId: '+15415550000', phones: [{ accountPhone: { phoneNumber: { e164: '+15415550100' } } }] } }) ) expect(await new GVoice(t).getAccount()).toBe('+15415550100') const { url, init } = calls[0] expect(url.pathname).toBe('/voice/v1/voiceclient/account/get') expect(url.searchParams.get('alt')).toBe('proto') expect(url.searchParams.get('key')).toBeTruthy() const h = init.headers as Record expect(h['Authorization']).toMatch(/^SAPISIDHASH \d+_[0-9a-f]{40}$/) expect(h['Content-Type']).toBe('application/json+protobuf') expect(JSON.parse(init.body as string)).toEqual([null, 1]) }) it('sends recipients only when starting a new thread', async () => { const { t, calls } = fakeTransport(() => protoResponse(S.RespSendSMS, { threadId: 't.+15415550199', threadItemId: 'i1', timestampMs: 1 })) const gv = new GVoice(t) await gv.sendSms({ threadId: 't.+15415550199', recipients: ['+15415550199'], text: 'a', transactionId: 7 }) await gv.sendSms({ threadId: '', recipients: ['+15415550199', '+15415550198'], text: 'b', transactionId: 8 }) const [first, second] = calls.map((c) => JSON.parse(c.init.body as string) as unknown[]) expect(first[6]).toBeNull() expect(first[5]).toBe('t.+15415550199') expect(second[6]).toEqual(['+15415550199', '+15415550198']) expect(second[10]).toEqual(['!']) // unsigned }) it("refuses attachments GV can't carry", async () => { const { t } = fakeTransport(() => new Response('')) await expect(new GVoice(t).sendSms({ threadId: 'x', recipients: [], text: '', transactionId: 1, media: { mime: 'video/mp4', data: new Uint8Array() } })).rejects.toThrow( /only photos/ ) }) it('reports a dead sign-in as signed out', async () => { const { t } = fakeTransport(() => new Response('nope', { status: 401 })) const err = await new GVoice(t).listThreads().catch((e: GVoiceError) => e) expect(err).toBeInstanceOf(GVoiceError) expect((err as GVoiceError).signedOut).toBe(true) const noCookie = fakeTransport(() => new Response(''), '') expect(((await new GVoice(noCookie.t).listThreads().catch((e) => e)) as GVoiceError).signedOut).toBe(true) }) it('classifies send failures for retrying', () => { expect(classifyGvError(new GVoiceError('x', undefined, 'ERR_INTERNET_DISCONNECTED'))).toBe('safe') expect(classifyGvError(new GVoiceError('x', undefined, 'ERR_CONNECTION_RESET'))).toBe('unknown') expect(classifyGvError(new GVoiceError('x', 429))).toBe('safe') expect(classifyGvError(new GVoiceError('x', 503))).toBe('unknown') expect(classifyGvError(new GVoiceError('x', 401))).toBe('fatal') expect(gvErrorMessage(new GVoiceError('x', undefined, 'ERR_NAME_NOT_RESOLVED'))).toMatch(/internet connection/) }) }) describe('Google Voice messages', () => { it('keeps message ids stable and out of voip.ms range', () => { expect(gvMessageId('abc')).toBe(gvMessageId('abc')) expect(gvMessageId('abc')).not.toBe(gvMessageId('abd')) expect(isGvMessageId(gvMessageId('abc'))).toBe(true) expect(Number.isSafeInteger(gvMessageId('zzz'))).toBe(true) expect(isGvMessageId(987654321)).toBe(false) }) it('maps threads to conversations', () => { expect(threadContact({ phoneNumbers: ['+15415550199'] }, SELF)).toBe('5415550199') expect(threadContact({ phoneNumbers: ['+15415550199', '+15415550101', '+15415550100'] }, SELF)).toBe('5415550101,5415550199') }) it('converts texts, photos and group senders; skips calls', () => { const inMsg = toNewMessage(sms('a', 10, S.MessageType.SMS_IN, 'hey'), SELF, '5415550199', false)! expect(inMsg).toMatchObject({ did: SELF, contact: '5415550199', direction: 'in', body: 'hey', ts: 10 }) expect(inMsg.sender).toBeUndefined() expect(toNewMessage(sms('b', 11, S.MessageType.SMS_OUT, 'yo'), SELF, '5415550199', false)!.direction).toBe('out') expect(toNewMessage(sms('c', 12, S.MessageType.VOICEMAIL, ''), SELF, '5415550199', false)).toBeUndefined() const photo = toNewMessage( sms('d', 13, S.MessageType.SMS_IN, '', { mms: { text: 'look', subject: '', senderPhoneNumber: '+15415550101', participants: [], attachments: [ { id: 'm1', mimeType: 'image/png', status: 1, metadata: [{ size: 2, width: 128, height: 96 }, { size: 1, width: 1280, height: 960 }] }, { id: 'm2', mimeType: 'video/mp4', status: 3, metadata: [] } ] } }), SELF, '5415550101,5415550199', true )! expect(photo).toMatchObject({ body: 'look', kind: 'mms', sender: '5415550101' }) expect(photo.media).toEqual([{ url: 'gv:m1', mime: 'image/png', width: 1280, height: 960 }]) }) }) describe('GvSyncEngine', () => { let dir: string let db: MessageDb beforeEach(() => { dir = fs.mkdtempSync(path.join(os.tmpdir(), 'voipms-gv-')) db = new MessageDb(path.join(dir, 'messages.db')) }) afterEach(() => { db.close() fs.rmSync(dir, { recursive: true, force: true }) }) // A fake GV account: threads listed newest first, each paged two messages at a time. function fakeGv(threads: S.GvThread[]) { return { listThreads: async (token = '') => { const start = Number(token || 0) const page = threads.slice(start, start + 1) return { threads: page.map((t) => ({ ...t, messages: t.messages.slice(0, 1) })), paginationToken: start + 1 < threads.length ? String(start + 1) : '' } }, getThread: async (id: string, _count = 100, token = '') => { const t = threads.find((x) => x.id === id)! const start = Number(token || 0) return { ...t, messages: t.messages.slice(start, start + 2), paginationToken: start + 2 < t.messages.length ? String(start + 2) : '' } } } } it('imports history, groups included, and keeps polling', async () => { const one = thread('t.+15415550199', ['+15415550199'], [ sms('1c', 30, S.MessageType.SMS_IN, 'third'), sms('1b', 20, S.MessageType.SMS_OUT, 'second'), sms('1a', 10, S.MessageType.SMS_IN, 'first') ]) const group = thread('g.abc', ['+15415550199', '+15415550101'], [ sms('2a', 25, S.MessageType.SMS_IN, 'hi all', { mms: { text: 'hi all', subject: '', senderPhoneNumber: '+15415550101', participants: [], attachments: [] } }) ]) const gv = fakeGv([one, group]) const engine = new GvSyncEngine(db, gv as unknown as GVoice, { did: SELF, pollSeconds: 60, backfillFloor: '1970-01-01' }) const done = new Promise((resolve) => engine.on('status', (s) => s.backfillDone && resolve())) engine.start() await done engine.stop() expect(db.getConversationMessages(SELF, '5415550199').map((m) => m.body)).toEqual(['first', 'second', 'third']) const g = db.getConversationMessages(SELF, '5415550101,5415550199') expect(g).toHaveLength(1) expect(g[0].sender).toBe('5415550101') expect(db.getGvThread(SELF, '5415550101,5415550199')).toBe('g.abc') const conv = db.listConversations().find((c) => c.contact.includes(','))! expect(conv.lastSender).toBe('5415550101') }) it('lets a polled copy of a sent message replace the pending one', async () => { const tempId = db.insertPending({ did: SELF, contact: '5415550199', direction: 'out', body: 'on my way', ts: 1000 }) const t = thread('t.+15415550199', ['+15415550199'], [sms('x1', 1500, S.MessageType.SMS_OUT, 'on my way')]) const engine = new GvSyncEngine(db, fakeGv([t]) as unknown as GVoice, { did: SELF, pollSeconds: 60, backfillFloor: '2100-01-01' }) await engine.pollNow() expect(db.getMessage(tempId)).toBeUndefined() expect(db.getMessage(gvMessageId('x1'))?.status).toBe('delivered') }) it("carries over GV's read state on import", async () => { const read = thread('t.+15415550199', ['+15415550199'], [sms('r1', 10, S.MessageType.SMS_IN, 'seen')]) const unread = { ...thread('t.+15415550198', ['+15415550198'], [sms('u1', 11, S.MessageType.SMS_IN, 'new')]), read: false } db.set('unread_floor', '0') const gv = { listThreads: async () => ({ threads: [read, unread], paginationToken: '' }) } const engine = new GvSyncEngine(db, gv as unknown as GVoice, { did: SELF, pollSeconds: 60, backfillFloor: '2100-01-01' }) await engine.pollNow() const unreadOf = (contact: string): number => db.listConversations().find((c) => c.contact === contact)!.unread expect(unreadOf('5415550199')).toBe(0) expect(unreadOf('5415550198')).toBe(1) }) it('goes to signed-out on 401 and leaves the rest alone', async () => { const gv = { listThreads: async () => Promise.reject(new GVoiceError('signed out', 401)) } const engine = new GvSyncEngine(db, gv as unknown as GVoice, { did: SELF, pollSeconds: 60, backfillFloor: '2100-01-01' }) await engine.pollNow() expect(engine.getStatus().phase).toBe('signedOut') }) }) describe('Google Voice sends', () => { it('fail for good when signed out, with the reason', async () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'voipms-gvsend-')) const db = new MessageDb(path.join(dir, 'messages.db')) const tempId = db.insertPending({ did: SELF, contact: '5415550199', direction: 'out', body: 'x', ts: 1 }) let calls = 0 await sendWithRetry(tempId, { db, attempt: async () => { calls++ throw new GVoiceError('Google Voice signed out. Reconnect it in Settings.', 401) }, checkSent: async () => undefined, sleep: async () => undefined, classify: classifyGvError, describe: gvErrorMessage }) expect(calls).toBe(1) expect(db.getMessage(tempId)).toMatchObject({ status: 'failed', error: 'Google Voice signed out. Reconnect it in Settings.' }) db.close() fs.rmSync(dir, { recursive: true, force: true }) }) })