Files
kandev/apps/web/e2e/tests/chat/mobile-voice-mode.spec.ts
T
João Salavisa aea266ee71 fix(web): fix hold-to-talk on mobile and add coarse-pointer toggle fallback (#1231)
* fix(web): make voice-mode hold gesture survive touch and add mobile coverage

Mobile users with the chat composer's mic button in hold-to-talk mode lost
their recording the moment a finger crept off the 28px target, because
`buildHoldHandlers` aborted on `pointerleave`/`pointercancel` without ever
claiming the pointer.

- `buildHoldHandlers` now claims the pointer on `pointerdown` via
  `setPointerCapture` and releases it on `pointerup`/`pointercancel`. The
  `pointerleave` handler is dropped — capture keeps the gesture alive across
  small finger movement, OS gesture handoff, and soft-keyboard reflows.
- `EnabledVoiceInputButton` resolves an `effectiveMode` via a coarse-pointer
  check (`matchMedia("(pointer: coarse)")`); when the stored preference is
  `hold` and the device is touch-first, the mic falls back to toggle so a
  bad stored preference can't trap users in a broken UX. Persisted
  `voiceMode.mode` is left untouched so docking a keyboard restores the
  user's choice without a save.
- The button bumps to 40×40 on coarse-pointer devices to meet the 44pt touch-
  target floor without disturbing the desktop submit/voice pair.
- The unsupported fallback path (`UnsupportedVoiceButton`) is unchanged so
  LAN-HTTP mobile users still get the HTTPS explanation toast.

Adds `voice-input-button.test.tsx` covering capture/release on pointer
events, the absence of `pointerleave` regression, the coarse-pointer toggle
override, and the touch-target class swap.

Adds `mobile-voice-mode.spec.ts` (Playwright mobile-chrome / Pixel 5)
verifying the Voice Mode settings page is reachable from the mobile sidebar
trigger and that the mic button mounts with `data-effective-mode="toggle"`
on the mobile chat composer.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(web): drop unused testPage param from mobile-voice-mode seedTask helper

The helper only calls apiClient and does not touch the Playwright Page
object — the parameter was carried over from the neighbour spec's pattern
where the helper also navigates. Removing it also drops the now-unused
`Page` type import.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* refactor(web): drop redundant Element type cast from safePointerRelease

`Element.hasPointerCapture` and `releasePointerCapture` are already declared
in lib.dom.d.ts, so the `target as Element & { hasPointerCapture?: ... }`
casts were over-defensive. Replace with a direct `typeof` runtime check,
which still guards against the headless DOMs that strip these methods.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(web): assert coarse-pointer touch-target class in mobile voice spec

The previous assertion (`data-effective-mode === "toggle"`) would have
passed even if the coarse-pointer override were reverted, since the default
stored `voiceMode.mode` is already `"toggle"`. Add a check on the `h-10
w-10` class swap — that class pair is only emitted when
`useIsCoarsePointer()` returned true, so the test now genuinely exercises
the override on the e2e surface.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(web): extend voice-input-button unit coverage for toggle paths

- Toggle-mode + fine-pointer: pointerdown must not start recording; click
  drives the toggle. Guards against an accidental future change that wires
  hold pointer handlers when stored mode is "toggle".
- Recording state + click: stop() fires (not start()), exercising the
  buildToggleHandler branch the original suite skipped.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

* test(web): seed hold mode in E2E and fix misleading test title

- Add `voice_mode` field to `saveUserSettings` in api-client.ts so E2E
  tests can PATCH the user's stored activation mode via the API.
- Seed `mode: "hold"` before the mic-button assertion so
  `data-mode="hold"` + `data-effective-mode="toggle"` together prove the
  coarse-pointer override fired (not that the default was already toggle).
- Rename settings-page test to drop the "from mobile sidebar" claim —
  the test navigates directly to the URL; sidebar existence is still
  asserted as a regression guard.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-01 18:45:50 +01:00

91 lines
4.1 KiB
TypeScript

import { test, expect } from "../../fixtures/test-base";
import type { SeedData } from "../../fixtures/test-base";
import type { ApiClient } from "../../helpers/api-client";
import { SessionPage } from "../../pages/session-page";
// Mobile (Pixel 5) regression coverage for the voice-mode entry point and
// the chat composer's mic button. Filename matches /mobile-.*\.spec\.ts/ so
// the `mobile-chrome` Playwright project picks it up.
//
// Two assertions:
// 1. The Voice Mode settings page renders all cards at mobile width when
// navigated to directly — guards against the "setting missing" symptom.
// 2. The mic button mounts on the mobile chat composer with the coarse-pointer
// override: a user with stored `voiceMode.mode = "hold"` gets toggle
// behaviour (data-effective-mode = "toggle") and a 40px touch target.
async function seedTask(apiClient: ApiClient, seedData: SeedData, title: string) {
return apiClient.createTaskWithAgent(seedData.workspaceId, title, seedData.agentProfileId, {
description: "/e2e:simple-message",
workflow_id: seedData.workflowId,
workflow_step_id: seedData.startStepId,
repository_ids: [seedData.repositoryId],
});
}
test.describe("Mobile voice mode", () => {
test.describe.configure({ retries: 1, timeout: 60_000 });
test("Voice Mode settings page renders every card at mobile width", async ({ testPage }) => {
await testPage.goto("/settings/voice-mode");
// Page header confirms route resolved to the right shell. The page title
// is rendered by SettingsSection — match the heading text exactly so we
// don't pick up the sidebar entry that shares the label.
await expect(testPage.getByRole("heading", { name: "Voice Mode", exact: true })).toBeVisible({
timeout: 15_000,
});
// The five cards rendered by `voice-mode-settings.tsx` at mobile width.
await expect(testPage.getByText("Enable Voice Input")).toBeVisible();
await expect(testPage.getByText("Transcription Engine")).toBeVisible();
await expect(testPage.getByText("Behavior")).toBeVisible();
await expect(testPage.getByText("Whisper Web Model")).toBeVisible();
await expect(testPage.getByText(/Shortcut$/)).toBeVisible();
// The mobile sidebar trigger (md:hidden) lives in the topbar; assert it
// exists so we know the sidebar entry chain that exposed this page is
// not silently removed by a future layout change.
const sidebarTrigger = testPage.locator('button[data-sidebar="trigger"]');
await expect(sidebarTrigger).toBeVisible();
});
test("voice input mic button mounts on the mobile chat composer with the coarse-pointer touch target and effective toggle mode", async ({
testPage,
apiClient,
seedData,
}) => {
// Seed hold mode so the coarse-pointer override assertion is non-vacuous:
// data-mode="hold" (stored pref) vs data-effective-mode="toggle" (override)
// proves the coarse-pointer branch fired, not that the default was toggle.
await apiClient.saveUserSettings({
voice_mode: {
enabled: true,
engine: "auto",
language: "auto",
mode: "hold",
auto_send: false,
whisper_web_model: "base",
},
});
const task = await seedTask(apiClient, seedData, "Mobile voice button");
await testPage.goto(`/t/${task.id}`);
const session = new SessionPage(testPage);
await session.waitForLoad();
const micButton = testPage.getByTestId("voice-input-button");
await expect(micButton).toBeVisible({ timeout: 15_000 });
// Pixel 5 advertises `(pointer: coarse)`. Three signals that the coarse-
// pointer override fired:
// - data-mode="hold" (the stored user preference)
// - data-effective-mode="toggle" (the override: hold→toggle on coarse)
// - h-10 w-10 (40px touch target, only set when isCoarsePointer=true)
await expect(micButton).toHaveAttribute("data-mode", "hold");
await expect(micButton).toHaveAttribute("data-effective-mode", "toggle");
await expect(micButton).toHaveClass(/(^|\s)h-10(\s|$)/);
await expect(micButton).toHaveClass(/(^|\s)w-10(\s|$)/);
});
});