| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347 |
- /* eslint-disable max-lines -- Upstream E2E 回放服务需要集中维护 fixture 匹配、capture artifact、chunk timeline 和 SSE 时间轴回放,拆开会让录制/回放协议更难追踪。 */
- import { randomUUID } from "node:crypto";
- import { readFileSync } from "node:fs";
- import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
- import type { AddressInfo } from "node:net";
- import { resolve } from "node:path";
- import type {
- E2ENetworkCaptureArtifact,
- E2ENetworkCaptureRecord,
- E2ENetworkCaptureResponseEvent,
- E2ENetworkResponseChunk,
- } from "./network-capture-proxy.js";
- import { writeE2EJsonFileAtomically } from "./e2e-atomic-json-file.js";
- import {
- redactE2ENetworkCaptureJson,
- redactE2ENetworkCaptureJsonText,
- sanitizeE2ENetworkCaptureHeaders,
- } from "./e2e-network-capture-redaction.js";
- import { resolveUpstreamModels } from "./upstream-recorded-models.js";
- // 夹具未写模型时,回放响应使用与 E2E 供应商相同的模型(环境变量或录制模型)。
- const DEFAULT_REPLAY_MODEL = resolveUpstreamModels().model;
- const LATEST_AGENT_ID_REPLAY_TOKEN = "{{latestAgentId}}";
- const LATEST_BASH_TASK_ID_REPLAY_TOKEN = "{{latestBashTaskId}}";
- const MEMORY_ROOT_REPLAY_TOKEN = "{{memoryRoot}}";
- const LATEST_AUTOMATION_ID_REPLAY_TOKEN = "{{latestAutomationId}}";
- const BROWSER_FIXTURE_URL_REPLAY_TOKEN = "{{browserFixtureUrl}}";
- const LATEST_BROWSER_TAB_ID_REPLAY_TOKEN = "{{latestBrowserTabId}}";
- const LATEST_BROWSER_TAB_A_ID_REPLAY_TOKEN = "{{latestBrowserTabAId}}";
- const LATEST_BROWSER_TAB_B_ID_REPLAY_TOKEN = "{{latestBrowserTabBId}}";
- export interface UpstreamReplayServer {
- artifactPath: string;
- baseUrl: string;
- host: string;
- port: number;
- stop(): Promise<void>;
- }
- interface StartUpstreamReplayServerOptions {
- artifactPath: string;
- artifactWriter?: typeof writeE2EJsonFileAtomically;
- fixtureVariables?: Readonly<Record<string, string>>;
- fixturePaths: string[];
- host?: string;
- port?: number;
- }
- interface UpstreamReplayFixtureFile {
- fixtures: UpstreamReplayFixture[];
- version: 1;
- }
- type UpstreamReplayCloseMode = "end" | "destroy";
- interface UpstreamReplayFixture {
- metadata?: { timingPolicy?: string };
- captured?: {
- capturedAt?: string;
- model?: string;
- requestBodyBytes?: number;
- responseBodyBytes?: number;
- statusCode?: number;
- url?: string;
- };
- description?: string;
- id: string;
- maxMatches?: number;
- match: {
- bodyExcludes?: string[];
- bodyIncludes?: string[];
- lastUserMessageExcludes?: string[];
- lastUserMessageIncludes?: string[];
- method?: string;
- pathIncludes?: string;
- };
- replay?: {
- consumeOnce?: boolean;
- };
- response: {
- body?: string;
- chunks?: UpstreamReplayResponseChunk[];
- closeMode?: UpstreamReplayCloseMode;
- delayMs?: number;
- /** 仅 controlled-stream:写出 N 个 chunk 后等待测试完成关键操作再释放。 */
- releaseAfterChunks?: number;
- events?: E2ENetworkCaptureResponseEvent[];
- headers?: Record<string, string>;
- statusCode: number;
- statusMessage?: string;
- text?: UpstreamReplayTextResponse | string;
- toolUse?: UpstreamReplayToolUseResponse;
- toolUses?: UpstreamReplayToolUseResponse[];
- };
- }
- interface ReplayVariables {
- browserFixtureUrl?: string;
- latestBrowserTabAId?: string;
- latestBrowserTabBId?: string;
- latestBrowserTabId?: string;
- latestAgentId?: string;
- latestBashTaskId?: string;
- memoryRoot?: string;
- latestAutomationId?: string;
- }
- interface UpstreamReplayResponseChunk {
- base64?: string;
- byteLength?: number;
- offsetMs: number;
- text?: string;
- }
- interface UpstreamReplayTextResponse {
- id?: string;
- inputTokens?: number;
- model?: string;
- outputTokens?: number;
- text: string;
- }
- interface UpstreamReplayToolUseResponse {
- id?: string;
- input: Record<string, unknown>;
- inputTokens?: number;
- messageId?: string;
- model?: string;
- name: string;
- outputTokens?: number;
- }
- type MutableCaptureRecord = E2ENetworkCaptureRecord;
- export async function startUpstreamReplayServer(
- options: StartUpstreamReplayServerOptions,
- ): Promise<UpstreamReplayServer> {
- const host = options.host ?? "127.0.0.1";
- const requestedPort = options.port ?? 0;
- const artifactPath = resolve(options.artifactPath);
- const artifactWriter = options.artifactWriter ?? writeE2EJsonFileAtomically;
- // Bug 根因:provider fixture 里的绝对路径过去只能写死为 POSIX 临时目录,
- // Windows Node 与 Git Bash 因而会访问不同文件。静态变量必须在 matcher 之前展开,
- // 否则 response 虽能拿到正确路径,请求仍会先因 matcher 含 token 而 miss。
- const fixtures = options.fixturePaths.flatMap((path) =>
- loadFixtureFile(path, options.fixtureVariables),
- );
- for (const fixture of fixtures) {
- const count = fixture.response.releaseAfterChunks;
- if (count === undefined) continue;
- if (
- fixture.metadata?.timingPolicy !== "controlled-stream" ||
- !Number.isInteger(count) ||
- count < 1 ||
- count >= (fixture.response.chunks?.length ?? 0) ||
- fixture.response.events?.length
- ) {
- throw new Error(`无效受控流 fixture: ${fixture.id};不得改变 recorded/fault stream 的时间轴`);
- }
- }
- const fixtureMatchCounts = new Map<string, number>();
- const records: MutableCaptureRecord[] = [];
- const gates = new Map<string, { release(): void; cancel(): void }>();
- const holdStream = (id: string, response: ServerResponse) =>
- new Promise<void>((resolveGate, rejectGate) => {
- if (gates.has(id)) {
- rejectGate(new Error(`重复的受控请求: ${id}`));
- return;
- }
- const finish = (error?: Error) => {
- clearTimeout(timer);
- response.off("close", closed);
- gates.delete(id);
- if (error) rejectGate(error);
- else resolveGate();
- };
- const closed = () => finish(new Error(`受控流已断开: ${id}`));
- const timer = setTimeout(() => finish(new Error(`受控流未被测试释放: ${id}`)), 120_000);
- gates.set(id, { release: () => finish(), cancel: closed });
- response.once("close", closed);
- });
- let serverPort = requestedPort;
- let baseUrl = `http://${host}:${serverPort}`;
- let artifactWriteQueue = Promise.resolve();
- const writeArtifact = () => {
- const artifact = {
- generatedAt: new Date().toISOString(),
- proxy: {
- caCertPath: "",
- host,
- port: serverPort,
- proxyUrl: baseUrl,
- },
- records: records.map(serializeRecord),
- version: 1,
- } satisfies E2ENetworkCaptureArtifact;
- // 同一 replay server 内也必须串行替换 artifact;否则多个并发 child request
- // 会让各自的临时文件竞争目标路径,并可能把较旧快照写到较新快照之后。
- const nextWrite = artifactWriteQueue
- .catch(() => undefined)
- .then(() => artifactWriter(artifactPath, artifact));
- artifactWriteQueue = nextWrite;
- return nextWrite;
- };
- const server = createServer((request, response) => {
- // 控制端不进入模型请求台账;每个独立 replay server 只释放自己的正在等待的 fixture。
- if (request.method === "GET" && request.url === "/__e2e__/gates") {
- response.setHeader("content-type", "application/json");
- response.end(JSON.stringify([...gates.keys()]));
- return;
- }
- if (request.method === "POST" && request.url?.startsWith("/__e2e__/release/")) {
- const gate = gates.get(request.url.slice("/__e2e__/release/".length));
- response.statusCode = gate ? 204 : 409;
- gate?.release();
- response.end();
- return;
- }
- void handleReplayRequest(request, response, {
- baseUrl,
- fixtureMatchCounts,
- fixtures,
- records,
- writeArtifact,
- holdStream,
- }).catch((error: unknown) => {
- // Bug 根因:旧 handler 是裸 `void Promise`,capture 写入抛错后没有任何人
- // 收口 response,provider 只能一直等 socket。证据通道失败必须显式结束请求。
- closeReplayResponseAfterHandlerError(response, error);
- });
- });
- await new Promise<void>((resolveStart, rejectStart) => {
- server.listen(requestedPort, host, () => {
- const address = server.address() as AddressInfo | null;
- if (!address) {
- rejectStart(new Error("Upstream replay server did not expose an address"));
- return;
- }
- serverPort = address.port;
- baseUrl = `http://${host}:${serverPort}`;
- resolveStart();
- });
- server.once("error", rejectStart);
- });
- try {
- await writeArtifact();
- } catch (error) {
- // Bug 根因:listen 成功后首次 artifact 写入失败会直接退出启动流程,
- // 但旧实现没有关闭 HTTP server,导致端口和进程句柄跨测试泄漏。
- // 清理错误不能覆盖原始写入错误;同时显式等待队列收口再向调用方失败。
- await closeServerListener(server).catch(() => undefined);
- await artifactWriteQueue.catch(() => undefined);
- throw error;
- }
- return {
- artifactPath,
- baseUrl,
- host,
- port: serverPort,
- stop: () => {
- for (const gate of gates.values()) gate.cancel();
- return stopServer(server, writeArtifact);
- },
- };
- }
- async function handleReplayRequest(
- request: IncomingMessage,
- response: ServerResponse,
- context: {
- baseUrl: string;
- fixtureMatchCounts: Map<string, number>;
- fixtures: UpstreamReplayFixture[];
- records: MutableCaptureRecord[];
- writeArtifact(): Promise<void>;
- holdStream(id: string, response: ServerResponse): Promise<void>;
- },
- ) {
- const startedAt = new Date().toISOString();
- const requestBody = await readRequestBody(request);
- const requestText = requestBody.toString("utf-8");
- const requestJson = parseJson(requestText);
- const requestUrl = buildRequestUrl(request, context.baseUrl);
- const record: MutableCaptureRecord = {
- host: requestUrl.host,
- id: randomUUID(),
- method: request.method ?? "GET",
- path: requestUrl.pathname + requestUrl.search,
- protocol: requestUrl.protocol === "https:" ? "https" : "http",
- requestBodyBytes: requestBody.length,
- requestHeaders: sanitizeE2ENetworkCaptureHeaders(request.headers),
- requestJson: requestJson ? redactE2ENetworkCaptureJson(requestJson) : undefined,
- requestTextPreview: requestJson ? undefined : requestText,
- responseBodyBytes: 0,
- responseHeaders: {},
- startedAt,
- status: "pending",
- url: requestUrl.toString(),
- };
- context.records.push(record);
- await context.writeArtifact();
- const fixtureIndex = findFixtureIndex(
- context.fixtures,
- record,
- requestText,
- requestJson,
- context.fixtureMatchCounts,
- );
- const fixture = fixtureIndex >= 0 ? context.fixtures[fixtureIndex] : undefined;
- if (!fixture) {
- const body = JSON.stringify(
- {
- error: {
- message: `Missing Upstream e2e fixture for ${record.method} ${record.path}`,
- },
- },
- null,
- 2,
- );
- await writeResponse(response, record, {
- body,
- headers: { "content-type": "application/json; charset=utf-8" },
- statusCode: 500,
- statusMessage: "Missing Fixture",
- });
- await context.writeArtifact();
- return;
- }
- context.fixtureMatchCounts.set(fixture.id, (context.fixtureMatchCounts.get(fixture.id) ?? 0) + 1);
- record.replay = {
- closeMode: fixture.response.closeMode ?? "end",
- fixtureId: fixture.id,
- };
- await context.writeArtifact();
- if (fixture.response.delayMs && fixture.response.delayMs > 0) {
- await new Promise((resolveDelay) => setTimeout(resolveDelay, fixture.response.delayMs));
- }
- if (fixture.response.events?.length) {
- await writeEventStreamResponse(response, record, {
- events: fixture.response.events,
- closeMode: fixture.response.closeMode ?? "end",
- headers: fixture.response.headers ?? {},
- statusCode: fixture.response.statusCode,
- statusMessage: fixture.response.statusMessage ?? "OK",
- });
- } else {
- const resolvedBody = resolveFixtureResponseBody(
- fixture.response,
- extractReplayVariables(requestText, requestJson),
- );
- if (resolvedBody.body.includes(BROWSER_FIXTURE_URL_REPLAY_TOKEN)) {
- // 修复原因:动态 URL marker 缺失或格式错误时保留模板会让 Browser tool
- // 导航到字面量 `{{browserFixtureUrl}}`,随后可能被 compact 摘要掩盖成成功。
- // replay 必须 fail closed,让 fixture/request 契约错误直接可见。
- await writeResponse(response, record, {
- body: JSON.stringify({
- error: { message: "Missing E2E_BROWSER_FIXTURE_URL replay variable" },
- }),
- headers: { "content-type": "application/json; charset=utf-8" },
- statusCode: 500,
- statusMessage: "Missing Replay Variable",
- });
- // Bug 根因:artifact 写入改为异步后,这个 fail-closed 分支仍然 fire-and-forget;
- // Windows 原子替换最终失败时 rejection 会逃出统一 handler catch。返回前等待,
- // 让诊断落盘失败按请求生命周期收口,且不覆盖已经返回的 500 业务证据。
- await context.writeArtifact();
- return;
- }
- await writeResponse(response, record, {
- body: resolvedBody.body,
- chunks: fixture.response.chunks,
- closeMode: fixture.response.closeMode,
- ...(fixture.response.releaseAfterChunks === undefined
- ? {}
- : {
- releaseAfterChunks: fixture.response.releaseAfterChunks,
- release: () => {
- if (fixture.metadata?.timingPolicy !== "controlled-stream")
- throw new Error("仅 controlled-stream 允许显式释放");
- return context.holdStream(fixture.id, response);
- },
- }),
- headers: resolvedBody.headers,
- statusCode: fixture.response.statusCode,
- statusMessage: fixture.response.statusMessage ?? "OK",
- });
- }
- if (fixture.replay?.consumeOnce) {
- context.fixtures.splice(fixtureIndex, 1);
- }
- await context.writeArtifact();
- }
- function closeReplayResponseAfterHandlerError(response: ServerResponse, error: unknown): void {
- const message = error instanceof Error ? error.message : String(error);
- console.warn(`[upstream-replay] request handler failed: ${message}`);
- if (response.writableEnded || response.destroyed) {
- return;
- }
- if (response.headersSent) {
- response.destroy(error instanceof Error ? error : undefined);
- return;
- }
- const body = JSON.stringify({
- error: { message: `Upstream E2E replay handler failed: ${message}` },
- });
- response.writeHead(500, "Replay Handler Failed", {
- "content-length": String(Buffer.byteLength(body, "utf-8")),
- "content-type": "application/json; charset=utf-8",
- });
- response.end(body);
- }
- function resolveFixtureResponseBody(
- response: UpstreamReplayFixture["response"],
- variables: ReplayVariables,
- ): { body: string; headers: Record<string, string> } {
- if (response.body !== undefined) {
- return {
- body: applyReplayVariablesToString(response.body, variables),
- headers: response.headers ?? {},
- };
- }
- if (response.toolUses?.length) {
- return {
- body: buildToolUsesSseBody(
- response.toolUses.map((toolUse) => resolveReplayToolUse(toolUse, variables)),
- ),
- headers: withEventStreamHeaders(response.headers),
- };
- }
- if (response.toolUse) {
- return {
- body: buildToolUseSseBody(resolveReplayToolUse(response.toolUse, variables)),
- headers: withEventStreamHeaders(response.headers),
- };
- }
- if (response.toolUses?.length) {
- return {
- body: buildToolUsesSseBody(response.toolUses),
- headers: withEventStreamHeaders(response.headers),
- };
- }
- if (response.text !== undefined) {
- return {
- body: buildTextSseBody(resolveReplayText(response.text, variables)),
- headers: withEventStreamHeaders(response.headers),
- };
- }
- return {
- body: "",
- headers: response.headers ?? {},
- };
- }
- function extractReplayVariables(requestText: string, requestJson: unknown): ReplayVariables {
- const requestContent = `${requestText}\n${JSON.stringify(requestJson ?? {})}`;
- const browserTabIds = readLatestBrowserTabIds(requestJson);
- return {
- browserFixtureUrl: readBrowserFixtureUrl(requestContent),
- ...browserTabIds,
- latestAgentId: readLatestAgentId(requestContent),
- latestBashTaskId: readLatestBashTaskId(requestContent),
- memoryRoot: readMemoryRoot(requestContent),
- latestAutomationId: readLatestAutomationId(requestJson),
- };
- }
- function readLatestBrowserTabIds(
- value: unknown,
- ): Pick<ReplayVariables, "latestBrowserTabAId" | "latestBrowserTabBId" | "latestBrowserTabId"> {
- const ids = {
- e2eBrowserTabAId: [] as string[],
- e2eBrowserTabBId: [] as string[],
- e2eBrowserTabId: [] as string[],
- };
- const names = Object.keys(ids) as Array<keyof typeof ids>;
- const visit = (candidate: unknown): void => {
- if (typeof candidate === "string") {
- // Bug 根因:真实 Node REPL tool_result 是 `=> { ... }\nStructured content:`,
- // 不是可直接 JSON.parse 的纯 JSON;只解析带 E2E 命名空间的显式字段,避免
- // 从任意 tab 文本猜 id,让 replay 真正模拟模型读取上一条工具结果。
- for (const name of names) {
- const pattern = new RegExp(`["']${name}["']\\s*:\\s*["']([^"']+)["']`, "gu");
- for (const match of candidate.matchAll(pattern)) {
- if (match[1]?.trim()) ids[name].push(match[1].trim());
- }
- }
- return;
- }
- if (Array.isArray(candidate)) {
- for (const item of candidate) visit(item);
- return;
- }
- if (!candidate || typeof candidate !== "object") return;
- for (const [key, child] of Object.entries(candidate)) {
- if (names.includes(key as keyof typeof ids) && typeof child === "string" && child.trim()) {
- ids[key as keyof typeof ids].push(child.trim());
- }
- visit(child);
- }
- };
- visit(value);
- return {
- latestBrowserTabAId: ids.e2eBrowserTabAId.at(-1),
- latestBrowserTabBId: ids.e2eBrowserTabBId.at(-1),
- latestBrowserTabId: ids.e2eBrowserTabId.at(-1),
- };
- }
- function readBrowserFixtureUrl(value: string): string | undefined {
- // 修复原因:Browser Use E2E 的本地 HTTP server 使用随机端口,provider replay
- // 必须从用户 marker 回填真实 URL,不能依赖固定端口或公网页面制造并发/网络抖动。
- const match = value.match(
- /E2E_BROWSER_FIXTURE_URL:(https?:\/\/(?:127\.0\.0\.1|localhost|\[::1\])(?::\d+)?\/[^\s"'<>),]*)/u,
- );
- return match?.[1];
- }
- function readLatestAgentId(value: string): string | undefined {
- const matches = [...value.matchAll(/agentId:\s*(agent_[A-Za-z0-9_-]+)/gu)];
- return matches.at(-1)?.[1];
- }
- function readLatestBashTaskId(value: string): string | undefined {
- // Bash task ID 每次运行都不同;只从真实 launch result 回填,不能把 Agent ID 当作 Bash ID。
- const matches = [
- ...value.matchAll(/Command running in background with ID:\s*([A-Za-z0-9_-]+)\./gu),
- ];
- return matches.at(-1)?.[1];
- }
- function readMemoryRoot(value: string): string | undefined {
- // Memory root 由 Runtime 按 workspace 计算,fixture 不能硬编码本机绝对路径。
- // 只从已经进入 provider request 的既有 Memory prompt 提取路径并回填 tool input。
- const matches = [
- ...value.matchAll(/persistent(?:,\s*)? file-based memory(?: system)? at `([^`]+?)[/\\]`/giu),
- ];
- return matches.at(-1)?.[1];
- }
- function readLatestAutomationId(value: unknown): string | undefined {
- const ids: string[] = [];
- const visit = (candidate: unknown): void => {
- if (typeof candidate === "string") {
- const trimmed = candidate.trim();
- if (trimmed.startsWith("{") || trimmed.startsWith("[")) {
- try {
- visit(JSON.parse(trimmed) as unknown);
- return;
- } catch {
- // tool_result 可能是带前后说明的文本,继续用正则提取其中的 JSON 字段。
- }
- }
- for (const match of candidate.matchAll(
- /["'](?:automationId|automation_id)["']\s*:\s*["']([^"']+)["']/gu,
- )) {
- if (match[1]?.trim()) ids.push(match[1].trim());
- }
- return;
- }
- if (Array.isArray(candidate)) {
- for (const item of candidate) visit(item);
- return;
- }
- if (!candidate || typeof candidate !== "object") return;
- for (const [key, child] of Object.entries(candidate)) {
- if (
- (key === "automationId" || key === "automation_id") &&
- typeof child === "string" &&
- child.trim()
- ) {
- ids.push(child.trim());
- }
- visit(child);
- }
- };
- visit(value);
- return ids.at(-1);
- }
- function resolveReplayToolUse(
- toolUse: UpstreamReplayToolUseResponse,
- variables: ReplayVariables,
- ): UpstreamReplayToolUseResponse {
- return {
- ...toolUse,
- id: toolUse.id === undefined ? undefined : applyReplayVariablesToString(toolUse.id, variables),
- input: applyReplayVariables(toolUse.input, variables) as Record<string, unknown>,
- messageId:
- toolUse.messageId === undefined
- ? undefined
- : applyReplayVariablesToString(toolUse.messageId, variables),
- };
- }
- function resolveReplayText(
- textResponse: UpstreamReplayTextResponse | string,
- variables: ReplayVariables,
- ): UpstreamReplayTextResponse | string {
- if (typeof textResponse === "string") {
- return applyReplayVariablesToString(textResponse, variables);
- }
- return {
- ...textResponse,
- id:
- textResponse.id === undefined
- ? undefined
- : applyReplayVariablesToString(textResponse.id, variables),
- text: applyReplayVariablesToString(textResponse.text, variables),
- };
- }
- function applyReplayVariables(value: unknown, variables: ReplayVariables): unknown {
- if (typeof value === "string") {
- return applyReplayVariablesToString(value, variables);
- }
- if (Array.isArray(value)) {
- return value.map((item) => applyReplayVariables(item, variables));
- }
- if (!value || typeof value !== "object") {
- return value;
- }
- return Object.fromEntries(
- Object.entries(value).map(([key, child]) => [key, applyReplayVariables(child, variables)]),
- );
- }
- function applyReplayVariablesToString(value: string, variables: ReplayVariables): string {
- let resolved = value;
- if (variables.latestBrowserTabId !== undefined) {
- resolved = resolved
- .split(LATEST_BROWSER_TAB_ID_REPLAY_TOKEN)
- .join(variables.latestBrowserTabId);
- }
- if (variables.latestBrowserTabAId !== undefined) {
- resolved = resolved
- .split(LATEST_BROWSER_TAB_A_ID_REPLAY_TOKEN)
- .join(variables.latestBrowserTabAId);
- }
- if (variables.latestBrowserTabBId !== undefined) {
- resolved = resolved
- .split(LATEST_BROWSER_TAB_B_ID_REPLAY_TOKEN)
- .join(variables.latestBrowserTabBId);
- }
- if (variables.latestAgentId !== undefined) {
- resolved = resolved.split(LATEST_AGENT_ID_REPLAY_TOKEN).join(variables.latestAgentId);
- }
- if (variables.latestBashTaskId !== undefined) {
- resolved = resolved.split(LATEST_BASH_TASK_ID_REPLAY_TOKEN).join(variables.latestBashTaskId);
- }
- if (variables.memoryRoot !== undefined) {
- resolved = resolved.split(MEMORY_ROOT_REPLAY_TOKEN).join(variables.memoryRoot);
- }
- if (variables.latestAutomationId !== undefined) {
- resolved = resolved.split(LATEST_AUTOMATION_ID_REPLAY_TOKEN).join(variables.latestAutomationId);
- }
- if (variables.browserFixtureUrl !== undefined) {
- resolved = resolved.split(BROWSER_FIXTURE_URL_REPLAY_TOKEN).join(variables.browserFixtureUrl);
- }
- return resolved;
- }
- function withEventStreamHeaders(
- headers: Record<string, string> | undefined,
- ): Record<string, string> {
- return {
- "cache-control": "no-cache",
- "content-type": "text/event-stream; charset=utf-8",
- ...headers,
- };
- }
- function buildToolUseSseBody(toolUse: UpstreamReplayToolUseResponse): string {
- return buildToolUsesSseBody([toolUse]);
- }
- function buildToolUsesSseBody(toolUses: UpstreamReplayToolUseResponse[]): string {
- const firstToolUse = toolUses[0];
- const messageId = firstToolUse?.messageId ?? `msg_${sanitizeSseId(firstToolUse?.name ?? "tool")}`;
- const inputTokens = firstToolUse?.inputTokens;
- const outputTokens = toolUses.reduce((sum, toolUse) => sum + (toolUse.outputTokens ?? 16), 0);
- return [
- sseEvent("message_start", {
- type: "message_start",
- message: {
- id: messageId,
- type: "message",
- role: "assistant",
- model: firstToolUse?.model ?? DEFAULT_REPLAY_MODEL,
- content: [],
- stop_reason: null,
- stop_sequence: null,
- usage: replayUsage(inputTokens, 0),
- },
- }),
- ...toolUses.flatMap((toolUse, index) => buildToolUseContentBlockEvents(toolUse, index)),
- sseEvent("message_delta", {
- type: "message_delta",
- delta: {
- stop_reason: "tool_use",
- stop_sequence: null,
- },
- usage: replayUsage(inputTokens, outputTokens),
- }),
- sseEvent("message_stop", { type: "message_stop" }),
- ].join("");
- }
- function buildToolUseContentBlockEvents(
- toolUse: UpstreamReplayToolUseResponse,
- index: number,
- ): string[] {
- const toolCallId = toolUse.id ?? `toolu_${sanitizeSseId(toolUse.name)}`;
- const inputJson = JSON.stringify(toolUse.input);
- return [
- sseEvent("content_block_start", {
- type: "content_block_start",
- index,
- content_block: {
- type: "tool_use",
- id: toolCallId,
- name: toolUse.name,
- input: {},
- },
- }),
- sseEvent("content_block_delta", {
- type: "content_block_delta",
- index,
- delta: {
- type: "input_json_delta",
- partial_json: inputJson,
- },
- }),
- sseEvent("content_block_stop", {
- type: "content_block_stop",
- index,
- }),
- ];
- }
- function buildTextSseBody(textResponse: UpstreamReplayTextResponse | string): string {
- const text = typeof textResponse === "string" ? textResponse : textResponse.text;
- const id =
- typeof textResponse === "string"
- ? "msg_upstream_e2e_text"
- : (textResponse.id ?? "msg_upstream_e2e_text");
- const model =
- typeof textResponse === "string"
- ? DEFAULT_REPLAY_MODEL
- : (textResponse.model ?? DEFAULT_REPLAY_MODEL);
- const inputTokens = typeof textResponse === "string" ? undefined : textResponse.inputTokens;
- const outputTokens = typeof textResponse === "string" ? undefined : textResponse.outputTokens;
- return [
- sseEvent("message_start", {
- type: "message_start",
- message: {
- id,
- type: "message",
- role: "assistant",
- model,
- content: [],
- stop_reason: null,
- stop_sequence: null,
- usage: replayUsage(inputTokens, 0),
- },
- }),
- sseEvent("content_block_start", {
- type: "content_block_start",
- index: 0,
- content_block: {
- type: "text",
- text: "",
- },
- }),
- sseEvent("content_block_delta", {
- type: "content_block_delta",
- index: 0,
- delta: {
- type: "text_delta",
- text,
- },
- }),
- sseEvent("content_block_stop", {
- type: "content_block_stop",
- index: 0,
- }),
- sseEvent("message_delta", {
- type: "message_delta",
- delta: {
- stop_reason: "end_turn",
- stop_sequence: null,
- },
- usage: replayUsage(inputTokens, outputTokens ?? 12),
- }),
- sseEvent("message_stop", { type: "message_stop" }),
- ].join("");
- }
- function replayUsage(inputTokens = 128, outputTokens = 0) {
- return {
- input_tokens: inputTokens,
- cache_creation_input_tokens: 0,
- cache_read_input_tokens: 0,
- output_tokens: outputTokens,
- service_tier: "standard",
- };
- }
- function sseEvent(event: string, data: unknown): string {
- return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`;
- }
- function sanitizeSseId(value: string): string {
- return value
- .replace(/[^a-zA-Z0-9]+/g, "_")
- .replace(/^_+|_+$/g, "")
- .toLowerCase();
- }
- async function writeResponse(
- response: ServerResponse,
- record: MutableCaptureRecord,
- fixtureResponse: {
- body: string;
- chunks?: UpstreamReplayResponseChunk[];
- closeMode?: UpstreamReplayCloseMode;
- releaseAfterChunks?: number;
- release?: () => Promise<void>;
- headers: Record<string, string>;
- statusCode: number;
- statusMessage: string;
- },
- ) {
- const responseChunks = normalizeReplayChunks(fixtureResponse.chunks);
- const destroyStream = fixtureResponse.closeMode === "destroy";
- // 同时带 text/chunks 的 fixture 实际发送 chunks,证据也必须记录真正发出的字节。
- const responseBody =
- responseChunks.length > 0
- ? Buffer.concat(responseChunks.map((chunk) => chunk.body))
- : Buffer.from(fixtureResponse.body, "utf-8");
- const responseHeaders = sanitizeResponseHeaders(fixtureResponse.headers);
- if (responseChunks.length === 0 && !destroyStream) {
- responseHeaders["content-length"] = String(responseBody.length);
- } else {
- delete responseHeaders["content-length"];
- }
- response.writeHead(fixtureResponse.statusCode, fixtureResponse.statusMessage, responseHeaders);
- let actualOffsets: number[] | undefined;
- if (responseChunks.length > 0) {
- if (
- fixtureResponse.release &&
- (!Number.isInteger(fixtureResponse.releaseAfterChunks) ||
- fixtureResponse.releaseAfterChunks! < 1 ||
- fixtureResponse.releaseAfterChunks! >= responseChunks.length)
- ) {
- throw new Error("releaseAfterChunks 必须位于流内部");
- }
- actualOffsets = await writeReplayChunks(response, responseChunks, fixtureResponse);
- if (!destroyStream) response.end();
- } else if (destroyStream) {
- await writeResponseChunk(response, responseBody);
- } else {
- response.end(responseBody);
- }
- record.status = destroyStream ? "error" : "complete";
- record.completedAt = new Date().toISOString();
- record.durationMs = durationMs(record.startedAt, record.completedAt);
- record.statusCode = fixtureResponse.statusCode;
- record.statusMessage = fixtureResponse.statusMessage;
- record.responseBodyBytes = responseBody.length;
- record.responseHeaders = sanitizeE2ENetworkCaptureHeaders(responseHeaders);
- record.responseTextPreview = redactE2ENetworkCaptureJsonText(responseBody.toString("utf-8"));
- if (responseChunks.length > 0) {
- record.responseChunkTimeline = responseChunks.map((chunk, index) => ({
- base64: chunk.body.toString("base64"),
- byteLength: chunk.byteLength,
- offsetMs: fixtureResponse.release ? actualOffsets![index]! : chunk.offsetMs,
- }));
- }
- if (destroyStream) {
- // chunks/body 与 events 必须执行相同的故障合同;只在 artifact 标 destroy
- // 却正常 end 会把网络中断测试变成正常 EOF,并制造“产品假完成”的错误结论。
- record.error = "Replay fixture destroyed SSE response stream";
- response.destroy();
- }
- }
- async function writeReplayChunks(
- response: ServerResponse,
- chunks: NormalizedReplayChunk[],
- control?: { releaseAfterChunks?: number; release?: () => Promise<void> },
- ) {
- let previousOffsetMs = 0;
- const startedAt = Date.now();
- const actualOffsets: number[] = [];
- for (const [index, chunk] of chunks.entries()) {
- if (index === control?.releaseAfterChunks && control.release) {
- await control.release();
- // 只移除被释放的那段人为慢窗口,后续 chunk 间距及内容原样保留。
- previousOffsetMs = chunk.offsetMs;
- }
- const delayMs = Math.max(0, chunk.offsetMs - previousOffsetMs);
- if (delayMs > 0) {
- await new Promise((resolveDelay) => setTimeout(resolveDelay, delayMs));
- }
- previousOffsetMs = chunk.offsetMs;
- actualOffsets.push(Date.now() - startedAt);
- // 等待写入完成后再进入下一帧/销毁连接,避免最后一帧只进用户态队列就被丢弃。
- await writeResponseChunk(response, chunk.body);
- }
- return actualOffsets;
- }
- interface NormalizedReplayChunk {
- body: Buffer;
- byteLength: number;
- offsetMs: number;
- }
- function normalizeReplayChunks(
- chunks: UpstreamReplayResponseChunk[] | undefined,
- ): NormalizedReplayChunk[] {
- return (
- chunks
- ?.map((chunk) => {
- const body =
- chunk.base64 !== undefined
- ? Buffer.from(chunk.base64, "base64")
- : Buffer.from(chunk.text ?? "", "utf-8");
- return {
- body,
- byteLength: chunk.byteLength ?? body.length,
- offsetMs: Math.max(0, Math.round(chunk.offsetMs)),
- };
- })
- .filter((chunk) => chunk.body.length > 0) ?? []
- );
- }
- async function writeEventStreamResponse(
- response: ServerResponse,
- record: MutableCaptureRecord,
- fixtureResponse: {
- closeMode: UpstreamReplayCloseMode;
- events: E2ENetworkCaptureResponseEvent[];
- headers: Record<string, string>;
- statusCode: number;
- statusMessage: string;
- },
- ) {
- const responseHeaders = sanitizeResponseHeaders(fixtureResponse.headers);
- delete responseHeaders["content-length"];
- response.writeHead(fixtureResponse.statusCode, fixtureResponse.statusMessage, responseHeaders);
- const startedAtMs = Date.now();
- const sortedEvents = [...fixtureResponse.events].sort(
- (left, right) => left.offsetMs - right.offsetMs || left.sequence - right.sequence,
- );
- const chunks: string[] = [];
- const replayedEvents: E2ENetworkCaptureResponseEvent[] = [];
- for (const event of sortedEvents) {
- const elapsedMs = Date.now() - startedAtMs;
- const waitMs = Math.max(0, event.offsetMs - elapsedMs);
- if (waitMs > 0) {
- await new Promise((resolveDelay) => setTimeout(resolveDelay, waitMs));
- }
- const text = event.text;
- await writeResponseChunk(response, text);
- chunks.push(text);
- replayedEvents.push({
- byteLength: Buffer.byteLength(text, "utf-8"),
- offsetMs: Math.max(0, Date.now() - startedAtMs),
- ...(event.partial ? { partial: true } : {}),
- sequence: replayedEvents.length,
- text,
- });
- }
- const body = chunks.join("");
- record.status = fixtureResponse.closeMode === "destroy" ? "error" : "complete";
- record.completedAt = new Date().toISOString();
- record.durationMs = durationMs(record.startedAt, record.completedAt);
- record.statusCode = fixtureResponse.statusCode;
- record.statusMessage = fixtureResponse.statusMessage;
- record.responseBodyBytes = Buffer.byteLength(body, "utf-8");
- record.responseEvents = replayedEvents;
- record.responseHeaders = sanitizeE2ENetworkCaptureHeaders(responseHeaders);
- record.responseTextPreview = redactE2ENetworkCaptureJsonText(body);
- record.replay = {
- ...record.replay,
- closeMode: fixtureResponse.closeMode,
- };
- if (fixtureResponse.closeMode === "destroy") {
- // 修复原因:SSE fault case 需要区分“provider 正常 EOF 但内容不完整”和
- // “连接在流式响应中异常断开”。这里按 fixture 显式销毁 socket,artifact 也标记为 error,
- // 让后续 E2E 能稳定构造断流语义,而不是依赖真实网络抖动。
- record.error = "Replay fixture destroyed SSE response stream";
- response.destroy();
- return;
- }
- response.end();
- }
- function writeResponseChunk(response: ServerResponse, text: string | Buffer): Promise<void> {
- return new Promise((resolveWrite, rejectWrite) => {
- response.write(text, (error) => {
- if (error) {
- rejectWrite(error);
- return;
- }
- resolveWrite();
- });
- });
- }
- function findFixtureIndex(
- fixtures: UpstreamReplayFixture[],
- record: E2ENetworkCaptureRecord,
- requestText: string,
- requestJson: unknown,
- fixtureMatchCounts: ReadonlyMap<string, number>,
- ) {
- return fixtures.findIndex((fixture) => {
- // 回放需要表达“同一类请求前 N 次失败、之后成功”的时序。
- // maxMatches 只影响测试 fixture 匹配,不进入产品协议。
- if (
- fixture.maxMatches !== undefined &&
- (fixtureMatchCounts.get(fixture.id) ?? 0) >= fixture.maxMatches
- ) {
- return false;
- }
- const expectedMethod = fixture.match.method?.toUpperCase();
- if (expectedMethod && expectedMethod !== record.method.toUpperCase()) {
- return false;
- }
- if (fixture.match.pathIncludes && !record.path.includes(fixture.match.pathIncludes)) {
- return false;
- }
- const latestUserMessageText =
- fixture.match.lastUserMessageIncludes || fixture.match.lastUserMessageExcludes
- ? readLatestUserMessageText(requestJson)
- : "";
- return (
- (fixture.match.bodyIncludes?.every(
- (text) => requestText.includes(text) || captureContainsText(requestJson, text),
- ) ??
- true) &&
- (fixture.match.bodyExcludes?.every(
- (text) => !requestText.includes(text) && !captureContainsText(requestJson, text),
- ) ??
- true) &&
- (fixture.match.lastUserMessageIncludes?.every((text) =>
- latestUserMessageText.includes(text),
- ) ??
- true) &&
- (fixture.match.lastUserMessageExcludes?.every(
- (text) => !latestUserMessageText.includes(text),
- ) ??
- true)
- );
- });
- }
- function readLatestUserMessageText(requestJson: unknown): string {
- if (!requestJson || typeof requestJson !== "object") {
- return "";
- }
- const messages = (requestJson as { messages?: unknown }).messages;
- if (!Array.isArray(messages)) {
- return "";
- }
- for (let index = messages.length - 1; index >= 0; index -= 1) {
- const message = messages[index];
- if (!message || typeof message !== "object") {
- continue;
- }
- if ((message as { role?: unknown }).role !== "user") {
- continue;
- }
- return readMessageContentText((message as { content?: unknown }).content);
- }
- return "";
- }
- function readMessageContentText(content: unknown): string {
- if (typeof content === "string") {
- return content;
- }
- if (!Array.isArray(content)) {
- return "";
- }
- return content
- .map((part) => {
- if (!part || typeof part !== "object") {
- return "";
- }
- const text = (part as { text?: unknown }).text;
- if (typeof text === "string") {
- return text;
- }
- // 修复原因:Anthropic Messages continuation 把工具结果放在 user/tool_result.content,
- // 旧实现只读 text,导致 fixture 只能在整段 body 搜 marker;而 tool code 本身
- // 也包含成功 marker,会把真实失败结果误匹配成成功 continuation。
- if ((part as { type?: unknown }).type === "tool_result") {
- return readMessageContentText((part as { content?: unknown }).content);
- }
- return "";
- })
- .filter(Boolean)
- .join("\n");
- }
- function loadFixtureFile(
- path: string,
- fixtureVariables: Readonly<Record<string, string>> = {},
- ): UpstreamReplayFixture[] {
- const resolvedPath = resolve(path);
- const parsed = JSON.parse(readFileSync(resolvedPath, "utf-8")) as
- | UpstreamReplayFixtureFile
- | E2ENetworkCaptureArtifact;
- if (isUpstreamReplayFixtureFile(parsed)) {
- return applyFixtureVariables(parsed.fixtures, fixtureVariables) as UpstreamReplayFixture[];
- }
- if (isNetworkCaptureArtifact(parsed)) {
- return applyFixtureVariables(
- convertCaptureArtifactToFixtures(parsed, resolvedPath),
- fixtureVariables,
- ) as UpstreamReplayFixture[];
- }
- throw new Error(`Invalid Upstream e2e replay fixture: ${path}`);
- }
- function applyFixtureVariables(
- value: unknown,
- variables: Readonly<Record<string, string>>,
- ): unknown {
- if (typeof value === "string") {
- return Object.entries(variables).reduce(
- (resolvedValue, [name, replacement]) => resolvedValue.split(`{{${name}}}`).join(replacement),
- value,
- );
- }
- if (Array.isArray(value)) {
- return value.map((item) => applyFixtureVariables(item, variables));
- }
- if (!value || typeof value !== "object") {
- return value;
- }
- return Object.fromEntries(
- Object.entries(value).map(([key, child]) => [key, applyFixtureVariables(child, variables)]),
- );
- }
- function isUpstreamReplayFixtureFile(
- value: UpstreamReplayFixtureFile | E2ENetworkCaptureArtifact,
- ): value is UpstreamReplayFixtureFile {
- return value.version === 1 && Array.isArray((value as UpstreamReplayFixtureFile).fixtures);
- }
- function isNetworkCaptureArtifact(
- value: UpstreamReplayFixtureFile | E2ENetworkCaptureArtifact,
- ): value is E2ENetworkCaptureArtifact {
- return value.version === 1 && Array.isArray((value as E2ENetworkCaptureArtifact).records);
- }
- function convertCaptureArtifactToFixtures(
- artifact: E2ENetworkCaptureArtifact,
- sourcePath: string,
- ): UpstreamReplayFixture[] {
- const fixtures = artifact.records
- .filter((record) => record.status === "complete" && record.statusCode)
- .filter(
- (record) =>
- record.responseTextPreview ||
- record.responseChunkTimeline?.length ||
- record.responseEvents?.length,
- )
- .map((record, index) => {
- const responseBody = resolveCapturedResponseBody(record);
- return {
- id: `capture-${String(index + 1).padStart(3, "0")}-${record.id}`,
- description: `Captured replay response from ${sourcePath}`,
- match: {
- method: record.method,
- pathIncludes: capturePathname(record),
- },
- replay: {
- consumeOnce: true,
- },
- captured: {
- capturedAt: record.completedAt ?? record.startedAt,
- requestBodyBytes: record.requestBodyBytes,
- responseBodyBytes: record.responseBodyBytes,
- statusCode: record.statusCode,
- url: record.url,
- },
- response: {
- body: responseBody,
- chunks: record.responseChunkTimeline?.map(captureChunkToReplayChunk),
- events: record.responseEvents,
- headers: record.responseHeaders,
- statusCode: record.statusCode ?? 200,
- statusMessage: record.statusMessage,
- },
- } satisfies UpstreamReplayFixture;
- });
- if (fixtures.length === 0) {
- throw new Error(`Upstream capture artifact has no replayable records: ${sourcePath}`);
- }
- return fixtures;
- }
- function resolveCapturedResponseBody(record: E2ENetworkCaptureRecord) {
- if (record.responseTextPreview && !record.responseBodyTruncated) {
- return record.responseTextPreview;
- }
- if (record.responseEvents?.length) {
- return record.responseEvents.map((event) => event.text).join("");
- }
- if (record.responseChunkTimeline?.length && !record.responseChunkTimelineTruncated) {
- return Buffer.concat(
- record.responseChunkTimeline.map((chunk) => Buffer.from(chunk.base64, "base64")),
- ).toString("utf-8");
- }
- return record.responseTextPreview ?? "";
- }
- function captureChunkToReplayChunk(chunk: E2ENetworkResponseChunk): UpstreamReplayResponseChunk {
- return {
- base64: chunk.base64,
- byteLength: chunk.byteLength,
- offsetMs: chunk.offsetMs,
- };
- }
- function capturePathname(record: E2ENetworkCaptureRecord) {
- try {
- return new URL(record.url).pathname;
- } catch {
- return record.path.split("?")[0] || record.path;
- }
- }
- async function readRequestBody(request: IncomingMessage): Promise<Buffer> {
- const chunks: Buffer[] = [];
- for await (const chunk of request) {
- chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
- }
- return Buffer.concat(chunks);
- }
- function buildRequestUrl(request: IncomingMessage, baseUrl: string): URL {
- const rawUrl = request.url ?? "/";
- if (/^https?:\/\//i.test(rawUrl)) {
- return new URL(rawUrl);
- }
- const path = rawUrl.startsWith("/") ? rawUrl : `/${rawUrl}`;
- return new URL(path, baseUrl);
- }
- function sanitizeResponseHeaders(headers: Record<string, string>): Record<string, string> {
- const sanitized: Record<string, string> = {};
- for (const [name, value] of Object.entries(headers)) {
- const normalizedName = name.toLowerCase();
- if (normalizedName === "connection" || normalizedName === "transfer-encoding") {
- continue;
- }
- sanitized[normalizedName] = value;
- }
- return sanitized;
- }
- function parseJson(text: string): unknown {
- if (!text.trim()) {
- return undefined;
- }
- try {
- return JSON.parse(text) as unknown;
- } catch {
- return undefined;
- }
- }
- function captureContainsText(value: unknown, expected: string): boolean {
- if (typeof value === "string") {
- return value.includes(expected);
- }
- if (Array.isArray(value)) {
- return value.some((item) => captureContainsText(item, expected));
- }
- if (!value || typeof value !== "object") {
- return false;
- }
- return Object.values(value).some((child) => captureContainsText(child, expected));
- }
- function serializeRecord(record: MutableCaptureRecord): E2ENetworkCaptureRecord {
- return record;
- }
- function durationMs(startedAt: string, completedAt: string): number {
- return Math.max(0, Date.parse(completedAt) - Date.parse(startedAt));
- }
- async function stopServer(server: Server, writeArtifact: () => Promise<void>): Promise<void> {
- await closeServerListener(server);
- await writeArtifact();
- }
- async function closeServerListener(server: Server): Promise<void> {
- await new Promise<void>((resolveStop, rejectStop) => {
- server.close((error) => {
- if (error) {
- rejectStop(error);
- return;
- }
- resolveStop();
- });
- });
- }
|