File size: 4,657 Bytes
63522a5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | import { beforeEach, describe, expect, it, vi } from "vitest";
import {
FileClient,
PluginsClient,
} from "@openhands/typescript-client/clients";
import {
setActiveSelection,
setRegisteredBackends,
} from "./backend-registry/active-store";
import PluginsService from "./plugins-service";
vi.mock("@openhands/typescript-client/clients", () => ({
PluginsClient: vi.fn(),
FileClient: vi.fn(),
}));
const getPluginsMarketplace = vi.fn();
const getPlugins = vi.fn();
const downloadFile = vi.fn();
const close = vi.fn();
function useBackend(kind: "local" | "cloud"): void {
setRegisteredBackends([
{
id: kind,
name: kind,
host: "http://127.0.0.1:8001",
apiKey: "session-key",
kind,
},
]);
setActiveSelection({ backendId: kind, orgId: null });
}
describe("PluginsService.getPluginsMarketplace", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(PluginsClient).mockImplementation(function MockPluginsClient() {
return { getPluginsMarketplace, close } as unknown as PluginsClient;
} as unknown as typeof PluginsClient);
});
it("returns the catalog from the local agent-server", async () => {
useBackend("local");
const plugin = {
name: "city-weather",
description: "Weather plugin",
source: "github:OpenHands/extensions",
ref: null,
repo_path: "plugins/city-weather",
installed: false,
};
getPluginsMarketplace.mockResolvedValue({ plugins: [plugin] });
const result = await PluginsService.getPluginsMarketplace();
expect(result).toEqual([plugin]);
expect(getPluginsMarketplace).toHaveBeenCalledTimes(1);
});
it("returns an empty catalog on a cloud backend without calling the client", async () => {
useBackend("cloud");
const result = await PluginsService.getPluginsMarketplace();
expect(result).toEqual([]);
expect(PluginsClient).not.toHaveBeenCalled();
});
it("returns an empty catalog when the local request fails", async () => {
useBackend("local");
getPluginsMarketplace.mockRejectedValue(new Error("unreachable"));
const result = await PluginsService.getPluginsMarketplace();
expect(result).toEqual([]);
});
});
describe("PluginsService.getLocalPlugins", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(PluginsClient).mockImplementation(function MockPluginsClient() {
return { getPlugins, close } as unknown as PluginsClient;
} as unknown as typeof PluginsClient);
});
it("requests user-level local plugins from the local agent-server", async () => {
useBackend("local");
const plugin = {
name: "hello-local",
version: "1.0.0",
description: "A local plugin",
};
getPlugins.mockResolvedValue({ plugins: [plugin] });
const result = await PluginsService.getLocalPlugins();
expect(result).toEqual([plugin]);
expect(getPlugins).toHaveBeenCalledWith({
load_user: true,
load_project: false,
});
});
it("returns an empty list on a cloud backend without calling the client", async () => {
useBackend("cloud");
const result = await PluginsService.getLocalPlugins();
expect(result).toEqual([]);
expect(PluginsClient).not.toHaveBeenCalled();
});
});
describe("PluginsService.getPluginFileContent", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(FileClient).mockImplementation(function MockFileClient() {
return { downloadFile, close } as unknown as FileClient;
} as unknown as typeof FileClient);
});
it("downloads the file under the plugin directory and decodes it as text", async () => {
useBackend("local");
downloadFile.mockResolvedValue(new TextEncoder().encode("# Hello").buffer);
const result = await PluginsService.getPluginFileContent(
"/plugins/demo",
"docs/README.md",
);
expect(result).toEqual({ kind: "text", text: "# Hello" });
expect(downloadFile).toHaveBeenCalledWith("/plugins/demo/docs/README.md");
});
it("flags content containing NUL bytes as binary", async () => {
useBackend("local");
downloadFile.mockResolvedValue(
new Uint8Array([0x89, 0x50, 0x00, 0x47]).buffer,
);
const result = await PluginsService.getPluginFileContent(
"/plugins/demo",
"logo.png",
);
expect(result).toEqual({ kind: "binary", text: null });
});
it("rejects on a cloud backend without calling the client", async () => {
useBackend("cloud");
await expect(
PluginsService.getPluginFileContent("/plugins/demo", "README.md"),
).rejects.toThrow();
expect(FileClient).not.toHaveBeenCalled();
});
});
|