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

const app = readFileSync(
  new URL("../../../app/App.tsx", import.meta.url),
  "utf8",
);
const api = readFileSync(new URL("../recycleBinApi.ts", import.meta.url), "utf8");
const state = readFileSync(
  new URL("../recycleBinState.ts", import.meta.url),
  "utf8",
);
const restoreModal = readFileSync(
  new URL("../RestoreDocumentModal.tsx", import.meta.url),
  "utf8",
);
const listModal = readFileSync(
  new URL("../RecycleBinModal.tsx", import.meta.url),
  "utf8",
);

describe("F8 scope and UI boundaries", () => {
  it("回收站入口只存在于ADMIN门禁后的真实后台组件", () => {
    expect(app).toContain(
      'page === "admin" && currentUser.roleCode === RoleCode.ADMIN',
    );
    const adminStart = app.indexOf("function RealAdminPage");
    const rootStart = app.indexOf("export default function App");
    expect(app.slice(adminStart, rootStart)).toContain("回收站");
    expect(app).toContain("<RecycleBinModal");
  });

  it("只封装正式列表与单条恢复路径", () => {
    expect(api).toContain('"recycle-bin/documents"');
    expect(api).toContain("`recycle-bin/documents/${id}/restore`");
    for (const forbidden of [
      "`documents/${id}/restore`",
      "batch-restore",
      "permanent-delete",
      "empty-recycle-bin",
      "categories/restore",
      "audit/export",
    ]) {
      expect(api).not.toContain(forbidden);
    }
  });

  it("列表具备300ms防抖、AbortController、计时器清理和版本保护", () => {
    expect(state).toContain("debounceMs = 300");
    expect(state).toContain("new AbortController()");
    expect(state).toContain("requestVersion");
    expect(state).toContain("window.clearTimeout(timer)");
  });

  it("恢复写请求有提交门禁、无自动重试并保留失败记录", () => {
    expect(restoreModal).toContain("const requestLocked = useRef(false)");
    expect(restoreModal).toContain("if (requestLocked.current || result) return");
    expect(restoreModal).toContain("requestLocked.current = true");
    expect(restoreModal).toContain("requestLocked.current = false");
    expect(restoreModal.match(/recycleBinApi\.restore/g)).toHaveLength(1);
    expect(restoreModal).not.toContain("setTimeout");
    expect(restoreModal).toContain("setError(next)");
  });

  it("恢复成功刷新真实数据但不打开详情或手工加减计数", () => {
    expect(restoreModal).toContain(
      'notifyContentChanged("documents", "attachments")',
    );
    expect(restoreModal).toContain("invalidateDetail");
    expect(restoreModal).not.toContain(".detail(");
    expect(restoreModal).not.toContain("viewCount +");
    expect(listModal).toContain("list.refresh()");
  });

  it("页面不展示路径、哈希、预览、下载或物理删除操作", () => {
    for (const forbidden of [
      "fileRelativePath",
      "fileHash",
      "FilePreviewModal",
      "DownloadButton",
      "物理删除按钮",
    ]) {
      expect(listModal).not.toContain(forbidden);
    }
    expect(listModal).toContain("恢复");
  });

  it("通过顶层Portal和安全边距完整显示回收站弹窗", () => {
    expect(listModal).toContain('createPortal(');
    expect(listModal).toContain('document.body');
    expect(listModal).toContain('z-[100]');
    expect(listModal).toContain('p-4 sm:p-6');
  });

  it("成功数量和跳过警告均可见", () => {
    expect(restoreModal).toContain("restoredPermissionCount");
    expect(restoreModal).toContain("skippedPermissionCount");
    expect(restoreModal).toContain("restoredBindingCount");
    expect(restoreModal).toContain("skippedBindingCount");
    expect(restoreModal).toContain("无效权限");
    expect(restoreModal).toContain("无效挂载");
  });
});
