Skip to content

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

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

const CORE_RPC_URL = process.env.PW_CORE_RPC_URL || 'http://127.0.0.1:17788/rpc';
const CORE_RPC_TOKEN = process.env.PW_CORE_RPC_TOKEN || 'openhuman-playwright-token';
const AUTH_CALLBACK_HOME_TIMEOUT_MS = 30_000;

let nextRpcId = 1;

interface JsonRpcSuccess<T> {
  result: T;
}

interface JsonRpcFailure {
  error: { message?: string; code?: number; data?: unknown };
}

class CoreRpcError extends Error {
  readonly code: number | undefined;

  constructor(method: string, error: JsonRpcFailure['error']) {
    super(`RPC ${method} failed: ${error.message || 'unknown error'}`);
    this.name = 'CoreRpcError';
    this.code = error.code;
  }
}

function buildBypassJwt(userId: string): string {
  const payload = Buffer.from(
    JSON.stringify({ sub: userId, userId, exp: Math.floor(Date.now() / 1000) + 3600 })
  ).toString('base64url');
  return `eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.${payload}.sig`;
}

export async function callCoreRpc<T>(
  method: string,
  params: Record<string, unknown> = {}
): Promise<T> {
  const response = await fetch(CORE_RPC_URL, {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${CORE_RPC_TOKEN}` },
    body: JSON.stringify({ jsonrpc: '2.0', id: nextRpcId++, method, params }),
  });

  if (!response.ok) {
    throw new Error(`RPC ${method} failed with HTTP ${response.status}`);
  }

  const payload = (await response.json()) as JsonRpcSuccess<T> & JsonRpcFailure;
  if (payload.error) {
    throw new CoreRpcError(method, payload.error);
  }
  return payload.result;
}

async function resetCoreForWebUser(userId: string, onboardingCompleted = true): Promise<void> {
  await callCoreRpc('openhuman.auth_clear_session', {});
  await callCoreRpc('openhuman.config_set_onboarding_completed', { value: onboardingCompleted });
  await callCoreRpc('openhuman.auth_store_session', {
    token: buildBypassJwt(userId),
    userId,
    user: { _id: userId, id: userId, displayName: 'Playwright User' },
  });
}

export async function seedBrowserCoreMode(
  page: Page,
  options: { markWalkthroughComplete?: boolean } = {}
): Promise<void> {
  await page.addInitScript(
    ({ rpcUrl, token, markWalkthroughComplete }) => {
      window.localStorage.setItem('openhuman_core_mode', 'cloud');
      window.localStorage.setItem('openhuman_core_rpc_url', rpcUrl);
      window.localStorage.setItem('openhuman_core_rpc_token', token);
      if (markWalkthroughComplete) {
        window.localStorage.setItem('openhuman:walkthrough_completed', 'true');
        window.localStorage.removeItem('openhuman:walkthrough_pending');
      }
    },
    {
      rpcUrl: CORE_RPC_URL,
      token: CORE_RPC_TOKEN,
      markWalkthroughComplete: options.markWalkthroughComplete !== false,
    }
  );
}

async function applyBrowserCoreModeInPage(page: Page): Promise<void> {
  await page.evaluate(
    ({ rpcUrl, token }) => {
      window.localStorage.setItem('openhuman_core_mode', 'cloud');
      window.localStorage.setItem('openhuman_core_rpc_url', rpcUrl);
      window.localStorage.setItem('openhuman_core_rpc_token', token);
      window.localStorage.setItem('openhuman:walkthrough_completed', 'true');
      window.localStorage.removeItem('openhuman:walkthrough_pending');
    },
    { rpcUrl: CORE_RPC_URL, token: CORE_RPC_TOKEN }
  );
}

async function completeAuthCallback(page: Page, token: string): Promise<void> {
  await page.goto(`/#/callback/auth?token=${encodeURIComponent(token)}`);
  try {
    // The app-side auth callback waits up to 15s for CoreStateProvider to
    // commit currentUser before navigating to the post-auth landing surface;
    // CI occasionally needs more than Playwright's default 10s assertion
    // window here. Since the root two-panel shell folded Home into the unified
    // chat surface, the post-auth landing is /chat (the former /home route now
    // redirects there). We must wait for the *settled* #/chat rather than the
    // transient #/home redirect frame — otherwise callers that navigate
    // immediately after sign-in can have their target clobbered by the late
    // /home → /chat redirect.
    await expect
      .poll(async () => page.evaluate(() => window.location.hash), {
        timeout: AUTH_CALLBACK_HOME_TIMEOUT_MS,
      })
      .toMatch(/^#\/chat/);
    return;
  } catch {
    // A cold renderer can occasionally miss the core-mode init script while
    // the callback is bootstrapping. Playwright's outer test retry proves the
    // same callback succeeds immediately on a fresh page; recover inside the
    // helper so a successful second bootstrap is not reported as a flaky test.
  }

  await applyBrowserCoreModeInPage(page);
  await page.goto(`/#/callback/auth?token=${encodeURIComponent(token)}`);
  try {
    await expect
      .poll(async () => page.evaluate(() => window.location.hash), {
        timeout: AUTH_CALLBACK_HOME_TIMEOUT_MS,
      })
      .toMatch(/^#\/chat/);
  } catch {
    throw new Error('auth callback did not reach the post-auth landing surface after retry');
  }
}

async function resetCoreForWebGuest(): Promise<void> {
  await callCoreRpc('openhuman.auth_clear_session', {});
  await callCoreRpc('openhuman.config_set_onboarding_completed', { value: true });
}

export async function bootRuntimeReadyGuestPage(page: Page): Promise<void> {
  await resetCoreForWebGuest();
  await seedBrowserCoreMode(page);
  await page.goto('/#/');
  await page.waitForSelector('#root');
}

/**
 * Open a new browser context against the session already active in the core.
 *
 * The serial web lane keeps one core alive for every Playwright shard. Some
 * route-only specs intentionally share that authenticated core but receive a
 * fresh browser context. Clearing and storing the same session again restarts
 * login-gated services and needlessly doubles their peak allocation.
 */
export async function bootRuntimeReadyExistingSessionPage(page: Page): Promise<void> {
  await seedBrowserCoreMode(page);
  await page.goto('/#/');
  await waitForAuthenticatedSnapshot(page);
  await waitForAppReady(page);
}

export async function signInViaCallbackToken(page: Page, token: string): Promise<void> {
  await completeAuthCallback(page, token);
  await waitForAuthenticatedSnapshot(page);
  await waitForAppReady(page);
}

export async function signInViaBypassUser(
  page: Page,
  userId: string,
  options: { waitForInitialThread?: boolean; onboardingCompleted?: boolean } = {}
): Promise<void> {
  await resetCoreForWebUser(userId, options.onboardingCompleted ?? true);
  await applyBrowserCoreModeInPage(page);
  await page.goto('/#/home');
  await waitForAuthenticatedSnapshot(page);
  await expect
    .poll(async () => page.evaluate(() => window.location.hash), {
      timeout: AUTH_CALLBACK_HOME_TIMEOUT_MS,
    })
    .toMatch(/^#\/chat/);
  await waitForAppReady(page);
  if (options.waitForInitialThread) {
    // Some fresh profiles create an initial thread asynchronously; others
    // leave the valid `/chat` landing route without one. Route-only tests need
    // a settled thread route before setting another hash, so select the chat
    // Select the current row explicitly: Redux can restore a selected ID while
    // the URL remains at `/chat`, and clicking New Conversation in that gap can
    // race with the pending route update.
    const selectedThreadId = await page.evaluate(() => {
      const store = (
        window as unknown as {
          __OPENHUMAN_STORE__?: {
            getState?: () => { thread?: { selectedThreadId?: string | null } };
          };
        }
      ).__OPENHUMAN_STORE__;
      return store?.getState?.().thread?.selectedThreadId ?? null;
    });
    if (selectedThreadId) {
      const selectedRow = page.getByTestId(`thread-row-${selectedThreadId}`);
      await expect(selectedRow).toBeVisible();
      await selectedRow.click();
    } else {
      await page.getByTestId('new-thread-button').click({ force: true });
    }
    await expect
      .poll(async () => page.evaluate(() => window.location.hash), {
        timeout: AUTH_CALLBACK_HOME_TIMEOUT_MS,
      })
      .toMatch(/^#\/chat\/thread-[^/?]+/);
  }
}

/** Re-authenticate the same local test user without resetting persisted config. */
export async function signInViaBypassUserPreservingConfig(
  page: Page,
  userId: string
): Promise<void> {
  await callCoreRpc('openhuman.auth_store_session', {
    token: buildBypassJwt(userId),
    userId,
    user: { _id: userId, id: userId, displayName: 'Playwright User' },
  });
  await applyBrowserCoreModeInPage(page);
  await page.goto('/#/home');
  await waitForAuthenticatedSnapshot(page);
  await expect
    .poll(async () => page.evaluate(() => window.location.hash), {
      timeout: AUTH_CALLBACK_HOME_TIMEOUT_MS,
    })
    .toMatch(/^#\/chat/);
  await waitForAppReady(page);
}

export async function bootAuthenticatedPage(
  page: Page,
  userId: string,
  hash: string = '/home',
  options: { markWalkthroughComplete?: boolean } = {}
): Promise<void> {
  await resetCoreForWebUser(userId);
  await seedBrowserCoreMode(page, options);
  await page.goto('/#/home');
  await waitForAuthenticatedSnapshot(page);
  await page.goto(`/#${hash}`);
  // `/home` is the legacy landing alias and redirects to the unified chat
  // shell. Wait for that redirect to settle before reporting the page ready:
  // under a busy browser lane the app could otherwise still be rendering the
  // pre-auth welcome surface when a caller immediately asserts the composer.
  if (hash === '/home') {
    await expect
      .poll(async () => page.evaluate(() => window.location.hash), {
        timeout: AUTH_CALLBACK_HOME_TIMEOUT_MS,
      })
      .toMatch(/^#\/chat/);
  }
  await waitForAppReady(page);
  // The shell can restore its persisted chat route after the first post-auth
  // navigation. Reapply a requested non-home route once the shell is ready so
  // callers never start assertions on that stale restoration.
  if (hash !== '/home' && !(await page.evaluate(() => window.location.hash)).includes(hash)) {
    await page.goto(`/#${hash}`);
    await waitForAppReady(page);
  }
}

export async function waitForAppReady(page: Page): Promise<void> {
  await page.waitForSelector('#root');
  await expect
    .poll(async () => {
      const text = await page
        .locator('#root')
        .innerText()
        .catch(() => '');
      return text.trim().length;
    })
    .toBeGreaterThan(20);
  await expect
    .poll(async () =>
      // Keyed off a test id, not the picker's heading. Matching the copy meant
      // that renaming it left this wait matching nothing, so it resolved
      // immediately and every spec raced the boot gate.
      page.evaluate(() => document.querySelector('[data-testid="boot-check-picker"]') !== null)
    )
    .toBe(false);

  // Harness setup can install runtimes on a cold core. It deliberately blocks
  // user input behind a full-screen dialog until the user chooses to continue
  // in the background, so settle that product flow before a spec drives UI.
  const readInitStatus = async (): Promise<{
    overall?: string;
    started_at?: string | null;
  } | null> => {
    try {
      const result = await callCoreRpc<{
        snapshot?: { overall?: string; started_at?: string | null };
      }>('openhuman.harness_init_status');
      return result.snapshot ?? null;
    } catch (error) {
      // Some slim or older cores do not expose this optional status method.
      if (
        error instanceof Error &&
        ((error as CoreRpcError).code === -32601 ||
          /(?:unknown method|method not found)/i.test(error.message))
      ) {
        return null;
      }
      throw error;
    }
  };
  const init = await readInitStatus();
  if (init?.overall === 'done' || init?.overall === 'idle') return;
  if (init?.overall && init.overall !== 'running' && init.overall !== 'failed') return;
  if (init?.overall === 'running' || init?.overall === 'failed') {
    const alreadyDismissed = await page.evaluate(
      startedAt =>
        window.sessionStorage.getItem('harness-init-dismissed-run') === (startedAt ?? 'pending'),
      init.started_at
    );
    if (alreadyDismissed) return;
  }

  const dialog = page.getByTestId('harness-init-dialog');
  const actionButton = dialog
    .getByTestId(/harness-init-(background|continue-anyway)/)
    .filter({ visible: true })
    .first();
  const action: { value: 'background' | 'continue' | 'terminal' } = { value: 'terminal' };
  await expect
    .poll(
      async () => {
        if (await actionButton.isVisible().catch(() => false)) {
          action.value =
            (await actionButton.getAttribute('data-testid')) === 'harness-init-background'
              ? 'background'
              : 'continue';
          return true;
        }
        const current = await readInitStatus();
        if (current?.overall === 'done' || current?.overall === 'idle') {
          action.value = 'terminal';
          return true;
        }
        if (current === null && !(await dialog.isVisible().catch(() => false))) {
          action.value = 'terminal';
          return true;
        }
        return false;
      },
      { timeout: 10_000 }
    )
    .toBe(true);
  if (action.value === 'background' || action.value === 'continue') {
    await actionButton.click();
    await expect(dialog).toBeHidden({ timeout: 5_000 });
  }
}

export async function dismissWalkthroughIfPresent(page: Page): Promise<void> {
  const skipButton = page.getByRole('button', { name: /Skip|Skip tour/i });
  const portal = page.locator('#react-joyride-portal');
  const deadline = Date.now() + 5_000;
  const markCompleted = async () => {
    await page.evaluate(() => {
      try {
        localStorage.setItem('openhuman:walkthrough_completed', 'true');
        localStorage.removeItem('openhuman:walkthrough_pending');
      } catch {}
    });
  };

  while (Date.now() < deadline) {
    if ((await portal.count()) === 0) return;
    if (
      (await skipButton.count()) > 0 &&
      (await skipButton
        .first()
        .isVisible()
        .catch(() => false))
    ) {
      await markCompleted();
      await skipButton
        .first()
        .click({ force: true, timeout: 1_000 })
        .catch(() => {});
      try {
        await expect
          .poll(
            async () => {
              const visible = await skipButton
                .first()
                .isVisible()
                .catch(() => false);
              return !visible;
            },
            { timeout: 5_000 }
          )
          .toBe(true);
        return;
      } catch {
        // Some routes keep the Joyride portal mounted even after the tour is
        // dismissed. Keep looping so we can re-check visibility and fall back
        // to the persisted completion flag below.
      }
    }
    await page.waitForTimeout(100);
  }

  await markCompleted();
  // Last-resort: a lingering #react-joyride-portal node will keep
  // intercepting clicks on the page even after we've persisted the
  // completion flag, AND React may re-mount one later (e.g. after a
  // hash-route navigation that runs the walkthrough effect again).
  // Strip every portal node now AND install a MutationObserver that
  // keeps stripping any future mount for the rest of the page
  // lifetime. The observer install is idempotent — re-runs of this
  // helper on the same page no-op.
  await page.evaluate(() => {
    document.querySelectorAll('#react-joyride-portal').forEach(node => node.remove());
    const win = window as unknown as { __openhumanJoyrideScrubInstalled?: boolean };
    if (win.__openhumanJoyrideScrubInstalled) return;
    win.__openhumanJoyrideScrubInstalled = true;
    const scrub = (root: ParentNode) => {
      root.querySelectorAll('#react-joyride-portal').forEach(node => node.remove());
    };
    const obs = new MutationObserver(mutations => {
      for (const m of mutations) {
        m.addedNodes.forEach(node => {
          if (!(node instanceof Element)) return;
          if (node.id === 'react-joyride-portal') {
            node.remove();
          } else {
            scrub(node);
          }
        });
      }
    });
    obs.observe(document.body, { childList: true, subtree: true });
  });
}

async function waitForAuthenticatedSnapshot(page: Page): Promise<void> {
  await expect
    .poll(
      async () =>
        page.evaluate(() => {
          const winAny = window as unknown as {
            __OPENHUMAN_CORE_STATE__?: () => {
              snapshot?: {
                sessionToken?: string | null;
                currentUser?: { _id?: string | null } | null;
              };
            };
          };
          const snapshot = winAny.__OPENHUMAN_CORE_STATE__?.()?.snapshot;
          return {
            hasToken: Boolean(snapshot?.sessionToken),
            hasUser: Boolean(snapshot?.currentUser?._id),
          };
        }),
      { timeout: 20_000 }
    )
    .toEqual({ hasToken: true, hasUser: true });
}