| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797 |
- import { mkdtemp, open, readFile, rm, writeFile } from "node:fs/promises";
- import { createServer as createNetServer, type Server as NetServer } from "node:net";
- import { tmpdir } from "node:os";
- import { join } from "node:path";
- import { afterEach, describe, expect, it, vi } from "vitest";
- import {
- startUpstreamReplayServer,
- type UpstreamReplayServer,
- } from "./e2e/helpers/upstream-replay-server.js";
- import type { E2ENetworkCaptureArtifact } from "./e2e/helpers/network-capture-proxy.js";
- const SSE_EVENT_TEXT = 'event: message_start\ndata: {"type":"message_start"}\n\n';
- const activeServers: UpstreamReplayServer[] = [];
- const tempDirs: string[] = [];
- afterEach(async () => {
- await Promise.all(activeServers.splice(0).map((server) => server.stop()));
- await Promise.all(tempDirs.splice(0).map((dir) => rm(dir, { force: true, recursive: true })));
- });
- describe("Upstream replay server", () => {
- it("closes the listener when the initial artifact write fails", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-start-failure-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- const artifactPath = join(tempDir, "artifact.json");
- const port = await reserveLoopbackPort();
- const initialWriteError = new Error("initial-artifact-write-failed");
- let artifactWriteCount = 0;
- await writeFile(fixturePath, JSON.stringify({ fixtures: [], version: 1 }, null, 2), "utf-8");
- await expect(
- startUpstreamReplayServer({
- artifactPath,
- artifactWriter: async () => {
- artifactWriteCount += 1;
- throw initialWriteError;
- },
- fixturePaths: [fixturePath],
- port,
- }),
- ).rejects.toBe(initialWriteError);
- expect(artifactWriteCount).toBe(1);
- await expectLoopbackPortAvailable(port);
- });
- it.runIf(process.platform === "win32")(
- "ends the provider response when a persistently locked artifact cannot be replaced",
- async () => {
- const { artifactPath, server } = await startFixtureServer("end");
- const reader = await open(artifactPath, "r");
- try {
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [{ role: "user", content: "SSE_CLOSE_MODE_PROBE" }],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- signal: AbortSignal.timeout(3000),
- });
- expect(response.status).toBe(500);
- await expect(response.text()).resolves.toContain("Upstream E2E replay handler failed");
- } finally {
- await reader.close();
- }
- },
- );
- it("expands static fixture variables before matching and replaying", async () => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-replay-vars-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- const artifactPath = join(tempDir, "artifact.json");
- const runtimeRoot = "C:/e2e-home/ZCodeProject/.zcode-e2e";
- await writeFile(
- fixturePath,
- JSON.stringify(
- {
- fixtures: [
- {
- id: "static-runtime-path",
- match: {
- bodyIncludes: ["Read {{e2eRuntimeRoot}}/shared/readonly.txt"],
- method: "POST",
- pathIncludes: "/messages",
- },
- response: {
- statusCode: 200,
- toolUse: {
- id: "toolu_static_runtime_path",
- input: {
- file_path: "{{e2eRuntimeRoot}}/shared/readonly.txt",
- },
- name: "Read",
- },
- },
- },
- ],
- version: 1,
- },
- null,
- 2,
- ),
- );
- const server = await startUpstreamReplayServer({
- artifactPath,
- fixturePaths: [fixturePath],
- fixtureVariables: { e2eRuntimeRoot: runtimeRoot },
- });
- activeServers.push(server);
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [
- {
- content: `Read ${runtimeRoot}/shared/readonly.txt`,
- role: "user",
- },
- ],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- signal: AbortSignal.timeout(5000),
- });
- const responseText = await response.text();
- expect(response.status).toBe(200);
- expect(responseText).toContain(`\\"file_path\\":\\"${runtimeRoot}/shared/readonly.txt\\"`);
- expect(responseText).not.toContain("{{e2eRuntimeRoot}}");
- });
- it.each([
- {
- output:
- "Command running in background with ID: bash_first. Output is being written to: /tmp/first.log.\nCommand running in background with ID: bash_last. Output is being written to: /tmp/last.log.",
- expected: "bash_last",
- },
- { output: "agentId: agent_other", expected: "{{latestBashTaskId}}" },
- ])(
- "resolves Bash task IDs from actual launch results: $expected",
- async ({ output, expected }) => {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-bash-task-replay-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- await writeFile(
- fixturePath,
- JSON.stringify({
- version: 1,
- fixtures: [
- {
- id: "bash-task-output",
- match: { method: "POST", pathIncludes: "/messages" },
- response: {
- statusCode: 200,
- toolUse: {
- id: "toolu_bash_task_output",
- name: "TaskOutput",
- input: { task_id: "{{latestBashTaskId}}", block: false },
- },
- },
- },
- ],
- }),
- );
- const server = await startUpstreamReplayServer({
- fixturePaths: [fixturePath],
- artifactPath: join(tempDir, "artifact.json"),
- });
- activeServers.push(server);
- const response = await fetch(`${server.baseUrl}/messages`, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({
- messages: [
- {
- role: "user",
- content: [
- {
- type: "tool_result",
- tool_use_id: "toolu_bash_launch",
- content: output,
- },
- ],
- },
- ],
- }),
- signal: AbortSignal.timeout(5000),
- });
- expect(response.status).toBe(200);
- expect(await response.text()).toContain(`\\"task_id\\":\\"${expected}\\"`);
- },
- );
- it("records normal SSE EOF as complete end", async () => {
- const { artifactPath, server } = await startFixtureServer("end");
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [{ role: "user", content: "SSE_CLOSE_MODE_PROBE" }],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- signal: AbortSignal.timeout(5000),
- });
- await expect(response.text()).resolves.toBe(SSE_EVENT_TEXT);
- await server.stop();
- activeServers.pop();
- const record = (await readArtifact(artifactPath)).records[0];
- expect(record?.status).toBe("complete");
- expect(record?.replay).toEqual({
- closeMode: "end",
- fixtureId: "sse-close-mode-end",
- });
- expect(record?.responseEvents).toHaveLength(1);
- });
- it("destroys SSE response streams when closeMode is destroy", async () => {
- const { artifactPath, server } = await startFixtureServer("destroy");
- let fetchFailed = false;
- try {
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [{ role: "user", content: "SSE_CLOSE_MODE_PROBE" }],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- signal: AbortSignal.timeout(5000),
- });
- await response.text();
- } catch {
- fetchFailed = true;
- }
- await server.stop();
- activeServers.pop();
- const record = (await readArtifact(artifactPath)).records[0];
- expect(fetchFailed).toBe(true);
- expect(record?.status).toBe("error");
- expect(record?.error).toBe("Replay fixture destroyed SSE response stream");
- expect(record?.replay).toEqual({
- closeMode: "destroy",
- fixtureId: "sse-close-mode-destroy",
- });
- expect(record?.responseEvents).toHaveLength(1);
- });
- it("injects a loopback Browser fixture URL without trailing punctuation", async () => {
- const server = await startBrowserVariableFixtureServer();
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [
- {
- role: "user",
- content:
- "BROWSER_VARIABLE_PROBE E2E_BROWSER_FIXTURE_URL:http://127.0.0.1:43123/basic),",
- },
- ],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- });
- expect(response.status).toBe(200);
- const body = await response.text();
- expect(body).toContain("http://127.0.0.1:43123/basic");
- expect(body).not.toContain("{{browserFixtureUrl}}");
- expect(body).not.toContain("/basic),");
- });
- it("injects the latest automation id from a CronList tool result", async () => {
- const server = await startAutomationVariableFixtureServer();
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [
- { role: "user", content: "AUTOMATION_VARIABLE_PROBE" },
- {
- role: "user",
- content: [
- {
- type: "tool_result",
- tool_use_id: "toolu_cron_list",
- content: JSON.stringify({
- automations: [{ automationId: "automation-runtime-123" }],
- }),
- },
- ],
- },
- ],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- });
- expect(response.status).toBe(200);
- const body = await response.text();
- expect(body).toContain("automation-runtime-123");
- expect(body).not.toContain("{{latestAutomationId}}");
- });
- it("injects Browser stable tab ids from the latest tool result", async () => {
- const server = await startBrowserTabVariableFixtureServer();
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [
- { role: "user", content: "BROWSER_TAB_VARIABLE_PROBE" },
- {
- role: "user",
- content: [
- {
- type: "tool_result",
- tool_use_id: "toolu_browser_tabs_stale",
- content:
- '=> {"e2eBrowserTabId":"tab-single-stale","e2eBrowserTabAId":"tab-a-stale","e2eBrowserTabBId":"tab-b-stale"}\n\nStructured content:\n',
- },
- ],
- },
- {
- role: "user",
- content: [
- {
- type: "tool_result",
- tool_use_id: "toolu_browser_tabs_list",
- content:
- '=> {\n "e2eBrowserTabAId": "tab-a-runtime",\n "e2eBrowserTabBId": "tab-b-runtime",\n "e2eBrowserTabId": "tab-single-runtime"\n}\n\nStructured content:\n',
- },
- ],
- },
- ],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- });
- expect(response.status).toBe(200);
- const body = await response.text();
- expect(body).toContain("tab-single-runtime");
- expect(body).toContain("tab-a-runtime");
- expect(body).toContain("tab-b-runtime");
- expect(body).not.toContain("{{latestBrowserTabId}}");
- expect(body).not.toContain("{{latestBrowserTabAId}}");
- expect(body).not.toContain("{{latestBrowserTabBId}}");
- expect(body).not.toContain("stale");
- });
- it("fails closed when the Browser fixture URL marker is not loopback", async () => {
- const server = await startBrowserVariableFixtureServer();
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [
- {
- role: "user",
- content: "BROWSER_VARIABLE_PROBE E2E_BROWSER_FIXTURE_URL:https://example.com/basic",
- },
- ],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- });
- expect(response.status).toBe(500);
- await expect(response.text()).resolves.toContain(
- "Missing E2E_BROWSER_FIXTURE_URL replay variable",
- );
- });
- it("contains a missing Browser fixture variable artifact failure in the request handler", async () => {
- let artifactWriteCount = 0;
- let failedMissingVariableArtifact = false;
- const unhandledRejections: unknown[] = [];
- const handlerWarning = vi.spyOn(console, "warn").mockImplementation(() => undefined);
- const onUnhandledRejection = (reason: unknown) => {
- unhandledRejections.push(reason);
- };
- process.on("unhandledRejection", onUnhandledRejection);
- try {
- const server = await startBrowserVariableFixtureServer({
- artifactWriter: async (path, value) => {
- artifactWriteCount += 1;
- const artifact = value as E2ENetworkCaptureArtifact;
- const containsMissingVariableResponse = artifact.records.some(
- (record) =>
- record.status === "complete" &&
- record.statusCode === 500 &&
- record.responseTextPreview?.includes(
- "Missing E2E_BROWSER_FIXTURE_URL replay variable",
- ),
- );
- if (containsMissingVariableResponse && !failedMissingVariableArtifact) {
- failedMissingVariableArtifact = true;
- throw new Error("missing-variable-artifact-write-failed");
- }
- await writeFile(path, JSON.stringify(value, null, 2), "utf-8");
- },
- });
- const response = await fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [{ role: "user", content: "BROWSER_VARIABLE_PROBE" }],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- });
- expect(response.status).toBe(500);
- await expect(response.text()).resolves.toContain(
- "Missing E2E_BROWSER_FIXTURE_URL replay variable",
- );
- await new Promise<void>((resolveImmediate) => setImmediate(resolveImmediate));
- expect(artifactWriteCount).toBeGreaterThanOrEqual(4);
- expect(failedMissingVariableArtifact).toBe(true);
- expect(unhandledRejections).toEqual([]);
- expect(handlerWarning).toHaveBeenCalledWith(
- expect.stringContaining("missing-variable-artifact-write-failed"),
- );
- } finally {
- process.off("unhandledRejection", onUnhandledRejection);
- handlerWarning.mockRestore();
- }
- });
- it("matches the latest tool_result instead of a success marker inside tool code", async () => {
- const server = await startLatestToolResultFixtureServer();
- const request = (toolResult: string) =>
- fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [
- {
- role: "assistant",
- content: [
- {
- type: "tool_use",
- input: { code: "return 'E2E_TOOL_RESULT_OK'" },
- },
- ],
- },
- {
- role: "user",
- content: [
- {
- type: "tool_result",
- content: toolResult,
- },
- ],
- },
- ],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- });
- const failedResult = await request("E2E_TOOL_RESULT_FAILED");
- expect(failedResult.status).toBe(500);
- await expect(failedResult.text()).resolves.toContain("Missing Upstream e2e fixture");
- const successfulResult = await request("E2E_TOOL_RESULT_OK");
- expect(successfulResult.status).toBe(200);
- await expect(successfulResult.text()).resolves.toContain("tool result matched");
- });
- it("matches the background prompt when task-notification only appears in tool schema", async () => {
- const server = await startBackgroundFixtureServer();
- const response = await requestBackgroundDirectPrompt(
- server,
- "E2E_BACKGROUND_DIRECT_USER_PROMPT: send immediately. Payload: E2E_BACKGROUND_DIRECT_USER_PROMPT_PAYLOAD.",
- );
- expect(response.status).toBe(200);
- await expect(response.text()).resolves.toContain("background-direct-user-prompt-received-ok");
- });
- it("rejects the background prompt when task-notification is in the latest user message", async () => {
- const server = await startBackgroundFixtureServer();
- const response = await requestBackgroundDirectPrompt(
- server,
- "<task-notification>completed</task-notification> E2E_BACKGROUND_DIRECT_USER_PROMPT_PAYLOAD",
- );
- expect(response.status).toBe(500);
- await expect(response.text()).resolves.toContain("Missing Upstream e2e fixture");
- });
- });
- async function startFixtureServer(closeMode: "end" | "destroy") {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-replay-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- const artifactPath = join(tempDir, "artifact.json");
- await writeFile(
- fixturePath,
- JSON.stringify(
- {
- fixtures: [
- {
- id: `sse-close-mode-${closeMode}`,
- match: {
- bodyIncludes: ["SSE_CLOSE_MODE_PROBE"],
- method: "POST",
- pathIncludes: "/messages",
- },
- response: {
- closeMode,
- events: [
- {
- byteLength: Buffer.byteLength(SSE_EVENT_TEXT, "utf-8"),
- offsetMs: 0,
- sequence: 0,
- text: SSE_EVENT_TEXT,
- },
- ],
- headers: { "content-type": "text/event-stream; charset=utf-8" },
- statusCode: 200,
- statusMessage: "OK",
- },
- },
- ],
- version: 1,
- },
- null,
- 2,
- ),
- );
- const server = await startUpstreamReplayServer({
- artifactPath,
- fixturePaths: [fixturePath],
- });
- activeServers.push(server);
- return { artifactPath, server };
- }
- async function startBrowserVariableFixtureServer(
- options: {
- artifactWriter?: Parameters<typeof startUpstreamReplayServer>[0]["artifactWriter"];
- } = {},
- ) {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-browser-variable-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- const artifactPath = join(tempDir, "artifact.json");
- await writeFile(
- fixturePath,
- JSON.stringify(
- {
- fixtures: [
- {
- id: "browser-variable",
- match: {
- bodyIncludes: ["BROWSER_VARIABLE_PROBE"],
- method: "POST",
- pathIncludes: "/messages",
- },
- response: {
- statusCode: 200,
- toolUse: {
- id: "toolu_browser_variable",
- input: {
- code: "await tab.goto('{{browserFixtureUrl}}')",
- title: "打开测试页面",
- },
- name: "mcp__node_repl__js",
- },
- },
- },
- ],
- version: 1,
- },
- null,
- 2,
- ),
- );
- const server = await startUpstreamReplayServer({
- artifactPath,
- fixturePaths: [fixturePath],
- ...(options.artifactWriter ? { artifactWriter: options.artifactWriter } : {}),
- });
- activeServers.push(server);
- return server;
- }
- async function startAutomationVariableFixtureServer() {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-automation-variable-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- const artifactPath = join(tempDir, "artifact.json");
- await writeFile(
- fixturePath,
- JSON.stringify(
- {
- fixtures: [
- {
- id: "automation-variable",
- match: {
- bodyIncludes: ["AUTOMATION_VARIABLE_PROBE"],
- method: "POST",
- pathIncludes: "/messages",
- },
- response: {
- statusCode: 200,
- toolUse: {
- id: "toolu_automation_variable",
- input: { id: "{{latestAutomationId}}", title: "updated" },
- name: "CronUpdate",
- },
- },
- },
- ],
- version: 1,
- },
- null,
- 2,
- ),
- );
- const server = await startUpstreamReplayServer({
- artifactPath,
- fixturePaths: [fixturePath],
- });
- activeServers.push(server);
- return server;
- }
- async function startBrowserTabVariableFixtureServer() {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-browser-tab-variable-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- const artifactPath = join(tempDir, "artifact.json");
- await writeFile(
- fixturePath,
- JSON.stringify(
- {
- fixtures: [
- {
- id: "browser-tab-variable",
- match: {
- bodyIncludes: ["BROWSER_TAB_VARIABLE_PROBE"],
- method: "POST",
- pathIncludes: "/messages",
- },
- response: {
- statusCode: 200,
- toolUse: {
- id: "toolu_browser_tab_variable",
- input: {
- code: [
- "const browser = await agent.browsers.get('iab');",
- "const tab = await browser.tabs.get('{{latestBrowserTabId}}');",
- "const tabA = await browser.tabs.get('{{latestBrowserTabAId}}');",
- "const tabB = await browser.tabs.get('{{latestBrowserTabBId}}');",
- ].join("\n"),
- title: "恢复 Browser tab",
- },
- name: "mcp__node_repl__js",
- },
- },
- },
- ],
- version: 1,
- },
- null,
- 2,
- ),
- );
- const server = await startUpstreamReplayServer({
- artifactPath,
- fixturePaths: [fixturePath],
- });
- activeServers.push(server);
- return server;
- }
- async function startLatestToolResultFixtureServer() {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-tool-result-"));
- tempDirs.push(tempDir);
- const fixturePath = join(tempDir, "fixture.json");
- const artifactPath = join(tempDir, "artifact.json");
- await writeFile(
- fixturePath,
- JSON.stringify(
- {
- fixtures: [
- {
- id: "latest-tool-result",
- match: {
- lastUserMessageIncludes: ["E2E_TOOL_RESULT_OK"],
- method: "POST",
- pathIncludes: "/messages",
- },
- response: { statusCode: 200, text: "tool result matched" },
- },
- ],
- version: 1,
- },
- null,
- 2,
- ),
- );
- const server = await startUpstreamReplayServer({
- artifactPath,
- fixturePaths: [fixturePath],
- });
- activeServers.push(server);
- return server;
- }
- async function startBackgroundFixtureServer() {
- const tempDir = await mkdtemp(join(tmpdir(), "zcode-upstream-background-fixture-"));
- tempDirs.push(tempDir);
- const artifactPath = join(tempDir, "artifact.json");
- const fixturePath = join(
- import.meta.dirname,
- "e2e/fixtures/upstream/conversation-session/conversation-session-background.json",
- );
- const server = await startUpstreamReplayServer({
- artifactPath,
- fixturePaths: [fixturePath],
- });
- activeServers.push(server);
- return server;
- }
- function requestBackgroundDirectPrompt(server: UpstreamReplayServer, latestUserText: string) {
- // Bug 根因:TaskOutput 的 provider schema 自身包含 notification 标签;
- // 是否已注入通知必须只看最新 user message,不能扫描整个请求体。
- return fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- messages: [{ role: "user", content: latestUserText }],
- tools: [
- {
- description: "Background tasks receive a <task-notification> when the task completes.",
- name: "TaskOutput",
- },
- ],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- });
- }
- async function readArtifact(path: string): Promise<E2ENetworkCaptureArtifact> {
- return JSON.parse(await readFile(path, "utf-8")) as E2ENetworkCaptureArtifact;
- }
- async function reserveLoopbackPort(): Promise<number> {
- const server = createNetServer();
- await listenOnLoopback(server, 0);
- const address = server.address();
- if (!address || typeof address === "string") {
- await closeNetServer(server);
- throw new Error("Temporary port server did not expose an address");
- }
- await closeNetServer(server);
- return address.port;
- }
- async function expectLoopbackPortAvailable(port: number): Promise<void> {
- const server = createNetServer();
- try {
- await listenOnLoopback(server, port);
- } finally {
- if (server.listening) {
- await closeNetServer(server);
- }
- }
- }
- function listenOnLoopback(server: NetServer, port: number): Promise<void> {
- return new Promise((resolveListen, rejectListen) => {
- const onError = (error: Error) => {
- rejectListen(error);
- };
- server.once("error", onError);
- server.listen(port, "127.0.0.1", () => {
- server.off("error", onError);
- resolveListen();
- });
- });
- }
- function closeNetServer(server: NetServer): Promise<void> {
- return new Promise((resolveClose, rejectClose) => {
- server.close((error) => {
- if (error) {
- rejectClose(error);
- return;
- }
- resolveClose();
- });
- });
- }
|