Files
orca/tests/e2e/helpers/workspace-layout-oracle-stream.ts
Neil 39902f398b refactor: bind browser and remote runtime consumers to concrete owners (#40135)
* refactor: use concrete browser and input owners

* refactor: bind remote runtime callers to concrete transport owners

* Use checked subscription mock argument types

* Type browser lease mock with its subscription contract

* refactor: bind sleeping VM status probe test to socket owner
2026-11-08 21:41:28 -08:00

65 lines
2.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Subscribe through the local renderer bridge or a headless host’s pairing offer.
import type { Page } from '@stablyai/playwright-test'
import { pairedRuntimeConnectionFromOffer } from '../../../src/shared/remote-runtime/pairing/paired-runtime-connection'
import { decodePairingOffer } from '../../../src/shared/remote-runtime/pairing/pairing'
import '../../../src/shared/remote-runtime/requests/remote-runtime-request-socket'
import { subscribeRemoteRuntimeTransport } from '../../../src/shared/remote-runtime/requests/remote-runtime-subscription-transport'
import type { RuntimeRpcResponse } from '../../../src/shared/runtime-rpc-envelope'
import type { LiveLayoutFeed } from './workspace-layout-oracle-feed'
declare global {
// oxlint-disable-next-line typescript-eslint/consistent-type-definitions -- declaration merging requires interface
interface Window {
/** Forwards each layout stream response from the renderer to the oracle. */
__oracleLayoutFrame?: (response: RuntimeRpcResponse<unknown>) => Promise<void>
}
}
function applyResponse(feed: LiveLayoutFeed, response: RuntimeRpcResponse<unknown>): void {
if (response.ok) {
feed.apply(response.result)
} else {
feed.fail(`layout stream error: ${response.error.code}: ${response.error.message}`)
}
}
// Bindings survive a reload, and a second registration on one page throws.
const boundPages = new WeakSet<Page>()
/** Subscribes in the window's renderer; call again after a reload, which ends the old stream. */
export async function subscribeWindowLayout(page: Page, feed: LiveLayoutFeed): Promise<void> {
feed.reopening()
if (!boundPages.has(page)) {
boundPages.add(page)
await page.exposeFunction('__oracleLayoutFrame', (response: RuntimeRpcResponse<unknown>) =>
applyResponse(feed, response)
)
}
await page.evaluate(async () => {
await window.api.runtime.subscribe({ method: 'layout.subscribe', params: {} }, (response) => {
void window.__oracleLayoutFrame?.(response)
})
})
}
/** Subscribes over a headless host's pairing offer; a serve restart ends it, so reopen after one. */
export async function subscribeHeadlessLayout(
pairingUrl: string,
feed: LiveLayoutFeed
): Promise<{ close: () => void }> {
feed.reopening()
const subscription = await subscribeRemoteRuntimeTransport<unknown>(
pairedRuntimeConnectionFromOffer(decodePairingOffer(pairingUrl)),
'layout.subscribe',
{},
30_000,
{
onResponse: (response) => applyResponse(feed, response),
onError: (error) => feed.fail(`layout stream error: ${error.message}`),
onClose: () => feed.reopening()
}
)
return { close: () => subscription.close() }
}