Файли
openidea/src/workbench/endpointProviders.test.ts
T
rohitkumardandaтаrohitdanda cb5e7081fa fix(platform): keep delivering IDE events after one handler throws (#2790)
Broadcasts called their listeners in a bare loop, so one throwing handler kept the rest (including the catch-all "*" IDE event handlers) from seeing the event. The IDE event bus, local diagnostics, endpoint-change and navigation-modifier broadcasts now go through `notifyEach`, which logs a throwing listener and moves on. `onIdeEvent` wraps each subscription so unsubscribing one of two identical handlers keeps the other.

Co-authored-by: rohitdanda <[email protected]>
2026-11-12 02:09:24 +08:00

72 рядки
2.2 KiB
TypeScript

import { expect, it, vi } from "vitest";
import {
endpointsChanged,
onEndpointsChanged,
registerEndpointProvider,
searchEndpoints,
type EndpointEntry,
} from "./contributions";
const endpoint: EndpointEntry = {
method: "GET",
url: "/menu/list",
controller: "MenuController",
methodName: "listMenus",
path: "/p/MenuController.java",
rel: "MenuController.java",
line: 4,
col: 11,
};
it("collects endpoints from all providers for the requested root", async () => {
const first = vi.fn(async () => [endpoint]);
const second = vi.fn(async () => [{ ...endpoint, url: "/other" }]);
const disposers = [
registerEndpointProvider({ id: "test.first", endpoints: first }),
registerEndpointProvider({ id: "test.second", endpoints: second }),
];
try {
expect(await searchEndpoints(null)).toEqual([]);
expect(first).not.toHaveBeenCalled();
expect(second).not.toHaveBeenCalled();
expect(await searchEndpoints("/p")).toEqual([endpoint, { ...endpoint, url: "/other" }]);
expect(first).toHaveBeenCalledWith("/p");
expect(second).toHaveBeenCalledWith("/p");
} finally {
disposers.forEach((dispose) => dispose());
}
expect(await searchEndpoints("/p")).toEqual([]);
});
it("keeps a replacement provider registered when its predecessor is disposed", async () => {
const disposeOld = registerEndpointProvider({ id: "test.replaced", endpoints: async () => [] });
const disposeNew = registerEndpointProvider({ id: "test.replaced", endpoints: async () => [endpoint] });
try {
disposeOld();
expect(await searchEndpoints("/p")).toEqual([endpoint]);
} finally {
disposeOld();
disposeNew();
}
expect(await searchEndpoints("/p")).toEqual([]);
});
it("keeps notifying endpoint listeners after one throws", () => {
const error = vi.spyOn(console, "error").mockImplementation(() => {});
const second = vi.fn();
const offs = [
onEndpointsChanged(() => {
throw new Error("boom");
}),
onEndpointsChanged(second),
];
try {
endpointsChanged();
} finally {
offs.forEach((off) => off());
}
expect(second).toHaveBeenCalledOnce();
expect(error).toHaveBeenCalledOnce();
error.mockRestore();
});