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

const componentPath = path.resolve(
  __dirname,
  "../../src/views/ccdiProject/detail.vue"
);
const source = fs.readFileSync(componentPath, "utf8");

assert(
  source.includes("this.$route.params.projectId"),
  "详情页应直接使用路由参数作为项目标识来源"
);

assert(
  source.includes("projectStatusPolling"),
  "详情页应保留项目状态轮询相关实现"
);

assert(
  /projectStatusPollingTimer:\s*null/.test(source),
  "详情页应声明项目状态轮询定时器"
);

assert(
  /projectStatusPollingInterval:\s*1000/.test(source),
  "详情页轮询间隔应为 1000ms"
);

assert(
  /beforeDestroy\(\)\s*\{[\s\S]*?this\.stopProjectStatusPolling\(\)/.test(source),
  "详情页销毁前应停止项目状态轮询"
);

assert(
  /syncProjectStatusPolling\(\)\s*\{[\s\S]*?String\(this\.projectInfo\.projectStatus\)\s*===\s*"3"[\s\S]*?this\.startProjectStatusPolling\(\)[\s\S]*?this\.stopProjectStatusPolling\(\)/.test(
    source
  ),
  "详情页应根据项目状态启停轮询"
);

assert(
  /startProjectStatusPolling\(\)\s*\{[\s\S]*?setInterval\([\s\S]*?this\.pollProjectStatus\(\)[\s\S]*?this\.projectStatusPollingInterval/.test(
    source
  ),
  "详情页应按固定间隔轮询项目状态"
);

assert(
  /pollProjectStatus\(\)\s*\{[\s\S]*?await this\.fetchProjectDetail\(\{ silent: true \}\)[\s\S]*?if\s*\(String\(this\.projectInfo\.projectStatus\)\s*!==\s*"3"\)\s*\{[\s\S]*?this\.stopProjectStatusPolling\(\)/.test(
    source
  ),
  "详情页轮询后应在状态脱离打标中时停止"
);

console.log("project-detail-tagging-polling test passed");
