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.

6.1 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// shared/cam-multi.js on a real three-device mesh (Linux hub), with a stand-in CamShare: A's camera goes to B
// and C at once, while A shows B's camera (picked at the top, not the sidebar's selection); a computer taken
// off the picker stops at once and hears it isn't shared; Stop for one leaves the other.
// Run: node shared/test/cam-multi.test.js
'use strict';

const assert = require('assert');
const crypto = require('crypto');
const P = require('../protocol.js');
const { Mesh } = require('../mesh.js');
const { Hub } = require('../../linux/mesh/hub.js');
const { LinkManager } = require('../../linux/mesh/links.js');
const { Server } = require('../../linux/mesh/server.js');
const { Pairing } = require('../../linux/mesh/hostpair.js');
const { waitFor, pair, memoryStorage, close } = require('../../linux/mesh/test/lib/mesh.js');
const { loadTestCert } = require('../../linux/mesh/test/lib/testcert.js');
const { CamMulti } = require('../cam-multi.js');

const T = P.T;
const linked = (a, b) => {
  const sa = a.hub.sessions.get(b.id);
  const sb = b.hub.sessions.get(a.id);
  return !!(sa && sa.connected && sb && sb.connected);
};

async function node(name) {
  const cert = loadTestCert();
  const id = crypto.randomUUID();
  const server = new Server({ key: cert.key, cert: cert.cert, port: 0 });
  await new Promise((resolve) => server.once('listening', resolve));
  const storage = memoryStorage();
  const pairing = new Pairing(server, { id, name, fp: cert.fp }, { storage });
  const links = new LinkManager({ device: { id, name }, storage, hostPairing: pairing });
  links.bindServer(server);
  const mesh = new Mesh({ selfId: id });
  const hub = new Hub({ device: { id, name }, links, server, pairing, mesh });
  hub.start();
  return { name, id, port: server.port, server, links, pairing, hub };
}

// CamShare's outline (no Electron): the share, who may see it, its sender and viewer, the policy it hears.
class FakeCam {
  constructor(o) {
    Object.assign(this, o);
    this.peerShares = false;
    this.sender = null;
    this.viewer = null;
    this.link.on(T.CAM_POLICY, (m) => { this.peerShares = !!m && m.share === true; this.onChange(); });
    this.link.on(T.CAM_OPEN, () => {
      if (!this.share || !this.allows(this.current())) return this.link.send({ t: T.CAM_STATE, status: 'refused' });
      this.sender = { status: 'live' };
      this.onChange();
    });
    this.link.on('welcome', () => this.sendPolicy());
  }
  get share() { return this.cfg().share === true; }
  allows(id) { const to = this.cfg().shareTo; return !Array.isArray(to) || to.includes(id); }
  sendPolicy() { if (this.link.connected()) this.link.send({ t: T.CAM_POLICY, share: this.share }); }
  async setShare(on) { this.cfg().share = !!on; }
  setShareTo(v) { this.cfg().shareTo = v; }
  setDevice() {}
  listDevices() {}
  stopSending() { if (this.sender) { this.sender = null; this.link.send({ t: T.CAM_CLOSE }); } }
  view() { this.viewer = { status: 'live' }; this.link.send({ t: T.CAM_OPEN }); }
  closeView() { this.viewer = null; }
  get state() { return { share: this.share, ble: false, peerShares: this.peerShares, viewing: !!this.viewer, viewStatus: this.viewer ? this.viewer.status : '' }; }
}

async function main() {
  const A = await node('A');
  const B = await node('B');
  const C = await node('C');
  const N = { A, B, C };
  const TRIANGLE = [['A', 'B'], ['A', 'C'], ['B', 'C']];
  try {
    for (const [x, y] of TRIANGLE) await pair(N[x], N[y]);
    await waitFor(() => TRIANGLE.every(([x, y]) => linked(N[x], N[y])), 'triangle linked');
    A.hub.select(C.id); // the sidebar has C: it doesn't matter for the camera shown

    const heard = { B: [], C: [] };
    for (const k of ['B', 'C']) {
      for (const t of [T.CAM_POLICY, T.CAM_STATE, T.CAM_CLOSE]) N[k].hub.bus.on(t, (m, from) => from === A.id && heard[k].push(m));
    }
    const opens = [];
    B.hub.bus.on(T.CAM_OPEN, (m, from) => from === A.id && opens.push(from));

    const cfg = { on: true, share: true, shareTo: [B.id, C.id], from: '' };
    const cm = new CamMulti({ hub: A.hub, CamShare: FakeCam, cfg: () => cfg, save: () => {}, popup: () => {}, onChange: () => {}, log: () => {} });
    await waitFor(() => heard.B.some((m) => m.t === T.CAM_POLICY && m.share) && heard.C.some((m) => m.t === T.CAM_POLICY && m.share), 'both hear it is shared');

    // B and C both open A's camera: two at once.
    B.hub.bus.send(A.id, { t: T.CAM_OPEN });
    C.hub.bus.send(A.id, { t: T.CAM_OPEN });
    await waitFor(() => cm.state.sendingTo.length === 2, 'sending to both');
    assert.deepStrictEqual(cm.state.sendingTo.map((x) => x.id).sort(), [B.id, C.id].sort());

    // B shares its camera with A: it's a source; picked at the top, it shows here while A still sends to both.
    B.hub.bus.send(A.id, { t: T.CAM_POLICY, share: true });
    await waitFor(() => cm.sources().some((x) => x.id === B.id), 'B is a source');
    assert.ok(!cm.sources().some((x) => x.id === C.id), 'C shares nothing');
    cm.setFrom(B.id);
    cm.view(false);
    await waitFor(() => opens.length === 1, 'B was asked for its camera');
    assert.strictEqual(cm.state.viewing, true);
    assert.strictEqual(cm.state.from, B.id);
    assert.strictEqual(cm.state.sendingTo.length, 2, 'receiving and sending at the same time');

    // C taken off the picker: its picture stops and it hears it isn't shared with it any more.
    cm.setShareTo([B.id]);
    await waitFor(() => heard.C.some((m) => m.t === T.CAM_CLOSE), 'C told it stopped');
    await waitFor(() => heard.C.filter((m) => m.t === T.CAM_POLICY).pop().share === false, 'C hears not shared');
    assert.deepStrictEqual(cm.state.sendingTo.map((x) => x.id), [B.id]);

    // Stop for B alone.
    cm.stopSending(B.id);
    assert.strictEqual(cm.state.sending, false);

    // C opens again: refused, not picked.
    C.hub.bus.send(A.id, { t: T.CAM_OPEN });
    await waitFor(() => heard.C.some((m) => m.t === T.CAM_STATE && m.status === 'refused'), 'C refused');
    console.log('passed: the camera goes to several computers while another one shows here');
  } finally {
    close(A, B, C);
  }
}

main().catch((e) => {
  console.error(e);
  process.exit(1);
});