import { describe, it, expect, beforeEach, vi } from 'vitest';
import { LSPClientManager } from '../../../src/lsp/client.js';

// Mock child_process
vi.mock('child_process', () => ({
  spawn: vi.fn(() => ({
    stdin: { on: vi.fn() },
    stdout: { on: vi.fn() },
    stderr: { on: vi.fn() },
    on: vi.fn(),
    kill: vi.fn(),
  })),
  execSync: vi.fn(),
}));

// Mock vscode-jsonrpc
vi.mock('vscode-jsonrpc/node.js', () => ({
  createMessageConnection: vi.fn(() => ({
    listen: vi.fn(),
    sendRequest: vi.fn().mockResolvedValue({}),
    sendNotification: vi.fn(),
    onNotification: vi.fn(),
    dispose: vi.fn(),
  })),
  StreamMessageReader: vi.fn(),
  StreamMessageWriter: vi.fn(),
}));

// Mock fs/promises
vi.mock('fs/promises', () => ({
  readFile: vi.fn().mockResolvedValue('file content'),
}));

// Mock language module
vi.mock('../../../src/lsp/language.js', () => ({
  getLanguageId: vi.fn((path: string) => {
    if (path.endsWith('.ts')) return 'typescript';
    if (path.endsWith('.py')) return 'python';
    return undefined;
  }),
}));

// Mock server module
vi.mock('../../../src/lsp/server.js', () => ({
  getServerConfig: vi.fn((languageId: string) => {
    if (languageId === 'typescript') {
      return {
        command: 'typescript-language-server',
        args: ['--stdio'],
        env: {},
      };
    }
    return null;
  }),
}));

import { spawn, execSync } from 'child_process';
import { getLanguageId } from '../../../src/lsp/language.js';
import { getServerConfig } from '../../../src/lsp/server.js';

describe('LSPClientManager - LSP 客户端管理器', () => {
  let manager: LSPClientManager;

  beforeEach(() => {
    vi.clearAllMocks();
    manager = new LSPClientManager('/test/project');

    // 默认命令存在
    vi.mocked(execSync).mockReturnValue(Buffer.from(''));
  });

  describe('构造函数', () => {
    it('使用提供的根路径', () => {
      const m = new LSPClientManager('/custom/path');
      expect(m).toBeDefined();
    });

    it('默认使用 process.cwd()', () => {
      const m = new LSPClientManager();
      expect(m).toBeDefined();
    });
  });

  describe('setRootPath - 设置根路径', () => {
    it('更新根路径', () => {
      manager.setRootPath('/new/path');
      // 无直接验证方式，但不应报错
      expect(true).toBe(true);
    });
  });

  describe('getClient - 获取客户端', () => {
    it('无服务器配置返回 undefined', async () => {
      vi.mocked(getServerConfig).mockReturnValue(null);

      const client = await manager.getClient('unknown' as any);

      expect(client).toBeUndefined();
    });

    it('命令不存在时返回 undefined', async () => {
      vi.mocked(execSync).mockImplementation(() => {
        throw new Error('command not found');
      });

      const client = await manager.getClient('typescript');

      expect(client).toBeUndefined();
    });
  });

  describe('touchFile - 通知文件变更', () => {
    it('不支持的语言返回 false', async () => {
      vi.mocked(getLanguageId).mockReturnValue(undefined);

      const result = await manager.touchFile('/test/file.xyz');

      expect(result).toBe(false);
    });
  });

  describe('getDiagnostics - 获取诊断', () => {
    it('无客户端时返回空 Map', () => {
      const diagnostics = manager.getDiagnostics();

      expect(diagnostics).toBeInstanceOf(Map);
      expect(diagnostics.size).toBe(0);
    });

    it('可以按文件过滤', () => {
      const diagnostics = manager.getDiagnostics('/test/file.ts');

      expect(diagnostics).toBeInstanceOf(Map);
    });
  });

  describe('getFileDiagnostics - 获取单文件诊断', () => {
    it('无诊断时返回空数组', () => {
      const diagnostics = manager.getFileDiagnostics('/test/file.ts');

      expect(Array.isArray(diagnostics)).toBe(true);
      expect(diagnostics.length).toBe(0);
    });
  });

  describe('isServerRunning - 检查服务器状态', () => {
    it('未启动的服务器返回 false', () => {
      expect(manager.isServerRunning('typescript')).toBe(false);
      expect(manager.isServerRunning('python')).toBe(false);
    });
  });

  describe('getRunningServers - 获取运行中的服务器', () => {
    it('无服务器时返回空数组', () => {
      const servers = manager.getRunningServers();

      expect(Array.isArray(servers)).toBe(true);
      expect(servers.length).toBe(0);
    });
  });

  describe('shutdown - 关闭', () => {
    it('无客户端时正常关闭', async () => {
      await expect(manager.shutdown()).resolves.not.toThrow();
    });
  });

  describe('closeFile - 关闭文件', () => {
    it('不支持的语言静默返回', async () => {
      vi.mocked(getLanguageId).mockReturnValue(undefined);

      await expect(manager.closeFile('/test/file.xyz')).resolves.not.toThrow();
    });

    it('未打开的文件静默返回', async () => {
      vi.mocked(getLanguageId).mockReturnValue('typescript');

      await expect(manager.closeFile('/test/file.ts')).resolves.not.toThrow();
    });
  });
});

describe('FileDiagnostic 类型', () => {
  it('包含必要字段', () => {
    const diagnostic = {
      file: '/test/file.ts',
      line: 1,
      column: 1,
      severity: 'error' as const,
      message: 'Test error',
    };

    expect(diagnostic.file).toBeDefined();
    expect(diagnostic.line).toBeDefined();
    expect(diagnostic.column).toBeDefined();
    expect(diagnostic.severity).toBeDefined();
    expect(diagnostic.message).toBeDefined();
  });

  it('支持可选字段', () => {
    const diagnostic = {
      file: '/test/file.ts',
      line: 1,
      column: 1,
      endLine: 2,
      endColumn: 5,
      severity: 'warning' as const,
      message: 'Test warning',
      source: 'typescript',
      code: 'TS2345',
    };

    expect(diagnostic.endLine).toBe(2);
    expect(diagnostic.endColumn).toBe(5);
    expect(diagnostic.source).toBe('typescript');
    expect(diagnostic.code).toBe('TS2345');
  });
});
