declare const process: { cwd(): string };
declare function require(name: string): {
  readFileSync?: (path: string, encoding: string) => string;
  join?: (...parts: string[]) => string;
};

const { readFileSync } = require("node:fs");
const { join } = require("node:path");

if (!readFileSync || !join) {
  throw new Error("测试运行环境缺少文件读取能力");
}

const root = process.cwd();
const css = readFileSync(join(root, "src/assets/styles.css"), "utf-8");
const statCard = readFileSync(join(root, "src/components/StatCard.vue"), "utf-8");

const compactPages = [
  "DashboardView.vue",
  "CommissionsView.vue",
  "ConfigCenterView.vue",
  "UsersView.vue",
  "OperationLogsView.vue",
  "PayrollView.vue",
];

for (const page of compactPages) {
  const view = readFileSync(join(root, "src/views", page), "utf-8");
  assertIncludes(view, "summary-strip", `${page} 应使用紧凑摘要条`);
  assertNotIncludes(view, "<section class=\"stats-grid", `${page} 不应再使用大统计卡片区`);
}

assertIncludes(statCard, "summary-metric", "StatCard 应输出紧凑指标项");
assertNotIncludes(statCard, "class=\"stat-card\"", "StatCard 不应再输出旧大卡片类名");

const summaryStripBlock = cssBlock(".summary-strip");
assertIncludes(summaryStripBlock, "display: flex;");
assertIncludes(summaryStripBlock, "padding: 6px 8px;");

const summaryMetricBlock = cssBlock(".summary-metric");
assertIncludes(summaryMetricBlock, "min-height: 42px;");
assertIncludes(summaryMetricBlock, "padding: 5px 8px;");

assertNotIncludes(css, "min-height: 118px;", "旧大统计卡片高度不应保留");

function cssBlock(selector: string): string {
  const pattern = new RegExp(`${escapeRegExp(selector)}\\s*\\{([\\s\\S]*?)\\n\\}`, "m");
  const match = css.match(pattern);
  if (!match) {
    throw new Error(`缺少样式块 ${selector}`);
  }
  return match[1];
}

function assertIncludes(content: string, expected: string, message?: string): void {
  if (!content.includes(expected)) {
    throw new Error(message || `期望包含 ${expected}`);
  }
}

function assertNotIncludes(content: string, unexpected: string, message?: string): void {
  if (content.includes(unexpected)) {
    throw new Error(message || `不应包含 ${unexpected}`);
  }
}

function escapeRegExp(value: string): string {
  return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
