first commit
Some checks failed
Build and Push Docker Image / build (push) Has been cancelled
Sync to CNB / sync (push) Has been cancelled
Delete old workflow runs / del_runs (push) Has been cancelled
Upstream Sync / Sync latest commits from upstream repo (push) Has been cancelled

This commit is contained in:
2026-01-30 16:57:44 +08:00
commit 3d175d75af
119 changed files with 35834 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import { NextRequest } from "next/server";
import { getTask, getZipPathForTask } from "@/lib/testcase-backend";
import fs from "fs/promises";
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ taskId: string }> }
) {
const { taskId } = await params;
if (!taskId) {
return Response.json({ error: "缺少 taskId" }, { status: 400 });
}
const task = getTask(taskId);
if (!task) {
return Response.json({ error: "任务未找到" }, { status: 404 });
}
if (task.status !== "completed") {
return Response.json({ error: "任务尚未完成" }, { status: 400 });
}
const zipPath = getZipPathForTask(taskId);
if (!zipPath) {
return Response.json({ error: "文件未找到" }, { status: 404 });
}
try {
const buffer = await fs.readFile(zipPath);
const filename = `${task.englishName}_testcases.zip`;
return new Response(buffer, {
status: 200,
headers: {
"Content-Type": "application/zip",
"Content-Disposition": `attachment; filename="${encodeURIComponent(filename)}"`,
},
});
} catch (e) {
console.error("[testcase] download read error:", e);
return Response.json({ error: "文件读取失败" }, { status: 500 });
}
}

View File

@@ -0,0 +1,75 @@
import { NextRequest } from "next/server";
import {
createTask,
getEstimatedTime,
findRunningTaskByEnglishName,
processTask,
} from "@/lib/testcase-backend";
export async function POST(req: NextRequest) {
try {
const body = await req.json();
const {
title,
englishName,
description,
standardCode,
serviceLevel = "standard",
testCaseCount: rawCount,
} = body;
if (!title || !englishName || !description || !standardCode) {
return Response.json(
{ success: false, error: "请填写必填字段题目标题、题目英文名、题目描述、C++标准程序" },
{ status: 400 }
);
}
if (!/^[a-z][a-z0-9_]*$/.test(String(englishName).trim())) {
return Response.json(
{ success: false, error: "题目英文名格式错误:只能包含小写字母、数字和下划线,必须以字母开头" },
{ status: 400 }
);
}
const testCaseCount = Math.min(100, Math.max(1, parseInt(String(rawCount), 10) || 10));
const existing = findRunningTaskByEnglishName(String(englishName));
if (existing) {
return Response.json(
{ success: false, error: "相同英文名的任务正在处理中,请稍后再试或使用不同的英文名" },
{ status: 409 }
);
}
const taskId = "task_" + Date.now() + "_" + Math.random().toString(36).slice(2, 11);
createTask(taskId, {
title: String(title).trim(),
englishName: String(englishName).trim().toLowerCase(),
description: String(description).trim(),
standardCode: String(standardCode).trim(),
testCaseCount,
serviceLevel: String(serviceLevel),
});
setImmediate(() => {
processTask(taskId).catch((err) => {
console.error(`[testcase] processTask error:`, err);
});
});
const estimated_time = getEstimatedTime(String(serviceLevel), testCaseCount);
return Response.json({
success: true,
task_id: taskId,
message: "任务已创建,正在处理中...",
estimated_time,
});
} catch (e) {
const message = e instanceof Error ? e.message : String(e);
return Response.json(
{ success: false, error: "创建任务时发生错误,请重试:" + message },
{ status: 500 }
);
}
}

View File

@@ -0,0 +1,37 @@
import { NextRequest } from "next/server";
import { getTask, getEstimatedTime } from "@/lib/testcase-backend";
export async function GET(
_req: NextRequest,
{ params }: { params: Promise<{ taskId: string }> }
) {
const { taskId } = await params;
if (!taskId) {
return Response.json({ error: "缺少 taskId" }, { status: 400 });
}
const task = getTask(taskId);
if (!task) {
return Response.json({ error: "任务未找到" }, { status: 404 });
}
const runningTime = Math.floor((Date.now() - task.createdAt.getTime()) / 1000);
const estimated_time = getEstimatedTime(task.serviceLevel, task.testCaseCount);
return Response.json({
task_id: task.id,
status: task.status,
progress: task.progress,
title: task.title,
english_name: task.englishName,
test_case_count: task.testCaseCount,
generated_cases: task.generatedCases ?? 0,
service_level: task.serviceLevel,
created_at: task.createdAt,
updated_at: task.updatedAt,
running_time: runningTime,
estimated_time,
error_message: task.errorMessage ?? undefined,
currentMessage: task.currentMessage ?? undefined,
});
}