Skip to content

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

12.8 KB
Raw Blame
const assert = require("node:assert/strict");
const test = require("node:test");
const React = require("react");
const { renderToStaticMarkup } = require("react-dom/server");
const { createRendererServer, installBrowserGlobals } = require("../lib/rendererTestHarness");

const noop = () => {};
const asyncNoop = async () => {};

async function createOnboardingRenderer(t, platform = "linux") {
  installBrowserGlobals(t, {
    window: { electronAPI: { getPlatform: () => platform } },
  });
  return createRendererServer(t, {
    cachePrefix: "openwhispr-onboarding-compatibility-",
    noExternal: ["react-i18next"],
    mockModules: {
      "react-i18next": `
        export function useTranslation() {
          return { t(key) { return key; } };
        }
      `,
      "onboarding-hero-dither.webp": `export default "hero-light.webp";`,
      "onboarding-hero-dither-dark.webp": `export default "hero-dark.webp";`,
      "onboarding-bg-light.svg": `export default "background-light.svg";`,
      "onboarding-bg-dark.svg": `export default "background-dark.svg";`,
      "onboarding-permission-microphone.webp": `export default "microphone.webp";`,
      "onboarding-permission-accessibility.webp": `export default "accessibility.webp";`,
      "onboarding-permission-system-audio.webp": `export default "system-audio.webp";`,
      "/utils/platform": `
        export function getPlatform() { return "${platform}"; }
        export function getCachedPlatform() { return "${platform}"; }
      `,
      "/stores/settingsStore": `
        export function useSettingsStore() { return {}; }
        useSettingsStore.getState = () => ({});
      `,
      "/ui/ProviderIcon": `
        import React from "react";
        export function ProviderIcon() { return React.createElement("span"); }
      `,
    },
  });
}

function permissions(overrides = {}) {
  return {
    micPermissionGranted: false,
    accessibilityPermissionGranted: false,
    micPermissionError: null,
    pasteToolsInfo: null,
    isCheckingPasteTools: false,
    accessibilityTroubleshooting: false,
    requestMicPermission: asyncNoop,
    requestAccessibilityPermission: asyncNoop,
    checkPasteToolsAvailability: async () => null,
    openMicPrivacySettings: asyncNoop,
    openSoundInputSettings: asyncNoop,
    setMicPermissionGranted: noop,
    setAccessibilityPermissionGranted: noop,
    ...overrides,
  };
}

// The action row carries mt-auto, so anything rendered after it rides down on
// that auto margin and lands under the buttons — below the fold on the compact
// frame. Contextual guidance has to precede it in the DOM, and so in tab order.
function assertGuidancePrecedesActions(markup, guidance) {
  assert.ok(
    markup.indexOf(guidance) < markup.indexOf("common.continue"),
    `${guidance} should render before the action row`
  );
}

const systemAudio = {
  granted: false,
  mode: "portal",
  supportsOnboardingGrant: false,
  request: async () => false,
};

const screenContext = {
  enabled: false,
  granted: false,
  needsRelaunch: false,
  request: async () => false,
};

test("Linux onboarding exposes labelled minimize, maximize, and close controls in both modes", async (t) => {
  const vite = await createOnboardingRenderer(t);
  const { default: OnboardingShell } = await vite.ssrLoadModule(
    "/components/onboarding/OnboardingShell.tsx"
  );

  for (const compact of [true, false]) {
    const mode = compact ? "compact" : "expanded";
    const markup = renderToStaticMarkup(
      React.createElement(OnboardingShell, { compact }, React.createElement("div"))
    );

    for (const control of ["minimize", "maximize", "close"]) {
      assert.match(
        markup,
        new RegExp(`title="windowControls\\.${control}"`),
        `${mode} ${control} title`
      );
      assert.match(
        markup,
        new RegExp(`aria-label="windowControls\\.${control}"`),
        `${mode} ${control} aria-label`
      );
    }
  }
});

test("a denied microphone exposes the existing Linux settings recovery", async (t) => {
  const vite = await createOnboardingRenderer(t);
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const markup = renderToStaticMarkup(
    React.createElement(CompactPermissionsStep, {
      permissions: permissions({ micPermissionError: "Microphone blocked by the OS" }),
      systemAudio,
      onContinue: noop,
    })
  );

  assert.match(markup, /Microphone blocked by the OS/);
  assert.match(markup, />hooks.permissions.warning.soundLabel<\/button>/);
  assertGuidancePrecedesActions(markup, "Microphone blocked by the OS");
});

test("Linux onboarding shows paste-tool installation and recheck guidance", async (t) => {
  const vite = await createOnboardingRenderer(t);
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const markup = renderToStaticMarkup(
    React.createElement(CompactPermissionsStep, {
      permissions: permissions({
        pasteToolsInfo: {
          platform: "linux",
          available: false,
          method: null,
          requiresPermission: false,
          isWayland: false,
          isWlroots: false,
          hasWtype: false,
          recommendedInstall: "xdotool",
        },
      }),
      systemAudio,
      onContinue: noop,
    })
  );

  assert.match(markup, /sudo apt install xdotool/);
  assert.match(markup, />pasteToolsInfo.recheck<\/button>/);
  assertGuidancePrecedesActions(markup, "sudo apt install xdotool");
});

// COSMIC pastes into other apps without wtype, but terminals like Warp only take
// the Ctrl+Shift+V that wtype sends there.
test("COSMIC onboarding recommends wtype for terminals until it is installed", async (t) => {
  const vite = await createOnboardingRenderer(t);
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const render = (hasWtype, available = true) =>
    renderToStaticMarkup(
      React.createElement(CompactPermissionsStep, {
        permissions: permissions({
          pasteToolsInfo: {
            platform: "linux",
            available,
            method: available ? "uinput" : null,
            requiresPermission: false,
            isWayland: true,
            isWlroots: false,
            isCosmic: true,
            hasWtype,
            recommendedInstall: available ? undefined : "xdotool",
          },
        }),
        systemAudio,
        onContinue: noop,
      })
    );

  const missing = render(false);
  assert.match(missing, /pasteToolsInfo\.wtypeCosmicTerminalsDescription/);
  assert.match(missing, /sudo apt install wtype/);
  assert.doesNotMatch(missing, /pasteToolsInfo\.withoutToolPrefix/);
  assertGuidancePrecedesActions(missing, "sudo apt install wtype");

  assert.doesNotMatch(render(true), /sudo apt install wtype/);

  // With nothing pasting at all, wtype alone would only fix terminals.
  const unavailable = render(false, false);
  assert.match(unavailable, /sudo apt install xdotool/);
  assert.doesNotMatch(unavailable, /wtypeCosmicTerminalsDescription/);
});

test("permissions offers Back ahead of Continue and gates Continue on microphone access", async (t) => {
  const vite = await createOnboardingRenderer(t, "darwin");
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const render = (micPermissionGranted, onBack) =>
    renderToStaticMarkup(
      React.createElement(CompactPermissionsStep, {
        permissions: permissions({ micPermissionGranted }),
        systemAudio,
        screenContext,
        onBack,
        onContinue: noop,
      })
    );

  const blocked = render(false, noop);
  assert.doesNotMatch(blocked, /common\.logout/);
  assert.ok(
    blocked.indexOf("common.back") < blocked.indexOf("common.continue"),
    "Back sits ahead of Continue"
  );
  // Continue is part of the step now rather than an overlay above it, so it
  // follows the permission rows in the DOM, and so in tab order.
  assert.ok(
    blocked.indexOf("onboarding.permissions.microphoneTitle") < blocked.indexOf("common.continue"),
    "Continue should render after the permission rows"
  );
  assert.match(blocked, /<button[^>]*\bdisabled=""[^>]*>common\.continue<\/button>/);

  const ready = render(true, noop);
  assert.match(ready, /<button[^>]*>common\.continue<\/button>/);
  assert.doesNotMatch(ready, /\bdisabled=""[^>]*>common\.continue/);

  // With nothing to return to (a resumed session with no history), Back is gone.
  assert.doesNotMatch(render(true, undefined), /common\.back/);
});
test("macOS onboarding offers optional Screen Context setup", async (t) => {
  const vite = await createOnboardingRenderer(t, "darwin");
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const markup = renderToStaticMarkup(
    React.createElement(CompactPermissionsStep, {
      permissions: permissions(),
      systemAudio,
      screenContext,
      onContinue: noop,
    })
  );

  assert.match(markup, /dictationAgent\.screenContext\.title/);
  assert.match(markup, /data-icon="laptop"/);
  assert.doesNotMatch(markup, /onboarding\.permissions\.recommended/);
  assert.equal(
    markup.match(/onboarding\.permissions\.optional/g)?.length,
    2,
    "System Audio and Screen Context should be labelled Optional on macOS"
  );
  assert.doesNotMatch(markup, /dictationAgent\.screenContext\.relaunchHint/);
});

test("macOS onboarding shows the Screen Context relaunch guidance when needed", async (t) => {
  const vite = await createOnboardingRenderer(t, "darwin");
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const markup = renderToStaticMarkup(
    React.createElement(CompactPermissionsStep, {
      permissions: permissions(),
      systemAudio,
      screenContext: {
        ...screenContext,
        enabled: true,
        granted: true,
        needsRelaunch: true,
      },
      onContinue: noop,
    })
  );

  assert.match(markup, /dictationAgent\.screenContext\.relaunchHint/);
  assert.match(markup, />onboarding\.rehaul\.permissions\.enabled<\/button>/);
  assertGuidancePrecedesActions(markup, "dictationAgent.screenContext.relaunchHint");
});

test("macOS onboarding omits Screen Context when the flow does not offer it", async (t) => {
  const vite = await createOnboardingRenderer(t, "darwin");
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const markup = renderToStaticMarkup(
    React.createElement(CompactPermissionsStep, {
      permissions: permissions(),
      systemAudio,
      onContinue: noop,
    })
  );

  assert.doesNotMatch(markup, /dictationAgent\.screenContext\.title/);
});

test("Windows onboarding offers Screen Context as a permissionless opt-in", async (t) => {
  const vite = await createOnboardingRenderer(t, "win32");
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const markup = renderToStaticMarkup(
    React.createElement(CompactPermissionsStep, {
      permissions: permissions(),
      systemAudio,
      screenContext: {
        ...screenContext,
        enabled: true,
        granted: true,
      },
      onContinue: noop,
    })
  );

  assert.match(markup, /dictationAgent\.screenContext\.title/);
  assert.match(markup, /data-icon="laptop"/);
  assert.equal(
    markup.match(/onboarding\.permissions\.optional/g)?.length,
    1,
    "Screen Context should be labelled Optional on Windows"
  );
  assert.match(markup, />onboarding\.rehaul\.permissions\.enabled<\/button>/);
  assert.doesNotMatch(markup, /dictationAgent\.screenContext\.relaunchHint/);
});

test("Linux onboarding does not show Screen Context", async (t) => {
  const vite = await createOnboardingRenderer(t);
  const { default: CompactPermissionsStep } = await vite.ssrLoadModule(
    "/components/onboarding/CompactPermissionsStep.tsx"
  );

  const markup = renderToStaticMarkup(
    React.createElement(CompactPermissionsStep, {
      permissions: permissions(),
      systemAudio,
      screenContext,
      onContinue: noop,
    })
  );

  assert.doesNotMatch(markup, /dictationAgent\.screenContext\.title/);
});

test("provider setup stages mark dictation complete before Assistant", async (t) => {
  const vite = await createOnboardingRenderer(t);
  const { SetupStageStepper } = await vite.ssrLoadModule(
    "/components/onboarding/ProviderSetupStep.tsx"
  );

  for (const stepId of ["byok-assistant", "local-assistant"]) {
    const markup = renderToStaticMarkup(React.createElement(SetupStageStepper, { stepId }));

    assert.match(markup, /data-icon="circle-check"/);
    assert.doesNotMatch(markup, /data-icon="audio-lines"/);
  }
});