/**
* Token 认证测试
* 测试 Token 的获取、验证、更新等流程
*/
/// <reference path="../global.d.ts" />
/// <reference path="../global.d.ts" />
import { createProject } from '../../src/tools/createProject';
/// <reference path="../global.d.ts" />
import { getTasks } from '../../src/tools/getTasks';
/// <reference path="../global.d.ts" />
import { createTask } from '../../src/tools/createTask';
/// <reference path="../global.d.ts" />
import { updateTaskStatus } from '../../src/tools/updateTaskStatus';
/// <reference path="../global.d.ts" />
import { batchCreateTasks } from '../../src/tools/batchCreateTasks';
/// <reference path="../global.d.ts" />
import { generateTestName } from '../setup';
/// <reference path="../global.d.ts" />
import { cleanupProject, cleanupTask, createBatchTaskData } from '../helpers/test-data';
describe('End-to-End Integration', () => {
beforeEach(() => {
global.testCache.clear();
});
test('should complete full project lifecycle', async () => {
// 1. 获取现有项目列表
const projectsResult = await getProjects(global.testClient, global.testCache, {
page: 1,
limit: 10
});
expect(projectsResult.isError).toBeFalsy();
console.log('✅ Retrieved project list');
// 2. 创建新项目
const projectName = generateTestName('E2E测试项目');
const project = await createProject(global.testClient, global.testCache, {
name: projectName,
code: \`E2E\${Date.now()}\`,
begin: '2025-11-01',
end: '2025-12-31',
acl: 'open'
});
expect(project.isError).toBeFalsy();
const projectIdMatch = project.content[0].text.match(/ID: (\d+)/);
expect(projectIdMatch).toBeTruthy();
const projectId = parseInt(projectIdMatch![1]);
console.log(\`✅ Created project with ID: \${projectId}\`);
try {
// 3. 创建任务
const task1 = await createTask(global.testClient, global.testCache, {
name: 'E2E测试任务-1',
project: projectId,
assignedTo: 's000001',
pri: 1,
estimate: 8
});
expect(task1.isError).toBeFalsy();
const taskIdMatch = task1.content[0].text.match(/ID: (\d+)/);
const taskId = parseInt(taskIdMatch![1]);
console.log(\`✅ Created task with ID: \${taskId}\`);
// 4. 更新任务状态
await updateTaskStatus(global.testClient, global.testCache, taskId, 'doing');
console.log('✅ Updated task status to doing');
await updateTaskStatus(global.testClient, global.testCache, taskId, 'done');
console.log('✅ Updated task status to done');
// 5. 验证项目详情
const projectDetails = await getProjectById(global.testClient, global.testCache, projectId);
expect(projectDetails.isError).toBeFalsy();
console.log('✅ Retrieved project details');
// 6. 验证任务列表
const tasks = await getTasks(global.testClient, global.testCache, { projectId });
expect(tasks.isError).toBeFalsy();
console.log('✅ Retrieved task list');
// 验证任务状态
const tasksText = tasks.content[0].text;
expect(tasksText).toContain('E2E测试任务-1');
console.log('✅ Verified task in list');
} finally {
// 清理项目及其任务
await cleanupProject(global.testClient, projectId);
console.log(\`✅ Cleaned up project \${projectId}\`);
}
});
test('should handle batch operations in workflow', async () => {
// 1. 创建项目
const project = await createProject(global.testClient, global.testCache, {
name: generateTestName('批量E2E项目'),
code: \`BATCH\${Date.now()}\`,
begin: '2025-11-01',
end: '2025-12-31'
});
if (project.isError) {
console.log('⏭️ Skipping batch test - project creation failed');
return;
}
const projectIdMatch = project.content[0].text.match(/ID: (\d+)/);
const projectId = parseInt(projectIdMatch![1]);
console.log(\`✅ Created project for batch test: \${projectId}\`);
try {
// 2. 批量创建任务
const tasks = createBatchTaskData(5, projectId);
const batchResult = await batchCreateTasks(global.testClient, global.testCache, tasks, {
continueOnError: true,
reportMode: 'detailed',
maxConcurrency: 3
});
expect(batchResult.success).toBe(5);
expect(batchResult.failed).toBe(0);
console.log(\`✅ Created \${batchResult.success} tasks via batch operation\`);
// 3. 批量更新任务状态
for (const result of batchResult.results) {
if (result.status === 'success') {
await updateTaskStatus(global.testClient, global.testCache, result.id, 'doing');
await updateTaskStatus(global.testClient, global.testCache, result.id, 'done');
}
}
console.log('✅ Updated all task statuses');
// 4. 验证所有任务已完成
const finalTasks = await getTasks(global.testClient, global.testCache, {
projectId,
status: 'done'
});
expect(finalTasks.isError).toBeFalsy();
console.log(\`✅ Verified \${finalTasks.content[0].text.split('任务').length - 1} completed tasks\`);
} finally {
// 清理
await cleanupProject(global.testClient, projectId);
console.log(\`✅ Cleaned up batch project \${projectId}\`);
}
});
test('should maintain cache consistency across operations', async () => {
global.testCache.clear();
// 1. 获取项目列表(填充缓存)
await getProjects(global.testClient, global.testCache, { page: 1, limit: 10 });
const initialCacheSize = global.testCache.size();
console.log(\`✅ Cache size after initial fetch: \${initialCacheSize}\`);
// 2. 创建新项目(应该清理项目列表缓存)
const project = await createProject(global.testClient, global.testCache, {
name: generateTestName('缓存一致性测试项目'),
code: \`CACHE\${Date.now()}\`,
begin: '2025-11-01'
});
if (!project.isError) {
// 3. 检查缓存是否被清理
const cacheAfterCreate = global.testCache.size();
console.log(\`✅ Cache size after project creation: \${cacheAfterCreate}\`);
// 4. 再次获取项目列表(重新填充缓存)
await getProjects(global.testClient, global.testCache, { page: 1, limit: 10 });
const cacheAfterRefresh = global.testCache.size();
console.log(\`✅ Cache size after refresh: \${cacheAfterRefresh}\`);
// 清理
const projectIdMatch = project.content[0].text.match(/ID: (\d+)/);
if (projectIdMatch) {
await cleanupProject(global.testClient, parseInt(projectIdMatch[1]));
}
}
console.log('✅ Cache consistency maintained');
});
test('should handle concurrent operations safely', async () => {
const projectId = 2;
// 并发执行多个操作
const operations = [
getProjects(global.testClient, global.testCache, { page: 1, limit: 10 }),
getTasks(global.testClient, global.testCache, { projectId, page: 1, limit: 10 }),
getProjectById(global.testClient, global.testCache, projectId),
getProjects(global.testClient, global.testCache, { page: 1, limit: 5 }),
getTasks(global.testClient, global.testCache, { projectId, page: 1, limit: 5 })
];
const results = await Promise.all(operations);
// 验证所有操作都成功
results.forEach((result, index) => {
expect(result).toBeDefined();
expect(result.isError).toBeFalsy();
});
console.log(\`✅ All \${operations.length} concurrent operations completed successfully\`);
});
});