Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

7.0 KB
Raw Blame
const test = require("node:test");
const assert = require("node:assert/strict");
const Module = require("node:module");

const handlersModulePath = require.resolve("../../src/helpers/ipcHandlers");
const originalLoad = Module._load;
const handlers = new Map();

const electronStub = {
  app: {
    getPath: () => "/tmp",
    getName: () => "test",
    getVersion: () => "0.0.0",
    isPackaged: false,
    on: () => {},
    requestSingleInstanceLock: () => true,
  },
  ipcMain: {
    handle: (channel, handler) => handlers.set(channel, handler),
    on: () => {},
    removeHandler: () => {},
  },
  net: { fetch: async () => ({ ok: true, status: 200, json: async () => ({}) }) },
  BrowserWindow: class BrowserWindow {
    static getAllWindows() {
      return [];
    }

    static fromWebContents() {
      return null;
    }
  },
  shell: {},
  dialog: {},
  screen: { getPrimaryDisplay: () => ({ workAreaSize: { width: 0, height: 0 } }) },
  systemPreferences: { getMediaAccessStatus: () => "granted" },
  session: { fromPartition: () => ({}) },
  clipboard: {},
  nativeImage: {},
  globalShortcut: {},
  utilityProcess: {},
  MessageChannelMain: class {},
};

Module._load = function loadWithMocks(request, parent, isMain) {
  if (request === "electron") return electronStub;
  if (
    parent?.filename === handlersModulePath &&
    (request.startsWith("./") || request.startsWith("../"))
  ) {
    return anything();
  }
  return originalLoad.call(this, request, parent, isMain);
};

function anything() {
  return new Proxy(function () {}, {
    get: (_target, property) => {
      if (property === Symbol.toPrimitive || property === "toString") return () => "";
      if (property === "then") return undefined;
      return anything();
    },
    apply: () => anything(),
  });
}

// A real HotkeyManager answers for Hold; only its backend and the listener
// probe are faked.
const HotkeyManager = require("../../src/helpers/hotkeyManager");

const inputDenied = () => ({ available: false, reason: "input_access_denied" });
let hotkeyManager;

test.before(() => {
  delete require.cache[handlersModulePath];
  const IPCHandlers = require(handlersModulePath);
  const target = {
    windowManager: {
      get hotkeyManager() {
        return hotkeyManager;
      },
      isUsingGnomeHotkeys: () => hotkeyManager.isUsingGnome(),
      isUsingHyprlandHotkeys: () => hotkeyManager.isUsingHyprland(),
      isUsingKDEHotkeys: () => hotkeyManager.isUsingKDE(),
      isUsingNativeShortcutHotkeys: () => hotkeyManager.isUsingNativeShortcut(),
      getActivationMode: () => "tap",
    },
    environmentManager: { getActivationMode: () => "push" },
  };
  IPCHandlers.prototype.setupHandlers.call(
    new Proxy(target, {
      get: (value, property) => (property in value ? value[property] : anything()),
    })
  );
});

test.after(() => {
  Module._load = originalLoad;
});

// The handler decides everything before its first await, so the patched
// platform covers the whole answer.
function hotkeyModeInfo(backend, platform = "linux", hotkey = "F8") {
  hotkeyManager = Object.assign(new HotkeyManager(), {
    isInitialized: true,
    nativeListenerProbe: inputDenied,
    ...backend,
  });
  const original = Object.getOwnPropertyDescriptor(process, "platform");
  Object.defineProperty(process, "platform", { value: platform, configurable: true });
  try {
    return handlers.get("get-hotkey-mode-info")({}, hotkey);
  } finally {
    Object.defineProperty(process, "platform", original);
  }
}

// GNOME (portal), KDE and Hyprland deliver press and release themselves, so a
// user without /dev/input access can still hold, and the setup box would send
// them to run sudo for nothing.
const DESKTOP_BACKENDS = {
  GNOME: { useGnome: true, gnomeManager: { supportsPushToTalk: () => true } },
  KDE: { useKDE: true },
  Hyprland: { useHyprland: true },
};

for (const [desktop, backend] of Object.entries(DESKTOP_BACKENDS)) {
  test(`${desktop} holds without the evdev listener, so no input setup is shown`, async () => {
    const info = await hotkeyModeInfo(backend);

    assert.equal(info.supportsPushToTalk, true);
    assert.equal(info.linuxInputAccessDenied, false);
  });
}

// Desktop backends can't hold a modifier-only chord, so the reason must ask for
// a regular key rather than blame a conflict (#1977).
for (const [desktop, backend] of Object.entries(DESKTOP_BACKENDS)) {
  test(`${desktop} says Hold needs a regular key for a modifier-only hotkey`, async () => {
    const info = await hotkeyModeInfo(backend, "linux", "Control+Super");

    assert.equal(info.supportsPushToTalk, false);
    assert.match(info.pushToTalkUnavailableReason, /regular key/);
    assert.doesNotMatch(info.pushToTalkUnavailableReason, /reserved/);
  });
}

// GNOME holds only through its GlobalShortcuts portal, which has nothing to do
// with OpenWhispr's own key listener.
test("GNOME without the shortcuts portal names the portal, not the key listener", async () => {
  const info = await hotkeyModeInfo({
    useGnome: true,
    gnomeManager: { supportsPushToTalk: () => false },
  });

  assert.equal(info.supportsPushToTalk, false);
  assert.match(info.pushToTalkUnavailableReason, /GNOME 48/);
  assert.doesNotMatch(info.pushToTalkUnavailableReason, /listener/);
});

test("GNOME without the portal names the portal even for a modifier-only hotkey", async () => {
  const info = await hotkeyModeInfo(
    { useGnome: true, gnomeManager: { supportsPushToTalk: () => false } },
    "linux",
    "Control+Super"
  );

  assert.match(info.pushToTalkUnavailableReason, /GNOME 48/);
  assert.doesNotMatch(info.pushToTalkUnavailableReason, /regular key/);
});

test("without a desktop backend, denied input access reaches the renderer", async () => {
  const info = await hotkeyModeInfo({});

  assert.equal(info.supportsPushToTalk, false);
  assert.equal(info.linuxInputAccessDenied, true);
  assert.match(info.pushToTalkUnavailableReason, /usermod/);
});

// Windows holds only through windows-key-listener.exe, which a build can lack
// (#2005). The Linux input-access setup box never applies there.
test("Windows without its key listener reports Hold unavailable", async () => {
  const info = await hotkeyModeInfo(
    { nativeListenerProbe: () => ({ available: false, reason: "binary_missing" }) },
    "win32"
  );

  assert.equal(info.supportsPushToTalk, false);
  assert.equal(info.pushToTalkUnavailableReason, "OpenWhispr's key listener isn't available.");
  assert.equal(info.linuxInputAccessDenied, false);
});

test("Windows with its key listener still offers Hold", async () => {
  const info = await hotkeyModeInfo({ nativeListenerProbe: () => ({ available: true }) }, "win32");

  assert.equal(info.supportsPushToTalk, true);
  assert.equal(info.pushToTalkUnavailableReason, null);
});

// Startup may fall back to Tap for the session while keeping a saved Hold for
// the next launch. A window that syncs afterwards must show the mode in effect.
test("renderers read the activation mode in effect, not the saved preference", async () => {
  assert.equal(await handlers.get("get-activation-mode")({}), "tap");
});