Файли
openidea/src/workbench/externalFiles.test.ts
T
Gabriel Gondim 9467c4ea1b fix(plugins): install-dir reads and default editors over associated apps (#4918)
A plugin's fs.read-*, editor.open and views.open-location rejected absolute paths under its own install (package) directory, so a linked plugin could not open files bundled next to its component. The install directory is now readable like fs.plugin-dir() (writes still need an fs.write grant). Custom editors with priority "default" now open in the IDE ahead of the Files opened in associated applications list, so a PDF editor plugin is not bypassed by the built-in *.pdf association. The SDK carries the updated WIT docs and the plugin components are rebuilt.

Fixes #4167

Co-authored-by: GGVidal <[email protected]>
2026-11-21 07:58:50 +08:00

95 рядки
3.1 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from "vitest";
import {
openFileExternally,
opensExternally,
registerExternalFileOpenHandler,
registerExternalOpenSuppression,
type ExternalFileOpenHandler,
} from "./contributions";
const disposers: Array<() => void> = [];
function register(handler: Omit<ExternalFileOpenHandler, "matches"> & Partial<ExternalFileOpenHandler>) {
const dispose = registerExternalFileOpenHandler({ matches: () => true, ...handler });
disposers.push(dispose);
return dispose;
}
afterEach(() => {
disposers.splice(0).forEach((dispose) => dispose());
vi.restoreAllMocks();
});
describe("external file openers", () => {
it("asks handlers in order and stops at the first that takes the file", async () => {
const calls: string[] = [];
register({
id: "second",
order: 20,
open: (p) => {
calls.push(`second:${p}`);
return false;
},
});
register({
id: "first",
order: 10,
open: (p) => {
calls.push(`first:${p}`);
return true;
},
});
register({
id: "never",
order: 30,
open: (p) => {
calls.push(`never:${p}`);
return true;
},
});
expect(await openFileExternally("/p/a.xlsx")).toBe(true);
expect(calls).toEqual(["first:/p/a.xlsx"]);
});
it("falls through when a handler declines or fails", async () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
register({ id: "declines", order: 10, open: () => false });
register({ id: "fails", order: 20, open: () => Promise.reject(new Error("no association")) });
register({ id: "takes", order: 30, open: () => true });
expect(await openFileExternally("/p/a.xlsx")).toBe(true);
});
it("leaves the file to the editors without a handler", async () => {
expect(await openFileExternally("/p/a.ts")).toBe(false);
});
it("skips handlers that do not match and reports whether one would", async () => {
const open = vi.fn(() => true);
register({ id: "pdf", matches: (p) => p.endsWith(".pdf"), open });
expect(opensExternally("/p/a.pdf")).toBe(true);
expect(opensExternally("/p/a.ts")).toBe(false);
expect(await openFileExternally("/p/a.ts")).toBe(false);
expect(open).not.toHaveBeenCalled();
});
it("disposal removes a handler", async () => {
const dispose = register({ id: "only", open: () => true });
expect(await openFileExternally("/p/a.xlsx")).toBe(true);
dispose();
expect(await openFileExternally("/p/a.xlsx")).toBe(false);
});
it("skips associated apps when a default custom editor claims the path", async () => {
const open = vi.fn(() => true);
register({ id: "pdf", matches: (p) => p.endsWith(".pdf"), open });
const dispose = registerExternalOpenSuppression("plugin.pdf-reader", (p) => p.endsWith(".pdf"));
disposers.push(dispose);
expect(opensExternally("/p/a.pdf")).toBe(false);
expect(await openFileExternally("/p/a.pdf")).toBe(false);
expect(open).not.toHaveBeenCalled();
dispose();
expect(opensExternally("/p/a.pdf")).toBe(true);
expect(await openFileExternally("/p/a.pdf")).toBe(true);
});
});