6k6g-v3 / test-submit-task.ts
fanyubo
fix: 修复 TypeScript 构建错误 + 添加缺失依赖
2aec246
Raw
History Blame Contribute Delete
3.33 kB
/**
* 直接通过Redis推送测试任务
*/
import { Redis } from '@upstash/redis';
// Upstash Redis 配置(从 game.5e1.com 获取)
const UPSTASH_REDIS_REST_URL = 'https://oriented-skink-40337.upstash.io';
const UPSTASH_REDIS_REST_TOKEN = 'AZ2RAAIncDEyMzkzNTVmNWVhOTc0NWNmOWZiYTlmNGYyOTRmNGMzM3AxNDAzMzc';
const redis = new Redis({
url: UPSTASH_REDIS_REST_URL,
token: UPSTASH_REDIS_REST_TOKEN,
});
const QUEUE_KEY = 'slow_task:queue:normal';
const RESULTS_KEY = 'slow_task:results';
async function submitTestTask() {
const taskId = `test-${Date.now()}`;
const task = {
taskId,
userId: 'test-user',
tier: 'free',
taskType: '灵感草案',
status: 'queued',
chatContext: [
{ role: 'user', content: '请帮我写一份关于人工智能发展趋势的分析报告,包含技术演进、应用场景和未来展望三个部分。' }
],
createdAt: Date.now(),
};
console.log('[Test] 推送测试任务:', taskId);
// 1. 先在 results 中创建初始状态
await redis.hset(RESULTS_KEY, {
[taskId]: JSON.stringify({
...task,
progress: 0,
updatedAt: Date.now(),
})
});
// 2. 推送到队列
await redis.lpush(QUEUE_KEY, JSON.stringify(task));
console.log('[Test] ✅ 任务已推送');
console.log('[Test] taskId:', taskId);
console.log('[Test] 监控状态: https://luoyike2003-6k6g.hf.space/internal/health');
return taskId;
}
async function monitorTask(taskId: string) {
console.log('\n[Test] 开始监控任务状态...');
for (let i = 0; i < 60; i++) {
await new Promise(resolve => setTimeout(resolve, 5000));
const state = await redis.hget(RESULTS_KEY, taskId);
if (!state) {
console.log(`[Test] ${i * 5}s: 任务不存在`);
continue;
}
const parsed = typeof state === 'string' ? JSON.parse(state) : state;
console.log(`[Test] ${i * 5}s: status=${parsed.status}, progress=${parsed.progress || 0}%`);
if (parsed.status === 'completed') {
console.log('\n[Test] ✅ 任务完成!');
console.log('[Test] downloadUrl:', parsed.downloadUrl);
console.log('[Test] expiresAt:', parsed.expiresAt);
console.log('[Test] duration:', parsed.duration, 'ms');
return parsed;
}
if (parsed.status === 'failed') {
console.log('\n[Test] ❌ 任务失败');
console.log('[Test] error:', parsed.errorMessage);
return parsed;
}
}
console.log('\n[Test] ⏰ 监控超时(5分钟)');
return null;
}
// 执行测试
submitTestTask()
.then(taskId => monitorTask(taskId))
.then(result => {
if (result && result.downloadUrl) {
console.log('\n[Test] 验证 downloadUrl:');
// 检查 URL 格式
if (result.downloadUrl.includes('cos.ap-guangzhou.myqcloud.com')) {
console.log('[Test] ✅ COS URL 正确格式');
} else if (result.downloadUrl.includes('assets.5e1.com')) {
console.log('[Test] ⚠️ 使用了备用域名');
} else if (result.downloadUrl.includes('game.5e1.com/api/slow-task/content-raw')) {
console.log('[Test] ⚠️ COS上传失败,使用备用端点');
} else {
console.log('[Test] ❌ 未知 URL 格式');
}
}
process.exit(0);
})
.catch(err => {
console.error('[Test] 错误:', err);
process.exit(1);
});