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.0 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
// shared/pad-share.js: states go only to the chosen device; the receiver plugs in its driver on the first one,
// writes 12-byte states, and unplugs on pad.off; nothing is plugged in while Receive is off.
// Run: node shared/test/pad-share.test.js
'use strict';
const assert = require('assert');
const { PadShare } = require('../pad-share.js');

(async () => {
  const handlers = {};
  const all = [];
  const sent = { push: (x) => all.push(x), get length() { return all.filter((x) => x[1].t === 'pad.state').length; } }; // states only (offers go too)
  const st = (i) => all.filter((x) => x[1].t === 'pad.state')[i];
  const settingsA = { pads: {} }; // a computer that had used it (no pads at all: off, light by default)
  const settingsB = { pads: { receive: true } };
  const writes = [];
  let plugged = 0;
  const driver = { check: async () => ({ supported: true, installed: true }), install: async () => {}, feed: () => { plugged++; return { stdin: { write: (b) => writes.push(b.length), end: () => { plugged--; } } }; } };
  const A = new PadShare({ send: (id, m) => sent.push([id, m]), on: () => {}, connected: (id) => id === 'B', getSettings: () => settingsA });
  const B = new PadShare({ send: () => {}, on: (t, fn) => { handlers[t] = fn; }, connected: () => true, getSettings: () => settingsB, loadPad: () => driver });

  A.fromWindow({ b: 1, lx: 0.5 });
  assert.strictEqual(sent.length, 0, 'no device chosen: nothing sent');
  A.set({ share: true, to: 'B' });
  A.fromWindow({ b: 1, lx: 0.5 });
  assert.strictEqual(sent.length, 1);
  assert.strictEqual(st(0)[0], 'B');
  assert.ok(all.some((x) => x[0] === 'B' && x[1].t === 'pad.offer'), 'B hears what it is');
  A.fromWindow({ b: 1, lx: 0.5 });
  assert.strictEqual(sent.length, 1, 'unchanged: not sent again at once');

  await handlers['pad.state'](st(0)[1], 'A');
  assert.strictEqual(plugged, 1);
  assert.deepStrictEqual(writes, [12]);
  assert.strictEqual(B.view().playing, 'A');
  handlers['pad.off']({}, 'A');
  assert.strictEqual(plugged, 0);

  B.set({ receive: false });
  await handlers['pad.state'](st(0)[1], 'A');
  assert.strictEqual(plugged, 0, 'Receive off: nothing plugged in');

  // The input lane: the sender's copy goes there too; the receiver keeps the newest, whichever path it came by.
  const { EventEmitter } = require('events');
  const laneOut = [];
  const laneA = { sendPad: (n, b) => { laneOut.push([n, b.length]); return true; } };
  const laneB = new EventEmitter();
  const settingsC = { pads: { to: 'B' } };
  const C = new PadShare({ send: (id, m) => sent.push([id, m]), on: () => {}, connected: () => true, lane: () => laneA, getSettings: () => settingsC });
  C.fromWindow({ b: 2 });
  assert.deepStrictEqual(laneOut, [[1, 12]]);
  const B2 = new PadShare({ send: () => {}, on: (t, fn) => { handlers[t] = fn; }, connected: () => true, lane: () => laneB, getSettings: () => ({ pads: { receive: true } }), loadPad: () => driver });
  writes.length = 0;
  await handlers['pad.state']({ n: 5, b: 1 }, 'C'); // the session's copy first: listens on C's lane from now
  laneB.emit('pad', 7, Buffer.alloc(12));
  await new Promise((r) => setImmediate(r));
  await handlers['pad.state']({ n: 6, b: 1 }, 'C'); // late: the lane's 7 came first
  assert.deepStrictEqual(writes, [12, 12]);
  B2.stop();

  // Several computers at once, and the receiver plays only the one it picked.
  {
    const out = [];
    const S = new PadShare({ send: (id, m) => out.push([id, m]), on: () => {}, connected: () => true, getSettings: ((o) => () => o)({ pads: { on: true } }) });
    S.set({ share: true, to: ['B', 'C'], padName: 'Pad' });
    S.fromWindow({ b: 4 });
    assert.deepStrictEqual(out.filter((x) => x[1].t === 'pad.state').map((x) => x[0]), ['B', 'C'], 'states go to every computer it is shared with');
    S.set({ to: ['C'] });
    assert.ok(out.some((x) => x[0] === 'B' && x[1].t === 'pad.off'), 'one taken off hears pad.off');
    // Share my controller off: the picked computers are kept, they hear pad.off, and nothing more goes.
    S.set({ share: false });
    assert.ok(out.some((x) => x[0] === 'C' && x[1].t === 'pad.off'), 'switched off: pad.off');
    const sent = out.length;
    S.fromWindow({ b: 8 });
    assert.strictEqual(out.length, sent, 'nothing sent while off');
    assert.deepStrictEqual(S.view().to, ['C'], 'the picked computers kept');
    S.set({ share: true });
    const h = {};
    const w = [];
    const drv = { check: async () => ({ supported: true, installed: true }), install: async () => {}, feed: () => ({ stdin: { write: (b) => w.push(b.length), end() {} } }) };
    const R = new PadShare({ send() {}, on: (t, fn) => { h[t] = fn; }, connected: () => true, getSettings: ((o) => () => o)({ pads: { on: true, receive: true, from: 'Y' } }), loadPad: () => drv });
    h['pad.offer']({ name: 'Xbox' }, 'X');
    h['pad.offer']({ name: 'DS4' }, 'Y');
    assert.deepStrictEqual(R.view().offers.map((o) => [o.from, o.name]), [['X', 'Xbox'], ['Y', 'DS4']]);
    await h['pad.state']({ n: 1, b: 1 }, 'X');
    assert.strictEqual(w.length, 0, 'not the one picked: ignored');
    await h['pad.state']({ n: 1, b: 1 }, 'Y');
    assert.strictEqual(w.length, 1, 'the picked one plays');
    assert.strictEqual(R.view().playing, 'Y');
    R.stop();
    S.stop();
  }
  // Settings from before: to as one id, no on → on, the same device.
  const { clean } = require('../pad-share.js');
  assert.deepStrictEqual(clean({ to: 'B', receive: true }), { on: true, share: true, pad: '', to: ['B'], receive: true, from: '', kind: 'xbox' });
  // A one-to-one link.
  {
    const { EventEmitter } = require('events');
    const link = new EventEmitter();
    link.current = { deviceId: 'P', name: 'PC', os: 'win' };
    const got = [];
    link.send = (m) => got.push(m);
    const L = PadShare.link(link);
    L.send('P', { t: 'x' });
    L.send('Q', { t: 'y' });
    assert.deepStrictEqual(got, [{ t: 'x' }], 'only the partner');
    assert.deepStrictEqual(L.devices().map((d) => d.id), ['P']);
  }

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