import { beforeEach, describe, expect, it, vi } from "vitest";
import { SystemRole } from "@capakraken/shared";
import {
  createToolContext,
  executeTool,
  resetAssistantImportToolTestState,
} from "./assistant-tools-import-dispo-webhooks-test-helpers.js";

describe("assistant webhook tools - read", () => {
  beforeEach(async () => {
    await resetAssistantImportToolTestState();
  });

  it("masks webhook secrets in assistant responses", async () => {
    const ctx = createToolContext(
      {
        webhook: {
          findMany: vi.fn().mockResolvedValue([
            {
              id: "wh_1",
              name: "Primary",
              url: "https://example.com/hook",
              secret: "super-secret",
              events: ["project.updated"],
              isActive: true,
              createdAt: new Date("2026-03-29T00:00:00.000Z"),
            },
          ]),
          findUnique: vi.fn().mockResolvedValue({
            id: "wh_1",
            name: "Primary",
            url: "https://example.com/hook",
            secret: "super-secret",
            events: ["project.updated"],
            isActive: true,
            createdAt: new Date("2026-03-29T00:00:00.000Z"),
          }),
        },
      },
      { userRole: SystemRole.ADMIN },
    );

    const listResult = await executeTool("list_webhooks", "{}", ctx);
    const getResult = await executeTool("get_webhook", JSON.stringify({ id: "wh_1" }), ctx);

    expect(JSON.parse(listResult.content)).toEqual([
      expect.objectContaining({
        id: "wh_1",
        hasSecret: true,
      }),
    ]);
    expect(JSON.parse(listResult.content)[0]).not.toHaveProperty("secret");
    expect(JSON.parse(getResult.content)).toEqual(
      expect.objectContaining({
        id: "wh_1",
        hasSecret: true,
      }),
    );
    expect(JSON.parse(getResult.content)).not.toHaveProperty("secret");
  });
});
