gongxiangp2/scripts/test-task-platform-api.ts

184 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 测试任务平台接口
* 检查 API Key 状态和测试接口是否能正常查询素材
*/
import { prisma } from '../lib/prisma';
import { validateTaskNo, getMaterialByTaskNo } from '../lib/product-package';
const TASK_NO = 'TASK202601141713525624-01';
async function main() {
console.log('==== 任务平台接口测试 ====\n');
// 1. 检查任务号格式
console.log('1. 检查任务号格式...');
const isValidFormat = validateTaskNo(TASK_NO);
console.log(` 任务号: ${TASK_NO}`);
console.log(` 格式验证: ${isValidFormat ? '✅ 通过' : '❌ 失败'}\n`);
// 2. 查询素材
console.log('2. 查询素材...');
const material = await getMaterialByTaskNo(TASK_NO);
if (material) {
console.log(` ✅ 找到素材:`);
console.log(` - ID: ${material.id}`);
console.log(` - 任务号: ${material.taskNo}`);
console.log(` - 素材类型: ${material.materialType}`);
console.log(` - 任务状态: ${material.taskStatus || 'null'}`);
console.log(` - 是否删除: ${material.deletedAt ? '是' : '否'}`);
console.log(` - 创建时间: ${material.createdAt}`);
console.log(` - 资产数量: ${(material as any).assets?.length || 0}`);
if ((material as any).assets && (material as any).assets.length > 0) {
console.log(` - 资产列表:`);
(material as any).assets.forEach((asset: any, index: number) => {
console.log(` ${index + 1}. ${asset.assetType} - ${asset.fileName || '无文件名'}`);
});
}
} else {
console.log(` ❌ 未找到素材(任务号: ${TASK_NO})`);
// 尝试查询所有包含该任务号前缀的素材
console.log(`\n 尝试查询相似任务号...`);
const similarMaterials = await prisma.material.findMany({
where: {
taskNo: {
startsWith: 'TASK202601141713525624',
},
deletedAt: null,
},
select: {
id: true,
taskNo: true,
materialType: true,
taskStatus: true,
createdAt: true,
},
orderBy: {
createdAt: 'desc',
},
take: 10,
});
if (similarMaterials.length > 0) {
console.log(` 找到 ${similarMaterials.length} 个相似任务号:`);
similarMaterials.forEach((m) => {
console.log(` - ${m.taskNo} (${m.materialType}, 状态: ${m.taskStatus || 'null'})`);
});
} else {
console.log(` 未找到相似任务号`);
}
}
console.log('');
// 3. 检查所有 API Key
console.log('3. 检查 API Key 状态...');
const apiKeys = await prisma.apiKey.findMany({
select: {
id: true,
name: true,
key: true,
isActive: true,
lastUsedAt: true,
createdAt: true,
},
orderBy: {
createdAt: 'desc',
},
});
if (apiKeys.length === 0) {
console.log(' ⚠️ 未找到任何 API Key');
} else {
console.log(` 找到 ${apiKeys.length} 个 API Key:\n`);
apiKeys.forEach((key, index) => {
console.log(` ${index + 1}. ${key.name}`);
console.log(` - ID: ${key.id}`);
console.log(` - Key: ${key.key.substring(0, 8)}...${key.key.substring(key.key.length - 4)}`);
console.log(` - 状态: ${key.isActive ? '✅ 启用' : '❌ 禁用'}`);
console.log(` - 最后使用: ${key.lastUsedAt ? key.lastUsedAt.toISOString() : '从未使用'}`);
console.log(` - 创建时间: ${key.createdAt.toISOString()}`);
console.log('');
});
}
// 4. 检查最近的 API 调用日志
console.log('4. 检查最近的 API 调用日志...');
const recentLogs = await prisma.apiCallLog.findMany({
where: {
taskNo: TASK_NO,
},
include: {
apiKey: {
select: {
name: true,
},
},
},
orderBy: {
createdAt: 'desc',
},
take: 10,
});
if (recentLogs.length === 0) {
console.log(` ⚠️ 未找到任务号 ${TASK_NO} 的调用日志`);
} else {
console.log(` 找到 ${recentLogs.length} 条调用日志:\n`);
recentLogs.forEach((log, index) => {
console.log(` ${index + 1}. ${log.createdAt.toISOString()}`);
console.log(` - API Key: ${log.apiKey.name}`);
console.log(` - 接口: ${log.method} ${log.endpoint}`);
console.log(` - 状态码: ${log.statusCode}`);
console.log(` - 耗时: ${log.duration}ms`);
console.log(` - IP: ${log.ipAddress || '未知'}`);
console.log('');
});
}
// 5. 检查所有包含该任务号的调用日志(包括相似任务号)
console.log('5. 检查所有相关调用日志...');
const allRelatedLogs = await prisma.apiCallLog.findMany({
where: {
taskNo: {
startsWith: 'TASK202601141713525624',
},
},
include: {
apiKey: {
select: {
name: true,
},
},
},
orderBy: {
createdAt: 'desc',
},
take: 20,
});
if (allRelatedLogs.length > 0) {
console.log(` 找到 ${allRelatedLogs.length} 条相关调用日志:\n`);
allRelatedLogs.forEach((log, index) => {
console.log(` ${index + 1}. ${log.createdAt.toISOString()}`);
console.log(` - 任务号: ${log.taskNo}`);
console.log(` - API Key: ${log.apiKey.name}`);
console.log(` - 接口: ${log.method} ${log.endpoint}`);
console.log(` - 状态码: ${log.statusCode}`);
console.log(` - 耗时: ${log.duration}ms`);
console.log('');
});
} else {
console.log(` ⚠️ 未找到相关调用日志`);
}
console.log('==== 测试完成 ====');
await prisma.$disconnect();
}
main().catch((error) => {
console.error('测试出错:', error);
process.exit(1);
});