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

import {
  getSkillMcpTools,
  getSkillToolOptions,
  updateSkillToolBindings,
} from './api';

const requestMocks = vi.hoisted(() => ({
  get: vi.fn(),
  post: vi.fn(),
}));

vi.mock('#/api/request', () => ({ api: requestMocks }));

describe('skill tool binding api', () => {
  beforeEach(() => vi.clearAllMocks());

  it('uses the safe candidate and MCP manifest endpoints', () => {
    getSkillToolOptions({
      keyword: '合同',
      pageNum: 1,
      pageSize: 100,
      toolType: 'WORKFLOW',
    });
    getSkillMcpTools(20);

    expect(requestMocks.get).toHaveBeenNthCalledWith(
      1,
      '/api/v1/skill/toolOptions',
      {
        params: {
          keyword: '合同',
          pageNum: 1,
          pageSize: 100,
          toolType: 'WORKFLOW',
        },
      },
    );
    expect(requestMocks.get).toHaveBeenNthCalledWith(
      2,
      '/api/v1/skill/mcpTools',
      { params: { mcpId: 20 } },
    );
  });

  it('submits only the binding whitelist', () => {
    updateSkillToolBindings(101, [
      {
        hitlEnabled: true,
        mcpToolCount: 2,
        mcpToolManifestHash: 'manifest-v2',
        resourceSummary: {
          available: true,
          description: '不应回传',
          title: '企业 MCP',
          toolCount: 2,
        },
        sortNo: 9,
        targetId: 20,
        toolType: 'MCP',
      },
    ]);

    expect(requestMocks.post).toHaveBeenCalledWith(
      '/api/v1/skill/toolBinding/update',
      {
        bindings: [
          {
            hitlEnabled: true,
            mcpToolManifestHash: 'manifest-v2',
            sortNo: 0,
            targetId: 20,
            toolType: 'MCP',
          },
        ],
        skillId: 101,
      },
    );
  });
});
