import { describe, expect, it } from "vitest"; import type { InstalledPlugin, PluginConnectionProviderContribution, PluginFormFieldValue } from "@/types/database"; import { buildPluginConnectionConfig, createFrontendPluginRegistry, initialPluginFormValues, parsePluginConnectionProviderOptionValue, pluginConnectionActionsForDialog, pluginConnectionConnectTimeoutDefault, pluginConnectionFormValues, pluginConnectionProviderIcon, pluginConnectionProviderOptionValue, } from "./frontendPlugin"; function installedPlugin(id: string, contributions: InstalledPlugin["manifest"]["contributions"] = []): InstalledPlugin { return { compatibility: { compatible: true }, manifest: { id, name: id, version: "1.0.0", drivers: [], contributions, }, }; } function connectionProvider(overrides: Partial = {}): PluginConnectionProviderContribution { return { type: "connection-provider", id: "connection", label: "Example", database_type: "example", fields: [ { key: "host", label: "Host", type: "text", required: true }, { key: "port", label: "Port", type: "number", default: 1234 }, { key: "tls", label: "TLS", type: "boolean", default: false }, ], ...overrides, }; } describe("FrontendPluginRegistry", () => { it("migrates a config-bound secret on edit, preserves multiline values and removes the plaintext copy", () => { const provider = connectionProvider({ fields: [{ key: "key", label: "Private key", type: "textarea", binding: "secret" }] }); const pem = "-----BEGIN PRIVATE KEY-----\nexample\n-----END PRIVATE KEY-----\n"; const existing = buildPluginConnectionConfig("example.plugin", connectionProvider({ fields: [] }), {}); existing.external_config = { key: pem, root: "/files" }; const values = pluginConnectionFormValues(provider, existing); expect(values.key).toBe(pem); const saved = buildPluginConnectionConfig("example.plugin", provider, values, existing); expect(saved.connection_secrets?.key).toBe(pem); expect(saved.external_config).toEqual({ root: "/files" }); expect(existing.external_config).toEqual({ key: pem, root: "/files" }); expect(pluginConnectionFormValues(provider, saved).key).toBe(pem); existing.connection_secrets = { key: "newer secret" }; expect(pluginConnectionFormValues(provider, existing).key).toBe("newer secret"); const cleared = buildPluginConnectionConfig("example.plugin", provider, { key: "" }, existing); expect(cleared.connection_secrets?.key).toBeUndefined(); expect(cleared.external_config).toEqual({ root: "/files" }); }); it("round-trips automatic ports as an empty input and preserves explicit custom ports", () => { const provider = connectionProvider({ fields: [{ key: "port", label: "Port", type: "number", binding: "port" }] }); const automatic = buildPluginConnectionConfig("example.plugin", provider, {}); expect(automatic.port).toBe(0); expect(pluginConnectionFormValues(provider, automatic).port).toBeUndefined(); const explicit = buildPluginConnectionConfig("example.plugin", provider, { port: 1636 }); expect(pluginConnectionFormValues(provider, explicit).port).toBe(1636); }); it("preserves the save-password preference for plugin connections", () => { const provider = connectionProvider({ fields: [] }); const defaultConfig = buildPluginConnectionConfig("example.plugin", provider, {}); expect(defaultConfig.save_password).toBe(true); const transient = buildPluginConnectionConfig("example.plugin", provider, {}, { ...defaultConfig, save_password: false }); expect(transient.save_password).toBe(false); }); it("materializes the provider's connect_timeout_secs default into the typed timeout", () => { const provider = connectionProvider({ fields: [{ key: "connect_timeout_secs", label: "Connect timeout", type: "number", default: 30 }], }); expect(pluginConnectionConnectTimeoutDefault(provider)).toBe(30); expect(buildPluginConnectionConfig("example.plugin", provider, {}).connect_timeout_secs).toBe(30); // An advanced-form value the user tuned is mirrored into the typed field too. expect(buildPluginConnectionConfig("example.plugin", provider, { connect_timeout_secs: 60 }).connect_timeout_secs).toBe(60); // Once the provider declares the field, its resolved value is the single // source of truth: a stale typed value from before the declaration is // healed to the declared default. const existing = buildPluginConnectionConfig("example.plugin", connectionProvider({ fields: [] }), {}); existing.connect_timeout_secs = 7; expect(buildPluginConnectionConfig("example.plugin", provider, {}, existing).connect_timeout_secs).toBe(30); // Providers without the well-known field keep the generic 10s default. const generic = connectionProvider({ fields: [] }); expect(pluginConnectionConnectTimeoutDefault(generic)).toBeUndefined(); expect(buildPluginConnectionConfig("example.plugin", generic, {}).connect_timeout_secs).toBe(10); }); it("round-trips plugin connection provider picker values", () => { const value = pluginConnectionProviderOptionValue("example/plugin", "ssh:main"); expect(parsePluginConnectionProviderOptionValue(value)).toEqual({ pluginId: "example/plugin", providerId: "ssh:main" }); expect(parsePluginConnectionProviderOptionValue("mysql")).toBeNull(); }); it("indexes declarative connection providers", () => { const registry = createFrontendPluginRegistry([installedPlugin("com.example.plugin", [connectionProvider()])]); expect(registry.listConnectionProviders()).toHaveLength(1); expect(registry.listConnectionProviders()[0]?.contribution.database_type).toBe("example"); }); it("indexes workbench, connection provider, and filesystem contributions", () => { const registry = createFrontendPluginRegistry([ installedPlugin("com.example.plugin", [ { type: "connection-provider", id: "example.connection", label: "Example", database_type: "example", fields: [], workbench: "example.main", filesystem_provider: "example.files", }, { type: "workbench", id: "example.main", label: "Example Workbench" }, { type: "filesystem-provider", id: "example.files", label: "Example Files", schemes: ["example"], root_uri: "example:/home", capabilities: ["read", "write"] }, ]), ]); expect(registry.listConnectionProviders()).toHaveLength(1); expect(registry.listWorkbenches()[0]?.contribution.id).toBe("example.main"); expect(registry.listFilesystemProviders()[0]?.contribution.schemes).toEqual(["example"]); expect(registry.listFilesystemProviders()[0]?.contribution.root_uri).toBe("example:/home"); expect(registry.listConnectionProviders()[0]?.contribution.filesystem_provider).toBe("example.files"); }); it("indexes result-view contributions", () => { const registry = createFrontendPluginRegistry([installedPlugin("com.example.plugin", [{ type: "result-view", id: "example.graph", label: "Graph" }])]); const views = registry.listResultViews(); expect(views).toHaveLength(1); expect(views[0]?.contribution.id).toBe("example.graph"); expect(views[0]?.contribution.label).toBe("Graph"); }); it("resolves the plugin UI contribution behind a workbench or result-view tab", () => { const registry = createFrontendPluginRegistry([ installedPlugin("com.example.plugin", [ { type: "workbench", id: "example.main", label: "Example Workbench", icon: "assets/main.svg" }, { type: "result-view", id: "example.graph", label: "Graph", icon: "assets/graph.svg" }, ]), ]); const workbench = registry.findUiContribution("com.example.plugin", "example.main"); expect(workbench?.contribution).toMatchObject({ type: "workbench", id: "example.main", label: "Example Workbench", icon: "assets/main.svg" }); // The result-view keeps its own id and display metadata: the plugin UI is // told which declared contribution the user opened, and it is not a workbench. const resultView = registry.findUiContribution("com.example.plugin", "example.graph"); expect(resultView?.contribution).toMatchObject({ type: "result-view", id: "example.graph", label: "Graph", icon: "assets/graph.svg" }); }); it("keeps workbench lookups scoped to workbench contributions", () => { const registry = createFrontendPluginRegistry([ installedPlugin("com.example.plugin", [ { type: "workbench", id: "example.main", label: "Example Workbench" }, { type: "result-view", id: "example.graph", label: "Graph" }, ]), ]); // `findWorkbench` answers `host.openWorkbench` and `connection-provider.workbench`: // a result-view id must never satisfy it. expect(registry.findWorkbench("com.example.plugin", "example.graph")).toBeUndefined(); expect(registry.findWorkbench("com.example.plugin", "example.main")?.contribution.id).toBe("example.main"); }); it("does not resolve unknown, foreign, or non-UI contributions as plugin UI", () => { const registry = createFrontendPluginRegistry([ installedPlugin("com.example.plugin", [ { type: "result-view", id: "example.graph", label: "Graph" }, { type: "context-menu", id: "example.inspect", label: "Inspect", menu: "connection" }, { type: "filesystem-provider", id: "example.files", label: "Files", schemes: ["example"] }, ]), installedPlugin("com.example.other", []), ]); expect(registry.findUiContribution("com.example.plugin", "example.graph")?.contribution.type).toBe("result-view"); expect(registry.findUiContribution("com.example.plugin", "example.missing")).toBeUndefined(); expect(registry.findUiContribution("com.example.other", "example.graph")).toBeUndefined(); // Context menus render natively and filesystem providers own their tab mode. expect(registry.findUiContribution("com.example.plugin", "example.inspect")).toBeUndefined(); expect(registry.findUiContribution("com.example.plugin", "example.files")).toBeUndefined(); }); it("does not resolve plugin UI contributions of incompatible plugins", () => { const plugin = installedPlugin("com.example.plugin", [ { type: "workbench", id: "example.main", label: "Example Workbench" }, { type: "result-view", id: "example.graph", label: "Graph" }, ]); plugin.compatibility = { compatible: false, errors: ["Unsupported host API"] }; const registry = createFrontendPluginRegistry([plugin]); expect(registry.findUiContribution("com.example.plugin", "example.main")).toBeUndefined(); expect(registry.findUiContribution("com.example.plugin", "example.graph")).toBeUndefined(); }); it("keeps declarative context-menu actions as context-menu metadata, not commands", () => { const registry = createFrontendPluginRegistry([ installedPlugin("com.example.plugin", [ { type: "workbench", id: "example.main", label: "Example Workbench" }, { type: "context-menu", id: "example.open", label: "Open Example", menu: "connection", action: { type: "open-workbench", workbench: "example.main" }, }, ]), ]); const item = registry.listContextMenuItems("connection")[0]; expect(item?.contribution.action).toEqual({ type: "open-workbench", workbench: "example.main" }); expect(registry.findWorkbench("com.example.plugin", "example.main")?.contribution.id).toBe("example.main"); expect(registry.findCommand("com.example.plugin", "example.open")).toBeUndefined(); expect(registry.listCommands()).toHaveLength(0); }); it("indexes context-menu contributions per menu surface", () => { const registry = createFrontendPluginRegistry([ installedPlugin("com.example.plugin", [ { type: "context-menu", id: "example.inspect", label: "Inspect endpoint", menu: "connection" }, { type: "context-menu", id: "example.inspect-table", label: "Inspect table", menu: "table" }, ]), ]); const connectionItems = registry.listContextMenuItems("connection"); expect(connectionItems).toHaveLength(1); expect(connectionItems[0]?.contribution.id).toBe("example.inspect"); expect(connectionItems[0]?.contribution.action).toBeUndefined(); expect(connectionItems[0]?.plugin.manifest.id).toBe("com.example.plugin"); const tableItems = registry.listContextMenuItems("table"); expect(tableItems).toHaveLength(1); expect(tableItems[0]?.contribution.id).toBe("example.inspect-table"); const emptyRegistry = createFrontendPluginRegistry([installedPlugin("com.example.empty")]); expect(emptyRegistry.listContextMenuItems("table")).toHaveLength(0); expect(registry.listContextMenuItems("missing")).toHaveLength(0); }); it("prefers provider display metadata and falls back to plugin metadata", () => { const explicit = installedPlugin("com.example.explicit", [ { type: "connection-provider", id: "explicit.connection", label: "Explicit connection", icon: "assets/provider.svg", database_type: "explicit", fields: [], }, ]); explicit.manifest.name = "Explicit plugin"; explicit.manifest.icon = "assets/plugin.svg"; const fallback = installedPlugin("com.example.fallback", [ { type: "connection-provider", id: "fallback.connection", label: "", database_type: "fallback", fields: [], }, ]); fallback.manifest.name = "Fallback plugin"; fallback.manifest.icon = "assets/fallback.svg"; const registry = createFrontendPluginRegistry([explicit, fallback]); const [explicitEntry, fallbackEntry] = registry.listConnectionProviders(); expect(explicitEntry?.contribution.label).toBe("Explicit connection"); expect(pluginConnectionProviderIcon(explicitEntry!)).toBe("assets/provider.svg"); expect(fallbackEntry?.contribution.label).toBe("Fallback plugin"); expect(pluginConnectionProviderIcon(fallbackEntry!)).toBe("assets/fallback.svg"); }); it("falls back to provider id and the generic icon when metadata is absent", () => { const plugin = installedPlugin("com.example.minimal", [ { type: "connection-provider", id: "minimal.connection", label: "", database_type: "minimal", fields: [], }, ]); plugin.manifest.name = ""; const entry = createFrontendPluginRegistry([plugin]).listConnectionProviders()[0]!; expect(entry.contribution.label).toBe("minimal.connection"); expect(pluginConnectionProviderIcon(entry)).toBeUndefined(); }); it("drops unsafe icon paths before rendering", () => { const plugin = installedPlugin("com.example.unsafe", [ { type: "connection-provider", id: "unsafe.connection", label: "Unsafe", icon: "../outside.svg", database_type: "unsafe", fields: [], }, // Every contribution the host renders through the plugin UI entrypoint // resolves its icon asset path the same way. { type: "workbench", id: "unsafe.main", label: "Unsafe workbench", icon: "../outside.svg" }, { type: "result-view", id: "unsafe.graph", label: "Unsafe graph", icon: "/outside.svg" }, ]); plugin.manifest.icon = "\\outside.svg"; const definition = createFrontendPluginRegistry([plugin]).listPlugins()[0]!; const entry = createFrontendPluginRegistry([plugin]).listConnectionProviders()[0]!; expect(definition.plugin.manifest.icon).toBeUndefined(); expect(pluginConnectionProviderIcon(entry)).toBeUndefined(); expect(createFrontendPluginRegistry([plugin]).listWorkbenches()[0]?.contribution.icon).toBeUndefined(); expect(createFrontendPluginRegistry([plugin]).listResultViews()[0]?.contribution.icon).toBeUndefined(); }); it("localizes plugin metadata, contributions, and form fields", () => { const plugin = installedPlugin("com.example.plugin", [connectionProvider({ id: "example.connection", description: "English description" })]); plugin.manifest.localizations = { "zh-CN": { name: "示例插件", description: "插件说明", contributions: { "example.connection": { label: "示例连接", description: "连接说明", fields: { host: { label: "主机", placeholder: "请输入主机" }, }, }, }, }, }; const registry = createFrontendPluginRegistry([plugin], "zh-CN"); const definition = registry.listPlugins()[0]!; const contribution = registry.listConnectionProviders()[0]!.contribution; expect(definition.plugin.manifest.name).toBe("示例插件"); expect(definition.plugin.manifest.description).toBe("插件说明"); expect(contribution.label).toBe("示例连接"); expect(contribution.description).toBe("连接说明"); expect(contribution.fields[0]).toMatchObject({ label: "主机", placeholder: "请输入主机" }); expect(plugin.manifest.name).toBe("com.example.plugin"); }); it("normalizes connection actions in manifest order and localizes their labels", () => { const plugin = installedPlugin("com.example.actions", [ { type: "connection-provider", id: "example.connection", label: "Example", database_type: "example", fields: [], capabilities: ["test"], actions: [ { id: "discover", label: "Discover", description: "Discover an endpoint", variant: "outline", requires_valid_form: false, }, { id: "edit", label: "Edit", variant: "secondary", when: "edit" }, ], }, ]); plugin.manifest.localizations = { "zh-CN": { contributions: { "example.connection": { actions: { discover: { label: "发现地址", description: "自动发现连接地址" }, }, }, }, }, }; const actions = createFrontendPluginRegistry([plugin], "zh-CN").listConnectionProviders()[0]?.contribution.actions; expect(actions?.map((action) => action.id)).toEqual(["discover", "edit"]); expect(actions?.[0]).toMatchObject({ label: "发现地址", description: "自动发现连接地址", requires_valid_form: false, }); expect(actions?.[1]).toMatchObject({ label: "Edit", variant: "secondary", when: "edit" }); }); it("treats omitted and explicitly empty custom action lists equivalently", () => { const registry = createFrontendPluginRegistry([ installedPlugin("com.example.defaults", [{ type: "connection-provider", id: "default.connection", label: "Default", database_type: "default", fields: [] }]), installedPlugin("com.example.empty", [{ type: "connection-provider", id: "empty.connection", label: "Empty", database_type: "empty", fields: [], actions: [] }]), ]); const [defaults, empty] = registry.listConnectionProviders(); expect(defaults?.contribution.actions).toBeUndefined(); expect(empty?.contribution.actions).toEqual([]); expect(pluginConnectionActionsForDialog(defaults!.contribution, false)).toEqual(pluginConnectionActionsForDialog(empty!.contribution, false)); }); it("builds compatible default footer actions and filters create/edit actions", () => { const provider: PluginConnectionProviderContribution = { type: "connection-provider", id: "example.connection", label: "Example", database_type: "example", fields: [], capabilities: ["test"], }; expect(pluginConnectionActionsForDialog(provider, false).map((action) => action.kind)).toEqual(["test", "save-and-connect"]); expect(pluginConnectionActionsForDialog(provider, true).map((action) => action.kind)).toEqual(["test", "save"]); expect( pluginConnectionActionsForDialog( { ...provider, actions: [ { id: "create", label: "Create", when: "create" }, { id: "edit", label: "Edit", when: "edit" }, { id: "always", label: "Always", when: "always" }, ], }, true, ).map((action) => action.id), ).toEqual(["edit", "always", "test", "save"]); }); it("creates initial values only from declared defaults", () => { const values = initialPluginFormValues(connectionProvider()); expect(values).toEqual({ port: 1234, tls: false }); expect(values).not.toHaveProperty("host"); }); it("treats a host-serialized null default as unset instead of a value", () => { // Hosts before the manifest serialization fix sent `"default": null` for // every field that declares no default, so an untouched password field // turned into the four-character string "null" once it was saved. const provider = connectionProvider({ fields: [ { key: "sudo_password", label: "Sudo password", type: "password", binding: "secret", default: null }, { key: "mode", label: "Mode", type: "text", binding: "config", default: null }, { key: "read_only", label: "Read only", type: "boolean", binding: "config", default: false }, ], }); expect(initialPluginFormValues(provider)).toEqual({ read_only: false }); const config = buildPluginConnectionConfig("io.dbx.ssh", provider, { sudo_password: null, mode: null, read_only: false, } as unknown as Record); expect(config.connection_secrets?.sudo_password).toBeUndefined(); expect(config.external_config).toEqual({ read_only: false }); }); it("preserves opaque 'null' credentials when reopening and saving connections", () => { const provider = connectionProvider({ fields: [ { key: "sudo_password", label: "Sudo password", type: "password", binding: "secret" }, { key: "totp_secret", label: "TOTP secret", type: "textarea", binding: "secret" }, { key: "sudo_source", label: "Sudo source", type: "text", binding: "config" }, ], }); const existing = buildPluginConnectionConfig("io.dbx.ssh", provider, {}); existing.connection_secrets = { sudo_password: "null", totp_secret: "JBSWY3DPEHPK3PXP" }; existing.external_config = { sudo_source: "custom", stale: null }; const values = pluginConnectionFormValues(provider, existing); expect(values.sudo_password).toBe("null"); expect(values.totp_secret).toBe("JBSWY3DPEHPK3PXP"); expect(values.stale).toBeUndefined(); expect(values.sudo_source).toBe("custom"); const saved = buildPluginConnectionConfig("io.dbx.ssh", provider, values, existing); expect(saved.connection_secrets?.sudo_password).toBe("null"); expect(saved.connection_secrets?.totp_secret).toBe("JBSWY3DPEHPK3PXP"); }); it.each([undefined, "secret", "password"] as const)("round-trips explicit 'null' credentials with binding %s", (binding) => { const provider = connectionProvider({ fields: [{ key: "credential", label: "Credential", type: "password", binding }] }); const saved = buildPluginConnectionConfig("example.plugin", provider, { credential: "null" }); expect(binding === "password" ? saved.password : saved.connection_secrets?.credential).toBe("null"); expect(pluginConnectionFormValues(provider, saved)).toEqual({ credential: "null" }); const reopened = buildPluginConnectionConfig("example.plugin", provider, pluginConnectionFormValues(provider, saved), saved); expect(pluginConnectionFormValues(provider, reopened)).toEqual({ credential: "null" }); }); it("migrates an opaque 'null' credential from config to secret storage without losing it", () => { const provider = connectionProvider({ fields: [{ key: "credential", label: "Credential", type: "password" }] }); const existing = buildPluginConnectionConfig("example.plugin", provider, {}); existing.external_config = { credential: "null" }; const values = pluginConnectionFormValues(provider, existing); expect(values).toEqual({ credential: "null" }); const saved = buildPluginConnectionConfig("example.plugin", provider, values, existing); expect(saved.connection_secrets).toEqual({ credential: "null" }); expect(saved.external_config).toEqual({}); }); it.each([null, undefined, ""])("keeps an unset credential %s absent without a default", (credential) => { const provider = connectionProvider({ fields: [{ key: "credential", label: "Credential", type: "password" }] }); const existing = buildPluginConnectionConfig("example.plugin", provider, {}); existing.connection_secrets = { credential: "old-secret" }; const saved = buildPluginConnectionConfig("example.plugin", provider, { credential } as unknown as Record, existing); expect(saved.connection_secrets).toEqual({}); expect(pluginConnectionFormValues(provider, saved)).toEqual({}); }); it("uses an explicit string default only for missing input, not a cleared input", () => { const provider = connectionProvider({ fields: [{ key: "credential", label: "Credential", type: "password", default: "null" }] }); expect(initialPluginFormValues(provider)).toEqual({ credential: "null" }); expect(buildPluginConnectionConfig("example.plugin", provider, {}).connection_secrets).toEqual({ credential: "null" }); expect(buildPluginConnectionConfig("example.plugin", provider, { credential: null } as unknown as Record).connection_secrets).toEqual({}); expect(buildPluginConnectionConfig("example.plugin", provider, { credential: "" }).connection_secrets).toEqual({}); }); it("maps provider fields into common, config, and secret storage", () => { const provider: PluginConnectionProviderContribution = { type: "connection-provider", id: "example.ssh", label: "SSH", database_type: "ssh", fields: [ { key: "display_name", label: "Name", type: "text", binding: "name" }, { key: "host", label: "Host", type: "text", binding: "host" }, { key: "port", label: "Port", type: "number", binding: "port", default: 22 }, { key: "private_key", label: "Private key", type: "password" }, { key: "keepalive", label: "Keepalive", type: "boolean", default: true }, ], }; const config = buildPluginConnectionConfig("example.plugin", provider, { display_name: "Production SSH", host: "example.com", port: 2222, private_key: "secret-key", keepalive: false, }); expect(config).toMatchObject({ name: "Production SSH", db_type: "plugin", host: "example.com", port: 2222, plugin_id: "example.plugin", plugin_connection_provider: "example.ssh", plugin_connection_type: "ssh", external_config: { keepalive: false }, connection_secrets: { private_key: "secret-key" }, }); expect(pluginConnectionFormValues(provider, config)).toMatchObject({ display_name: "Production SSH", host: "example.com", port: 2222, private_key: "secret-key", keepalive: false, }); }); it("builds from a reactive (non-structured-cloneable) existing config", () => { // Connection configs reach the builder through props as Vue reactive // proxies; structuredClone refuses them ("The object can not be cloned."). const provider = connectionProvider({ id: "example.ssh", database_type: "ssh", fields: [{ key: "authentication", label: "Authentication", type: "select", binding: "config", default: "password", options: [] }], }); const existing = { id: "ssh-1", external_config: new Proxy({ authentication: "private-key" }, {}), } as unknown as Parameters[3]; const config = buildPluginConnectionConfig("example.plugin", provider, { authentication: "private-key" }, existing); expect(config.external_config).toEqual({ authentication: "private-key" }); expect(config.id).toBe("ssh-1"); }); });