auto-request
Version:
通过Yapi JSON Schema生成接口Axios或Taro接口
253 lines (220 loc) • 7.73 kB
text/typescript
/**
* 向后兼容性测试
* 确保新的参数对比功能不会破坏现有功能
*/
import { compareSwagger, generateComparisonMarkdown } from '../src/core/swagger-comparator';
console.log('🔄 向后兼容性测试\n');
console.log('=' .repeat(80));
// 测试1: 简单的 Swagger 对比(旧功能)
console.log('\n✅ 测试 1: 基本接口对比功能(原有功能)');
const oldSimple = {
swagger: '2.0',
info: { title: 'API', version: '1.0.0' },
paths: {
'/test': {
get: {
summary: 'Test',
responses: { '200': { description: 'OK' } },
},
},
},
};
const newSimple = {
swagger: '2.0',
info: { title: 'API', version: '1.0.0' },
paths: {
'/test': {
get: {
summary: 'Test',
responses: { '200': { description: 'OK' } },
},
},
'/test2': {
post: {
summary: 'Test2',
responses: { '201': { description: 'Created' } },
},
},
},
};
try {
const result1 = compareSwagger(JSON.stringify(oldSimple), JSON.stringify(newSimple));
console.log(` ✓ 成功检测到 ${result1.added.length} 个新增接口`);
console.log(` ✓ 成功检测到 ${result1.removed.length} 个删除接口`);
console.log(` ✓ 成功检测到 ${result1.modified.length} 个修改接口`);
if (result1.added.length === 1 && result1.added[0].path === '/test2') {
console.log(' ✓ 新增接口检测正确');
} else {
throw new Error('新增接口检测失败');
}
} catch (error) {
console.error(' ✗ 测试失败:', (error as Error).message);
process.exit(1);
}
// 测试2: 没有参数的接口对比(确保 parameterChanges 为 undefined 或不存在)
console.log('\n✅ 测试 2: 无参数接口的对比(确保不产生额外数据)');
const oldNoParams = {
swagger: '2.0',
info: { title: 'API', version: '1.0.0' },
paths: {
'/test': {
get: {
summary: 'Old Summary',
responses: { '200': { description: 'OK' } },
},
},
},
};
const newNoParams = {
swagger: '2.0',
info: { title: 'API', version: '1.0.0' },
paths: {
'/test': {
get: {
summary: 'New Summary',
responses: { '200': { description: 'OK' } },
},
},
},
};
try {
const result2 = compareSwagger(JSON.stringify(oldNoParams), JSON.stringify(newNoParams));
if (result2.modified.length === 1) {
console.log(' ✓ 成功检测到修改的接口');
const modified = result2.modified[0];
if (modified.changes.includes('摘要已修改')) {
console.log(' ✓ 成功检测到摘要修改');
}
// 确保没有参数变化时,parameterChanges 为 undefined 或空数组
if (!modified.parameterChanges || modified.parameterChanges.length === 0) {
console.log(' ✓ 无参数变化时不产生多余数据');
} else {
throw new Error('无参数接口不应该有 parameterChanges');
}
} else {
throw new Error('应该检测到1个修改的接口');
}
} catch (error) {
console.error(' ✗ 测试失败:', (error as Error).message);
process.exit(1);
}
// 测试3: Markdown 生成功能(原有功能)
console.log('\n✅ 测试 3: Markdown 生成功能(原有功能)');
try {
const result3 = compareSwagger(JSON.stringify(oldSimple), JSON.stringify(newSimple));
const markdown = generateComparisonMarkdown(result3);
if (markdown.includes('### 新增接口')) {
console.log(' ✓ Markdown 包含新增接口标题');
}
if (markdown.includes('/test2')) {
console.log(' ✓ Markdown 包含新增接口路径');
}
if (markdown.includes('| POST |')) {
console.log(' ✓ Markdown 包含正确的表格格式');
}
} catch (error) {
console.error(' ✗ 测试失败:', (error as Error).message);
process.exit(1);
}
// 测试4: 新功能 - 参数详细对比
console.log('\n✅ 测试 4: 新增的参数详细对比功能');
const oldWithParams = {
swagger: '2.0',
info: { title: 'API', version: '1.0.0' },
paths: {
'/users': {
get: {
summary: 'Get users',
parameters: [
{ name: 'page', in: 'query', type: 'integer', required: false },
],
responses: { '200': { description: 'OK' } },
},
},
},
};
const newWithParams = {
swagger: '2.0',
info: { title: 'API', version: '1.0.0' },
paths: {
'/users': {
get: {
summary: 'Get users',
parameters: [
{ name: 'page', in: 'query', type: 'integer', required: true }, // 必填状态改变
{ name: 'limit', in: 'query', type: 'integer', required: false }, // 新增参数
],
responses: { '200': { description: 'OK' } },
},
},
},
};
try {
const result4 = compareSwagger(JSON.stringify(oldWithParams), JSON.stringify(newWithParams));
if (result4.modified.length === 1) {
console.log(' ✓ 检测到修改的接口');
const modified = result4.modified[0];
if (modified.parameterChanges && modified.parameterChanges.length > 0) {
console.log(` ✓ 检测到 ${modified.parameterChanges.length} 处参数变化`);
const addedParams = modified.parameterChanges.filter(p => p.changeType === 'added');
const modifiedParams = modified.parameterChanges.filter(p => p.changeType === 'modified');
if (addedParams.length === 1 && addedParams[0].name === 'limit') {
console.log(' ✓ 正确检测到新增参数');
}
if (modifiedParams.length === 1 && modifiedParams[0].name === 'page') {
console.log(' ✓ 正确检测到修改的参数');
if (modifiedParams[0].details?.includes('必填: 否 → 是')) {
console.log(' ✓ 正确检测到必填状态变化');
}
}
} else {
throw new Error('应该检测到参数变化');
}
}
// 验证 Markdown 包含参数详细信息
const markdown4 = generateComparisonMarkdown(result4);
if (markdown4.includes('参数详细变化')) {
console.log(' ✓ Markdown 包含参数详细变化信息');
}
if (markdown4.includes('新增参数')) {
console.log(' ✓ Markdown 包含新增参数部分');
}
if (markdown4.includes('修改参数')) {
console.log(' ✓ Markdown 包含修改参数部分');
}
} catch (error) {
console.error(' ✗ 测试失败:', (error as Error).message);
process.exit(1);
}
// 测试5: 错误处理(原有功能)
console.log('\n✅ 测试 5: 错误处理(原有功能)');
try {
try {
compareSwagger('invalid json', '{}');
console.error(' ✗ 应该抛出错误');
process.exit(1);
} catch (error) {
console.log(' ✓ 正确处理无效 JSON');
}
// 测试空 paths
const emptySwagger = JSON.stringify({
swagger: '2.0',
info: { title: 'API', version: '1.0.0' },
});
const result5 = compareSwagger(emptySwagger, emptySwagger);
if (result5.added.length === 0 && result5.removed.length === 0 && result5.modified.length === 0) {
console.log(' ✓ 正确处理空 Swagger');
}
} catch (error) {
console.error(' ✗ 测试失败:', (error as Error).message);
process.exit(1);
}
console.log('\n' + '=' .repeat(80));
console.log('\n🎉 所有向后兼容性测试通过!');
console.log('\n总结:');
console.log(' ✓ 原有的接口对比功能正常工作');
console.log(' ✓ 原有的 Markdown 生成功能正常工作');
console.log(' ✓ 新增的参数详细对比功能正常工作');
console.log(' ✓ 参数详细信息正确集成到 Markdown 中');
console.log(' ✓ 错误处理功能正常');
console.log(' ✓ 向后兼容性完好,不会破坏现有代码\n');