import { beforeEach, describe, expect, it, vi } from "vitest";

// Mock the OAuth token lookup so the test never touches the DB/env.
vi.mock("../auth/slack-oauth.ts", () => ({
  getSlackMcpToken: vi.fn(),
}));

import { buildSlackIntegrationPrompt, buildSlackNativeMcpServers } from "./slack-native-mcp.ts";
import { getSlackMcpToken } from "../auth/slack-oauth.ts";

const mockedToken = getSlackMcpToken as unknown as ReturnType<typeof vi.fn>;

describe("buildSlackNativeMcpServers", () => {
  beforeEach(() => mockedToken.mockReset());

  it("returns no servers when no token is connected", async () => {
    mockedToken.mockResolvedValue(null);
    const servers = await buildSlackNativeMcpServers("local");
    expect(servers).toEqual({});
  });

  it("registers an in-process proxy server (not a raw remote http) when a token exists", async () => {
    mockedToken.mockResolvedValue("xoxp-test-123");
    const servers = await buildSlackNativeMcpServers("local");
    expect(Object.keys(servers)).toEqual(["slack"]);
    // The proxy avoids the remote-connect race: it's an in-process `sdk` server,
    // not a `{ type: "http", url: mcp.slack.com }` config. Building it does NOT
    // open a network connection (that happens lazily on the first tool call).
    const cfg = servers.slack as { type?: string; name?: string; instance?: unknown };
    expect(cfg.type).toBe("sdk");
    expect(cfg.name).toBe("slack");
    expect(cfg.instance).toBeDefined();
  });
});

describe("buildSlackIntegrationPrompt", () => {
  it("asserts active access + the mcp__slack__ tools when connected", () => {
    const p = buildSlackIntegrationPrompt(true);
    expect(p).toContain("mcp__slack__");
    expect(p.toLowerCase()).toContain("active");
  });

  it("says Slack is not connected otherwise", () => {
    expect(buildSlackIntegrationPrompt(false).toLowerCase()).toContain("not connected");
  });
});
