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.6 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// shared/mesh-mgr.js: a manager (TwinDesk Professional or a Mac) picks computers; each is added only once it accepts
// on its own screen; power commands run only for what was accepted, only straight from the manager;
// Biometric unlock is offered per computer, only to a TwinDesk Professional one, and needs its own acceptance;
// Revoke and turning manager mode off drop everything; a standard build can't turn manager mode on.
// Run: node shared/test/mesh-mgr.test.js
'use strict';

const assert = require('assert');
const { meshMgr } = require('../mesh-mgr.js');

const tick = () => new Promise((r) => setTimeout(r, 5));
const links = { A: ['B', 'C'], B: ['A', 'C'], C: ['A', 'B'] };
const nodes = {};
function node(id, { canManage = false, pro = false } = {}) {
  const handlers = new Map();
  const peerFns = new Map();
  const settings = {};
  const did = [];
  const deliver = (msg, from) => (handlers.get(msg.t) || []).forEach((fn) => fn(msg, from));
  const bus = {
    on: (t, fn) => { if (!handlers.has(t)) handlers.set(t, []); handlers.get(t).push(fn); },
    onPeer: (ev, fn) => { if (!peerFns.has(ev)) peerFns.set(ev, []); peerFns.get(ev).push(fn); },
    send: (to, msg) => { if (!links[id].includes(to)) return false; setImmediate(() => nodes[to].deliver(JSON.parse(JSON.stringify(msg)), id)); return true; },
    connectedIds: () => links[id],
  };
  const mesh = { peers: () => links[id].map((x) => ({ id: x, mesh: true, send: (m) => bus.send(x, m) })) };
  const m = meshMgr({
    bus, mesh, selfId: id, name: () => 'pc-' + id, settings: () => settings, save: () => {}, isPhone: () => false,
    devices: () => links[id].map((x) => ({ id: x, name: 'pc-' + x, online: true })), canManage, pro,
    local: { can: () => ({ lock: true, sleep: true, restart: true, shutdown: true }), do: (a) => { did.push(a); } },
  });
  nodes[id] = { m, deliver, settings, did, welcome: (x) => (peerFns.get('welcome') || []).forEach((fn) => fn(x)) };
  return nodes[id];
}

(async () => {
  const A = node('A', { canManage: true, pro: true }); // TwinDesk Professional
  const B = node('B', { canManage: true, pro: true }); // TwinDesk Professional
  const C = node('C'); // the standard build
  for (const n of [A, B, C]) n.m.tick();
  await tick();

  // The standard build can't be a manager.
  assert.strictEqual(C.m.setOn(true), false);
  assert.strictEqual(C.m.view().canManage, false);

  // A turns manager mode on and picks B and C: each is asked, nothing granted yet.
  assert.ok(A.m.setOn(true));
  A.m.pick('B', true);
  A.m.pick('C', true);
  await tick();
  assert.deepStrictEqual(B.m.view().asks, [{ id: 'A', name: 'pc-A', perms: ['power'] }]);
  assert.deepStrictEqual(C.m.view().asks.map((a) => a.perms), [['power']]);
  assert.strictEqual(B.m.view().grants.length, 0);
  // Not accepted: a command is refused there.
  for (const n of [A, B, C]) n.m.tick();
  await tick();
  let r = await A.m.act('B', 'lock');
  assert.strictEqual(r.ok, false);
  assert.deepStrictEqual(B.did, []);

  // B and C accept: A may lock them.
  B.m.answer('A', 'power', true);
  C.m.answer('A', 'power', true);
  await tick();
  r = await A.m.act('C', 'lock');
  assert.deepStrictEqual(r, { ok: true, why: '' });
  assert.deepStrictEqual(C.did, ['lock']);
  const v = A.m.view().computers;
  assert.deepStrictEqual(v.map((d) => [d.id, d.power, d.pro]), [['B', 'accepted', true], ['C', 'accepted', false]]);

  // Biometric unlock: not for C (standard), offered to B, which accepts the risk itself.
  assert.strictEqual(A.m.setUnlock('C', true), false, 'C has no TwinDesk Professional: grayed');
  assert.ok(A.m.setUnlock('B', true));
  await tick();
  assert.deepStrictEqual(B.m.view().asks.map((a) => a.perms), [['unlock']]);
  assert.strictEqual(B.m.mayUnlock('A'), false, 'offered, not accepted');
  B.m.answer('A', 'unlock', true);
  await tick();
  assert.strictEqual(B.m.mayUnlock('A'), true);
  assert.strictEqual(C.m.mayUnlock('A'), false);

  // A command from a computer that isn't the manager is refused, and nothing runs.
  r = await B.m.act('C', 'lock');
  assert.strictEqual(r.ok, false);
  assert.deepStrictEqual(C.did, ['lock']);

  // C revokes: A can't any more.
  C.m.revoke('A');
  C.m.tick();
  await tick();
  r = await A.m.act('C', 'sleep');
  assert.strictEqual(r.ok, false);

  // A turns manager mode off: B drops everything it granted.
  A.m.setOn(false);
  await tick();
  assert.deepStrictEqual(B.m.view().grants, []);
  assert.strictEqual(B.m.mayUnlock('A'), false);

  console.log('mesh-mgr: ok');
  process.exit(0);
})().catch((e) => { console.error(e); process.exit(1); });