diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1e3c7d74..bdf8775b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -30,6 +30,11 @@ jobs: # the panel Chrome layout pass must run here; a missing browser is a failure, never a skip PANEL_CHROME_REQUIRED: "1" + - name: Test webview + run: pnpm --filter agentnet-webview test + - name: Build webview + run: pnpm --filter agentnet-webview build + # The runtime smoke test (pnpm run test:run) is intentionally NOT run here: # it drives the real claude + codex CLIs and needs them installed and logged # in, which a CI runner has not. Keep it as a local/manual integration check. diff --git a/docs/qa/google-login-navigation.md b/docs/qa/google-login-navigation.md new file mode 100644 index 00000000..36666b1a --- /dev/null +++ b/docs/qa/google-login-navigation.md @@ -0,0 +1,43 @@ +# Google login navigation — 2026-09-17 + +Base: `bfbe88ec` (includes merged #245). + +## Browser reproduction + +A temporary Vite page invoked the exact pre-fix helper and the patched imported +`openExternalUrl` from separate buttons, targeting a local HTML page. Chrome was +controlled through the browser tool; no Google credentials or grants were involved. + +| Check | Before | After | +| --- | --- | --- | +| Original app page stays open | No; navigated to destination | Yes | +| Destination tabs opened per click | Two, including original tab | One new tab | +| Destination `window.opener` | null | null | +| New destination `document.referrer` | Not recorded before subsequent dev reloads | empty | + +The original helper interprets the null return from successful +`window.open(url, '_blank', 'noopener,noreferrer')` as popup blocking and navigates +the original tab too. The fix opens a same-origin blank tab, clears its opener, +then follows a noreferrer link. A genuinely blocked popup retains same-tab fallback. + +## Regression coverage + +- Real App, Sessions and ConnectDriveForm mounted in the React test, with a mocked + host store and external opener: current main opens three times across settings + remounts; the fixed App owner opens once. StrictMode and a second OAuth attempt + are included. The test fails on main and passes on this change. +- Helper tests cover new-tab navigation, opener isolation/referrer policy, blocked + popup fallback, and the unchanged native Android bridge. +- Browser checks cover navigation mechanics, not a fresh Google token exchange. + No OAuth backend, permissions, storage or wallet behavior is changed. + +## Checks + +- Webview: 11 tests passed; production build passed (existing chunk-size warning). +- Core: 547 passed, 5 skipped with two workers; typecheck passed. +- The first core run, concurrent with webview tests/build, exceeded the existing + one-second wait in `cloud-resume.spec.ts` (30 messages observed before the new + append finished). The unchanged suite passed with bounded concurrency. No runtime + or storage code was modified by this PR. +- Webview tests and build are now included in CI so these regressions are checked + on clean installs rather than relying only on this local run. diff --git a/surfaces/webview/src/App.tsx b/surfaces/webview/src/App.tsx index 16363e01..e3e15775 100644 --- a/surfaces/webview/src/App.tsx +++ b/surfaces/webview/src/App.tsx @@ -14,6 +14,7 @@ import { Alert } from "./Alert"; import { useVisualViewportVars, useKeyboardChrome, useIsDesktop } from "./layoutEffects"; import { useEffect, useRef, useState, type PointerEvent } from "react"; import { syncAgentService, notifyApproval, clearApprovalNotice, ensureBackgroundConsent, notifyTurnComplete } from "./platform/agentService"; +import { useAutoOpenExternalUrl } from "./platform/useAutoOpenExternalUrl"; import { haptics } from "./haptics"; // Phase router. Entry is NEVER blocked: `init` drops a fresh user straight into a @@ -28,6 +29,7 @@ import { haptics } from "./haptics"; // chat → runtime ready → the tab shell export function App() { const { state, getClientId, send, closeMarket, clearCelebrate, dismissAuth } = useStore(); + useAutoOpenExternalUrl(state.googleLoginUrl); useVisualViewportVars(); useKeyboardChrome(); diff --git a/surfaces/webview/src/chat/Sessions.tsx b/surfaces/webview/src/chat/Sessions.tsx index 50f040a2..1333806c 100644 --- a/surfaces/webview/src/chat/Sessions.tsx +++ b/surfaces/webview/src/chat/Sessions.tsx @@ -36,7 +36,6 @@ function SyncIcon({ className, style }: { className?: string; style?: CSSPropert } import { forgetAndroidWallet } from "../onboarding/androidWallet"; import { openExternalUrl } from "../platform/openExternalUrl"; -import { useAutoOpenExternalUrl } from "../platform/useAutoOpenExternalUrl"; import { HeliusKeyForm } from "../settings/HeliusKeyForm"; // Leaf subpath (browser-safe, no node imports) so the bundle stays free of the Node SDK. import { CUSTOM_ENGINE_EGRESS_WARNING, CUSTOM_ENGINE_TOOL_WARNING } from "@iqlabs-official/agent-sdk/account/customEngineMeta"; @@ -335,7 +334,6 @@ export function Sessions({ const info = storage?.info as { kind?: string; connected?: boolean; account?: string; location?: string } | null; const cloudConnected = !!(info && info.connected && info.kind !== "local"); - useAutoOpenExternalUrl(googleLoginUrl); useEffect(() => { send({ type: "getRpcStatus" }); diff --git a/surfaces/webview/src/googleLogin.spec.tsx b/surfaces/webview/src/googleLogin.spec.tsx new file mode 100644 index 00000000..4c0f5af0 --- /dev/null +++ b/surfaces/webview/src/googleLogin.spec.tsx @@ -0,0 +1,47 @@ +import { act, StrictMode } from "react"; +import { createRoot } from "react-dom/client"; +import { afterEach, expect, it, vi } from "vitest"; +import { App } from "./App"; +import { ConnectDriveForm } from "./settings/ConnectDriveForm"; +import { initialState } from "./state/store"; +import { LangProvider } from "./i18n"; +import { openExternalUrl } from "./platform/openExternalUrl"; + +const store = vi.hoisted(() => ({ state: {} as any, send: vi.fn(), getClientId: () => "qa", closeMarket: vi.fn() })); +vi.mock("./state/store", async importOriginal => ({ ...await importOriginal(), useStore: () => store })); +vi.mock("./platform/openExternalUrl", () => ({ openExternalUrl: vi.fn() })); +vi.mock("./unlock/WelcomeTutorial", () => ({ WelcomeTutorial: () => null })); +vi.mock("./unlock/StarterTemplates", () => ({ StarterTemplates: () => null })); +vi.mock("./layoutEffects", () => ({ useVisualViewportVars: () => {}, useKeyboardChrome: () => {}, useIsDesktop: () => true, useOnline: () => true })); +vi.mock("./unlock/UnlockProvider", () => ({ useUnlock: () => ({ requestUnlock: vi.fn() }), LockedGate: () => null, LinkRow: () => null })); + +afterEach(() => vi.clearAllMocks()); +it("opens once across mounted Drive UI, settings remounts and StrictMode, but permits a new attempt", async () => { + (globalThis as any).IS_REACT_ACT_ENVIRONMENT = true; + store.state = { ...initialState, googleLoginUrl: "https://accounts.google.com/qa?state=one" }; + const host = document.createElement("div"); + document.body.append(host); + const root = createRoot(host); + const render = () => act(async () => root.render()); + try { + await render(); + expect(openExternalUrl).toHaveBeenCalledTimes(1); + // Sessions mounts for engine settings and unmounts on leaving that screen. + store.state = { ...store.state, phase: "customAuth" }; + await render(); + store.state = { ...store.state, phase: "connecting" }; + await render(); + store.state = { ...store.state, phase: "customAuth" }; + await render(); + expect(openExternalUrl).toHaveBeenCalledTimes(1); + store.state = { ...store.state, googleLoginUrl: null }; + await render(); + store.state = { ...store.state, googleLoginUrl: "https://accounts.google.com/qa?state=two" }; + await render(); + expect(openExternalUrl).toHaveBeenCalledTimes(2); + expect(openExternalUrl).toHaveBeenLastCalledWith(store.state.googleLoginUrl); + } finally { + await act(async () => root.unmount()); + host.remove(); + } +}); diff --git a/surfaces/webview/src/platform/openExternalUrl.spec.ts b/surfaces/webview/src/platform/openExternalUrl.spec.ts new file mode 100644 index 00000000..055550c2 --- /dev/null +++ b/surfaces/webview/src/platform/openExternalUrl.spec.ts @@ -0,0 +1,34 @@ +import { afterEach, expect, it, vi } from "vitest"; +import { openExternalUrl } from "./openExternalUrl"; + +afterEach(() => vi.unstubAllGlobals()); + +it("opens one browser tab and clears its opener before navigating", () => { + const link = { href: "", rel: "", click: vi.fn(() => expect(tab.opener).toBeNull()) }; + const document = { createElement: vi.fn(() => link) }; + const tab = { document, opener: {} as unknown }; + const open = vi.fn(() => tab), assign = vi.fn(); + vi.stubGlobal("window", { open, location: { assign } }); + openExternalUrl("https://accounts.google.com/example"); + expect(open).toHaveBeenCalledExactlyOnceWith("about:blank", "_blank"); + expect(link.href).toBe("https://accounts.google.com/example"); + expect(link.rel).toBe("noreferrer"); + expect(link.click).toHaveBeenCalledOnce(); + expect(assign).not.toHaveBeenCalled(); +}); + +it("uses same-tab navigation when a popup is blocked", () => { + const assign = vi.fn(); + vi.stubGlobal("window", { open: vi.fn(() => null), location: { assign } }); + openExternalUrl("https://accounts.google.com/example"); + expect(assign).toHaveBeenCalledExactlyOnceWith("https://accounts.google.com/example"); +}); + +it("delegates to the native Android bridge without browser navigation", () => { + const openUrl = vi.fn(), open = vi.fn(), assign = vi.fn(); + vi.stubGlobal("window", { AgentNetShell: { openUrl }, open, location: { assign } }); + openExternalUrl("https://accounts.google.com/example"); + expect(openUrl).toHaveBeenCalledExactlyOnceWith("https://accounts.google.com/example"); + expect(open).not.toHaveBeenCalled(); + expect(assign).not.toHaveBeenCalled(); +}); diff --git a/surfaces/webview/src/platform/openExternalUrl.ts b/surfaces/webview/src/platform/openExternalUrl.ts index 836b5eba..8b7f6420 100644 --- a/surfaces/webview/src/platform/openExternalUrl.ts +++ b/surfaces/webview/src/platform/openExternalUrl.ts @@ -4,6 +4,17 @@ export function openExternalUrl(url: string): void { window.AgentNetShell.openUrl(url); return; } - const opened = window.open(url, "_blank", "noopener,noreferrer"); - if (!opened) window.location.assign(url); + // noopener makes window.open return null even when it succeeds, so it cannot + // distinguish a blocked popup. Open an empty tab, sever its opener, then navigate. + const opened = window.open("about:blank", "_blank"); + if (opened) { + opened.opener = null; + // Navigate through a noreferrer link to preserve the original referrer policy. + const link = opened.document.createElement("a"); + link.href = url; + link.rel = "noreferrer"; + link.click(); + } else { + window.location.assign(url); + } } diff --git a/surfaces/webview/src/settings/ConnectDriveForm.tsx b/surfaces/webview/src/settings/ConnectDriveForm.tsx index 97f93352..b7fdb39a 100644 --- a/surfaces/webview/src/settings/ConnectDriveForm.tsx +++ b/surfaces/webview/src/settings/ConnectDriveForm.tsx @@ -1,7 +1,6 @@ import { useEffect, useRef, useState } from "react"; import { useStore } from "../state/store"; import { openExternalUrl } from "../platform/openExternalUrl"; -import { useAutoOpenExternalUrl } from "../platform/useAutoOpenExternalUrl"; // Compact Google Drive connect for the unlock tutorial's Cloud_Backup step: start OAuth, // auto-open the URL, manual-code fallback, dev client-id entry — trimmed to a single @@ -21,7 +20,6 @@ export function ConnectDriveForm({ onDone, skipLabel = "Skip for now" }: { onDon const [savingCreds, setSavingCreds] = useState(false); const advanced = useRef(false); - useAutoOpenExternalUrl(googleLoginUrl); // Drive reported connected → advance exactly once. useEffect(() => {