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

import {useAgentStudioModel, resolveCapabilityNodePosition} from './useAgentStudioModel';

describe('resolveCapabilityNodePosition', () => {
  it('无视口信息时沿用默认左侧列位置', () => {
    expect(
      resolveCapabilityNodePosition({
        fallbackIndex: 1,
        nodeId: 'knowledge:new',
      }),
    ).toEqual({ x: 70, y: 252 });
  });

  it('有视口信息时投放到当前可见区域左侧', () => {
    expect(
      resolveCapabilityNodePosition({
        canvasSize: { height: 720, width: 1280 },
        fallbackIndex: 0,
        layout: {
          nodePositions: {},
          viewport: { x: 250, y: 100, zoom: 1 },
        },
        nodeId: 'tool:new',
      }),
    ).toEqual({ x: -202, y: -4 });
  });

  it('新增位置与已有能力节点冲突时向下避让', () => {
    expect(
      resolveCapabilityNodePosition({
        canvasSize: { height: 720, width: 1280 },
        fallbackIndex: 0,
        layout: {
          nodePositions: {},
          viewport: { x: 250, y: 100, zoom: 1 },
        },
        nodeId: 'tool:new',
        occupiedPositions: [{ x: -202, y: -4 }],
      }),
    ).toEqual({ x: -202, y: 100 });
  });

  it('已有持久化位置时优先保留用户拖拽后的布局', () => {
    expect(
      resolveCapabilityNodePosition({
        canvasSize: { height: 720, width: 1280 },
        fallbackIndex: 0,
        layout: {
          nodePositions: {
            'tool:existing': { x: 128, y: 256 },
          },
          viewport: { x: 250, y: 100, zoom: 1 },
        },
        nodeId: 'tool:existing',
      }),
    ).toEqual({ x: 128, y: 256 });
  });
});

describe('useAgentStudioModel', () => {
  it('MCP 绑定缺少资源快照时从选项中回显节点信息', () => {
    const model = useAgentStudioModel(
      {
        agent: {
          name: '测试智能体',
        },
        dirty: false,
        knowledgeBindings: [],
        panelMode: 'capability',
        selectedNodeId: 'tool:mcp-1',
        toolBindings: [
          {
            localId: 'mcp-1',
            targetId: '1001',
            toolType: 'MCP',
          },
        ],
      },
      () => 'tool:mcp-1',
      undefined,
      undefined,
      undefined,
      () => ({
        mcp: [
          {
            label: 'context7',
            value: '1001',
            raw: {
              id: '1001',
              title: 'context7',
              tools: [{ name: 'resolve-library-id' }, { name: 'query-docs' }],
            },
          },
        ],
      }),
    );

    const mcpNode = model.value.nodes.find((node) => node.id === 'tool:mcp-1');
    expect(mcpNode?.data.title).toBe('context7 · 2 个工具');
    expect(mcpNode?.data.detail).toBe('context7 · 2 个工具');
  });
});
