const assert = require("assert");
const fs = require("fs");
const path = require("path");

function readSource(relativePath) {
  return fs.readFileSync(path.resolve(__dirname, "../../", relativePath), "utf8");
}

const detailSource = readSource("src/views/ccdiProject/detail.vue");
const tableSource = readSource("src/views/ccdiProject/components/ProjectTable.vue");
const searchBarSource = readSource("src/views/ccdiProject/components/SearchBar.vue");
const indexSource = readSource("src/views/ccdiProject/index.vue");
const uploadSource = readSource("src/views/ccdiProject/components/detail/UploadData.vue");
const paramSource = readSource("src/views/ccdiProject/components/detail/ParamConfig.vue");

assert(
  detailSource.includes("latestTagTaskErrorMessage") &&
    detailSource.includes("latestTagTaskEndTime"),
  "项目详情接口字段应承载最近失败任务的错误信息和结束时间"
);

assert(
  detailSource.includes("tagFailureDialogVisible") &&
    detailSource.includes("项目打标失败") &&
    detailSource.includes("查看完整错误"),
  "项目详情页应展示打标失败提示并提供完整错误弹窗"
);

assert(
  /isProjectTagFailed\(\)\s*\{\s*return String\(this\.projectInfo\.projectStatus\) === "4";\s*\}/.test(
    detailSource
  ),
  "项目详情页应显式识别 4-打标失败状态"
);

assert(
  /4:\s*"danger"/.test(detailSource) && /4:\s*"打标失败"/.test(detailSource),
  "项目详情页状态映射应支持 4-打标失败"
);

assert(
  /String\(this\.projectInfo\.projectStatus\)\s*!==\s*"3"/.test(detailSource),
  "项目状态轮询应在状态变为打标失败后停止"
);

assert(
  tableSource.includes("['0', '3', '4'].includes(scope.row.status)"),
  "项目列表中打标失败项目应只开放进入项目入口"
);

assert(
  !tableSource.includes("latestTagTaskErrorMessage"),
  "项目列表不应展示完整打标失败错误"
);

assert(
  searchBarSource.includes("{ label: '打标失败', value: '4', count: 0 }"),
  "项目筛选栏应提供打标失败筛选项"
);

assert(
  indexSource.includes("'4': counts.status4 || 0"),
  "项目首页应接收 status4 统计"
);

assert(
  uploadSource.includes('return ["0", "3", "4"].includes(String(this.projectInfo.projectStatus));'),
  "上传数据页应将打标失败按进行中口径禁用报告入口"
);

assert(
  !uploadSource.includes("isProjectTagFailed") &&
    !paramSource.includes("isProjectTagFailed") &&
    !paramSource.includes('projectStatus) === "4"'),
  "上传和参数配置页不应把打标失败作为只读锁定状态"
);

console.log("project-tag-failed-status test passed");
