| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049 |
- 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("分阶段工具参数必须逐次释放,第一次释放不能越过第二个观察点", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-replay-stages-"));
- tempDirs.push(dir);
- const fixture = join(dir, "fixture.json");
- await writeFile(
- fixture,
- JSON.stringify({
- version: 1,
- fixtures: [
- {
- id: "stages",
- metadata: { timingPolicy: "controlled-stream" },
- match: { pathIncludes: "/messages" },
- response: {
- statusCode: 200,
- releaseAfterChunks: [1, 2],
- chunks: [
- { offsetMs: 0, text: "partial\n" },
- { offsetMs: 45000, text: "path-ready\n" },
- { offsetMs: 90000, text: "completed\n" },
- ],
- },
- },
- ],
- }),
- );
- const server = await startUpstreamReplayServer({
- artifactPath: join(dir, "artifact.json"),
- fixturePaths: [fixture],
- });
- activeServers.push(server);
- const response = await fetch(`${server.baseUrl}/messages`, {
- signal: AbortSignal.timeout(5000),
- });
- const reader = response.body!.getReader();
- for (const text of ["partial\n", "path-ready\n"]) {
- expect(new TextDecoder().decode((await reader.read()).value)).toBe(text);
- expect(await fetch(`${server.baseUrl}/__e2e__/gates`).then((r) => r.json())).toEqual([
- "stages",
- ]);
- expect(
- (await fetch(`${server.baseUrl}/__e2e__/release/stages`, { method: "POST" })).status,
- ).toBe(204);
- }
- expect(new TextDecoder().decode((await reader.read()).value)).toBe("completed\n");
- expect((await reader.read()).done).toBe(true);
- expect(
- (await fetch(`${server.baseUrl}/__e2e__/release/stages`, { method: "POST" })).status,
- ).toBe(409);
- });
- it("从最新真实 workflow-hole 通知绑定 run ID,缺失通知时拒绝伪造 FillWorkflowHole", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-workflow-hole-replay-"));
- tempDirs.push(dir);
- const fixture = join(dir, "fixture.json");
- await writeFile(
- fixture,
- JSON.stringify({
- version: 1,
- fixtures: [
- {
- id: "fill-hole",
- match: { pathIncludes: "/messages" },
- response: {
- statusCode: 200,
- toolUse: {
- name: "FillWorkflowHole",
- input: {
- run_id: "{{latestWorkflowRunId}}",
- hole_id: "hole#21b40fca",
- script: "return 1;",
- },
- },
- },
- },
- ],
- }),
- );
- const server = await startUpstreamReplayServer({
- artifactPath: join(dir, "artifact.json"),
- fixturePaths: [fixture],
- });
- activeServers.push(server);
- const request = (content: string) =>
- fetch(`${server.baseUrl}/messages`, {
- method: "POST",
- headers: { "content-type": "application/json" },
- body: JSON.stringify({ messages: [{ role: "user", content }] }),
- });
- const response = await request(
- "<workflow-hole><run-id>run-old</run-id></workflow-hole>\n<workflow-hole><run-id>run-current</run-id></workflow-hole>",
- );
- const body = await response.text();
- expect(response.status).toBe(200);
- expect(body).toContain("run-current");
- expect(body).not.toContain("run-old");
- expect(body).not.toContain("{{latestWorkflowRunId}}");
- const missing = await request("no workflow notification");
- expect(missing.status).toBe(500);
- expect(await missing.text()).toContain("Missing");
- });
- it("按请求的 stream 属性选择 SSE 或 JSON 响应格式", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-replay-response-format-"));
- tempDirs.push(dir);
- const fixture = join(dir, "fixture.json");
- await writeFile(
- fixture,
- JSON.stringify({
- version: 1,
- fixtures: [
- {
- id: "format-text",
- match: { bodyIncludes: ["FORMAT_PROBE_TEXT"], pathIncludes: "/messages" },
- response: { statusCode: 200, text: { id: "msg_format_text", text: "FORMAT_TEXT_OK" } },
- },
- {
- id: "format-tool",
- match: { bodyIncludes: ["FORMAT_PROBE_TOOL"], pathIncludes: "/messages" },
- response: {
- statusCode: 200,
- toolUse: { id: "toolu_format_tool", input: { value: 1 }, name: "Read" },
- },
- },
- ],
- }),
- );
- const server = await startUpstreamReplayServer({
- artifactPath: join(dir, "artifact.json"),
- fixturePaths: [fixture],
- });
- activeServers.push(server);
- const request = (content: string, stream?: true) =>
- fetch(`${server.baseUrl}/messages`, {
- body: JSON.stringify({
- ...(stream ? { stream } : {}),
- messages: [{ role: "user", content }],
- }),
- headers: { "content-type": "application/json" },
- method: "POST",
- signal: AbortSignal.timeout(5000),
- });
- const nonStreamText = await request("FORMAT_PROBE_TEXT");
- expect(nonStreamText.headers.get("content-type")).toContain("application/json");
- expect(await nonStreamText.json()).toMatchObject({
- id: "msg_format_text",
- type: "message",
- role: "assistant",
- content: [{ type: "text", text: "FORMAT_TEXT_OK" }],
- stop_reason: "end_turn",
- });
- const streamingText = await request("FORMAT_PROBE_TEXT", true);
- expect(streamingText.headers.get("content-type")).toContain("text/event-stream");
- const streamingBody = await streamingText.text();
- expect(streamingBody).toContain("event: message_start");
- expect(streamingBody).toContain("FORMAT_TEXT_OK");
- const nonStreamTool = await request("FORMAT_PROBE_TOOL");
- expect(nonStreamTool.headers.get("content-type")).toContain("application/json");
- expect(await nonStreamTool.json()).toMatchObject({
- type: "message",
- role: "assistant",
- content: [{ type: "tool_use", id: "toolu_format_tool", name: "Read", input: { value: 1 } }],
- stop_reason: "tool_use",
- });
- });
- it("受控流在释放前保持 running,释放后不再等待旧的 45 秒时间轴", async () => {
- const dir = await mkdtemp(join(tmpdir(), "zcode-replay-gate-"));
- tempDirs.push(dir);
- const fixture = join(dir, "fixture.json");
- await writeFile(
- fixture,
- JSON.stringify({
- version: 1,
- fixtures: [
- {
- id: "controlled",
- metadata: { timingPolicy: "controlled-stream" },
- match: { pathIncludes: "/messages" },
- response: {
- statusCode: 200,
- releaseAfterChunks: 1,
- headers: { "content-type": "text/event-stream" },
- chunks: [
- { offsetMs: 0, text: "running\n" },
- { offsetMs: 45000, text: "complete\n" },
- ],
- },
- },
- ],
- }),
- );
- const server = await startUpstreamReplayServer({
- artifactPath: join(dir, "artifact.json"),
- fixturePaths: [fixture],
- });
- activeServers.push(server);
- const response = await fetch(`${server.baseUrl}/messages`, {
- signal: AbortSignal.timeout(5000),
- });
- const reader = response.body!.getReader();
- expect(new TextDecoder().decode((await reader.read()).value)).toBe("running\n");
- expect(
- (await fetch(`${server.baseUrl}/__e2e__/release/unknown`, { method: "POST" })).status,
- ).toBe(409);
- const held = await fetch(`${server.baseUrl}/__e2e__/gates`).then((result) => result.json());
- expect(held).toEqual(["controlled"]);
- expect(
- (await fetch(`${server.baseUrl}/__e2e__/release/controlled`, { method: "POST" })).status,
- ).toBe(204);
- expect(new TextDecoder().decode((await reader.read()).value)).toBe("complete\n");
- expect((await reader.read()).done).toBe(true);
- expect(
- (await fetch(`${server.baseUrl}/__e2e__/release/controlled`, { method: "POST" })).status,
- ).toBe(409);
- // 第二个 runner 使用相同 fixture ID,也必须有独立的 gate 与请求台账。
- const other = await startUpstreamReplayServer({
- artifactPath: join(dir, "other.json"),
- fixturePaths: [fixture],
- });
- activeServers.push(other);
- const second = await fetch(`${other.baseUrl}/messages`, { signal: AbortSignal.timeout(5000) });
- const secondReader = second.body!.getReader();
- await secondReader.read();
- expect(
- (await fetch(`${server.baseUrl}/__e2e__/release/controlled`, { method: "POST" })).status,
- ).toBe(409);
- expect(await fetch(`${other.baseUrl}/__e2e__/gates`).then((value) => value.json())).toEqual([
- "controlled",
- ]);
- await fetch(`${other.baseUrl}/__e2e__/release/controlled`, { method: "POST" });
- expect(new TextDecoder().decode((await secondReader.read()).value)).toBe("complete\n");
- expect((await secondReader.read()).done).toBe(true);
- });
- 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({
- stream: true,
- 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({
- stream: true,
- 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.each(["events", "chunks", "body"] as const)(
- "destroys %s SSE responses when closeMode is destroy",
- async (format) => {
- const { artifactPath, server } = await startFixtureServer("destroy", format);
- 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",
- });
- if (format === "events") expect(record?.responseEvents).toHaveLength(1);
- if (format === "chunks") expect(record?.responseChunkTimeline).toHaveLength(1);
- expect(record?.responseBodyBytes).toBe(Buffer.byteLength(SSE_EVENT_TEXT));
- },
- );
- 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",
- format: "events" | "chunks" | "body" = "events",
- ) {
- 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,
- ...(format === "body"
- ? { body: SSE_EVENT_TEXT }
- : {
- [format]: [
- {
- 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();
- });
- });
- }
|