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

const loginScreen = cssBlock(".login-screen");
assertIncludes(loginScreen, "grid-template-columns: minmax(340px, 34vw) minmax(0, 1fr);");

const loginFormSide = cssBlock(".login-form-side");
assertIncludes(loginFormSide, "background: linear-gradient(180deg, #f8fafc 0%, #f3f6fa 100%);");
assertIncludes(loginFormSide, "box-shadow: inset -1px 0 0 rgba(var(--primary-rgb), 0.08);");

const loginCard = cssBlock(".login-card");
assertIncludes(loginCard, "width: min(360px, 100%);");

const loginVisual = cssBlock(".login-visual");
assertIncludes(loginVisual, "justify-items: start;");
assertNotIncludes(loginVisual, "border-left: 1px solid var(--line);");

const ledgerVisual = cssBlock(".ledger-visual");
assertIncludes(ledgerVisual, "width: min(360px, 100%);");
assertIncludes(ledgerVisual, "background: rgba(255, 255, 255, 0.68);");
assertIncludes(ledgerVisual, "backdrop-filter: blur(10px);");
assertIncludes(ledgerVisual, "padding: 22px;");

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): 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 escapeRegExp(value: string): string {
  return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
