openclaw/extensions/browser/test-fetch.ts
Peter Steinberger c757675f34
improve: keep isolated tests under one second (#100019)
* test: speed up isolated test suite

* test: finish isolated latency cleanup

* test: eliminate remaining isolated latency spikes

* test: remove final isolated timing outliers

* test: bound full-suite tooling processes

* test: bound native test process lifetime

* test: warm isolated runtime suites

* test: eliminate final isolated timing outliers

* test: fix isolated timing fixture types

* test: make timeout cleanup timing deterministic

* test: pin media manifests to source checkout

* test: isolate provider manifest contracts

* test: eliminate residual isolated timing spikes

* test: restore final isolated timing fixes

* test: eliminate remaining isolated timing spikes

* test: warm Zalo lifecycle imports

* test: keep isolated suites below one second

* test: use readable browser response fixtures
2026-07-05 10:57:19 -07:00

51 lines
1.7 KiB
TypeScript

/**
* Test fetch helper that adds no-op preconnect support expected by Browser tests.
*/
type FetchPreconnectOptions = {
dns?: boolean;
tcp?: boolean;
http?: boolean;
https?: boolean;
};
type FetchWithPreconnect = {
preconnect: (url: string | URL, options?: FetchPreconnectOptions) => void;
__openclawAcceptsDispatcher: true;
};
type FetchFunction = (...args: unknown[]) => unknown;
// Bounded CDP readers consume arrayBuffer(); keep lightweight json-only test
// responses compatible without weakening the production response boundary.
function addArrayBufferFallback(response: unknown): unknown {
if (!response || typeof response !== "object") {
return response;
}
const partial = response as {
arrayBuffer?: () => Promise<ArrayBuffer>;
json?: () => Promise<unknown>;
};
if (typeof partial.arrayBuffer === "function" || typeof partial.json !== "function") {
return response;
}
partial.arrayBuffer = async () =>
new TextEncoder().encode(JSON.stringify(await partial.json!())).buffer;
return response;
}
/** Adds Browser test preconnect metadata to a fetch-like function. */
export function withBrowserFetchPreconnect<T extends typeof fetch>(fn: T): T & FetchWithPreconnect;
export function withBrowserFetchPreconnect<T extends object>(
fn: T,
): T & FetchWithPreconnect & typeof fetch;
export function withBrowserFetchPreconnect(fn: object) {
const fetchFn = Object.assign(fn as FetchFunction, {
preconnect: (_url: string | URL, _options?: FetchPreconnectOptions) => {},
__openclawAcceptsDispatcher: true as const,
});
return new Proxy(fetchFn, {
async apply(target, thisArg, args) {
return addArrayBufferFallback(await Reflect.apply(target, thisArg, args));
},
});
}