import { readFileSync } from "node:fs";
import { resolve } from "node:path";
import { describe, expect, it } from "vitest";

function source(path: string): string {
  return readFileSync(resolve(process.cwd(), "src", path), "utf8");
}

describe("Q1-F page coverage", () => {
  it("上传、批量导入和编辑共用层级分类与统一字典", () => {
    const modal = source("features/mutations/MutationModals.tsx");
    expect(modal).toContain("flattenCategoryOptions(categories.tree)");
    expect(modal).toContain("category.displayLabel");
    for (const name of [
      "documentTypes",
      "attachmentTypes",
      "securityLevels",
    ]) {
      expect(modal).toContain(`dictionaries.options("${name}")`);
    }
    expect(modal).toContain("categoryId,");
    expect(modal).not.toContain('dictionaries.options("visibilityTypes")');
    expect(modal).not.toContain('dictionaries.options("documentStatuses")');
    expect(modal).not.toContain("categoryPath:");
  });

  it("回收站和分类父级选择使用层级选项且循环防护仍在", () => {
    const recycle = source("features/recycle-bin/RecycleBinModal.tsx");
    const app = source("app/App.tsx");
    const tree = source("features/categories/categoryTree.ts");
    expect(recycle).toContain("flattenCategoryOptions(categories.tree)");
    expect(recycle).toContain("category.displayLabel");
    expect(app).toContain("getCategoryParentOptions");
    expect(app).toContain("option.displayLabel");
    expect(tree).toContain("!excluded.has(node.id)");
  });

  it("权限、审计、挂载及导航不使用已废止的分散映射", () => {
    const permission = source("features/permissions/PermissionModal.tsx");
    const audit = source("features/audit/AuditDashboard.tsx");
    const binding = source("features/bindings/AttachmentBindingModal.tsx");
    const app = source("app/App.tsx");
    expect(permission).toContain('dictionaries.options("visibilityTypes")');
    expect(permission).toContain('dictionaries.label("allowedActions"');
    expect(audit).toContain('dictionaries.label("auditActions"');
    expect(audit).toContain('dictionaries.label("auditResults"');
    expect(audit).toContain('dictionaries.label("auditTargets"');
    expect(binding).toContain('dictionaries.options("attachmentTypes")');
    expect(app).toContain('dictionaries.label("allowedModules", module)');
    expect(`${permission}${audit}${binding}${app}`).not.toContain("AUDIT_ACTION_LABELS");
    expect(`${permission}${audit}${binding}${app}`).not.toContain("VISIBILITY_LABELS");
  });

  it("字典失败、重试和登出清理在应用级统一处理", () => {
    const app = source("app/App.tsx");
    expect(app).toContain('title="系统字典加载失败"');
    expect(app).toContain("dictionaries.retry(currentUser.id)");
    expect(app.match(/dictionaries\.clear\(\)/g)?.length).toBeGreaterThanOrEqual(2);
  });
});
