Files
kandev/apps/web/lib/plugins/host-api.test.ts
T
Kandev Agent c53f699714 feat: add plugin system with grpc runtime and native ui plugins
- go plugin backends ship as release tarballs (per-platform binaries, checksums) that
  kandev installs from url/upload or filesystem sideload+sync, then spawns and
  supervises via hashicorp/go-plugin (unix socket / loopback tcp+automtls)
- strict typed grpc contract (kandev.plugin.v1): DeliverEvent/InvokeTool/HandleWebhook
  plus a capability-gated host service (state, secrets, emit); author sdk in
  pkg/pluginsdk; packaging cli in cmd/plugin-pack
- native frontend bundles: window.registerKandevPlugin + registry/host loader with
  dynamic routes, nav items, slots and ws handlers; plugins settings page with
  install dialog, sync button and enable/disable/uninstall
- gated behind the plugins feature flag (prod off, dev/e2e on); covered by unit and
  integration tests plus playwright e2e against a real packaged fixture plugin

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-16 17:55:45 +01:00

78 lines
2.7 KiB
TypeScript

import { describe, it, expect, vi, afterEach } from "vitest";
import * as React from "react";
import { createAppStore } from "@/lib/state/store";
import { buildHostApi } from "./host-api";
describe("buildHostApi", () => {
const originalFetch = global.fetch;
afterEach(() => {
global.fetch = originalFetch;
vi.unstubAllEnvs();
});
it("scopes api.fetch to /api/plugins/{pluginId}/... and forwards init", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 200 }));
global.fetch = fetchMock as unknown as typeof fetch;
const host = buildHostApi("jira", createAppStore(), "light");
await host.api.fetch("/issues", { method: "POST" });
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0];
expect(url).toContain("/api/plugins/jira/issues");
expect(init).toEqual({ method: "POST" });
});
it("normalizes a path that doesn't start with a slash", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 200 }));
global.fetch = fetchMock as unknown as typeof fetch;
const host = buildHostApi("jira", createAppStore(), "light");
await host.api.fetch("issues");
const [url] = fetchMock.mock.calls[0];
expect(url).toContain("/api/plugins/jira/issues");
});
it("exposes the host React instance and a jsx alias for React.createElement", () => {
const host = buildHostApi("jira", createAppStore(), "dark");
expect(host.React).toBe(React);
expect(host.jsx).toBe(React.createElement);
});
it("wires store.getState/setState/subscribe to the passed StoreApi", () => {
const store = createAppStore();
const getStateSpy = vi.spyOn(store, "getState");
const setStateSpy = vi.spyOn(store, "setState");
const subscribeSpy = vi.spyOn(store, "subscribe");
const host = buildHostApi("jira", store, "dark");
expect(host.store.getState()).toBe(store.getState());
host.store.setState({});
const listener = vi.fn();
const unsubscribe = host.store.subscribe(listener);
expect(getStateSpy).toHaveBeenCalled();
expect(setStateSpy).toHaveBeenCalled();
expect(subscribeSpy).toHaveBeenCalled();
unsubscribe();
});
it("exposes the requested theme and a curated ui component subset", () => {
const host = buildHostApi("jira", createAppStore(), "dark");
expect(host.theme).toBe("dark");
expect(host.ui.Button).toBeDefined();
expect(host.ui.Card).toBeDefined();
expect(host.ui.Badge).toBeDefined();
});
it("sets pluginId on the returned host api", () => {
const host = buildHostApi("jira", createAppStore(), "light");
expect(host.pluginId).toBe("jira");
});
});