дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
A page session can answer requests itself: rules (URL part → status, content type, body) are kept per project, edited from the Network tab (Overrides, Override Response… on a request), and applied through js-debug's CDP proxy with Fetch.enable / Fetch.fulfillRequest. Interception is switched off before the connection is dropped, unknown statuses get a reason phrase, cross-origin pages get CORS headers and preflight answers, and rules present at page start wait for the launch response. The Network list marks a request overridden only when it was sent after the rule that answers it was saved or enabled. Co-authored-by: ntbang0901 <[email protected]>
259 рядки
11 KiB
TypeScript
259 рядки
11 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
|
|
import type { JsNetworkEvent, JsResponseBody } from "@/platform/ipc";
|
|
|
|
const asked: Array<[number, string]> = [];
|
|
const evaluated: unknown[][] = [];
|
|
let answer: { status: "ok"; data: JsResponseBody } | { status: "error"; error: { kind: string; message: string } };
|
|
vi.mock("@/platform/ipc", async (orig) => {
|
|
const real = await orig<typeof import("@/platform/ipc")>();
|
|
return {
|
|
...real,
|
|
commands: {
|
|
...real.commands,
|
|
debugJsResponseBody: (session: number, requestId: string) => {
|
|
asked.push([session, requestId]);
|
|
return Promise.resolve(answer);
|
|
},
|
|
debugEvaluateIn: (...args: unknown[]) => {
|
|
evaluated.push(args);
|
|
return Promise.resolve({ status: "ok", data: { value: "", variablesReference: 0 } });
|
|
},
|
|
debugJsResponseOverrides: () => Promise.resolve({ status: "ok", data: null }),
|
|
},
|
|
};
|
|
});
|
|
|
|
const { applyNetworkEvents, useJsNetwork } = await import("../js/network");
|
|
const { NetworkView } = await import("../js/NetworkView");
|
|
const { useJsBreakpoints } = await import("../js/eventBreakpoints");
|
|
const { loadResponseOverrides, saveOverride } = await import("../js/responseOverrides");
|
|
const { setRefused } = await import("../js/refusals");
|
|
const { handleDebugEvent, useDebug } = await import("../state");
|
|
|
|
const event = (kind: JsNetworkEvent["kind"], requestId: string, p: Partial<JsNetworkEvent> = {}): JsNetworkEvent => ({
|
|
kind,
|
|
requestId,
|
|
timestampMs: 1000,
|
|
url: null,
|
|
method: null,
|
|
resourceType: null,
|
|
status: null,
|
|
mimeType: null,
|
|
headers: {},
|
|
encodedLength: null,
|
|
errorText: null,
|
|
initiator: null,
|
|
...p,
|
|
});
|
|
|
|
function load() {
|
|
applyNetworkEvents(7, [
|
|
event("request", "1", {
|
|
url: "http://h/api/items",
|
|
method: "GET",
|
|
resourceType: "Fetch",
|
|
headers: { Accept: "*/*" },
|
|
}),
|
|
event("response", "1", {
|
|
status: 200,
|
|
mimeType: "application/json",
|
|
headers: { "Content-Type": "application/json" },
|
|
}),
|
|
event("finished", "1", { timestampMs: 1250, encodedLength: 2048 }),
|
|
event("request", "2", { url: "http://h/logo.png", method: "GET", resourceType: "Image" }),
|
|
event("request", "3", { url: "http://h/gone", method: "POST", resourceType: "XHR" }),
|
|
event("failed", "3", { timestampMs: 1100, errorText: "net::ERR_FAILED" }),
|
|
]);
|
|
}
|
|
|
|
beforeEach(() => {
|
|
localStorage.clear();
|
|
loadResponseOverrides(null);
|
|
useJsNetwork.setState({ rows: {} });
|
|
asked.length = 0;
|
|
evaluated.length = 0;
|
|
answer = { status: "ok", data: { body: '{"items":[]}', base64Encoded: false, truncated: false } };
|
|
});
|
|
|
|
describe("Network view", () => {
|
|
it("lists a session's requests with status, type, size and time", () => {
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
const rows = screen.getAllByRole("row").slice(1);
|
|
expect(rows).toHaveLength(3);
|
|
expect(rows[0]).toHaveTextContent("items");
|
|
expect(rows[0]).toHaveTextContent("200");
|
|
expect(rows[0]).toHaveTextContent("Fetch");
|
|
expect(rows[0]).toHaveTextContent("2.0 kB");
|
|
expect(rows[0]).toHaveTextContent("250 ms");
|
|
expect(rows[2]).toHaveTextContent("net::ERR_FAILED");
|
|
});
|
|
|
|
it("filters by a part of the URL", () => {
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.change(screen.getByLabelText("Filter requests"), { target: { value: "logo" } });
|
|
expect(screen.getAllByRole("row").slice(1)).toHaveLength(1);
|
|
});
|
|
|
|
it("shows a selected request's headers and loads its body", async () => {
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getAllByRole("row")[1]!);
|
|
expect(screen.getByText("http://h/api/items")).toBeInTheDocument();
|
|
expect(screen.getByText("Accept")).toBeInTheDocument();
|
|
expect(screen.getByText("Content-Type")).toBeInTheDocument();
|
|
await waitFor(() => expect(screen.getByText('{"items":[]}')).toBeInTheDocument());
|
|
expect(asked).toEqual([[7, "1"]]);
|
|
});
|
|
|
|
it("says so when a body is binary, cut or gone", async () => {
|
|
load();
|
|
answer = { status: "ok", data: { body: "AAEC", base64Encoded: true, truncated: false } };
|
|
const { unmount } = render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getAllByRole("row")[1]!);
|
|
await waitFor(() => expect(screen.getByText("Binary content")).toBeInTheDocument());
|
|
expect(screen.queryByText("AAEC")).toBeNull();
|
|
unmount();
|
|
|
|
answer = { status: "ok", data: { body: "abc", base64Encoded: false, truncated: true } };
|
|
const second = render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getAllByRole("row")[1]!);
|
|
await waitFor(() => expect(screen.getByText("Showing the first 1 MB")).toBeInTheDocument());
|
|
second.unmount();
|
|
|
|
// The session ended, or the browser dropped the response.
|
|
answer = { status: "error", error: { kind: "unavailable", message: "gone" } };
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getAllByRole("row")[1]!);
|
|
await waitFor(() => expect(screen.getByText("Response body is no longer available")).toBeInTheDocument());
|
|
});
|
|
|
|
it("clears the list", () => {
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getByRole("button", { name: "Clear" }));
|
|
expect(screen.queryAllByRole("row").slice(1)).toHaveLength(0);
|
|
expect(screen.getByText(/^No requests yet/)).toBeInTheDocument();
|
|
});
|
|
|
|
it("filters by the kind of resource, like a browser's network panel", () => {
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getByRole("button", { name: "Fetch/XHR" }));
|
|
expect(screen.getByRole("button", { name: "Fetch/XHR" })).toHaveAttribute("aria-pressed", "true");
|
|
const shown = screen.getAllByRole("row").slice(1);
|
|
expect(shown).toHaveLength(2);
|
|
expect(shown[0]).toHaveTextContent("items");
|
|
expect(shown[1]).toHaveTextContent("gone");
|
|
fireEvent.click(screen.getByRole("button", { name: "Img" }));
|
|
expect(screen.getAllByRole("row").slice(1)).toHaveLength(1);
|
|
fireEvent.click(screen.getByRole("button", { name: "All" }));
|
|
expect(screen.getAllByRole("row").slice(1)).toHaveLength(3);
|
|
});
|
|
|
|
it("can show only what the project's own code requested", () => {
|
|
applyNetworkEvents(7, [
|
|
event("request", "1", {
|
|
url: "http://h/api/items",
|
|
resourceType: "Fetch",
|
|
initiator: { function: "load", url: "http://h/src/App.vue", line: 9, column: 2 },
|
|
}),
|
|
event("request", "2", {
|
|
url: "http://h/api/track",
|
|
resourceType: "Fetch",
|
|
initiator: { function: "send", url: "http://h/node_modules/.vite/deps/analytics.js", line: 1, column: 1 },
|
|
}),
|
|
]);
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getByLabelText("My code only"));
|
|
const shown = screen.getAllByRole("row").slice(1);
|
|
expect(shown).toHaveLength(1);
|
|
expect(shown[0]).toHaveTextContent("items");
|
|
});
|
|
|
|
it("turns a request into a breakpoint on the next one to that address", () => {
|
|
useJsBreakpoints.setState({ root: null, events: {}, requests: [] });
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getAllByRole("row")[1]!);
|
|
fireEvent.click(screen.getByRole("button", { name: "Break on Next Request" }));
|
|
expect(useJsBreakpoints.getState().requests).toEqual([{ pattern: "/api/items", enabled: true }]);
|
|
});
|
|
|
|
it("reloads the page, to list the requests of its load", () => {
|
|
render(<NetworkView session={7} />);
|
|
// Recording starts while the page is already loading.
|
|
expect(screen.getByText("No requests yet. Reload the page to see the requests of its load.")).toBeInTheDocument();
|
|
fireEvent.click(screen.getByRole("button", { name: "Reload Page" }));
|
|
expect(evaluated).toEqual([[7, null, "location.reload()", "repl"]]);
|
|
});
|
|
|
|
it("says that requests are not reported instead of waiting for them", () => {
|
|
render(<NetworkView session={7} />);
|
|
expect(screen.getByText(/^No requests yet/)).toBeInTheDocument();
|
|
const page = { id: 7, name: "page", status: "running", caps: { jsPage: true } };
|
|
useDebug.setState({ sessions: { 7: page } as unknown as ReturnType<typeof useDebug.getState>["sessions"] });
|
|
act(() => handleDebugEvent(7, { kind: "jsNetworkUnavailable" }));
|
|
expect(
|
|
screen.getByText("This version of the JavaScript debugger does not report a page's requests."),
|
|
).toBeInTheDocument();
|
|
setRefused(7, "network", null);
|
|
});
|
|
|
|
it("marks the requests whose response a rule replaces", () => {
|
|
loadResponseOverrides("/p");
|
|
saveOverride({ pattern: "/api/items", status: 200, contentType: "application/json", body: "[]", enabled: true });
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
const rows = screen.getAllByRole("row").slice(1);
|
|
expect(rows[0]).toHaveTextContent("overridden");
|
|
expect(rows[1]).not.toHaveTextContent("overridden");
|
|
expect(screen.getByRole("button", { name: "Overrides (1)" })).toBeInTheDocument();
|
|
});
|
|
|
|
it("does not mark a request as overridden when the page's debugger refused the rules, and says why", () => {
|
|
loadResponseOverrides("/p");
|
|
saveOverride({ pattern: "/api/items", status: 200, contentType: "application/json", body: "[]", enabled: true });
|
|
load();
|
|
setRefused(7, "overrides", "no browser connection");
|
|
render(<NetworkView session={7} />);
|
|
expect(screen.getAllByRole("row")[1]).not.toHaveTextContent("overridden");
|
|
expect(screen.getByRole("alert")).toHaveTextContent(
|
|
"Responses are not replaced in this page: no browser connection",
|
|
);
|
|
setRefused(7, "overrides", null);
|
|
});
|
|
|
|
it("opens the override editor on a request, filled with its response", async () => {
|
|
loadResponseOverrides("/p");
|
|
load();
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getAllByRole("row")[1]!);
|
|
fireEvent.click(screen.getByRole("button", { name: "Override Response…" }));
|
|
await waitFor(() => expect(screen.getByLabelText("Part of the URL")).toHaveValue("/api/items"));
|
|
expect(screen.getByLabelText("Status")).toHaveValue("200");
|
|
expect(screen.getByLabelText("Content type")).toHaveValue("application/json");
|
|
expect(screen.getByLabelText("Response body")).toHaveValue('{"items":[]}');
|
|
});
|
|
|
|
it("starts from an empty body when the request's own can't be read", async () => {
|
|
loadResponseOverrides("/p");
|
|
load();
|
|
answer = { status: "error", error: { kind: "unavailable", message: "gone" } };
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getAllByRole("row")[1]!);
|
|
fireEvent.click(screen.getByRole("button", { name: "Override Response…" }));
|
|
await waitFor(() => expect(screen.getByLabelText("Part of the URL")).toHaveValue("/api/items"));
|
|
expect(screen.getByLabelText("Response body")).toHaveValue("");
|
|
});
|
|
|
|
it("opens the list of overrides from the toolbar", () => {
|
|
loadResponseOverrides("/p");
|
|
render(<NetworkView session={7} />);
|
|
fireEvent.click(screen.getByRole("button", { name: "Overrides" }));
|
|
expect(screen.getByText("No overrides yet")).toBeInTheDocument();
|
|
});
|
|
});
|