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 view = readFileSync(join(process.cwd(), "src/views/SalaryProfilesView.vue"), "utf-8");
const css = readFileSync(join(process.cwd(), "src/assets/styles.css"), "utf-8");

assertIncludes(view, "salary-profile-page");
assertIncludes(view, "salary-profile-summary-strip");
assertIncludes(view, "salary-profile-summary-item");
assertIncludes(view, "salary-profile-toolbar");
assertIncludes(view, "salary-profile-modal-section");
assertIncludes(view, "salary-profile-modal-section-title");
assertBefore(view, "salary-profile-summary-strip", "薪资结构一览");
assertBefore(view, "薪资结构一览", "salary-profile-edit-modal");

const summaryBlock = cssBlock(".salary-profile-summary-strip");
assertIncludes(summaryBlock, "grid-template-columns: repeat(4, minmax(0, 1fr));");
assertIncludes(summaryBlock, "padding: 10px;");
assertNotIncludes(summaryBlock, "min-height: 178px;");

const toolbarBlock = cssBlock(".salary-profile-toolbar");
assertIncludes(toolbarBlock, "display: grid;");
assertIncludes(toolbarBlock, "grid-template-columns:");

const modalSectionBlock = cssBlock(".salary-profile-modal-section");
assertIncludes(modalSectionBlock, "border: var(--card-inner-border);");
assertIncludes(css, ".salary-profile-modal-section {\n  display: grid;");
assertIncludes(css, "padding: 14px;");
assertIncludes(css, ".salary-profile-modal-fields");
assertIncludes(css, ".salary-profile-summary-strip,");
assertIncludes(css, ".salary-profile-toolbar");

function cssBlock(selector: string): string {
  const escaped = selector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  const pattern = new RegExp(`${escaped}\\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): void {
  if (!content.includes(expected)) {
    throw new Error(`期望包含 ${expected}`);
  }
}

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

function assertBefore(content: string, earlier: string, later: string): void {
  const earlierIndex = content.indexOf(earlier);
  const laterIndex = content.indexOf(later);
  if (earlierIndex === -1 || laterIndex === -1 || earlierIndex >= laterIndex) {
    throw new Error(`期望 ${earlier} 出现在 ${later} 之前`);
  }
}
