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

const pagePath = path.resolve(__dirname, "../../src/views/ccdiProject/index.vue");
const tablePath = path.resolve(__dirname, "../../src/views/ccdiProject/components/ProjectTable.vue");
const pageSource = fs.readFileSync(pagePath, "utf8");
const tableSource = fs.readFileSync(tablePath, "utf8");
const searchBarPath = path.resolve(__dirname, "../../src/views/ccdiProject/components/SearchBar.vue");
const searchBarSource = fs.readFileSync(searchBarPath, "utf8");

assert(
  searchBarSource.includes("{ label: '已删除', value: 'deleted'"),
  "搜索条应提供独立的已删除列表入口"
);

assert(
  searchBarSource.includes("tab.value !== 'deleted' || this.showDeletedTab"),
  "已删除入口应由 showDeletedTab 控制可见性"
);

assert(
  searchBarSource.includes("const includeDeleted = this.activeTab === 'deleted'"),
  "切换已删除入口时应生成 includeDeleted 查询态"
);

assert(
  searchBarSource.includes("status: includeDeleted || this.activeTab === 'all' ? null : this.activeTab"),
  "已删除列表不应和普通状态 tab 混用"
);

assert(
  tableSource.includes('v-if="deletedList"'),
  "删除列表应使用独立操作区"
);

assert(
  tableSource.includes('@click="handleRestore(scope.row)"'),
  "删除列表应提供恢复按钮"
);

assert(
  tableSource.includes('v-if="canDelete(scope.row) && scope.row.status !== \'5\'"'),
  "普通列表应按 canDelete 展示删除按钮且排除已删除状态"
);

assert(
  /<template v-if="deletedList">[\s\S]*?handleRestore\(scope\.row\)[\s\S]*?<\/template>\s*<template v-else>[\s\S]*?handleViewResult/.test(tableSource),
  "删除列表只展示恢复动作，进入项目和查看结果应留在普通列表分支"
);

assert(
  pageSource.includes("rebuildProjectTags({ projectId: row.projectId })"),
  "重新分析应调用真实重打标接口"
);

assert(
  pageSource.includes("await this.$modal.confirm("),
  "重新分析前应先弹出确认框"
);

assert(
  pageSource.includes("确认对项目“${row.projectName}”重新分析吗？重新分析将重新计算项目标签。"),
  "确认弹窗文案应覆盖项目名称和重新计算标签提醒"
);

assert(
  pageSource.includes('confirmError === "cancel"') ||
    pageSource.includes("confirmError === 'cancel'"),
  "取消确认时应直接结束，不继续提交"
);

assert(
  /await this\.\$modal\.confirm\([\s\S]*?this\.\$set\(this\.reAnalyzeLoadingMap,\s*projectKey,\s*true\)/.test(pageSource),
  "只有确认后才允许进入按钮 loading"
);

assert(
  pageSource.includes('this.$modal.msgSuccess("已开始重新分析")') ||
    pageSource.includes("this.$modal.msgSuccess('已开始重新分析')"),
  "重新分析成功提示应统一为已开始重新分析"
);

assert(
  /finally\s*\([\s\S]*?this\.getList\(/.test(pageSource) || pageSource.includes("this.getList()"),
  "重新分析成功后应刷新列表"
);

assert(
  tableSource.includes(':loading="reAnalyzeLoadingMap[String(scope.row.projectId)]"') ||
    tableSource.includes(':loading="reAnalyzeLoadingMap[scope.row.projectId]"'),
  "表格中的重新分析按钮应支持提交态"
);

assert(
  pageSource.includes("重新分析失败，请稍后重试"),
  "重新分析失败时应有统一兜底提示"
);

assert(
  pageSource.includes("error.message"),
  "有明确业务异常时应优先透传后端返回文案"
);

console.log("project-list-reanalyze-flow test passed");
