Skip to content

Commit 5ab430f

Browse files
committed
docs: document browser server context
1 parent 29ddb9d commit 5ab430f

18 files changed

Lines changed: 122 additions & 3 deletions

extensions/browser/src/browser/server-context.availability.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
/**
2+
* Browser profile availability operations: reachability probes, managed Chrome
3+
* launch/restart, Chrome MCP attach, and profile stop handling.
4+
*/
15
import fs from "node:fs";
26
import { resolveCdpReachabilityPolicy } from "./cdp-reachability-policy.js";
37
import {
@@ -133,6 +137,7 @@ function assertManagedLaunchNotCoolingDown(profileName: string, profileState: Pr
133137
);
134138
}
135139

140+
/** Builds reachability, ensure, and stop operations for one resolved browser profile. */
136141
export function createProfileAvailability({
137142
opts,
138143
profile,

extensions/browser/src/browser/server-context.chrome-test-harness.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Shared Chrome module mocks for Browser server-context tests.
3+
*/
14
import { vi } from "vitest";
25
import { installChromeUserDataDirHooks } from "./chrome-user-data-dir.test-harness.js";
36

extensions/browser/src/browser/server-context.constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
/**
2+
* Timing and size constants for Browser profile/tab runtime operations.
3+
*/
14
import { DEFAULT_BROWSER_LOCAL_CDP_READY_TIMEOUT_MS } from "./constants.js";
25

6+
/** Maximum managed page tabs kept open before best-effort cleanup starts. */
37
export const MANAGED_BROWSER_PAGE_TAB_LIMIT = 8;
48

59
export const OPEN_TAB_DISCOVERY_WINDOW_MS = 2000;

extensions/browser/src/browser/server-context.lifecycle.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
/**
2+
* Browser profile lifecycle helpers shared by availability, reset, and runtime
3+
* teardown.
4+
*/
15
import type { ResolvedBrowserProfile } from "./config.js";
26
import { getBrowserProfileCapabilities } from "./profile-capabilities.js";
37
import { getPwAiModule } from "./pw-ai-module.js";
48

9+
/** Resolves how an idle stop should behave for local, remote, or attach-only profiles. */
510
export function resolveIdleProfileStopOutcome(profile: ResolvedBrowserProfile): {
611
stopped: boolean;
712
closePlaywright: boolean;
@@ -19,6 +24,7 @@ export function resolveIdleProfileStopOutcome(profile: ResolvedBrowserProfile):
1924
};
2025
}
2126

27+
/** Closes cached Playwright CDP connections for one profile without requiring the module. */
2228
export async function closePlaywrightBrowserConnectionForProfile(cdpUrl?: string): Promise<void> {
2329
try {
2430
const mod = await getPwAiModule({ mode: "soft" });

extensions/browser/src/browser/server-context.remote-profile-tab-ops.test-helpers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
/**
2+
* Lazy-loaded dependency bundle for remote-profile tab operation tests.
3+
*/
14
import { afterEach, beforeEach, vi } from "vitest";
25

6+
/** Modules and helpers shared by remote-profile tab operation tests. */
37
export type RemoteProfileTestDeps = {
48
cdpModule: typeof import("./cdp.js");
59
chromeModule: typeof import("./chrome.js");
@@ -16,6 +20,7 @@ export type RemoteProfileTestDeps = {
1620

1721
let remoteProfileTestDepsPromise: Promise<RemoteProfileTestDeps> | undefined;
1822

23+
/** Loads remote-profile tab operation dependencies after Chrome mocks are installed. */
1924
export async function loadRemoteProfileTestDeps(): Promise<RemoteProfileTestDeps> {
2025
remoteProfileTestDepsPromise ??= (async () => {
2126
await import("./server-context.chrome-test-harness.js");
@@ -49,6 +54,7 @@ export async function loadRemoteProfileTestDeps(): Promise<RemoteProfileTestDeps
4954
return await remoteProfileTestDepsPromise;
5055
}
5156

57+
/** Installs per-test mock reset and Playwright connection cleanup. */
5258
export function installRemoteProfileTestLifecycle(deps: RemoteProfileTestDeps): void {
5359
beforeEach(() => {
5460
vi.clearAllMocks();

extensions/browser/src/browser/server-context.remote-tab-ops.harness.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Remote-tab operation harness for Browser server-context tests.
3+
*/
14
import { vi } from "vitest";
25
import { withBrowserFetchPreconnect } from "../../test-fetch.js";
36
import { resolveCdpControlPolicy } from "./cdp-reachability-policy.js";
@@ -6,8 +9,10 @@ import { createProfileSelectionOps } from "./server-context.selection.js";
69
import { createProfileTabOps } from "./server-context.tab-ops.js";
710
import type { BrowserServerState, ProfileRuntimeState } from "./server-context.types.js";
811

12+
/** Original global fetch restored between remote-tab harness tests. */
913
export const originalFetch = globalThis.fetch;
1014

15+
/** Creates Browser server state for remote or local profile tab tests. */
1116
export function makeState(
1217
profile: "remote" | "openclaw",
1318
): BrowserServerState & { profiles: Map<string, { lastTargetId?: string | null }> } {
@@ -95,6 +100,7 @@ function resolveProfileForTest(
95100
};
96101
}
97102

103+
/** Creates a minimal Browser route context for profile operation tests. */
98104
export function createTestBrowserRouteContext(opts: { getState: () => BrowserServerState }) {
99105
const forProfile = (profileName?: string) => {
100106
const state = opts.getState();
@@ -125,6 +131,7 @@ export function createTestBrowserRouteContext(opts: { getState: () => BrowserSer
125131
return { forProfile };
126132
}
127133

134+
/** Creates a remote profile context with a preconnected fetch mock. */
128135
export function createRemoteRouteHarness(fetchMock?: (url: unknown) => Promise<Response>) {
129136
const activeFetchMock = fetchMock ?? makeUnexpectedFetchMock();
130137
global.fetch = withBrowserFetchPreconnect(activeFetchMock);
@@ -133,6 +140,7 @@ export function createRemoteRouteHarness(fetchMock?: (url: unknown) => Promise<R
133140
return { state, remote: ctx.forProfile("remote"), fetchMock: activeFetchMock };
134141
}
135142

143+
/** Returns a page lister that yields prepared responses in order. */
136144
export function createSequentialPageLister<T>(responses: T[]) {
137145
return async () => {
138146
const next = responses.shift();
@@ -151,6 +159,7 @@ type JsonListEntry = {
151159
type: "page";
152160
};
153161

162+
/** Creates a /json/list fetch mock with static entries. */
154163
export function createJsonListFetchMock(entries: JsonListEntry[]) {
155164
return async (url: unknown) => {
156165
const u = String(url);
@@ -174,6 +183,7 @@ function makeManagedTab(id: string, ordinal: number): JsonListEntry {
174183
};
175184
}
176185

186+
/** Creates eight old managed tabs plus one new tab for cleanup-limit tests. */
177187
export function makeManagedTabsWithNew(params?: { newFirst?: boolean }): JsonListEntry[] {
178188
const oldTabs = Array.from({ length: 8 }, (_, index) =>
179189
makeManagedTab(`OLD${index + 1}`, index + 1),

extensions/browser/src/browser/server-context.reset.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Browser profile reset operations for local managed profiles.
3+
*/
14
import fs from "node:fs";
25
import type { ResolvedBrowserProfile } from "./config.js";
36
import { BrowserResetUnsupportedError } from "./errors.js";
@@ -18,6 +21,7 @@ type ResetOps = {
1821
resetProfile: () => Promise<{ moved: boolean; from: string; to?: string }>;
1922
};
2023

24+
/** Builds the reset-profile operation for one resolved browser profile. */
2125
export function createProfileResetOps({
2226
profile,
2327
getProfileState,

extensions/browser/src/browser/server-context.selection.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Browser tab selection operations for default tab choice, focus, and close.
3+
*/
14
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
25
import type { SsrFPolicy } from "../infra/net/ssrf.js";
36
import { fetchOk, normalizeCdpHttpBaseForJsonEndpoints } from "./cdp.helpers.js";
@@ -26,6 +29,7 @@ type SelectionOps = {
2629
closeTab: (targetId: string) => Promise<void>;
2730
};
2831

32+
/** Builds tab selection/focus/close operations for one resolved browser profile. */
2933
export function createProfileSelectionOps({
3034
profile,
3135
getProfileState,

extensions/browser/src/browser/server-context.tab-ops.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Browser tab listing, opening, labeling, and alias management for one profile.
3+
*/
14
import { resolveBrowserNavigationProxyMode } from "./browser-proxy-mode.js";
25
import { resolveCdpControlPolicy } from "./cdp-reachability-policy.js";
36
import { isSelectableCdpBrowserTarget } from "./cdp-target-filter.js";
@@ -170,6 +173,7 @@ function assignTabAliases(profileState: ProfileRuntimeState, tabs: BrowserTab[])
170173
return tabs.map((tab) => assignTabAlias({ profileState, tab }));
171174
}
172175

176+
/** Builds list/open/label tab operations for one resolved browser profile. */
173177
export function createProfileTabOps({
174178
profile,
175179
state,

extensions/browser/src/browser/server-context.test-harness.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
/**
2+
* Test factories for Browser profile/runtime state and launched Chrome mocks.
3+
*/
14
import type { ChildProcessWithoutNullStreams } from "node:child_process";
25
import { EventEmitter } from "node:events";
36
import type { RunningChrome } from "./chrome.js";
47
import type { ResolvedBrowserProfile } from "./config.js";
58
import type { BrowserServerState } from "./server-context.js";
69

10+
/** Creates a resolved Browser profile for unit tests. */
711
export function makeBrowserProfile(
812
overrides: Partial<ResolvedBrowserProfile> = {},
913
): ResolvedBrowserProfile {
@@ -21,6 +25,7 @@ export function makeBrowserProfile(
2125
};
2226
}
2327

28+
/** Creates Browser server state around a test profile. */
2429
export function makeBrowserServerState(params?: {
2530
profile?: ResolvedBrowserProfile;
2631
resolvedOverrides?: Partial<BrowserServerState["resolved"]>;
@@ -69,6 +74,7 @@ export function makeBrowserServerState(params?: {
6974
};
7075
}
7176

77+
/** Mocks a launched OpenClaw Chrome process with the supplied pid. */
7278
export function mockLaunchedChrome(
7379
launchOpenClawChrome: { mockResolvedValue: (value: RunningChrome) => unknown },
7480
pid: number,

0 commit comments

Comments
 (0)