feat(image): 新建 knowai-core:1.0.0 镜像并完成推送
Some checks reported errors
continuous-integration/drone/push Build was killed
Some checks reported errors
continuous-integration/drone/push Build was killed
- 搭建 api、auth、utils 等逻辑模块 - 通过 tsc、eslint、vitest 测试验证 BREAKING CHANGE: 新镜像分支
This commit is contained in:
239
test/unit/api/modules/model.test.ts
Normal file
239
test/unit/api/modules/model.test.ts
Normal file
@@ -0,0 +1,239 @@
|
||||
/**
|
||||
* AI模型API模块测试
|
||||
* 测试AI模型相关的API调用,包括获取AI模型广场、获取模型详情、获取模型评论等功能
|
||||
*/
|
||||
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { modelApi } from '@/api/modules/model';
|
||||
import { createMockAxios } from '@/test/mocks/http-client';
|
||||
import {
|
||||
createMockAIModel,
|
||||
createMockModelComment,
|
||||
createMockUser
|
||||
} from '@/test/mocks/data-factory';
|
||||
import type { ApiClient } from '@/api/client';
|
||||
import type {
|
||||
GetAIPlazaRequest,
|
||||
GetAIPlazaResponse,
|
||||
GetModelDetailRequest,
|
||||
GetModelDetailResponse,
|
||||
GetModelCommentsRequest,
|
||||
GetModelCommentsResponse
|
||||
} from '@/types/model/api';
|
||||
|
||||
describe('AI模型API模块', () => {
|
||||
let mockClient: ApiClient;
|
||||
let modelApiInstance: ReturnType<typeof modelApi>;
|
||||
|
||||
beforeEach(() => {
|
||||
// 创建模拟的API客户端
|
||||
mockClient = {
|
||||
get: vi.fn(),
|
||||
post: vi.fn(),
|
||||
put: vi.fn(),
|
||||
delete: vi.fn(),
|
||||
patch: vi.fn(),
|
||||
request: vi.fn(),
|
||||
getWithResponse: vi.fn(),
|
||||
postWithResponse: vi.fn(),
|
||||
putWithResponse: vi.fn(),
|
||||
deleteWithResponse: vi.fn(),
|
||||
patchWithResponse: vi.fn(),
|
||||
requestWithResponse: vi.fn(),
|
||||
addRequestInterceptor: vi.fn(),
|
||||
addResponseInterceptor: vi.fn(),
|
||||
removeRequestInterceptor: vi.fn(),
|
||||
removeResponseInterceptor: vi.fn(),
|
||||
setDefaults: vi.fn(),
|
||||
setBaseURL: vi.fn(),
|
||||
createInstance: vi.fn(),
|
||||
} as unknown as ApiClient;
|
||||
|
||||
// 创建AI模型API实例
|
||||
modelApiInstance = modelApi(mockClient);
|
||||
});
|
||||
|
||||
describe('获取AI模型广场功能', () => {
|
||||
it('应该能够获取AI模型广场数据', async () => {
|
||||
const getRequest: GetAIPlazaRequest = {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
category: 'text-generation'
|
||||
};
|
||||
const mockModels = [
|
||||
createMockAIModel({ id: 'model-1', category: 'text-generation' }),
|
||||
createMockAIModel({ id: 'model-2', category: 'text-generation' })
|
||||
];
|
||||
const mockResponse: GetAIPlazaResponse = {
|
||||
models: mockModels,
|
||||
categories: [
|
||||
{ id: 'text-generation', name: '文本生成', count: 50 },
|
||||
{ id: 'image-generation', name: '图像生成', count: 30 }
|
||||
],
|
||||
pagination: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 50,
|
||||
totalPages: 5
|
||||
}
|
||||
};
|
||||
|
||||
mockClient.get.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await modelApiInstance.getAIPlaza(getRequest);
|
||||
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/models/plaza', { params: getRequest });
|
||||
expect(result).toEqual(mockResponse);
|
||||
});
|
||||
|
||||
it('应该能够在不带参数的情况下获取AI模型广场数据', async () => {
|
||||
const mockModels = [
|
||||
createMockAIModel({ id: 'model-1' }),
|
||||
createMockAIModel({ id: 'model-2' })
|
||||
];
|
||||
const mockResponse: GetAIPlazaResponse = {
|
||||
models: mockModels,
|
||||
categories: [
|
||||
{ id: 'text-generation', name: '文本生成', count: 50 },
|
||||
{ id: 'image-generation', name: '图像生成', count: 30 }
|
||||
],
|
||||
pagination: {
|
||||
page: 1,
|
||||
limit: 20,
|
||||
total: 2,
|
||||
totalPages: 1
|
||||
}
|
||||
};
|
||||
|
||||
mockClient.get.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await modelApiInstance.getAIPlaza();
|
||||
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/models/plaza', {});
|
||||
expect(result).toEqual(mockResponse);
|
||||
});
|
||||
|
||||
it('应该处理获取AI模型广场失败的情况', async () => {
|
||||
const getRequest: GetAIPlazaRequest = {
|
||||
page: -1, // 无效的页码
|
||||
limit: 10
|
||||
};
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
data: null,
|
||||
message: '页码必须大于0',
|
||||
code: 400
|
||||
};
|
||||
|
||||
mockClient.get.mockRejectedValue(errorResponse);
|
||||
|
||||
await expect(modelApiInstance.getAIPlaza(getRequest)).rejects.toEqual(errorResponse);
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/models/plaza', { params: getRequest });
|
||||
});
|
||||
});
|
||||
|
||||
describe('获取模型详情功能', () => {
|
||||
it('应该能够获取模型详情', async () => {
|
||||
const getRequest: GetModelDetailRequest = {
|
||||
modelId: 'model-123'
|
||||
};
|
||||
const mockModel = createMockAIModel({ id: 'model-123' });
|
||||
const mockResponse: GetModelDetailResponse = {
|
||||
model: mockModel,
|
||||
relatedModels: [
|
||||
createMockAIModel({ id: 'related-1' }),
|
||||
createMockAIModel({ id: 'related-2' })
|
||||
]
|
||||
};
|
||||
|
||||
mockClient.get.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await modelApiInstance.getModelDetail(getRequest);
|
||||
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/models/model-123', { params: {} });
|
||||
expect(result).toEqual(mockResponse);
|
||||
});
|
||||
|
||||
it('应该处理获取不存在模型详情的情况', async () => {
|
||||
const getRequest: GetModelDetailRequest = {
|
||||
modelId: 'non-existent-model'
|
||||
};
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
data: null,
|
||||
message: '模型不存在',
|
||||
code: 404
|
||||
};
|
||||
|
||||
mockClient.get.mockRejectedValue(errorResponse);
|
||||
|
||||
await expect(modelApiInstance.getModelDetail(getRequest)).rejects.toEqual(errorResponse);
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/models/non-existent-model', { params: {} });
|
||||
});
|
||||
});
|
||||
|
||||
describe('获取模型评论功能', () => {
|
||||
it('应该能够获取模型评论', async () => {
|
||||
const getRequest: GetModelCommentsRequest = {
|
||||
modelId: 'model-123',
|
||||
page: 1,
|
||||
limit: 10,
|
||||
sortBy: 'newest'
|
||||
};
|
||||
const mockComments = [
|
||||
createMockModelComment({ id: 'comment-1', modelId: 'model-123' }),
|
||||
createMockModelComment({ id: 'comment-2', modelId: 'model-123' })
|
||||
];
|
||||
const mockResponse: GetModelCommentsResponse = {
|
||||
comments: mockComments,
|
||||
pagination: {
|
||||
page: 1,
|
||||
limit: 10,
|
||||
total: 25,
|
||||
totalPages: 3
|
||||
},
|
||||
statistics: {
|
||||
averageRating: 4.5,
|
||||
totalComments: 25,
|
||||
ratingDistribution: {
|
||||
5: 15,
|
||||
4: 7,
|
||||
3: 2,
|
||||
2: 1,
|
||||
1: 0
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
mockClient.get.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await modelApiInstance.getModelComments(getRequest);
|
||||
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/models/model-123/comments', {
|
||||
params: { page: 1, limit: 10, sortBy: 'newest' }
|
||||
});
|
||||
expect(result).toEqual(mockResponse);
|
||||
});
|
||||
|
||||
it('应该处理获取不存在模型的评论的情况', async () => {
|
||||
const getRequest: GetModelCommentsRequest = {
|
||||
modelId: 'non-existent-model',
|
||||
page: 1,
|
||||
limit: 10
|
||||
};
|
||||
const errorResponse = {
|
||||
success: false,
|
||||
data: null,
|
||||
message: '模型不存在',
|
||||
code: 404
|
||||
};
|
||||
|
||||
mockClient.get.mockRejectedValue(errorResponse);
|
||||
|
||||
await expect(modelApiInstance.getModelComments(getRequest)).rejects.toEqual(errorResponse);
|
||||
expect(mockClient.get).toHaveBeenCalledWith('/models/non-existent-model/comments', {
|
||||
params: { page: 1, limit: 10 }
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user