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.
// Multi-device Terminal: another computer can ask for a shell here, never turn it on (shared/pairsync-mesh.js
// GUARD, shared/consent.js). Its ask is a request this computer answers; turning it off is always taken; a
// device met for the first time starts without a shell here, whatever the device shown has.
// Run: node shared/test/terminal-ask.test.js
'use strict';

const assert = require('assert');
const { EventEmitter } = require('events');
const { FEATURES } = require('../../windows/pairsync.js');
const { pairSyncMesh } = require('../pairsync-mesh.js');
const { consent } = require('../consent.js');

const features = { terminal: FEATURES.terminal, clipboard: FEATURES.clipboard, audio: FEATURES.audio }; // terminal: out = allowMac (a shell here)
const settings = { terminal: { allowMac: true, enabled: false }, clipboard: { send: true, receive: true }, audio: { send: true, receive: true }, pairSync: {} };
const hub = new EventEmitter();
hub.selectedId = 'A';
const sent = [];
const on = new Map();
const bus = { on: (t, fn) => on.set(t, fn), onPeer: () => {}, send: (id, m) => { sent.push([id, m]); return true; } };
const asked = [];
const ps = pairSyncMesh({
  bus, hub, features, values: {}, settings: () => settings, save: () => {},
  setFeature: (f, dir, v) => { const [sec, key] = features[f][dir]; settings[sec][key] = v; },
  setValue: () => {}, peerRole: () => 'host', peerOs: () => 'win', onAsk: (id, f) => asked.push([id, f]),
});
const c = consent({ features, settings: () => settings, selectedId: () => hub.selectedId });
const heard = (m, from) => on.get('pair.sync')(m, from);

// A is shown and allowed (the settings). B, never met, has no shell here although the settings say on.
assert.strictEqual(c.get('A', 'terminal', 'out'), true);
assert.strictEqual(c.get('B', 'terminal', 'out'), false, 'a device with no pair: no shell here');
assert.strictEqual(c.get('B', 'clipboard', 'out'), true, 'other switches still follow the settings');
assert.strictEqual(ps.pairOf('B').terminal.out, false, 'told to B: off');
// Sound on (for A) doesn't reach every device: B has none either way until turned on for it.
assert.strictEqual(c.get('A', 'audio', 'out'), true);
assert.strictEqual(c.get('B', 'audio', 'out'), false, 'sound: off for a device with no pair');
assert.strictEqual(c.get('B', 'audio', 'in'), false);

// B asks (its "open a terminal there" on: dir in, from its side): a request, nothing turned on.
heard({ feature: 'terminal', dir: 'in', on: true }, 'B');
assert.deepStrictEqual(ps.asks().map((a) => [a.id, a.feature]), [['B', 'terminal']]);
assert.strictEqual(c.get('B', 'terminal', 'out'), false, 'asked, not allowed');
assert.deepStrictEqual(asked, [['B', 'terminal']]);
// The same in a whole pair ({all}, as a host tells at the start).
heard({ all: { terminal: { in: true, out: false }, clipboard: { in: false, out: true } } }, 'C');
assert.strictEqual(c.get('C', 'terminal', 'out'), false);
assert.strictEqual(settings.pairSync.C.clipboard.in, true, 'the rest of the pair is taken');
assert.strictEqual(ps.asks().length, 2);

// Allow B: on for B, told to B.
sent.length = 0;
ps.answer('B', 'terminal', true);
assert.strictEqual(c.get('B', 'terminal', 'out'), true);
assert.deepStrictEqual(sent, [['B', { t: 'pair.sync', feature: 'terminal', dir: 'out', on: true }]]);
// Decline C: told it's off, the request gone.
sent.length = 0;
ps.answer('C', 'terminal', false);
assert.strictEqual(c.get('C', 'terminal', 'out'), false);
assert.deepStrictEqual(sent, [['C', { t: 'pair.sync', feature: 'terminal', dir: 'out', on: false }]]);
assert.deepStrictEqual(ps.asks(), []);

// Already allowed: asking again changes nothing. B stopping (off) is taken.
heard({ feature: 'terminal', dir: 'in', on: true }, 'B');
assert.deepStrictEqual(ps.asks(), []);
heard({ feature: 'terminal', dir: 'in', on: false }, 'B');
assert.strictEqual(c.get('B', 'terminal', 'out'), false, 'off is always taken');

// The pointer crosses to D, never met: the settings (A's: on) don't become D's.
hub.selectedId = 'D';
hub.emit('selected', 'D');
assert.strictEqual(settings.terminal.allowMac, false, 'D starts without a shell here');
assert.deepStrictEqual([settings.audio.send, settings.audio.receive], [false, false], 'and without sound either way');
// D asks while shown: a request; Allow turns the settings' switch on and tells D.
heard({ feature: 'terminal', dir: 'in', on: true }, 'D');
assert.strictEqual(settings.terminal.allowMac, false);
sent.length = 0;
ps.answer('D', 'terminal', true);
assert.strictEqual(settings.terminal.allowMac, true);
assert.deepStrictEqual(sent, [['D', { t: 'pair.sync', feature: 'terminal', dir: 'out', on: true }]]);
// Back to A: its own pair (on) comes back.
hub.selectedId = 'A';
hub.emit('selected', 'A');
assert.strictEqual(settings.terminal.allowMac, true);
assert.deepStrictEqual([settings.audio.send, settings.audio.receive], [true, true], 'A’s sound comes back');

console.log('terminal-ask: ok');