/**
* Token 认证测试
* 测试 Token 的获取、验证、更新等流程
*/
/// <reference path="../global.d.ts" />
describe('get_projects Tool', () => {
beforeEach(() => {
global.testCache.clear();
});
test('should fetch project list successfully', async () => {
const result = await getProjects(global.testClient, global.testCache, {
page: 1,
limit: 50
});
expect(result).toBeDefined();
expect(result.content).toBeDefined();
expect(result.content.length).toBeGreaterThan(0);
expect(result.content[0]).toHaveProperty('type', 'text');
expect(result.content[0]).toHaveProperty('text');
const text = result.content[0].text;
expect(text).toContain('项目');
console.log(`✅ Successfully fetched project list: ${text.substring(0, 100)}...`);
});
test('should filter projects by status', async () => {
const result = await getProjects(global.testClient, global.testCache, {
page: 1,
limit: 50,
status: 'doing'
});
expect(result).toBeDefined();
const text = result.content[0].text;
expect(text).toContain('项目');
console.log(`✅ Filtered projects by status: ${text.substring(0, 100)}...`);
});
test('should use default pagination when not specified', async () => {
const result = await getProjects(global.testClient, global.testCache);
expect(result).toBeDefined();
const text = result.content[0].text;
expect(text).toContain('项目');
console.log(`✅ Used default pagination`);
});
test('should handle orderBy parameter', async () => {
const result = await getProjects(global.testClient, global.testCache, {
page: 1,
limit: 10,
orderBy: 'id_desc'
});
expect(result).toBeDefined();
const text = result.content[0].text;
expect(text).toContain('项目');
console.log(`✅ Ordered projects successfully`);
});
test('should cache project list', async () => {
const cacheKey = 'projects:{"page":1,"limit":50}';
// 第一次调用
const result1 = await getProjects(global.testClient, global.testCache, {
page: 1,
limit: 50
});
// 检查缓存
const cached = global.testCache.get(cacheKey);
expect(cached).toBeDefined();
expect(Array.isArray(cached)).toBe(true);
console.log(`✅ Project list cached successfully`);
});
test('should return cached result on second call', async () => {
const params = { page: 1, limit: 50 };
// 第一次调用
const start1 = Date.now();
const result1 = await getProjects(global.testClient, global.testCache, params);
const time1 = Date.now() - start1;
// 第二次调用(应该从缓存返回)
const start2 = Date.now();
const result2 = await getProjects(global.testClient, global.testCache, params);
const time2 = Date.now() - start2;
// 缓存命中应该显著更快
expect(result2).toBeDefined();
expect(time2).toBeLessThan(time1 * 0.5);
console.log(`✅ Cached response faster: ${time2}ms vs ${time1}ms`);
});
test('should return different cache for different parameters', async () => {
const params1 = { page: 1, limit: 10 };
const params2 = { page: 2, limit: 10 };
const cacheKey1 = `projects:${JSON.stringify(params1)}`;
const cacheKey2 = `projects:${JSON.stringify(params2)}`;
await getProjects(global.testClient, global.testCache, params1);
await getProjects(global.testClient, global.testCache, params2);
expect(global.testCache.get(cacheKey1)).toBeDefined();
expect(global.testCache.get(cacheKey2)).toBeDefined();
expect(global.testCache.get(cacheKey1)).not.toBe(global.testCache.get(cacheKey2));
console.log(`✅ Different params use different cache keys`);
});
test('should handle empty result', async () => {
const result = await getProjects(global.testClient, global.testCache, {
page: 9999,
limit: 10
});
expect(result).toBeDefined();
expect(result.content).toBeDefined();
console.log(`✅ Handled empty result gracefully`);
});
test('should format output correctly', async () => {
const result = await getProjects(global.testClient, global.testCache, {
page: 1,
limit: 5
});
const text = result.content[0].text;
// 检查输出格式
expect(text).toMatch(/\d+ 个项目/);
console.log(`✅ Output formatted correctly`);
console.log(`Sample output:\n${text.substring(0, 200)}...`);
});
test('should include project details in output', async () => {
const result = await getProjects(global.testClient, global.testCache, {
page: 1,
limit: 5
});
const text = result.content[0].text;
// 检查是否包含项目详细信息
expect(text).toContain('ID:');
expect(text).toContain('代码:');
expect(text).toContain('状态:');
console.log(`✅ Project details included in output`);
});
test('should handle API errors gracefully', async () => {
// 使用错误的参数
const invalidClient = {
get: jest.fn().mockRejectedValue(new Error('API Error'))
};
const result = await getProjects(invalidClient as any, global.testCache, {
page: 1,
limit: 50
});
expect(result).toBeDefined();
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain('失败');
console.log(`✅ API errors handled gracefully: ${result.content[0].text}`);
});
test('should handle network errors', async () => {
const networkErrorClient = {
get: jest.fn().mockRejectedValue(new Error('Network timeout'))
};
const result = await getProjects(networkErrorClient as any, global.testCache, {
page: 1,
limit: 50
});
expect(result.isError).toBe(true);
expect(result.content[0].text).toContain('失败');
console.log(`✅ Network errors handled: ${result.content[0].text}`);
});
test('should handle invalid parameters', async () => {
const result = await getProjects(global.testClient, global.testCache, {
page: -1,
limit: 'invalid' as any
} as any);
// 应该仍然能够处理(使用默认值或处理错误)
expect(result).toBeDefined();
console.log(`✅ Invalid parameters handled`);
});
});