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.

9.5 KB
Raw Blame
// TwinDesk — Copyright (C) 2026 inukaasith. GPL-3.0, see LICENSE.
'use strict';
const assert = require('node:assert/strict');
const { EventEmitter } = require('node:events');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const Module = require('node:module');
const { test } = require('node:test');

const ipcMain = new EventEmitter();
const handles = new Map();
ipcMain.handle = (name, fn) => handles.set(name, fn);
const revealed = [], dragged = [];
let windowId = 0;
class Window extends EventEmitter {
  constructor(opts) {
    super(); this.opts = opts; this.destroyed = false;
    this.webContents = new EventEmitter();
    Object.assign(this.webContents, { id: ++windowId, mainFrame: {}, send() {}, setWindowOpenHandler() {}, startDrag(item) { dragged.push(item); } });
  }
  loadFile() { return Promise.resolve(); }
  isDestroyed() { return this.destroyed; }
  show() { this.shown = true; }
  focus() {}
  close() { this.destroy(); }
  destroy() { this.destroyed = true; this.emit('closed'); }
}
const original = Module._load;
Module._load = function(name) {
  if (name === 'electron') return { BrowserWindow: Window, ipcMain,
    screen: { getCursorScreenPoint: () => ({ x: 1, y: 1 }), getDisplayNearestPoint: () => ({ workArea: { x: 0, y: 0, width: 1000, height: 800 } }) },
    nativeImage: { createFromPath: () => ({ resize() { return this; }, isEmpty: () => false }) },
    shell: { showItemInFolder: p => revealed.push(p) } };
  return original.apply(this, arguments);
};
const { FileShelf, FileShelves } = require('../file-shelf.js');
Module._load = original;
const event = shelf => ({ sender: shelf.win.webContents, senderFrame: shelf.win.webContents.mainFrame });
const read = shelf => handles.get('file-shelf:read')(event(shelf));
const tmp = () => fs.mkdtempSync(path.join(fs.realpathSync(os.tmpdir()), 'twindesk-shelf-test-'));

// The production class handles this end to end, while Electron effects are captured.
test('shelf presents bounded saved files and native drag or reveal only for its own items', () => {
  const dir = tmp();
  const drop = new EventEmitter();
  drop.recent = [];
  try {
    for (let i = 0; i < 12; i++) { const file = path.join(dir, `file-${i}.txt`); fs.writeFileSync(file, 'fixture'); drop.recent.push({ path: file, ok: true }); }
    const shelf = new FileShelf({ drop });
    assert.equal(drop.listenerCount('change'), 0);
    shelf.show({ view: 'recent' });
    assert.equal(drop.listenerCount('change'), 1);
    const state = read(shelf);
    assert.equal(state.items.length, 8);
    assert.equal(state.items[0].kind, 'file');
    assert.equal(new Set(state.items.map(x => x.name)).size, 8, 'Recent entries are unique');
    assert.ok(state.items.every(item => !('path' in item)));
    const id = state.items[0].id;
    assert.equal(read(shelf).items[0].id, id, 'ordinary refresh retains focus identity');
    ipcMain.emit('file-shelf:drag', event(shelf), id);
    assert.equal(dragged.at(-1).file, drop.recent[0].path);
    ipcMain.emit('file-shelf:reveal', event(shelf), id);
    assert.equal(revealed.at(-1), drop.recent[0].path);
    shelf.refresh();
    assert.equal(read(shelf).items[0].id, id, 'same authorized local file retains focus across refresh');
    shelf.shutdown();
    assert.equal(drop.listenerCount('change'), 0);
  } finally { fs.rmSync(dir, { recursive: true, force: true }); }
});

test('shelf refuses foreign frames, removed drops and replaced or symlinked local files', () => {
  const dir = tmp();
  const drop = new EventEmitter();
  const file = path.join(dir, 'file.txt'), link = path.join(dir, 'link.txt');
  fs.writeFileSync(file, 'original'); fs.symlinkSync(file, link);
  drop.recent = [{ path: file, ok: true }, { path: link, ok: true }];
  const shelf = new FileShelf({ drop });
  const oldDrags = dragged.length, oldReveals = revealed.length;
  try {
    shelf.show({ view: 'recent' });
    const state = read(shelf);
    assert.equal(state.items.length, 1, 'symlink is omitted');
    const id = state.items[0].id;
    const ownEvent = event(shelf);
    ipcMain.emit('file-shelf:drag', { ...ownEvent, senderFrame: {} }, id);
    ipcMain.emit('file-shelf:reveal', { ...ownEvent, sender: { id: -1 } }, id);
    fs.renameSync(file, path.join(dir, 'old.txt')); fs.writeFileSync(file, 'replacement');
    ipcMain.emit('file-shelf:drag', ownEvent, id);
    const replacementId = read(shelf).items[0].id;
    drop.recent = [];
    ipcMain.emit('file-shelf:reveal', ownEvent, replacementId);
    assert.equal(dragged.length, oldDrags);
    assert.equal(revealed.length, oldReveals);
    assert.equal(read(shelf).items.length, 0, 'revoked item leaves the shelf');
    shelf.shutdown();
    assert.equal(handles.get('file-shelf:read')(ownEvent), null, 'retired contents cannot read shelf');
  } finally { shelf.shutdown(); fs.rmSync(dir, { recursive: true, force: true }); }
});

test('a received drop stops matching when its file birth time changes', (t) => {
  const dir = tmp();
  const file = path.join(dir, 'drop.txt');
  fs.writeFileSync(file, 'a drop');
  const drop = new EventEmitter();
  drop.recent = [{ path: file, ok: true }];
  const shelf = new FileShelf({ drop });
  const counts = [dragged.length, revealed.length];
  const stat = fs.lstatSync;
  try {
    shelf.show({ view: 'recent' });
    const { id } = read(shelf).items[0];
    t.mock.method(fs, 'lstatSync', (p, ...args) => {
      const result = stat(p, ...args);
      if (p === file) result.birthtimeMs += 1000;
      return result;
    });
    ipcMain.emit('file-shelf:drag', event(shelf), id);
    ipcMain.emit('file-shelf:reveal', event(shelf), id);
    assert.deepEqual([dragged.length, revealed.length], counts);
  } finally {
    t.mock.restoreAll();
    shelf.shutdown();
    fs.rmSync(dir, { recursive: true, force: true });
  }
});

// Windows and Linux: the received list is a function (fileRecv.recent), refreshed by the app's state pushes.
test('shelf takes a recent() list without a change event', () => {
  const dir = tmp();
  const list = [];
  try {
    const shelf = new FileShelf({ recent: () => list });
    shelf.show({ view: 'recent' });
    assert.deepEqual(read(shelf).items, []);
    const file = path.join(dir, 'a.txt'); fs.writeFileSync(file, 'x');
    list.unshift({ path: file, ok: true }, { path: 42, ok: true }, null);
    shelf.refresh();
    assert.equal(read(shelf).items.length, 1);
    shelf.shutdown();
  } finally { fs.rmSync(dir, { recursive: true, force: true }); }
});

// The shelf's own files: dropped (paths), in order, removed; sent only to a device there; let go when it closes.
test('shelf keeps dropped files while open, sends them to a connected device only', () => {
  const dir = tmp();
  const a = path.join(dir, 'a.txt'), b = path.join(dir, 'b.txt');
  fs.writeFileSync(a, 'a'); fs.writeFileSync(b, 'bb');
  const sent = [];
  try {
    const shelf = new FileShelf({ recent: () => [], devices: () => [{ id: 'pc', name: 'PC', os: 'win' }], send: (paths, id) => sent.push({ paths, id }) });
    shelf.show({ view: 'shelf' });
    ipcMain.emit('file-shelf:add', event(shelf), [a, b, '/nope/missing', 'relative.txt']);
    let state = read(shelf);
    assert.equal(state.view, 'shelf');
    assert.deepEqual(state.items.map(i => i.name), ['b.txt', 'a.txt'], 'newest first, missing and relative paths left out');
    assert.deepEqual(state.devices, [{ id: 'pc', name: 'PC', os: 'win', portal: false }], 'no portal without a way there');
    ipcMain.emit('file-shelf:send', event(shelf), { device: 'elsewhere', ids: [state.items[0].id], paths: [] });
    assert.deepEqual(sent, [], 'not a device that is connected');
    ipcMain.emit('file-shelf:send', event(shelf), { device: 'pc', ids: [state.items[0].id], paths: [] });
    assert.deepEqual(sent, [{ paths: [b], id: 'pc' }]);
    ipcMain.emit('file-shelf:drag', event(shelf), state.items.map(i => i.id));
    assert.deepEqual(dragged.at(-1).files, [b, a], 'All drags every file');
    ipcMain.emit('file-shelf:remove', event(shelf), [state.items[0].id]);
    assert.deepEqual(read(shelf).items.map(i => i.name), ['a.txt']);
    fs.rmSync(a);
    assert.deepEqual(read(shelf).items, [], 'a file deleted elsewhere leaves the shelf');
    fs.writeFileSync(a, 'a');
    ipcMain.emit('file-shelf:add', event(shelf), [a, b]);
    assert.equal(read(shelf).items.length, 2);
    ipcMain.emit('file-shelf:close', event(shelf));
    shelf.show({ view: 'shelf' });
    assert.deepEqual(read(shelf).items, [], 'closed: its files let go');
    assert.ok(fs.existsSync(a) && fs.existsSync(b), 'the files themselves stay');
    shelf.shutdown();
  } finally { fs.rmSync(dir, { recursive: true, force: true }); }
});

// A shake opens a new shelf each time; the tray and Files show the last one open, or a new one.
test('shelves: a shake opens another, the rest show the last one open', () => {
  const dir = tmp();
  const a = path.join(dir, 'a.txt'); fs.writeFileSync(a, 'a');
  try {
    const shelves = new FileShelves({ recent: () => [] });
    shelves.show({ view: 'shelf' });
    const first = shelves.open[0];
    ipcMain.emit('file-shelf:add', event(first), [a]);
    shelves.show({ view: 'shelf' });
    assert.equal(shelves.open.length, 1, 'shown again, not another');
    shelves.show({ view: 'shelf', at: { x: 50, y: 50 } });
    assert.equal(shelves.open.length, 2, 'a shake: a new shelf');
    assert.deepEqual(read(shelves.open[1]).items, [], 'empty');
    assert.equal(read(first).items.length, 1, 'the first keeps its own');
    ipcMain.emit('file-shelf:close', event(shelves.open[1]));
    assert.deepEqual(shelves.open, [first]);
    shelves.shutdown();
    assert.deepEqual(shelves.open, []);
  } finally { fs.rmSync(dir, { recursive: true, force: true }); }
});