import { mkdtemp, rm } from 'node:fs/promises' import { tmpdir } from 'node:os' import { join } from 'node:path' import { afterAll, beforeAll, describe, expect, it } from 'vitest' import { chromium } from 'playwright-core' import { buildMobileWebAppBundle } from '../../build/mobile-web-app/build-mobile-web-app-bundle.mjs' import { mobileWebAppDependenciesPresent } from '../mobile-web-app-bundle-dependencies.mjs' import { createBundleServer, installShellDouble, readBridgeFaultGrant, readBridgeProtocolVersion, readShellCsp } from '../preview/mobile-web-app-render-harness.mjs' // Wide sidebar pushes Tasks through useRouteHandoff; local navigation inherits opener grants and silently refuses copy actions. // Browser check proves control is present/reachable and shell handoff leaves document in place. const HOST_ROUTE = '/h/render-check-host' const HOST_PATTERN = '/h/[hostId]' const TASKS_PATTERN = '/h/[hostId]/tasks' /** The bundle key for the target route, so its chunk can be named rather than inferred. */ const TASKS_ROUTE_KEY = './h/[hostId]/tasks.tsx' const FILES_PATTERN = '/h/[hostId]/files/[worktreeId]' const SHELL_SESSION_ID = 'render-check-session' const SHELL_BUILD_ID = 'render-check-build' const SHELL_HOST = { id: 'render-check-host', name: 'Render Check Host', endpoint: 'ws://render-check', lastConnected: 1 } /** The manifest's own pairs, as the shell would send them. */ const PAGE_ROUTE_GRANTS = [ { pathname: HOST_PATTERN, grants: ['navigate', 'storage', 'externalLink', 'haptics'] }, { pathname: FILES_PATTERN, grants: ['navigate', 'storage', 'externalLink', 'haptics'] }, { pathname: TASKS_PATTERN, grants: ['navigate', 'storage', 'externalLink', 'haptics', 'native.clipboard.write'] } ] /** Wide enough for `app/h/_layout.tsx` to render the sidebar beside the route. */ const WIDE = { width: 1180, height: 820 } const bundles = mobileWebAppDependenciesPresent() const describeRender = bundles ? describe : describe.skip let scratch let server let browser let origin let cspHeader = null let bridgeVersion = null let faultGrant = null // Resolve target chunk from build; content-hash URLs cannot identify routes by name. let tasksChunk = null beforeAll(async () => { if (!bundles) { return } cspHeader = await readShellCsp() bridgeVersion = await readBridgeProtocolVersion() faultGrant = await readBridgeFaultGrant() scratch = await mkdtemp(join(tmpdir(), 'orca-mobile-web-app-handoff-')) const built = await buildMobileWebAppBundle({ outDir: join(scratch, 'bundle') }) tasksChunk = built.routeChunks[TASKS_ROUTE_KEY] // Require resolvable target chunk or every absence assertion could pass on a typo. expect(tasksChunk, `no chunk for ${TASKS_ROUTE_KEY}`).toBeTruthy() const served = await createBundleServer({ outDir: built.outDir, cspHeader }) server = served.server origin = served.origin const executablePath = process.env.ORCA_MOBILE_WEB_RENDER_BROWSER browser = await chromium.launch({ headless: true, ...(executablePath ? { executablePath } : {}) }) }, 180_000) afterAll(async () => { await browser?.close() server?.close() if (scratch) { await rm(scratch, { recursive: true, force: true }) } }) /** Opens the worktree list at a viewport, under a named set of session grants. */ async function openHostRoute({ viewport, grants, pageRouteGrants = PAGE_ROUTE_GRANTS, route = HOST_ROUTE, awaitText = SHELL_HOST.name, ownsHostArea = false }) { const page = await browser.newPage({ viewport }) await page.addInitScript(installShellDouble, { version: bridgeVersion, sessionId: SHELL_SESSION_ID, buildId: SHELL_BUILD_ID, route: { pathname: route }, host: SHELL_HOST, storage: {}, faultGrant, grants, pageRoutes: [HOST_PATTERN, FILES_PATTERN, TASKS_PATTERN], pageRouteGrants, ownsHostArea }) const errors = [] // Record every response status: failed chunk fetches must count as requests, not false evidence of no navigation. const jsResponses = [] page.on('pageerror', (error) => errors.push(`${error.name}: ${error.message}`)) page.on('console', (message) => { if (message.type() === 'error') { errors.push(`console.error: ${message.text()}`) } }) page.on('response', (response) => { const path = new URL(response.url()).pathname if (!path.endsWith('.js')) { return } jsResponses.push({ status: response.status(), path }) }) await page.goto(`${origin}/`, { waitUntil: 'load' }) await page.waitForFunction(() => document.documentElement.dataset.orcaWebEntry === 'mounted', { timeout: 30_000, polling: 250 }) await page.waitForFunction((needle) => document.body.innerText.includes(needle), awaitText, { timeout: 30_000, polling: 250 }) return { page, errors, jsResponses } } /** Every `navigate` notify the page posted, in order. */ function navigates(page) { return page.evaluate(() => (globalThis.__orcaRenderCheckNotifies ?? []).filter((frame) => frame.name === 'navigate') ) } // Race navigation wait with page errors; React Navigation throws once blanked document and hid behind a bare 30s timeout. /** Tasks sits in the host header's ⋯ menu; open the menu and pick it, as a user does. */ async function openTasksFromHeader( page, more = page.getByRole('button', { name: 'More actions', exact: true }).first() ) { await more.click() await page.getByRole('menuitem', { name: 'Tasks', exact: true }).click() } async function waitForTasksRoute(page, opened, clickedAt) { try { await page.waitForFunction(() => location.pathname.endsWith('/tasks'), { timeout: 30_000, polling: 250 }) } catch (cause) { const seen = await page.evaluate(() => ({ pathname: location.pathname, text: document.body.innerText.slice(0, 300) })) throw new Error( [ `the document never reached /tasks; pathname is ${seen.pathname}`, `page errors: ${JSON.stringify(opened.errors)}`, `navigate notifies: ${JSON.stringify(await navigates(page))}`, `body text: ${JSON.stringify(seen.text)}`, `js responses since the click: ${JSON.stringify(opened.jsResponses.slice(clickedAt))}` ].join('\n'), { cause } ) } } describeRender('the sidebar hop to tasks, under the session it was opened with', () => { it('hands the hop to the shell when the session cannot cover tasks', async () => { const opened = await openHostRoute({ viewport: WIDE, grants: [faultGrant, 'navigate', 'storage', 'haptics'] }) const { page, errors, jsResponses } = opened // The header's own menu, by the names a user reads; it is the sidebar's on a wide layout. await openTasksFromHeader(page) await expect .poll(() => navigates(page)) .toEqual([ { v: bridgeVersion, type: 'notify', name: 'navigate', href: `${HOST_ROUTE}/tasks` } ]) expect(await navigates(page)).toEqual([ { v: bridgeVersion, type: 'notify', name: 'navigate', href: `${HOST_ROUTE}/tasks` } ]) // Require original route and no target-chunk request at any status; opener chunks can still arrive after click and must not count as target navigation. expect(await page.evaluate(() => location.pathname)).toBe(HOST_ROUTE) expect(jsResponses.filter(({ path }) => path.endsWith(tasksChunk))).toEqual([]) expect(errors).toEqual([]) await page.close() }, 60_000) it('keeps the hop in the document when the session covers tasks', async () => { // Grant-positive control proves the same tap can navigate; absence alone could pass on an inert page. const opened = await openHostRoute({ viewport: WIDE, grants: [ faultGrant, 'navigate', 'storage', 'externalLink', 'haptics', 'native.clipboard.write' ] }) const { page, errors } = opened const clickedAt = opened.jsResponses.length await openTasksFromHeader(page) await waitForTasksRoute(page, opened, clickedAt) expect(await navigates(page)).toEqual([]) expect(errors).toEqual([]) await page.close() }, 60_000) it('keeps the old behaviour when the shell sent no pairs at all', async () => { // Older shells omit coverage field; absence must not force every hop into shell handoff. const opened = await openHostRoute({ viewport: WIDE, grants: [faultGrant, 'navigate', 'storage', 'haptics'], pageRouteGrants: null }) const { page, errors } = opened const clickedAt = opened.jsResponses.length await openTasksFromHeader(page) await waitForTasksRoute(page, opened, clickedAt) expect(await navigates(page)).toEqual([]) expect(errors).toEqual([]) await page.close() }, 60_000) })