Skip to content

The agent that grows with you

test(webapp): check every smoke viewport in the tab the launch link signed in

emozilla1eb4669e8ddf
diff --git a/apps/desktop/scripts/smoke-browser-host-tab.mjs b/apps/desktop/scripts/smoke-browser-host-tab.mjs
new file mode 100644
index 00000000000..111308d8b18
--- /dev/null
+++ b/apps/desktop/scripts/smoke-browser-host-tab.mjs
@@ -0,0 +1,60 @@
+// The browser smoke checks every viewport in one tab, for the launch link's sake.
+
+/** *href* without its fragment: the address the document is requested from. A launch link's ticket
+ * rides in the fragment, which navigation keeps and HTTP responses never contain. */
+export function documentAddress(href) {
+ const address = new URL(href)
+ address.hash = ''
+
+ return address.href
+}
+
+/**
+ * Loads each smoke viewport in one browser tab. The smoke's input URL may be the one-use launch link
+ * `hermes webapp --no-open` prints: loading it spends the ticket, and only the tab that spent it keeps
+ * the grant it bought (the Webapp keeps it in that tab's sessionStorage). So the first viewport opens
+ * the input URL, and every later one reopens, in the same tab, the address the first one landed on.
+ */
+export class ViewportTab {
+ constructor(client, url) {
+ this.client = client
+ this.url = url
+ this.landed = null
+ }
+
+ /**
+ * Sizes the tab for *viewport* and loads its page, after leaving the previous viewport's page for
+ * about:blank so that page's teardown is over before *beforeLoad* runs. *beforeLoad* gets the
+ * address this load requests, the one whose HTTP status the checks read. Resolves with it once the
+ * page's DOM is ready.
+ */
+ async open(viewport, beforeLoad) {
+ const url = this.landed ?? this.url
+
+ if (this.landed) {
+ const blank = this.client.waitFor('Page.loadEventFired', 30_000)
+ await this.client.send('Page.navigate', { url: 'about:blank' })
+ await blank
+ }
+
+ beforeLoad(documentAddress(url))
+ await this.client.send('Emulation.setDeviceMetricsOverride', {
+ width: viewport.width,
+ height: viewport.height,
+ deviceScaleFactor: 1,
+ mobile: viewport.mobile ?? false
+ })
+ // A mobile viewport's browser-controls override outlives its page in this tab.
+ await this.client.send('Emulation.setSmallViewportHeightDifferenceOverride', { difference: 0 })
+ const domReady = this.client.waitFor('Page.domContentEventFired', 30_000)
+ await this.client.send('Page.navigate', { url })
+ await domReady
+
+ return documentAddress(url)
+ }
+
+ /** Remembers where the first viewport's page landed: the Webapp itself, for a launch link. */
+ async settle(evaluate) {
+ this.landed ??= documentAddress(await evaluate('location.href'))
+ }
+}
diff --git a/apps/desktop/scripts/smoke-browser-host-tab.test.mjs b/apps/desktop/scripts/smoke-browser-host-tab.test.mjs
new file mode 100644
index 00000000000..1048f191434
--- /dev/null
+++ b/apps/desktop/scripts/smoke-browser-host-tab.test.mjs
@@ -0,0 +1,101 @@
+import { describe, expect, it } from 'vitest'
+
+import { documentAddress, ViewportTab } from './smoke-browser-host-tab.mjs'
+
+const LAUNCH = 'http://127.0.0.1:9120/webapp/launch#ticket=' + 't'.repeat(43)
+
+/** A CDP client whose navigations load at once; it records what the smoke asked of the tab. */
+function fakeClient() {
+ const calls = []
+ const waiting = new Map()
+
+ return {
+ calls,
+ async send(method, params = {}) {
+ calls.push([method, params])
+
+ if (method === 'Page.navigate') {
+ for (const event of ['Page.domContentEventFired', 'Page.loadEventFired']) {
+ waiting.get(event)?.()
+ waiting.delete(event)
+ }
+ }
+
+ return {}
+ },
+ waitFor(event) {
+ return new Promise(resolve => waiting.set(event, resolve))
+ }
+ }
+}
+
+const navigations = calls => calls.filter(([method]) => method === 'Page.navigate').map(([, params]) => params.url)
+
+describe('browser smoke viewport tab', () => {
+ // The smoke's input URL may be the one-use launch link `hermes webapp --no-open` prints, and only
+ // the tab that spent it keeps the grant it bought. Every viewport therefore loads in one tab: the
+ // first opens the input URL, and each later one reopens the address the first one landed on.
+ it('spends a launch link once and reopens the Webapp it landed on for every later viewport', async () => {
+ const client = fakeClient()
+ const tab = new ViewportTab(client, LAUNCH)
+ const loads = []
+
+ loads.push(await tab.open({ height: 390, width: 844, mobile: true }, address => loads.push(['before', address])))
+ await tab.settle(async expression =>
+ expression === 'location.href' ? 'http://127.0.0.1:9120/?profile=work#/' : null
+ )
+ loads.push(await tab.open({ height: 800, width: 1280 }, address => loads.push(['before', address])))
+ loads.push(await tab.open({ height: 844, width: 390 }, address => loads.push(['before', address])))
+
+ expect(navigations(client.calls)).toEqual([
+ LAUNCH,
+ 'about:blank',
+ 'http://127.0.0.1:9120/?profile=work',
+ 'about:blank',
+ 'http://127.0.0.1:9120/?profile=work'
+ ])
+ // HTTP responses never carry the fragment: each load's status is read for the address without it.
+ expect(loads).toEqual([
+ ['before', 'http://127.0.0.1:9120/webapp/launch'],
+ 'http://127.0.0.1:9120/webapp/launch',
+ ['before', 'http://127.0.0.1:9120/?profile=work'],
+ 'http://127.0.0.1:9120/?profile=work',
+ ['before', 'http://127.0.0.1:9120/?profile=work'],
+ 'http://127.0.0.1:9120/?profile=work'
+ ])
+ })
+
+ it('leaves the previous page before a later viewport starts counting its errors', async () => {
+ const client = fakeClient()
+ const tab = new ViewportTab(client, 'http://127.0.0.1:9120/')
+ await tab.open({ height: 390, width: 844 }, () => {})
+ await tab.settle(async () => 'http://127.0.0.1:9120/#/')
+
+ let callsBeforeLoad = -1
+ await tab.open({ height: 800, width: 1280 }, () => {
+ callsBeforeLoad = client.calls.length
+ })
+
+ const blank = client.calls.findIndex(
+ ([method, params]) => method === 'Page.navigate' && params.url === 'about:blank'
+ )
+ expect(blank).toBeGreaterThanOrEqual(0)
+ expect(callsBeforeLoad).toBeGreaterThan(blank)
+ })
+
+ it('sizes the tab for each viewport and clears the mobile browser-controls override', async () => {
+ const client = fakeClient()
+ const tab = new ViewportTab(client, 'http://127.0.0.1:9120/')
+ await tab.open({ height: 800, width: 1280 }, () => {})
+
+ expect(client.calls).toContainEqual([
+ 'Emulation.setDeviceMetricsOverride',
+ { deviceScaleFactor: 1, height: 800, mobile: false, width: 1280 }
+ ])
+ expect(client.calls).toContainEqual(['Emulation.setSmallViewportHeightDifferenceOverride', { difference: 0 }])
+ })
+
+ it('reads a document address without its fragment', () => {
+ expect(documentAddress(LAUNCH)).toBe('http://127.0.0.1:9120/webapp/launch')
+ })
+})
diff --git a/apps/desktop/scripts/smoke-browser-host.mjs b/apps/desktop/scripts/smoke-browser-host.mjs
index ed3510aa436..2db77978550 100644
--- a/apps/desktop/scripts/smoke-browser-host.mjs
+++ b/apps/desktop/scripts/smoke-browser-host.mjs
@@ -9,11 +9,9 @@ import process from 'node:process'
import { CDP, sleep } from './perf/lib/cdp.mjs'
import { shownStartupTitlesExpression, startupScreenFailures } from './smoke-browser-host-screens.mjs'
+import { ViewportTab } from './smoke-browser-host-tab.mjs'
const url = process.env.HERMES_BROWSER_HOST_URL || process.argv[2] || 'http://127.0.0.1:9120/'
-// Navigation keeps the private launch fragment; HTTP responses never contain it.
-const documentUrl = new URL(url)
-documentUrl.hash = ''
const allowGatewayFailure = process.env.HERMES_BROWSER_ALLOW_GATEWAY_FAILURE === '1'
const requireTerminal = process.env.HERMES_BROWSER_REQUIRE_TERMINAL === '1'
// The gateway serving the page is already up, so its socket opens shortly after the shell paints.
@@ -289,57 +287,64 @@ try {
browserControl = await CDP.open(browserVersion.webSocketDebuggerUrl)
}
+ // One tab for every viewport: a launch link signs in only the tab that spends it (ViewportTab).
+ const target = await createTarget(debugPort)
+ const client = await CDP.open(target.webSocketDebuggerUrl)
+ const evaluate = expression => client.eval(expression, { userGesture: true })
+ const tab = new ViewportTab(client, url)
+
+ // What the current viewport's page load did; each load starts a new one. A request is counted
+ // against the load that sent it.
+ let page = null
+ const requests = new Map()
+
+ client.on('Runtime.consoleAPICalled', params => {
+ if (params.type !== 'error') return
+ const text = consoleText(params)
+ if (!ignoredConsoleError(text)) page?.consoleErrors.push(text)
+ })
+ client.on('Runtime.exceptionThrown', params => {
+ const error = params.exceptionDetails?.exception?.description || params.exceptionDetails?.text || 'page exception'
+ page?.pageErrors.push(error)
+ })
+ client.on('Network.requestWillBeSent', params => {
+ requests.set(params.requestId, { page, url: params.request?.url || '' })
+ })
+ client.on('Network.webSocketCreated', params => {
+ if (String(params.url || '').includes('/api/ws')) page?.gatewaySockets.push(new URL(params.url))
+ })
+ client.on('Network.responseReceived', params => {
+ if (page && params.type === 'Document' && params.response?.url === page.documentUrl) {
+ page.httpStatus = params.response.status
+ }
+ })
+ client.on('Network.loadingFailed', params => {
+ const request = requests.get(params.requestId)
+ const requestUrl = request?.url || ''
+ if (allowGatewayFailure && requestUrl.includes('/api/ws')) return
+ const owner = request ? request.page : page
+ owner?.failedRequests.push(`${requestUrl || params.requestId} (${params.errorText || 'request failed'})`)
+ })
+
+ await Promise.all([client.send('Page.enable'), client.send('Runtime.enable'), client.send('Network.enable')])
+
for (const viewport of [
{ width: 844, height: 390, mobile: true, smallViewportHeightDifference: 120 },
{ width: 1280, height: 800 },
{ width: 390, height: 844 },
{ width: 320, height: 568 }
]) {
- const target = await createTarget(debugPort)
- const client = await CDP.open(target.webSocketDebuggerUrl)
- const evaluate = expression => client.eval(expression, { userGesture: true })
-
- const consoleErrors = []
- const pageErrors = []
- const failedRequests = []
- const requestUrls = new Map()
- let httpStatus = null
-
- client.on('Runtime.consoleAPICalled', params => {
- if (params.type !== 'error') return
- const text = consoleText(params)
- if (!ignoredConsoleError(text)) consoleErrors.push(text)
- })
- client.on('Runtime.exceptionThrown', params => {
- const error = params.exceptionDetails?.exception?.description || params.exceptionDetails?.text || 'page exception'
- pageErrors.push(error)
- })
- client.on('Network.requestWillBeSent', params => {
- requestUrls.set(params.requestId, params.request?.url || '')
+ await tab.open(viewport, documentUrl => {
+ page = {
+ consoleErrors: [],
+ documentUrl,
+ failedRequests: [],
+ gatewaySockets: [],
+ httpStatus: null,
+ pageErrors: []
+ }
})
- const gatewaySockets = []
- client.on('Network.webSocketCreated', params => {
- if (String(params.url || '').includes('/api/ws')) gatewaySockets.push(new URL(params.url))
- })
- client.on('Network.responseReceived', params => {
- if (params.type === 'Document' && params.response?.url === documentUrl.href) httpStatus = params.response.status
- })
- client.on('Network.loadingFailed', params => {
- const requestUrl = requestUrls.get(params.requestId) || ''
- if (allowGatewayFailure && requestUrl.includes('/api/ws')) return
- failedRequests.push(`${requestUrl || params.requestId} (${params.errorText || 'request failed'})`)
- })
-
- await Promise.all([client.send('Page.enable'), client.send('Runtime.enable'), client.send('Network.enable')])
- await client.send('Emulation.setDeviceMetricsOverride', {
- width: viewport.width,
- height: viewport.height,
- deviceScaleFactor: 1,
- mobile: viewport.mobile ?? false
- })
- const domReady = client.waitFor('Page.domContentEventFired', 30_000)
- await client.send('Page.navigate', { url })
- await domReady
+ const { consoleErrors, failedRequests, gatewaySockets, pageErrors } = page
await sleep(4_000)
const socketDeadline = Date.now() + GATEWAY_SOCKET_TIMEOUT_MS
while (
@@ -375,6 +380,7 @@ try {
if (viewport.smallViewportHeightDifference) mobileViewportState = await evaluate(mobileViewportExpression)
if (requireTerminal && viewport.width === 1280) terminalState = await evaluate(terminalExpression)
+ const httpStatus = page.httpStatus
const failures = []
if (httpStatus === null || httpStatus >= 400) failures.push(`HTTP ${httpStatus ?? 'no response'}`)
if (state.host !== 'browser' || !state.bridge) failures.push('browser Desktop bridge did not install')
@@ -431,18 +437,20 @@ try {
2
)
)
- try {
- await client.send('Page.close')
- } catch {
- /* target may already be closing */
- }
- client.close()
if (failures.length) {
process.exitCode = 1
break
}
+ await tab.settle(evaluate)
+ }
+
+ try {
+ await client.send('Page.close')
+ } catch {
+ /* target may already be closing */
}
+ client.close()
} finally {
// Ask Chromium to shut down through its own browser-level CDP endpoint first.
// This lets profile writers and helper processes flush before we remove the