Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

6.7 KB
Raw Blame
import { expect, type Page, test } from '@playwright/test';

import {
  bootAuthenticatedPage,
  callCoreRpc,
  dismissWalkthroughIfPresent,
} from '../helpers/core-rpc';

async function openChat(page: Page): Promise<void> {
  await bootAuthenticatedPage(page, 'pw-voice-mode', '/chat');
  await page.goto('/#/chat');
  await page.evaluate(() => {
    localStorage.setItem('openhuman:walkthrough_completed', 'true');
    localStorage.removeItem('openhuman:walkthrough_pending');
  });
  await dismissWalkthroughIfPresent(page);
  const skipButton = page.getByRole('button', { name: /Skip|Skip tour/i });
  if (
    await skipButton
      .first()
      .isVisible()
      .catch(() => false)
  ) {
    await skipButton.first().click({ force: true });
    await expect(skipButton.first()).toBeHidden();
  }
  await expect(page.getByTestId('chat-message-input')).toBeVisible();
}

async function installGetUserMediaError(page: Page, name: string): Promise<void> {
  await page.evaluate(errorName => {
    const mediaDevices = navigator.mediaDevices as MediaDevices & {
      __e2e_original_getUserMedia?: MediaDevices['getUserMedia'];
    };
    if (!mediaDevices.__e2e_original_getUserMedia) {
      mediaDevices.__e2e_original_getUserMedia = mediaDevices.getUserMedia.bind(mediaDevices);
    }
    Object.defineProperty(mediaDevices, 'getUserMedia', {
      configurable: true,
      value: () =>
        Promise.reject(new DOMException(`[Playwright voice mock] ${errorName}`, errorName)),
    });
  }, name);
}

async function restoreGetUserMedia(page: Page): Promise<void> {
  if (page.isClosed()) return;
  await page.evaluate(() => {
    const mediaDevices = navigator.mediaDevices as MediaDevices & {
      __e2e_original_getUserMedia?: MediaDevices['getUserMedia'];
    };
    if (mediaDevices.__e2e_original_getUserMedia) {
      Object.defineProperty(mediaDevices, 'getUserMedia', {
        configurable: true,
        value: mediaDevices.__e2e_original_getUserMedia,
      });
      delete mediaDevices.__e2e_original_getUserMedia;
    }
  });
}

async function switchChatIntoMicComposer(page: Page): Promise<void> {
  await dismissWalkthroughIfPresent(page);
  const voiceModeButton = page.getByRole('button', { name: 'Voice mode' });
  await expect(voiceModeButton).toBeEnabled();
  await voiceModeButton.click();
  await expect(page.getByText(/Tap and speak|Waiting for agent/i)).toBeVisible();
  await expect(page.getByRole('button', { name: 'Switch to text' })).toBeVisible();
}

test.describe('Voice mode integration', () => {
  test.beforeEach(async ({ page }) => {
    await openChat(page);
  });

  test('chat mic button switches into MicComposer and can return to text mode', async ({
    page,
  }) => {
    await switchChatIntoMicComposer(page);

    await page.getByRole('button', { name: 'Switch to text' }).click();
    await expect(page.getByTestId('chat-message-input')).toBeVisible();
    await expect(page.getByTestId('chat-message-input')).toBeVisible();
  });

  test('Human route shows the shipped live voice controls', async ({ page }) => {
    await page.goto('/#/human');
    await expect(page).toHaveURL(/#\/human$/);
    await expect(page.getByTestId('live-voice-controls')).toBeVisible();
    await expect(page.getByTestId('live-voice-start')).toBeVisible();
  });

  test('mic failure never inserts a beep placeholder as a user message', async ({ page }) => {
    try {
      await switchChatIntoMicComposer(page);
      await installGetUserMediaError(page, 'NotAllowedError');
      const viewport = page.locator('[data-slot="aui_thread-viewport"]');
      await expect(viewport).toBeVisible();
      const userMessages = viewport.locator('[data-slot="aui_user-message-root"]');
      const userMessageCount = await userMessages.count();
      await page.getByRole('button', { name: 'Start recording' }).click();
      await expect(page.locator('[data-chat-send-error-code="voice_transcription"]')).toBeVisible();

      await expect(userMessages).toHaveCount(userMessageCount);
      const messageText = (await userMessages.allTextContents()).join(' ');
      expect(messageText).not.toMatch(/beep/i);
    } finally {
      await restoreGetUserMedia(page);
    }
  });

  test('permission-denied getUserMedia shows a specific voice-transcription error', async ({
    page,
  }) => {
    try {
      await switchChatIntoMicComposer(page);
      await installGetUserMediaError(page, 'NotAllowedError');
      await page.getByRole('button', { name: 'Start recording' }).click();

      const errorBanner = page.locator('[data-chat-send-error-code="voice_transcription"]');
      await expect(errorBanner).toBeVisible();
      await expect(errorBanner).toContainText(/permission|denied|microphone/i);
      await expect(errorBanner).not.toContainText(/something went wrong/i);
    } finally {
      await restoreGetUserMedia(page);
    }
  });

  test('missing-device getUserMedia shows a specific unavailable-device error', async ({
    page,
  }) => {
    try {
      await switchChatIntoMicComposer(page);
      await installGetUserMediaError(page, 'NotFoundError');
      await page.getByRole('button', { name: 'Start recording' }).click();

      const errorBanner = page.locator('[data-chat-send-error-code="voice_transcription"]');
      await expect(errorBanner).toBeVisible();
      await expect(errorBanner).toContainText(/unavailable|device|microphone|not found/i);
      await expect(errorBanner).not.toContainText(/something went wrong/i);
    } finally {
      await restoreGetUserMedia(page);
    }
  });
});

test.describe('Voice mode - hosted STT contract (voice_status RPC)', () => {
  test.beforeEach(async ({ page }) => {
    await bootAuthenticatedPage(page, 'pw-voice-mode-status', '/home');
  });

  test('voice_status RPC returns a well-formed response', async () => {
    const status = await callCoreRpc<unknown>('openhuman.voice_status', {});
    const root = (status ?? {}) as Record<string, unknown>;
    const payload =
      root && typeof root === 'object' && 'result' in root
        ? (root.result as Record<string, unknown>)
        : root;

    expect(typeof payload.stt_available).toBe('boolean');
    expect(typeof payload.tts_available).toBe('boolean');
    expect(typeof payload.stt_engine).toBe('string');
    expect(payload.stt_error === null || typeof payload.stt_error === 'string').toBe(true);
  });

  test('voice_status reports the resolved STT engine', async () => {
    const status = await callCoreRpc<unknown>('openhuman.voice_status', {});
    const root = (status ?? {}) as Record<string, unknown>;
    const payload =
      root && typeof root === 'object' && 'result' in root
        ? (root.result as Record<string, unknown>)
        : root;

    expect(String(payload.stt_engine ?? '').length).toBeGreaterThan(0);
    expect(payload.stt_error === null || typeof payload.stt_error === 'string').toBe(true);
  });
});