Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

6.1 KB
Raw Blame
import { describe, expect, it, vi } from "vitest";
import { buildPluginAiRequest, createPluginAiConversation, pluginComposerConnectionLabel, pluginContextConnectionId, pluginContextFromMessages, streamPluginAiConversation } from "../aiPluginConversation";
import type { AiConfig } from "@/types/ai";
import type { AgentEvent } from "@/lib/backend/tauri";

const identity = { id: "market", name: "Market Watch" };
const input = () => ({ title: "AAPL", prompt: "Analyse risks", context: { quotes: [{ symbol: "AAPL", price: 100, currency: "USD" }], snapshotId: "snap-1" } });

describe("plugin conversations in DBX AI", () => {
  it("keeps the composer label on the connection instead of the recommendation title", () => {
    const context = createPluginAiConversation(identity, input()).context;
    expect(pluginComposerConnectionLabel(context, "orb", "Saved connection")).toBe("orb");
    expect(pluginComposerConnectionLabel(context, undefined, "Saved connection")).toBe("Saved connection");
    expect(pluginComposerConnectionLabel(context)).toBe("Market Watch");
  });

  it("reads an optional connection id from the plugin snapshot without requiring it", () => {
    const context = createPluginAiConversation(identity, { ...input(), context: { connectionId: "conn-orb" } }).context;
    expect(pluginContextConnectionId(context)).toBe("conn-orb");
    expect(pluginContextConnectionId(createPluginAiConversation(identity, input()).context)).toBeUndefined();
  });

  it("binds the host identity and freezes a data snapshot without auto-sending by default", () => {
    const request = input();
    const opened = createPluginAiConversation(identity, { ...request, pluginId: "other-plugin", pluginName: "spoof" });
    request.context.quotes[0].price = 999;
    expect(opened.send).toBe(false);
    expect(opened.context).toMatchObject({ pluginId: "market", pluginName: "Market Watch", data: { quotes: [{ price: 100 }] } });
    expect(opened).not.toHaveProperty("apiKey");
  });

  it("keeps legacy opens in snapshot Ask mode and accepts explicit Agent mode", () => {
    const legacy = createPluginAiConversation(identity, input());
    expect(legacy.mode).toBeUndefined();
    expect(legacy.context.mode).toBeUndefined();
    const live = createPluginAiConversation(identity, { ...input(), mode: "agent" });
    expect(live.mode).toBe("agent");
    expect(live.context.mode).toBe("agent");
    expect(() => createPluginAiConversation(identity, { ...input(), mode: "invalid" })).toThrow();
  });

  it.each([{ title: "" }, { prompt: "" }, { context: [] }, { send: "true" }, { context: { callback: () => {} } }, { context: { big: "x".repeat(2 * 1024 * 1024) } }])("rejects malformed or oversized requests", (override) => {
    expect(() => createPluginAiConversation(identity, { ...input(), ...override })).toThrow();
  });

  it("retains the snapshot in restored messages and passes it to follow-ups with the transcript", () => {
    const opened = createPluginAiConversation(identity, input());
    const transcript = JSON.parse(
      JSON.stringify([
        { role: "user", content: "Analyse risks", pluginContext: opened.context },
        { role: "assistant", content: "The supplied price is 100 USD." },
        { role: "user", content: "What is missing?" },
      ]),
    );
    const context = pluginContextFromMessages(transcript)!;
    const request = buildPluginAiRequest(
      { model: "test-model" } as AiConfig,
      context,
      transcript.map(({ role, content }: { role: "user" | "assistant"; content: string }) => ({ role, content })),
    );
    expect(request.messages[0].content).toContain('"price": 100');
    expect(request.messages[0].content).toContain("snap-1");
    expect(request.messages.slice(1).map((message) => message.content)).toEqual(transcript.map((message: { content: string }) => message.content));
    expect(request.taskContract).toEqual({ action: "plugin", mode: "ask", userRequest: "What is missing?" });
    expect(request).not.toHaveProperty("connectionId");
    expect(request.systemPrompt).toContain("no database, filesystem or trading tools");
  });

  it("uses the AI panel event pipeline and waits for stream completion before reporting success", async () => {
    const events: AgentEvent[] = [];
    let complete!: () => void;
    const backendDone = new Promise<void>((resolve) => {
      complete = resolve;
    });
    const request = buildPluginAiRequest({ model: "test" } as AiConfig, createPluginAiConversation(identity, input()).context, []);
    const stream = vi.fn(async (sessionId, _request, onChunk) => {
      onChunk({ session_id: sessionId, reasoning_delta: "checking", delta: "", done: false });
      onChunk({ session_id: sessionId, delta: "reply", done: true });
      await backendDone;
    });
    const pending = streamPluginAiConversation(stream, "owned-session", request, (event) => events.push(event));
    expect(events).toEqual([
      { type: "reasoning_delta", delta: "checking" },
      { type: "text_delta", delta: "reply" },
    ]);
    complete();
    await pending;
    expect(events[2]).toEqual({ type: "agent_end" });
    expect(stream).toHaveBeenCalledWith("owned-session", request, expect.any(Function));
  });

  it("rejects a disconnected stream without a terminal event", async () => {
    const onEvent = vi.fn();
    const request = buildPluginAiRequest({} as AiConfig, createPluginAiConversation(identity, input()).context, []);
    await expect(
      streamPluginAiConversation(
        async (_id, _request, onChunk) => {
          onChunk({ session_id: "s", delta: "partial", done: false });
        },
        "s",
        request,
        onEvent,
      ),
    ).rejects.toThrow("AI stream ended before completion");
    expect(onEvent).toHaveBeenCalledExactlyOnceWith({ type: "text_delta", delta: "partial" });
  });

  it("does not turn a failed stream into a successful assistant message", async () => {
    const onEvent = vi.fn();
    const request = buildPluginAiRequest({} as AiConfig, createPluginAiConversation(identity, input()).context, []);
    await expect(streamPluginAiConversation(vi.fn().mockRejectedValue(new Error("provider unavailable")), "s", request, onEvent)).rejects.toThrow("provider unavailable");
    expect(onEvent).not.toHaveBeenCalled();
  });
});