Skip to content

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

41.2 KB
Raw Blame
// @ts-nocheck
/**
 * Shared E2E flow helpers for Linux (tauri-driver).
 *
 * Extracted from individual spec files to avoid duplication.
 * All navigation uses browser.execute() with window.location.hash
 * because sidebar nav buttons are icon-only (aria-label, no text content).
 */
import { getRequestLog } from '../mock-server';
import { waitForAppReady, waitForAuthBootstrap as waitForAuthenticatedCore } from './app-helpers';
import { callOpenhumanRpc } from './core-rpc';
import { triggerAuthDeepLink } from './deep-link-helpers';
import {
  clickButton,
  clickText,
  dumpAccessibilityTree,
  textExists,
  waitForWebView,
  waitForWindowVisible,
} from './element-helpers';
import { supportsExecuteScript } from './platform';

function safeRpcErrorSummary(result: { error?: string } | null) {
  if (typeof result?.error !== 'string') return null;
  return result.error
    .replace(/https?:\/\/[^\s)]+/gi, '<url>')
    .replace(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}/gi, '<email>')
    .replace(/\beyJ[A-Z0-9._-]+/gi, '<token>')
    .replace(/\b[A-Z0-9_-]{32,}\b/gi, '<redacted>')
    .replace(/\/(?:[^\s,;]+\/)*[^\s,;]+/g, '<path>')
    .slice(0, 160);
}

function safeRpcFailureKind(result: { ok?: boolean; error?: string } | null) {
  if (!result || result.ok) return null;
  const message = (result.error ?? '').toLowerCase();
  if (/timed out|timeout|deadline/.test(message)) return 'timeout';
  if (/connection refused|tcp connect|connection|connect|socket|network/.test(message))
    return 'transport';
  if (/\b401\b|unauthorized/.test(message)) return 'unauthorized';
  if (/method not found|unknown method/.test(message)) return 'method_not_found';
  if (/invalid.*params|bad request/.test(message)) return 'invalid_request';
  return result.error ? 'rpc_error' : 'empty_error';
}

// ---------------------------------------------------------------------------
// Accounts page helpers
// ---------------------------------------------------------------------------

/**
 * Open the "Add Account" modal on /accounts.
 *
 * The "Add app" affordance is a button whose only labelled descendants are an
 * SVG plus a tooltip span with `pointer-events: none`. None of the shared
 * `clickButton`/`clickText` helpers can target it cleanly because the
 * accessible name lives only on `aria-label`, so this helper reaches for the
 * explicit selector. Tracking a follow-up `clickByAriaLabel` helper.
 */
export async function openAddAccountModal(): Promise<void> {
  const page = await browser.$('[data-testid="accounts-page"]');
  await page.waitForDisplayed({ timeout: 15_000 });

  const opened = await browser.execute(() => {
    const addBtn = document.querySelector<HTMLButtonElement>('[data-testid="accounts-add-button"]');
    if (!addBtn) return false;
    addBtn.click();
    return true;
  });
  if (!opened) {
    throw new Error('Could not locate Add Account button on /chat accounts page');
  }
  const modal = await browser.$('[data-testid="add-account-modal"]');
  await modal.waitForDisplayed({ timeout: 5_000 });
}

export async function waitForAccountsPage(timeout = 15_000): Promise<void> {
  const page = await browser.$('[data-testid="accounts-page"]');
  await page.waitForDisplayed({ timeout });
}

export async function clickAddAccountProvider(provider: string, timeout = 10_000): Promise<void> {
  const tile = await browser.$(`[data-testid="add-account-provider-${provider}"]`);
  await tile.waitForDisplayed({ timeout });
  await tile.click();
}

export async function waitForAddAccountModalClosed(timeout = 5_000): Promise<void> {
  const modal = await browser.$('[data-testid="add-account-modal"]');
  await modal.waitForExist({ timeout, reverse: true });
}

// ---------------------------------------------------------------------------
// Generic helpers
// ---------------------------------------------------------------------------

export async function waitForRequest(log, method, urlFragment, timeout = 15_000) {
  const deadline = Date.now() + timeout;
  while (Date.now() < deadline) {
    const match = log().find(r => r.method === method && r.url.includes(urlFragment));
    if (match) return match;
    await browser.pause(500);
  }
  return undefined;
}

export async function waitForHomePage(timeout = 15_000) {
  // Home page (Home.tsx) renders t('home.askAssistant') = 'Ask your assistant anything...'
  // as a stable CTA button. The animated typewriter heading ('Welcome, <name> 👋' etc.)
  // and old strings ('Good morning', 'Message OpenHuman', 'Upgrade to Premium') are gone.
  // After the /home → /chat redirect (AppRoutes.tsx), the chat new-window hero renders
  // t('home.statusOk') instead, so include both the old CTA text and the new status copy.
  const candidates = [
    'Ask your assistant anything',
    'Your device is connected',
    'Your assistant is ready when you are',
    'Type something below to get started',
  ];
  const deadline = Date.now() + timeout;
  while (Date.now() < deadline) {
    // The unified chat surface can legitimately render without a greeting
    // marker (for example while its thread data is still loading). Once the
    // authenticated root shell is mounted, the post-onboarding route is ready
    // for the caller to navigate to its feature under test.
    if (
      supportsExecuteScript() &&
      (await browser.execute(
        () => document.querySelector('[data-testid="root-shell-sidebar"]') !== null
      ))
    ) {
      return 'application shell';
    }
    for (const text of candidates) {
      if (await textExists(text)) return text;
    }
    await browser.pause(1_000);
  }
  return null;
}

/**
 * Click the first matching text from a list of candidates.
 */
async function clickFirstMatch(candidates, timeout = 5_000) {
  for (const text of candidates) {
    if (await textExists(text)) {
      await clickText(text, timeout);
      return text;
    }
  }
  return null;
}

// ---------------------------------------------------------------------------
// Navigation helpers (JS hash-based — icon-only sidebar buttons)
// ---------------------------------------------------------------------------

/**
 * Appium Mac2 cannot run W3C Execute Script in WKWebView — use sidebar labels
 * instead.
 *
 * Current IA (bottom-tab bar, see app/src/config/navConfig.ts): the tabs are
 * Chat, Human, Brain, Connections. Settings is reached via the gear icon in the
 * sidebar header. Home no longer has its own tab (it was merged into Chat as the
 * empty "new window" state — /home redirects via HASH_REDIRECTS below). Human is
 * a first-class tab: it owns the dedicated mascot stage, while Chat carries the
 * same mascot docked on its composer. The earlier "Assistant"/"Activity"/"Alerts"
 * labels are gone. Only real tabs belong here; routes that redirect (e.g. /home,
 * /activity, /intelligence, /skills, /channels, /webhooks) are resolved through
 * HASH_REDIRECTS below — they have no sidebar button.
 */
const HASH_TO_SIDEBAR_LABEL = {
  '/chat': 'Chat',
  '/human': 'Human',
  '/connections': 'Connections',
  '/settings': 'Settings',
};

/**
 * Routes that AppRoutes.tsx serves via <Navigate replace>. A route can redirect
 * through another retired route, so resolve the full chain before waiting for
 * the final hash. Keep in sync with app/src/AppRoutes.tsx.
 */
const HASH_REDIRECTS = {
  '/home': '/chat',
  '/accounts': '/chat',
  '/skills': '/connections',
  '/channels': '/connections?tab=messaging',
  '/activity': '/settings/notifications',
  '/intelligence': '/settings/notifications',
  '/routines': '/flows',
  '/webhooks': '/connections',
  '/settings/features': '/settings',
  '/settings/screen-intelligence': '/settings',
  '/settings/screen-awareness-debug': '/settings',
  '/settings/mascot': '/settings/face',
  '/settings/notifications': '/settings/account',
  '/settings/tools': '/connections?tab=agent-tools',
  '/settings/composio-triggers': '/connections?tab=composio-key',
  '/settings/autonomy': '/settings/agent-access',
  '/settings/composio-routing': '/connections?tab=composio-key',
  // The retired debug panels both redirect to the surviving LLM surface.
  // Do not append their former fragment identifiers: the browser treats a
  // second `#` as part of the hash and the router intentionally removes it.
  '/settings/agent-chat': '/connections?tab=llm',
  '/settings/local-model-debug': '/connections?tab=llm',
  '/settings/llm': '/connections?tab=llm',
  '/settings/voice': '/connections?tab=voice',
  '/settings/search': '/connections?tab=search',
  // Memory v2: the retired Brain page and the v1 memory settings panels all
  // land on a chip of the Memory page (Connections → Memory, `?brain=`).
  '/brain': '/connections?tab=brain',
  '/settings/intelligence': '/connections?tab=brain',
  '/settings/memory-engine': '/connections?tab=brain&brain=engine',
  '/settings/memory-data': '/connections?tab=brain&brain=brain',
  '/settings/memory-sync': '/connections?tab=brain&brain=brain',
  '/settings/memory-debug': '/connections?tab=brain&brain=ask',
  '/settings/tasks': '/connections?tab=brain&brain=ask',
};

/** Resolve a requested hash to where the router actually settles. */
function resolveRedirect(normalized) {
  let resolved = normalized;
  const seen = new Set();
  while (HASH_REDIRECTS[resolved] && !seen.has(resolved)) {
    seen.add(resolved);
    resolved = HASH_REDIRECTS[resolved];
  }
  return resolved;
}

function normalizeHash(value) {
  const raw = String(value || '');
  const withPrefix = raw.startsWith('#') ? raw : `#${raw}`;
  return withPrefix.replace(/\/$/, '');
}

function routeReadySelector(hash) {
  const path = resolveRedirect(normalizeHash(hash).replace(/^#/, ''));
  const selectors = {
    '/notifications': '[data-testid="integration-notifications-section"]',
    '/settings/notifications': '[data-testid="integration-notifications-section"]',
    '/settings/cron-jobs': '[data-testid="cron-jobs-panel"]',
    '/settings/privacy': '[data-testid="settings-privacy-panel"]',
    '/settings/migration': '[data-testid="migration-form"]',
    '/settings/voice': '[data-testid="voice-providers-section"]',
    '/connections?tab=brain': '[data-testid="memory-page"]',
    '/connections?tab=brain&brain=engine': '[data-testid="memory-page"]',
    '/connections?tab=brain&brain=brain': '[data-testid="memory-page"]',
    '/connections?tab=brain&brain=ask': '[data-testid="memory-page"]',
    '/settings/recovery-phrase': '[data-testid="recovery-phrase-panel"]',
  };
  return selectors[path] || null;
}

async function waitForHashRouteReady(hash, options = {}) {
  const { timeout = 15_000 } = options;
  // Routes that redirect (e.g. /activity → /settings/notifications) settle on
  // the resolved target, so wait for that hash rather than the requested one.
  const expected = normalizeHash(`#${resolveRedirect(normalizeHash(hash).replace(/^#/, ''))}`);
  const hashMatches = current =>
    current === expected ||
    // On wide desktop layouts, the settings index immediately selects its
    // default panel. Accept that final destination as well.
    (expected === '#/settings' && current === '#/settings/account');
  const readySelector = routeReadySelector(hash);
  // We deliberately do NOT use a root-innerText "signature changed" heuristic:
  // the TwoPanelLayout shell keeps a persistent sidebar whose text dominates the
  // first 500 chars of root.innerText, so that signature is identical across all
  // settings sub-panels and the heuristic never fires. Instead we key off
  // readyState + the resolved hash (and a route-ready selector when known).
  // A stable but unrelated hash is not evidence of navigation: accepting one
  // masks failed route changes (for example, a test continuing on /chat after
  // asking to open /connections?tab=brain).
  await browser.waitUntil(
    async () => {
      const res = await browser.execute(
        ({ selector }) => {
          if (document.readyState !== 'complete') return { loading: true };
          const root = document.getElementById('root');
          if (!root) return { loading: true };
          return {
            loading: false,
            hasSelector: selector ? root.querySelector(selector) !== null : false,
            current: window.location.hash.replace(/\/$/, ''),
          };
        },
        { selector: readySelector }
      );
      if (res.loading) return false;
      // A known route-ready selector being present is a definitive signal the
      // target panel rendered — accept it regardless of the hash, since routes
      // can redirect to a different hash (e.g. /settings/memory-data →
      // /connections?tab=brain&brain=brain).
      if (res.hasSelector) return true;
      // Otherwise require the resolved target hash. Redirects are accounted
      // for above when computing `expected`.
      return hashMatches(res.current);
    },
    {
      timeout,
      interval: 250,
      timeoutMsg: `hash route ${expected} did not become ready within ${timeout}ms`,
    }
  );
}

export async function navigateViaHash(hash) {
  const normalized = String(hash).replace(/\/$/, '') || hash;
  // A redirecting route settles on its target hash, so the settle-check must
  // expect that target (e.g. requesting /activity lands on /settings/notifications).
  const resolved = resolveRedirect(normalized);
  const expectedHash = `#${resolved}`;
  const hashMatches = currentHash =>
    currentHash === expectedHash || String(currentHash).startsWith(`${expectedHash}/`);
  const waitForHash = async (timeout = 8_000) =>
    browser.waitUntil(
      async () => {
        const currentHash = await browser.execute(() => window.location.hash);
        if (!hashMatches(currentHash)) return false;
        await browser.pause(300);
        const stableHash = await browser.execute(() => window.location.hash);
        return hashMatches(stableHash);
      },
      { timeout, interval: 250, timeoutMsg: `hash did not settle on ${hash}` }
    );

  if (supportsExecuteScript()) {
    // Try sidebar button click first — more reliable than direct hash set.
    const label = HASH_TO_SIDEBAR_LABEL[normalized];
    if (label) {
      try {
        const clicked = await browser.execute((targetLabel: string) => {
          const buttons = Array.from(document.querySelectorAll('button')) as HTMLButtonElement[];
          const button = buttons.find(btn => {
            const aria = btn.getAttribute('aria-label')?.trim();
            const title = btn.getAttribute('title')?.trim();
            const text = btn.textContent?.trim();
            return aria === targetLabel || title === targetLabel || text === targetLabel;
          });
          if (!button) return false;
          button.click();
          return true;
        }, label);
        if (clicked) {
          await waitForHash();
          const currentHash = await browser.execute(() => window.location.hash);
          console.log(`[E2E] Navigated to ${hash} via "${label}" (current: ${currentHash})`);
          return;
        }
      } catch (buttonErr) {
        console.log(`[E2E] Button navigation to ${hash} failed:`, buttonErr);
      }
    }

    // Fallback: direct hash set + wait for route readiness.
    try {
      await browser.execute(h => {
        window.location.hash = h;
      }, hash);
      await waitForHashRouteReady(hash);
      const currentHash = await browser.execute(() => window.location.hash);
      console.log(`[E2E] Navigated to ${hash} (current: ${currentHash})`);
      return;
    } catch (err) {
      console.log(`[E2E] Hash navigation to ${hash} failed:`, err);
    }

    // Last resort: retry button click.
    if (label) {
      try {
        const clicked = await browser.execute((targetLabel: string) => {
          const buttons = Array.from(document.querySelectorAll('button')) as HTMLButtonElement[];
          const button = buttons.find(btn => {
            const aria = btn.getAttribute('aria-label')?.trim();
            const title = btn.getAttribute('title')?.trim();
            const text = btn.textContent?.trim();
            return aria === targetLabel || title === targetLabel || text === targetLabel;
          });
          if (!button) return false;
          button.click();
          return true;
        }, label);
        if (!clicked) {
          throw new Error(`could not find nav button "${label}"`);
        }
        await waitForHash();
        const currentHash = await browser.execute(() => window.location.hash);
        console.log(`[E2E] Navigated to ${hash} via "${label}" (current: ${currentHash})`);
        return;
      } catch (fallbackErr) {
        console.log(`[E2E] Button navigation to ${hash} failed:`, fallbackErr);
      }
    }

    throw new Error(`[E2E] Failed to navigate to ${hash}`);
  }

  // Appium Mac2 — Settings → Billing (nested route)
  if (normalized === '/settings/billing') {
    try {
      await clickText('Settings', 12_000);
      await browser.pause(1_500);
      const sub = await clickFirstMatch(['Billing & Usage', 'Billing'], 12_000);
      if (!sub) {
        throw new Error('Mac2: could not find Billing / Billing & Usage after opening Settings');
      }
      await browser.pause(2_000);
      console.log(`[E2E] Mac2 navigated to ${hash} via Settings → ${sub}`);
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err);
      throw new Error(`[E2E] Mac2: failed to navigate to ${hash}: ${msg}`);
    }
    return;
  }

  // Resolve redirect before label lookup so that e.g. /home → Chat works on Mac2.
  const label = HASH_TO_SIDEBAR_LABEL[resolveRedirect(normalized)];
  if (label) {
    try {
      await clickText(label, 12_000);
      await browser.pause(2_000);
      console.log(`[E2E] Mac2 sidebar navigation to ${hash} via "${label}"`);
    } catch (err) {
      console.log(`[E2E] Mac2 sidebar navigation to ${hash} failed:`, err);
    }
    return;
  }

  throw new Error(
    `[E2E] Mac2: no sidebar mapping for hash "${hash}". Extend HASH_TO_SIDEBAR_LABEL or add a branch in navigateViaHash.`
  );
}

export async function navigateToHome() {
  // /home redirects to /chat (AppRoutes.tsx). Navigate directly to /chat so
  // the sidebar button click path uses the 'Chat' label which exists, rather
  // than 'Home' which no longer has a dedicated tab.
  await navigateViaHash('/chat');
  const homeText = await waitForHomePage(10_000);
  if (!homeText) {
    if (supportsExecuteScript()) {
      try {
        await browser.execute(() => {
          window.location.hash = '/chat';
        });
      } catch {
        /* ignore */
      }
    } else {
      try {
        await clickText('Chat', 8_000);
      } catch {
        /* ignore */
      }
    }
    await browser.pause(2_000);
    await waitForHomePage(10_000);
  }
}

export async function navigateToSettings() {
  await navigateViaHash('/settings');
}

export async function navigateToBilling() {
  // Direct hash navigation can fail transiently while the settings shell is
  // still settling after a deep-link auth flow. Keep going so the established
  // Settings → Billing recovery path below can make a second, UI-driven
  // attempt instead of making that recovery unreachable.
  try {
    await navigateViaHash('/settings/billing');
  } catch (err) {
    console.log('[E2E] Initial billing navigation failed; running fallback:', err);
  }

  const billingMarkers = ['Billing moved to the web', 'Open billing dashboard', 'Open dashboard'];
  const deadline = Date.now() + 15_000;
  let hasBilling = false;
  while (Date.now() < deadline) {
    for (const marker of billingMarkers) {
      hasBilling = await textExists(marker);
      if (hasBilling) break;
    }
    if (hasBilling) break;
    await browser.pause(500);
  }

  if (hasBilling) {
    console.log('[E2E] Billing page loaded');
    return;
  }

  console.log('[E2E] Billing content not found after initial navigation; running fallback');

  await navigateViaHash('/settings');
  await browser.pause(3_000);

  if (supportsExecuteScript()) {
    const currentHash = await browser.execute(() => window.location.hash);
    console.log(`[E2E] Billing fallback: current hash ${currentHash}`);

    const clicked = await browser.execute(() => {
      const allText = document.querySelectorAll('*');
      for (const el of allText) {
        const text = el.textContent?.trim() || '';
        if (
          (text === 'Billing & Usage' || text === 'Billing') &&
          el.closest('button, [role="button"], a, [class*="MenuItem"]')
        ) {
          (el.closest('button, [role="button"], a, [class*="MenuItem"]') as HTMLElement).click();
          return 'clicked';
        }
      }
      window.location.hash = '/settings/billing';
      return 'hash-fallback';
    });
    console.log(`[E2E] Billing fallback: ${clicked}`);
  } else {
    const sub = await clickFirstMatch(['Billing & Usage', 'Billing'], 10_000);
    console.log(`[E2E] Billing fallback (Mac2): clicked ${sub}`);
  }
  await browser.pause(3_000);

  // Verify billing actually loaded after fallback
  let finalCheck = false;
  const finalDeadline = Date.now() + 15_000;
  while (Date.now() < finalDeadline) {
    for (const marker of billingMarkers) {
      finalCheck = await textExists(marker);
      if (finalCheck) break;
    }
    if (finalCheck) break;
    await browser.pause(500);
  }
  if (!finalCheck) {
    let finalHash = '';
    if (supportsExecuteScript()) {
      finalHash = await browser.execute(() => window.location.hash);
    }
    const tree = await dumpAccessibilityTree();
    console.log(`[E2E] Billing verification failed after fallback. Hash: ${finalHash}`);
    console.log(`[E2E] Accessibility tree:\n`, tree.slice(0, 4000));
    throw new Error(
      `navigateToBilling: billing markers not found after fallback (hash: ${finalHash})`
    );
  }
  console.log('[E2E] Billing page loaded (after fallback)');
}

/** @deprecated Phase 2: use navigateToConnections() instead. Still works via redirect. */
export async function navigateToSkills() {
  // `/connections` now lands on the Welcome landing; jump to the functional
  // integrations view (the pre-Welcome default) so dependent flows still work.
  await navigateViaHash('/connections?tab=composio');
}

/** Navigate to the Connections page (was /skills in Phase 1). */
export async function navigateToConnections() {
  // `/connections` now lands on the Welcome landing; jump to the functional
  // integrations view (the pre-Welcome default).
  await navigateViaHash('/connections?tab=composio');
}

/** @deprecated The legacy intelligence route still works via redirect. */
export async function navigateToIntelligence() {
  await navigateViaHash('/activity');
}

export async function navigateToConversations() {
  await navigateViaHash('/chat');
}

// ---------------------------------------------------------------------------
// Onboarding walkthrough
// Current flow: Welcome → Skills → optional Context gathering.
// ---------------------------------------------------------------------------

/** Labels used to detect the onboarding overlay (same strings as Onboarding copy). */
const ONBOARDING_OVERLAY_TEXTS = [
  'Skip',
  'Welcome',
  "Hi. I'm OpenHuman.",
  "Let's Start",
  'Connect your Gmail',
  'Skip for Now',
  'Building your profile',
  'Almost there',
  'Continue to chat',
  'Run AI Models Locally',
  'Screen & Accessibility',
  'Enable Tools',
  'Install Skills',
] as const;

/** True when the routed full-screen onboarding flow is visible. */
async function onboardingOverlayLikelyVisible(): Promise<boolean> {
  if (supportsExecuteScript()) {
    const routedOnboarding = await browser.execute(() => {
      const onOnboardingRoute = window.location.hash.startsWith('#/onboarding');
      const hasOnboardingShell =
        document.querySelector('[data-testid="onboarding-layout"]') !== null ||
        document.querySelector('[data-testid="onboarding-next-button"]') !== null;
      return onOnboardingRoute && hasOnboardingShell;
    });
    if (routedOnboarding) return true;
  }

  for (const label of ONBOARDING_OVERLAY_TEXTS) {
    if (label === 'Welcome') continue;
    if (await textExists(label)) return true;
  }
  return false;
}

export async function waitForOnboardingOverlayVisible(timeout = 10_000): Promise<boolean> {
  const deadline = Date.now() + timeout;
  while (Date.now() < deadline) {
    if (await onboardingOverlayLikelyVisible()) return true;
    await browser.pause(400);
  }
  return false;
}

export async function dismissWalkthroughIfVisible(timeout = 6_000): Promise<boolean> {
  const deadline = Date.now() + timeout;
  while (Date.now() < deadline) {
    if (supportsExecuteScript()) {
      const status = await browser.execute(() => {
        const buttons = Array.from(document.querySelectorAll<HTMLButtonElement>('button'));
        const skip = buttons.find(button => (button.textContent ?? '').trim() === 'Skip tour');
        if (!skip) return 'not-visible';
        ['mousedown', 'mouseup', 'click'].forEach(type => {
          skip.dispatchEvent(
            new MouseEvent(type, { bubbles: true, cancelable: true, view: window, button: 0 })
          );
        });
        return 'clicked';
      });
      if (status === 'clicked') {
        await browser.waitUntil(async () => !(await textExists('Skip tour')), {
          timeout: 4_000,
          interval: 250,
          timeoutMsg: 'walkthrough skip button remained visible',
        });
        return true;
      }
    } else if (await textExists('Skip tour')) {
      await clickText('Skip tour', 2_000);
      return true;
    }
    await browser.pause(400);
  }
  return false;
}

/**
 * BootCheckGate shows a "Choose core mode" modal on fresh storage. It sits
 * *in front of* the routed page, so onboarding never mounts behind it. We
 * click the primary "Continue" CTA via a synthetic MouseEvent and retry
 * until the modal is gone (a single click can race against the gate's
 * re-render). Exported so specs that bypass `walkOnboarding` can still
 * call this directly.
 */
export async function dismissBootCheckGateIfVisible(timeoutMs = 12_000): Promise<boolean> {
  if (!supportsExecuteScript()) return false;
  const deadline = Date.now() + timeoutMs;
  let everSeen = false;
  while (Date.now() < deadline) {
    const status = await browser.execute(() => {
      // The BootCheckGate renders a full-screen `.fixed` overlay with a
      // heading. Check for both "Choose core mode" (legacy) and
      // "Select a Runtime" (current i18n key bootCheck.chooseCoreMode).
      // Important: only match headings inside a `.fixed` overlay — the
      // Welcome page also has a "Select a Runtime" button, but that is
      // NOT the BootCheckGate and clicking it would reset the core mode.
      const heading = Array.from(document.querySelectorAll('.fixed h2')).find(h => {
        const text = (h.textContent ?? '').trim();
        return text === 'Choose core mode' || text === 'Select a Runtime';
      });
      if (!heading) return 'gone';
      const modal = heading.closest('.fixed') ?? heading.parentElement;
      if (!modal) return 'gone';
      const buttons = Array.from(modal.querySelectorAll<HTMLButtonElement>('button'));
      const primary =
        buttons.find(b => (b.textContent ?? '').trim() === 'Continue') ??
        buttons.find(b => (b.textContent ?? '').trim().includes('Local')) ??
        buttons.find(b => /bg-ocean-500|bg-primary/.test(b.className)) ??
        buttons[buttons.length - 1];
      if (!primary) return 'visible-no-button';
      ['mousedown', 'mouseup', 'click'].forEach(type => {
        primary.dispatchEvent(
          new MouseEvent(type, { bubbles: true, cancelable: true, view: window, button: 0 })
        );
      });
      return 'clicked';
    });
    if (status === 'gone') return everSeen;
    everSeen = true;
    await browser.pause(800);
  }
  return everSeen;
}

async function waitForPostOnboardingHome(logPrefix, timeout = 20_000) {
  if (supportsExecuteScript()) {
    // After onboarding the app routes to either #/home or #/chat depending on
    // the DefaultRedirect guard and the user's onboarding state. Accept both.
    await browser.waitUntil(
      async () =>
        Boolean(
          await browser.execute(() => {
            const h = window.location.hash.replace(/\/$/, '');
            return !h.startsWith('#/onboarding');
          })
        ),
      {
        timeout: Math.min(timeout, 10_000),
        interval: 300,
        timeoutMsg: 'onboarding completed but hash did not settle on #/home or #/chat',
      }
    );
  }

  // Check for Home page markers, but don't fail if we're on /chat instead.
  const homeText = await waitForHomePage(Math.min(timeout, 8_000));
  if (!homeText) {
    // The app may have routed to /chat. Check for chat markers.
    const onChat =
      supportsExecuteScript() &&
      (await browser.execute(() => window.location.hash.startsWith('#/chat')));
    if (onChat) {
      console.log(`${logPrefix} Post-onboarding landed on /chat (accepted)`);
      return;
    }
    const tree = await dumpAccessibilityTree();
    console.log(`${logPrefix} Home page not ready after onboarding. Tree:\n`, tree.slice(0, 4000));
    throw new Error('Onboarding dismissed but Home page did not become ready');
  }

  console.log(`${logPrefix} Post-onboarding Home page confirmed: found "${homeText}"`);
}

/**
 * Walk through onboarding by advancing the `data-testid="onboarding-next-button"`
 * until it unmounts. The button is rendered on every step (see
 * app/src/pages/onboarding/components/OnboardingNextButton.tsx), so we don't
 * need to track step *titles* — title-based detection silently skipped any
 * step that wasn't in `ONBOARDING_OVERLAY_TEXTS` (e.g. "Connect your Gmail")
 * and left specs wedged behind onboarding while reporting success.
 *
 * We dispatch a real synthetic MouseEvent so React's onClick fires reliably,
 * and bail out if the button gets stuck in a permanently-disabled state.
 *
 * Dismisses BootCheckGate ("Choose core mode") first if it's blocking the
 * route — onboarding sits behind it, so without this the walker just times
 * out waiting for the next-button to mount.
 */
export async function walkOnboarding(logPrefix = '[E2E]', maxSteps = 12): Promise<void> {
  if (!supportsExecuteScript()) {
    // Mac2/no-script fallback: legacy "Continue" matcher, kept so the
    // unsupported-driver branch isn't a hard failure for old harnesses.
    const clicked = await clickFirstMatch(['Continue'], 3_000);
    if (clicked) console.log(`${logPrefix} Onboarding: clicked Continue (legacy fallback)`);
    return;
  }

  // Onboarding mounts beneath BootCheckGate. If the user is fresh-installed
  // the gate is up and onboarding will never render until we confirm it.
  const dismissed = await dismissBootCheckGateIfVisible();
  if (dismissed) {
    console.log(`${logPrefix} Dismissed BootCheckGate before onboarding`);
    await browser.pause(1_500);
  }

  // Wait for the onboarding shell to actually mount. If the user is already
  // onboarded (e.g. resuming an existing session — the common case in the
  // shared-workspace E2E run) the button never appears and this wait is pure
  // dead time on *every* resetApp, pushing the whole bring-up toward the 30s
  // Mocha hook ceiling. 8s keeps ample headroom for a cold CEF boot to paint
  // the button (it appears within a few seconds when onboarding is genuinely
  // needed); the rare cold-boot miss is caught by the spec-file retry.
  const appeared = await browser
    .waitUntil(
      async () =>
        Boolean(
          await browser.execute(
            () => document.querySelector('[data-testid="onboarding-next-button"]') !== null
          )
        ),
      { timeout: 8_000, interval: 500, timeoutMsg: 'onboarding-next-button never appeared' }
    )
    .catch(() => false);

  if (!appeared) {
    console.log(`${logPrefix} Onboarding next-button never appeared — assuming already onboarded`);
    await dismissWalkthroughIfVisible(3_000);
    return;
  }

  for (let step = 0; step < maxSteps; step += 1) {
    const status = await browser.execute(() => {
      const btn = document.querySelector<HTMLButtonElement>(
        '[data-testid="onboarding-next-button"]'
      );
      const onOnboardingHash = window.location.hash.startsWith('#/onboarding');
      if (!btn) return onOnboardingHash ? 'gone-but-onboarding-hash' : 'gone';
      if (btn.disabled) return 'disabled';
      ['mousedown', 'mouseup', 'click'].forEach(type => {
        btn.dispatchEvent(
          new MouseEvent(type, { bubbles: true, cancelable: true, view: window, button: 0 })
        );
      });
      return 'clicked';
    });

    if (status === 'gone') {
      console.log(`${logPrefix} Onboarding dismissed after ${step} step(s)`);
      await waitForPostOnboardingHome(logPrefix);
      // Completing routed onboarding starts the app-wide Joyride tour
      // asynchronously. Dismiss it before a caller navigates, otherwise the
      // tour's own route transitions can race a spec and take it back to Chat.
      await dismissWalkthroughIfVisible(8_000);
      return;
    }
    if (status === 'gone-but-onboarding-hash') {
      // The button momentarily unmounts between steps (animation / lazy render).
      // Don't claim victory yet — wait for the next step to render.
      console.log(
        `${logPrefix} Onboarding next-button absent but hash still on /onboarding — waiting`
      );
      await browser.pause(1_500);
      continue;
    }
    if (status === 'disabled') {
      // Some steps gate the button on async work (skill catalog fetch, local
      // AI download check). Give it a beat, then retry; if it stays disabled
      // for too long we bail rather than spinning forever.
      console.log(`${logPrefix} Onboarding step ${step}: next-button disabled — waiting`);
      await browser.pause(2_000);
      continue;
    }
    console.log(`${logPrefix} Onboarding step ${step}: clicked Continue`);
    await browser.pause(step >= 4 ? 3_000 : 1_500);
  }
  console.log(`${logPrefix} Onboarding hit max steps (${maxSteps}) — moving on`);
  await dismissWalkthroughIfVisible(8_000);
}

/**
 * Walk through onboarding if it is visible, or no-op if already on Home.
 *
 * Delegates to walkOnboarding, which polls up to 8 × 400 ms for the overlay
 * to appear before giving up — safe to call unconditionally after auth so
 * timing races do not cause the helper to skip onboarding prematurely.
 */
export async function completeOnboardingIfVisible(logPrefix = '[E2E]') {
  // Deep-link delivery resolves when the URL has reached the WebView, before
  // CoreStateProvider has necessarily applied the authenticated snapshot.
  // Waiting for that snapshot prevents a slow bootstrap from being mistaken
  // for an already-onboarded session when the onboarding button has not
  // mounted yet.
  // Desktop cold starts can spend most of the core's 60-second startup budget
  // before the loopback session is installed; auth bootstrap must have the
  // same budget instead of expiring at the old 20-second default.
  await waitForAuthenticatedCore(60_000);
  await walkOnboarding(logPrefix);
  const marker = await waitForHomePage(15_000);
  if (marker) return;
  if (supportsExecuteScript()) {
    const onChat = await browser.execute(() => window.location.hash.startsWith('#/chat'));
    if (onChat) {
      console.log(`${logPrefix} Onboarding complete; chat route accepted without home marker`);
      return;
    }
  }
  throw new Error('Onboarding completed but neither home nor chat became ready');
}

export async function waitForLoggedOutState(timeout = 10_000): Promise<string | null> {
  const welcomeCandidates = ['Welcome', 'Sign in', 'Login', 'Get Started'];
  const deadline = Date.now() + timeout;
  while (Date.now() < deadline) {
    for (const text of welcomeCandidates) {
      if (await textExists(text)) {
        return text;
      }
    }
    await browser.pause(500);
  }
  return null;
}

export async function logoutViaSettings(logPrefix = '[E2E]') {
  // Logout + Clear App Data moved out of the main /settings page and into
  // the Account section in PR #2550 (LogoutAndClearActions footer on
  // /settings/account). Navigate straight to the section that actually
  // renders the buttons.
  await navigateViaHash('/settings/account');

  try {
    await clickButton('Log out', 10_000);
  } catch (err) {
    const tree = await dumpAccessibilityTree();
    console.log(`${logPrefix} Logout button not found. Tree:\n`, tree.slice(0, 4000));
    throw new Error('Could not find logout button in Settings', { cause: err });
  }
  console.log(`${logPrefix} Logout clicked through the account action button`);

  await browser.pause(2_000);

  const hasConfirm =
    (await textExists('Confirm')) || (await textExists('Yes')) || (await textExists('Log Out'));
  if (hasConfirm) {
    const confirmed = await browser.execute(() => {
      const candidates = document.querySelectorAll('button, [role="button"], a');
      for (const el of candidates) {
        const text = el.textContent?.trim() || '';
        const label = el.getAttribute('aria-label') || '';
        if (
          ['Confirm', 'Yes', 'Log Out'].some(candidate => text === candidate || label === candidate)
        ) {
          (el as HTMLElement).click();
          return true;
        }
      }
      return false;
    });
    if (!confirmed) {
      throw new Error('Logout confirmation dialog appeared but confirm button was not clickable');
    }
    console.log(`${logPrefix} Logout confirmation accepted`);
  }

  const loggedOutMarker = await waitForLoggedOutState(10_000);
  if (!loggedOutMarker) {
    const tree = await dumpAccessibilityTree();
    console.log(`${logPrefix} Logged-out state not detected. Tree:\n`, tree.slice(0, 4000));
    throw new Error('Logged-out state was not visible after logout');
  }

  console.log(`${logPrefix} Logged-out state confirmed: "${loggedOutMarker}"`);
}

// ---------------------------------------------------------------------------
// Full login flow
// ---------------------------------------------------------------------------

/**
 * @param token          Deep link token string.
 * @param logPrefix      Prefix for console log lines.
 * @param postLoginVerifier  Optional async callback invoked after the Home page
 *   is confirmed.  Receives `logPrefix` so it can log consistently.  If the
 *   verifier throws, performFullLogin propagates the error — callers can use
 *   this to assert that auth side-effects (e.g. token consume, profile fetch)
 *   actually occurred rather than relying on UI alone.
 */
export async function performFullLogin(
  token = 'e2e-test-token',
  logPrefix = '[E2E]',
  postLoginVerifier?: (logPrefix: string) => Promise<void>
) {
  await triggerAuthDeepLink(token);
  await waitForWindowVisible(25_000);
  await waitForWebView(15_000);
  await waitForAppReady(15_000);
  // The embedded core's startup budget is 60 seconds, and OAuth readiness
  // waits for that core before installing the session. Keep this E2E wait
  // within the product's cold-start budget instead of failing while the
  // BootCheckGate is still bringing the core up.
  try {
    await waitForAuthenticatedCore(60_000);
  } catch (error) {
    // Report only endpoint names, never request bodies or credentials. This
    // distinguishes a core-readiness timeout from a backend auth request that
    // reached the mock but failed to complete.
    const authRequests = getRequestLog()
      .filter(entry => typeof entry.url === 'string' && entry.url.startsWith('/auth/'))
      .map(entry => `${entry.method} ${entry.url.split('?')[0]} -> ${entry.status ?? 'pending'}`);
    const coreState = await browser
      .execute(() => {
        const win = window as unknown as { __OPENHUMAN_CORE_STATE__?: () => any };
        const state = win.__OPENHUMAN_CORE_STATE__?.();
        return {
          isReady: state?.isReady ?? null,
          isBootstrapping: state?.isBootstrapping ?? null,
          hasSessionToken: Boolean(state?.snapshot?.sessionToken),
          hasAuthUser: Boolean(state?.snapshot?.auth?.userId),
          authStoreFailureKind:
            (window as unknown as { __OPENHUMAN_E2E_AUTH_STORE_FAILURE__?: string })
              .__OPENHUMAN_E2E_AUTH_STORE_FAILURE__ ?? null,
        };
      })
      .catch(() => null);
    const [sessionState, sessionToken] = await Promise.all([
      callOpenhumanRpc('openhuman.auth_get_state').catch(() => null),
      callOpenhumanRpc('openhuman.auth_get_session_token').catch(() => null),
    ]);
    const stateValue = sessionState?.result?.value ?? sessionState?.result;
    const tokenValue = sessionToken?.result?.value ?? sessionToken?.result;
    const sessionRpc = {
      stateOk: sessionState?.ok ?? false,
      authenticated: stateValue?.isAuthenticated ?? null,
      credential: stateValue?.credential ?? null,
      tokenOk: sessionToken?.ok ?? false,
      hasStoredToken: Boolean(tokenValue?.token),
      stateHttpStatus: sessionState?.httpStatus ?? null,
      stateErrorKind: safeRpcFailureKind(sessionState),
      stateErrorSummary: safeRpcErrorSummary(sessionState),
      tokenHttpStatus: sessionToken?.httpStatus ?? null,
      tokenErrorKind: safeRpcFailureKind(sessionToken),
      tokenErrorSummary: safeRpcErrorSummary(sessionToken),
    };
    console.error(
      `${logPrefix} auth bootstrap failed; mock auth endpoints=${JSON.stringify(authRequests)}; core state=${JSON.stringify(coreState)}; session RPC=${JSON.stringify(sessionRpc)}`
    );
    throw error;
  }

  await walkOnboarding(logPrefix);

  const homeText = await waitForHomePage(15_000);
  if (!homeText) {
    const tree = await dumpAccessibilityTree();
    console.log(`${logPrefix} Home page not reached after login. Tree:\n`, tree.slice(0, 4000));
    throw new Error('Full login did not reach Home page');
  }

  if (postLoginVerifier) {
    await postLoginVerifier(logPrefix);
  }

  console.log(`${logPrefix} Home page confirmed: found "${homeText}"`);
}