// The relay against the dev app on a plugged-in Rabbit (DEVICE=1 npm test -- // test/relay.device.test.ts). A throwaway local relay, reached from the Rabbit // through adb reverse; the dev app's made-up data (tools/push_sandbox.py). // Never the real app, never jai. import { execFileSync, spawn, type ChildProcess } from 'node:child_process' import fs from 'node:fs' import net from 'node:net' import os from 'node:os' import path from 'node:path' import { afterAll, describe, expect, it, vi } from 'vitest' const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'relay-device-')) vi.mock('electron', () => ({ app: { getPath: () => tmp }, safeStorage: { isEncryptionAvailable: () => true, encryptString: (s: string) => Buffer.from(s), decryptString: (b: Buffer) => b.toString() }, nativeImage: { createFromBuffer: () => ({ getSize: () => ({ width: 0, height: 0 }) }) }, net: { fetch: () => Promise.reject(new Error('no network in tests')) } })) const { MessageDb } = await import('../src/main/db') const { MediaStore } = await import('../src/main/media') const { newKey, RelayEngine } = await import('../src/main/relay') const PKG = 'me.teafry.voipmessages.dev' const DEVICE_PORT = 18080 let server: ChildProcess | undefined afterAll(() => { server?.kill() try { execFileSync('adb', ['reverse', '--remove', `tcp:${DEVICE_PORT}`]) } catch { /* gone already */ } }) const sleep = (ms: number): Promise => new Promise((r) => setTimeout(r, ms)) describe.skipIf(!process.env['DEVICE'])('relay with the dev app on the Rabbit', () => { it('seals in Java and opens in Node, and back', { timeout: 240_000 }, async () => { const port = await new Promise((resolve) => { const s = net.createServer().listen(0, '127.0.0.1', () => { const p = (s.address() as net.AddressInfo).port s.close(() => resolve(p)) }) }) const data = path.join(tmp, 'server') server = spawn('python3', [path.join(__dirname, '../server/bubble_relay.py'), '--port', String(port), '--data', data], { stdio: 'ignore' }) const url = `http://127.0.0.1:${port}` for (let i = 0; i < 50 && !(await fetch(`${url}/health`).then((r) => r.ok).catch(() => false)); i++) await sleep(100) const token = fs.readFileSync(path.join(data, 'token'), 'utf8').trim() const key = newKey() execFileSync('adb', ['reverse', `tcp:${DEVICE_PORT}`, `tcp:${port}`]) execFileSync('python3', [path.join(__dirname, '../android/tools/push_sandbox.py'), '--relay', `http://127.0.0.1:${DEVICE_PORT}`], { env: { ...process.env, RELAY_TOKEN: token, RELAY_KEY: key }, stdio: 'ignore' }) const db = new MessageDb(path.join(tmp, 'messages.db')) const media = new MediaStore(path.join(tmp, 'media'), db) const engine = new RelayEngine(db, media, { url, token, key }, 30) ;(engine as unknown as { running: boolean }).running = true db.setName('sim', '5415550150', 'Made-up SIM friend') engine.stateChanged() const tempId = db.insertPending({ did: 'sim', contact: '5415550150', direction: 'out', body: 'from the desk', ts: Date.now() }) engine.send(tempId, '5415550150', 'from the desk', []) await engine.syncNow() // Opening the app has it check the relay (a job a few seconds later). execFileSync('adb', ['shell', 'am', 'start', '-n', `${PKG}/me.teafry.voipmessages.MainActivity`]) let simIds: number[] = [] for (let i = 0; i < 40; i++) { await sleep(4000) await engine.syncNow() simIds = db.getConversationMessages('sim', '5415550150').map((m) => m.id) if (simIds.includes(2 ** 51 + 1) && db.getMessage(tempId)?.status === 'failed') break // The send's answer comes on the Rabbit's next round: nudge it. if (i % 5 === 4) execFileSync('adb', ['shell', 'am', 'start', '-S', '-n', `${PKG}/me.teafry.voipmessages.MainActivity`]) } // The made-up SIM history came over, decrypted. expect(simIds).toEqual(expect.arrayContaining([2 ** 51 + 1, 2 ** 51 + 2])) expect(db.getConversationMessages('sim', '5415550150').find((m) => m.id === 2 ** 51 + 1)?.body).toBe('Texting your SIM number now') // The send was read and answered (the dev app isn't the default SMS app, so: a reason). expect(db.getMessage(tempId)?.status).toBe('failed') expect(db.getMessage(tempId)?.error).toMatch(/texting app/) engine.stop() db.close() }) })