Skip to content

All your computers and your phone/iPhone, working as one desk. Screens, apps, files, clipboard, calls, texts, notifications, mouse and keyboard, shared between Mac, Windows, Linux , Android, Web and ios / ipados . Free and open source. No account, no cloud, and no shared network needed.

4.9 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// A phone debugged through the computer it's connected to (shared/phone-debug.js, meshdebug.js phone()):
// another computer's request for it is answered there, as the phone (from: its id), under that
// computer's rules; its log comes from the app (phone.dbg over its session), in pieces; an older app
// has none; what a phone can't do says so. The computer's own Local API asks it the same way.
// Run: node shared/test/phone-debug.test.js
'use strict';

const assert = require('assert');
const { MeshDebug } = require('../meshdebug.js');
const { phoneDebug } = require('../phone-debug.js');

// ryzen — nitro — poco (the phone, on nitro only). Messages are delivered on the next turn, as a socket's.
function world({ allow = ['ryzen'], caps = ['debug.log'], log = 'x'.repeat(600 * 1024) } = {}) {
  const links = ['ryzen:nitro', 'nitro:poco'];
  const linked = (a, b) => links.includes(`${a}:${b}`) || links.includes(`${b}:${a}`);
  const handlers = { ryzen: {}, nitro: {}, poco: {} };
  const asked = [];
  const bus = (me) => ({
    on: (t, fn) => { (handlers[me][t] = handlers[me][t] || []).push(fn); },
    send: (to, m) => {
      if (!linked(me, to)) return false;
      const copy = JSON.parse(JSON.stringify(m));
      setImmediate(() => (handlers[to][copy.t] || []).forEach((fn) => fn(copy, me)));
      return true;
    },
    connectedIds: () => ['ryzen', 'nitro', 'poco'].filter((n) => linked(me, n)),
  });
  // The app on the phone (DebugLog.kt): its files, and a read of at most 256 KB.
  const file = Buffer.from(log);
  const pbus = bus('poco');
  pbus.on('phone.dbg', (m, from) => {
    asked.push(m.op);
    if (m.op === 'files') return pbus.send(from, { t: 'phone.dbg.res', req: m.req, files: [{ name: 'TwinDesk.log', size: file.length }, { name: '../etc', size: 1 }] });
    const part = file.subarray(m.from, m.from + Math.min(m.len, 256 * 1024));
    pbus.send(from, { t: 'phone.dbg.res', req: m.req, size: file.length, data: part.toString('base64') });
  });
  const local = { hello: async () => ({ name: 'self' }), files: async () => [] };
  const nbus = bus('nitro');
  const hub = { bus: nbus, sessions: new Map([['poco', { connected: true, transport: 'ip', rtt: 4 }]]) };
  const phones = () => [{ id: 'poco', name: 'POCO', info: { model: 'X5 Pro', maker: 'POCO', release: '14', app: '3.2.2', caps }, adb: () => null, bridge: { state: 'idle', message: '' } }];
  const phone = phoneDebug({ hub, phones, self: () => 'nitro' });
  const nitro = new MeshDebug({ bus: nbus, self: 'nitro', route: (id) => (linked('nitro', id) ? id : null), allowed: () => allow, local, phone, log: () => {} }).start();
  const ryzen = new MeshDebug({ bus: bus('ryzen'), self: 'ryzen', route: (id) => (linked('ryzen', id) ? id : 'nitro'), local, log: () => {} }).start();
  return { nitro, ryzen, asked, file };
}

setInterval(() => {}, 1000); // meshdebug's timers are unref'd: this keeps the test alive while it waits

(async () => {
  {
    const { ryzen, asked, file } = world();
    const h = await ryzen.ask('poco', 'hello');
    assert.strictEqual(h.data.phone, true);
    assert.strictEqual(h.data.through, 'nitro', 'nitro answers for it');
    assert.strictEqual(h.data.version, '3.2.2');
    assert.strictEqual(h.data.allowed, true);
    const f = await ryzen.ask('poco', 'files');
    assert.deepStrictEqual(f.data, [{ name: 'TwinDesk.log', size: file.length }], 'only its own logs');
    const tail = await ryzen.ask('poco', 'log', { file: 'TwinDesk.log', tail: 300 * 1024 });
    assert.strictEqual(tail.bin.length, 300 * 1024, 'a range bigger than one read: in pieces from the app');
    assert.ok(asked.filter((x) => x === 'read').length >= 2);
    const s = await ryzen.ask('poco', 'status');
    assert.strictEqual(s.data.session.transport, 'ip');
    assert.strictEqual(s.data.adb.state, 'idle');
    assert.strictEqual(await ryzen.ask('poco', 'shot').then(() => 'ok', (e) => e.code), 'no adb: its screen needs wireless debugging (or the cable) up');
    assert.strictEqual(await ryzen.ask('poco', 'diag', { name: 'network' }).then(() => 'ok', (e) => e.code), 'not on a phone');
  }
  {
    // nitro doesn't let ryzen debug it: nor its phones.
    const { ryzen } = world({ allow: [] });
    assert.strictEqual((await ryzen.ask('poco', 'hello')).data.allowed, false);
    assert.strictEqual(await ryzen.ask('poco', 'files').then(() => 'ok', (e) => e.code), 'not allowed');
  }
  {
    // An app without DebugLog.kt: no log, said so; nitro's own Local API asks the same.
    const { nitro, asked } = world({ caps: ['mirror.native'] });
    assert.strictEqual(await nitro.ask('poco', 'files').then(() => 'ok', (e) => e.code), 'the app on the phone keeps no log: update TwinDesk there');
    assert.deepStrictEqual(asked, []);
    assert.strictEqual((await nitro.ask('poco', 'hello')).data.through, 'nitro');
  }
  console.log('phone-debug: ok');
  process.exit(0);
})().catch((e) => { console.error(e); process.exit(1); });