import { describe, it, expect } from 'vitest';
import {
  getServerConfig,
  hasServerConfig,
  getSupportedLanguages,
  getAllServerConfigs,
  getUniqueServers,
} from '../../../src/lsp/server.js';

describe('LSP Server - 语言服务器配置', () => {
  describe('getServerConfig - 获取服务器配置', () => {
    it('返回 TypeScript 配置', () => {
      const config = getServerConfig('typescript');

      expect(config).toBeDefined();
      expect(config?.command).toBe('typescript-language-server');
      expect(config?.args).toContain('--stdio');
      expect(config?.displayName).toBe('TypeScript');
    });

    it('返回 JavaScript 配置（共用 TypeScript）', () => {
      const config = getServerConfig('javascript');

      expect(config).toBeDefined();
      expect(config?.command).toBe('typescript-language-server');
    });

    it('返回 Python 配置', () => {
      const config = getServerConfig('python');

      expect(config).toBeDefined();
      expect(config?.command).toBe('pyright-langserver');
      expect(config?.install.npm).toBe('pyright');
    });

    it('返回 Go 配置', () => {
      const config = getServerConfig('go');

      expect(config).toBeDefined();
      expect(config?.command).toBe('gopls');
      expect(config?.install.go).toContain('gopls');
    });

    it('返回 Rust 配置', () => {
      const config = getServerConfig('rust');

      expect(config).toBeDefined();
      expect(config?.command).toBe('rust-analyzer');
      expect(config?.install.rustup).toBe('rust-analyzer');
    });

    it('返回 C/C++ 配置', () => {
      const cConfig = getServerConfig('c');
      const cppConfig = getServerConfig('cpp');

      expect(cConfig?.command).toBe('clangd');
      expect(cppConfig?.command).toBe('clangd');
    });

    it('返回 Vue 配置', () => {
      const config = getServerConfig('vue');

      expect(config).toBeDefined();
      expect(config?.command).toBe('vue-language-server');
    });

    it('返回 HTML/CSS/JSON 配置', () => {
      expect(getServerConfig('html')?.command).toBe('vscode-html-language-server');
      expect(getServerConfig('css')?.command).toBe('vscode-css-language-server');
      expect(getServerConfig('json')?.command).toBe('vscode-json-language-server');
    });

    it('不支持的语言返回 undefined', () => {
      const config = getServerConfig('unknown' as any);

      expect(config).toBeUndefined();
    });
  });

  describe('hasServerConfig - 检查服务器配置', () => {
    it('已配置的语言返回 true', () => {
      expect(hasServerConfig('typescript')).toBe(true);
      expect(hasServerConfig('python')).toBe(true);
      expect(hasServerConfig('go')).toBe(true);
      expect(hasServerConfig('rust')).toBe(true);
    });

    it('未配置的语言返回 false', () => {
      expect(hasServerConfig('unknown' as any)).toBe(false);
    });
  });

  describe('getSupportedLanguages - 获取支持的语言', () => {
    it('返回所有支持的语言 ID', () => {
      const languages = getSupportedLanguages();

      expect(Array.isArray(languages)).toBe(true);
      expect(languages.length).toBeGreaterThan(0);
      expect(languages).toContain('typescript');
      expect(languages).toContain('javascript');
      expect(languages).toContain('python');
      expect(languages).toContain('go');
      expect(languages).toContain('rust');
    });
  });

  describe('getAllServerConfigs - 获取所有配置', () => {
    it('返回所有服务器配置对象', () => {
      const configs = getAllServerConfigs();

      expect(typeof configs).toBe('object');
      expect(configs.typescript).toBeDefined();
      expect(configs.python).toBeDefined();
    });
  });

  describe('getUniqueServers - 获取唯一服务器列表', () => {
    it('返回去重后的服务器列表', () => {
      const servers = getUniqueServers();

      expect(Array.isArray(servers)).toBe(true);

      // typescript-language-server 被多个语言共用
      const tsServer = servers.find((s) => s.id === 'typescript-language-server');
      expect(tsServer).toBeDefined();
      expect(tsServer?.languages).toContain('typescript');
      expect(tsServer?.languages).toContain('javascript');
    });

    it('每个服务器包含必要字段', () => {
      const servers = getUniqueServers();

      for (const server of servers) {
        expect(server.id).toBeDefined();
        expect(server.config).toBeDefined();
        expect(server.languages).toBeDefined();
        expect(Array.isArray(server.languages)).toBe(true);
        expect(server.languages.length).toBeGreaterThan(0);
      }
    });

    it('clangd 被 C 和 C++ 共用', () => {
      const servers = getUniqueServers();
      const clangd = servers.find((s) => s.id === 'clangd');

      expect(clangd).toBeDefined();
      expect(clangd?.languages).toContain('c');
      expect(clangd?.languages).toContain('cpp');
    });

    it('vscode-css-language-server 被 CSS/SCSS/Less 共用', () => {
      const servers = getUniqueServers();
      const cssServer = servers.find((s) => s.id === 'vscode-css-language-server');

      expect(cssServer).toBeDefined();
      expect(cssServer?.languages).toContain('css');
      expect(cssServer?.languages).toContain('scss');
      expect(cssServer?.languages).toContain('less');
    });
  });

  describe('安装配置', () => {
    it('TypeScript 服务器有 npm 安装配置', () => {
      const config = getServerConfig('typescript');

      expect(config?.install.npm).toContain('typescript-language-server');
    });

    it('Python 服务器有多种安装方式', () => {
      const config = getServerConfig('python');

      expect(config?.install.npm).toBe('pyright');
      expect(config?.install.pip).toBe('pyright');
    });

    it('Go 服务器有 go install 配置', () => {
      const config = getServerConfig('go');

      expect(config?.install.go).toContain('gopls');
    });

    it('Rust 服务器有 rustup 和 brew 安装配置', () => {
      const config = getServerConfig('rust');

      expect(config?.install.rustup).toBe('rust-analyzer');
      expect(config?.install.brew).toBe('rust-analyzer');
    });

    it('Ruby 服务器有 gem 安装配置', () => {
      const config = getServerConfig('ruby');

      expect(config?.install.gem).toBe('solargraph');
    });
  });
});
