保存完整任务报告及完成审计原子化基线,最新回归待确认

This commit is contained in:
xuans committed 2026-10-02 17:51:37 +08:00
1 parent 8a8b1a5e0e
commit 4e8d50919a
21 files changed
+505 -52

No files matched your search

+16 -2
View File
@@ -6,8 +6,15 @@ export function taskIsPending(task) {
return !terminalStatuses.has(task?.status);
}
export function latestDeploymentReport(task, attempts, results) {
export function latestDeploymentReport(task, attempts, results, reports = []) {
const latest = Math.max(0, ...attempts.map((item) => item.attempt_number || 0));
if (taskIsPending(task) || latest === 0) return null;
const attempt = attempts.find((item) => item.attempt_number === latest);
const stored = reports.find((item) => item.task_id === task.id &&
item.attempt_id === attempt.id && item.attempt_number === latest);
if (stored && Array.isArray(stored.report?.results)) {
return { ...stored.report, status: task.status };
}
const items = results.filter((item) => item.attempt_number === latest);
if (taskIsPending(task) || latest === 0 || items.length === 0) return null;
return {
@@ -58,7 +65,14 @@ export function useTaskMonitor({ fetchAdmin, tasks, deploymentResult, showTaskDe
if (!taskIsPending(task) && latestAttempt && latestAttempt.status !== task.status) {
throw new Error("任务记录正在更新,将重新查询一致的执行结果");
}
return { task, attempts: attempts.items, report: latestDeploymentReport(task, attempts.items, results.items) };
if (results.reports !== undefined && !Array.isArray(results.reports)) {
throw new Error("完整部署报告响应不完整,将自动重试");
}
return {
task,
attempts: attempts.items,
report: latestDeploymentReport(task, attempts.items, results.items, results.reports),
};
}
async function poll() {
@@ -8,6 +8,50 @@ import { deferred, mountComposable } from "../../test-support/mount.js";
const initialTask = { id: "async-task", domain_id: "domain-1", task_type: "certificate_deploy", status: "running" };
const target = { server_id: "server-1", server: "部署目标", status: "success", attempt_number: 1 };
test("完整报告恢复站点验证与回滚事实,不被逐目标结果覆盖", () => {
const task = { ...initialTask, status: "failed" };
const attempts = [{ id: "attempt-2", attempt_number: 2, status: "failed" }];
const persisted = {
task_id: task.id, attempt_id: "attempt-2", attempt_number: 2,
report: {
total: 1, success: 0, failed: 1, status: "failed",
verification_status: "failed", recovery_status: "success",
results: [{ ...target, status: "failed", failure_code: "recovery_restored" }],
},
};
const report = latestDeploymentReport(task, attempts, [target], [persisted]);
assert.equal(report.verification_status, "failed");
assert.equal(report.recovery_status, "success");
assert.equal(report.results[0].failure_code, "recovery_restored");
assert.equal(report.success, 0);
assert.match(deploymentSummary(report), /恢复/);
});
test("完整报告按任务和尝试精确匹配,不复用旧尝试或其他任务报告", () => {
const task = { ...initialTask, status: "success" };
const attempts = [{ id: "attempt-2", attempt_number: 2 }];
for (const mismatch of [
{ task_id: "another-task", attempt_id: "attempt-2", attempt_number: 2 },
{ task_id: task.id, attempt_id: "attempt-1", attempt_number: 2 },
{ task_id: task.id, attempt_id: "attempt-2", attempt_number: 1 },
]) {
assert.equal(latestDeploymentReport(task, attempts, [], [{
...mismatch, report: { status: "success", verification_status: "success", results: [] },
}]), null);
}
});
test("完整报告无逐目标结果仍可读取,执行成功不能覆盖任务收尾失败", () => {
const task = { ...initialTask, status: "failed" };
const attempts = [{ id: "attempt-1", attempt_number: 1 }];
const reports = [{
task_id: task.id, attempt_id: "attempt-1", attempt_number: 1,
report: { status: "success", total: 0, success: 0, failed: 0, results: [] },
}];
assert.equal(latestDeploymentReport(task, attempts, [], reports).status, "failed");
assert.equal(latestDeploymentReport(initialTask, attempts, [], reports), null);
});
function taskFetcher(readTask, onPost = () => {}) {
return async (path, options = {}) => {
const task = readTask();
@@ -23,6 +67,30 @@ function taskFetcher(readTask, onPost = () => {}) {
};
}
test("页面刷新查询完整报告并展示恢复状态", async (t) => {
const task = { ...initialTask, status: "failed" };
const fetchTask = taskFetcher(() => task);
const { state } = await mountComposable(t, useDashboard, (path, options) => {
if (path.endsWith("/attempts")) {
return Response.json({ items: [{ id: "stored-attempt", attempt_number: 1, status: "failed" }] });
}
if (path.endsWith("/deployment-results")) {
return Response.json({ items: [target], reports: [{
task_id: task.id, attempt_id: "stored-attempt", attempt_number: 1,
report: {
status: "failed", total: 1, success: 0, failed: 1,
verification_status: "failed", recovery_status: "unconfirmed", results: [target],
},
}] });
}
return fetchTask(path, options);
});
await state.openTaskDetails(task);
assert.equal(state.taskDeploymentReport.value.verification_status, "failed");
assert.equal(state.taskDeploymentReport.value.recovery_status, "unconfirmed");
assert.match(deploymentSummary(state.taskDeploymentReport.value), /恢复/);
});
test("异步部署接收后不误报成功或缺失结果,最终失败不被逐目标成功覆盖", async (t) => {
let current = { ...initialTask };
const posts = [];