skill.test.ts 3.23 KB
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { getSkillList, getSkillDetail } from './skill';
import { apiClient, ApiError } from './client';

vi.mock('./client', () => ({
  apiClient: {
    get: vi.fn(),
    post: vi.fn(),
    put: vi.fn(),
    delete: vi.fn(),
  },
  ApiError: class ApiError extends Error {
    constructor(message: string, public code: number) {
      super(message);
      this.name = 'ApiError';
    }
  },
}));

describe('skill API', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  describe('getSkillList', () => {
    it('调用正确的接口路径', async () => {
      vi.mocked(apiClient.get).mockResolvedValueOnce({
        list: [],
      });

      await getSkillList();
      expect(apiClient.get).toHaveBeenCalledWith('/skills');
    });

    it('提交关键词时透传 keyword 查询参数', async () => {
      vi.mocked(apiClient.get).mockResolvedValueOnce({
        list: [],
      });

      await getSkillList('browser');

      expect(apiClient.get).toHaveBeenCalledWith('/skills', {
        params: { keyword: 'browser' },
      });
    });

    it('返回 Skill 列表', async () => {
      const mockResponse = {
        list: [
          { skill_code: 'query-metric', skill_name: '指标查询', description: '查询业务指标数据' },
          { skill_code: 'query-detail', skill_name: '明细查询', description: '查询业务明细数据' },
        ],
      };
      vi.mocked(apiClient.get).mockResolvedValueOnce(mockResponse);

      const result = await getSkillList();
      expect(result.list).toHaveLength(2);
      expect(result.list[0].skill_code).toBe('query-metric');
      expect(result.list[0].skill_name).toBe('指标查询');
    });

    it('抛出错误时传播', async () => {
      vi.mocked(apiClient.get).mockRejectedValueOnce(new ApiError('网络错误', 0));

      await expect(getSkillList()).rejects.toThrow('网络错误');
    });
  });

  describe('getSkillDetail', () => {
    it('调用正确的接口路径', async () => {
      vi.mocked(apiClient.get).mockResolvedValueOnce({
        skill_code: 'query-metric',
        skill_name: '指标查询',
        description: '查询业务指标数据',
        doc_markdown: '# 指标查询 Skill',
      });

      await getSkillDetail('query-metric');
      expect(apiClient.get).toHaveBeenCalledWith('/skills/query-metric');
    });

    it('返回 Skill 详情', async () => {
      const mockResponse = {
        skill_code: 'query-metric',
        skill_name: '指标查询',
        description: '查询业务指标数据',
        doc_markdown: '# 指标查询 Skill\n\n## 使用方法\n...',
      };
      vi.mocked(apiClient.get).mockResolvedValueOnce(mockResponse);

      const result = await getSkillDetail('query-metric');
      expect(result.skill_code).toBe('query-metric');
      expect(result.skill_name).toBe('指标查询');
      expect(result.doc_markdown).toContain('# 指标查询 Skill');
    });

    it('不存在时抛出错误', async () => {
      vi.mocked(apiClient.get).mockRejectedValueOnce(new ApiError('Skill 不存在', 404));

      await expect(getSkillDetail('non-existent')).rejects.toThrow('Skill 不存在');
    });
  });
});