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

const app = readFileSync(new URL("../../../app/App.tsx", import.meta.url), "utf8");
const modal = readFileSync(new URL("../MutationModals.tsx", import.meta.url), "utf8");
const documentApi = readFileSync(new URL("../documentMutationApi.ts", import.meta.url), "utf8");
const attachmentApi = readFileSync(new URL("../attachmentMutationApi.ts", import.meta.url), "utf8");
const fileApi = readFileSync(new URL("../../files/fileApi.ts", import.meta.url), "utf8");
const sources = documentApi + attachmentApi + fileApi;

describe("F5 scope and UI capabilities", () => {
  it("只包含10个B5路径，不包含恢复、挂载写、权限或附件专用文件路径", () => {
    for (const marker of [
      '"documents"',
      '"documents/batch-import"',
      "`documents/${id}`",
      '"attachments"',
      '"attachments/batch-import"',
      "`attachments/${id}`",
      "`documents/${id}/preview`",
      "`documents/${id}/download`",
    ]) expect(sources).toContain(marker);
    for (const forbidden of [
      "/restore",
      "/bind",
      "/permissions",
      "`attachments/${id}/preview`",
      "`attachments/${id}/download`",
    ]) expect(sources).not.toContain(forbidden);
  });

  it("上传支持multiple、拖放、删除待上传项和失败项重试", () => {
    expect(modal).toContain("multiple");
    expect(modal).toContain("onDrop=");
    expect(modal).toContain("移除");
    expect(modal).toContain("仅保留失败项重试");
  });

  it("页面接入真实上传、编辑、删除、预览和下载组件", () => {
    for (const marker of [
      "<SingleUploadModal",
      "<F5BatchImportModal",
      "<EditMetadataModal",
      "<DeleteDocumentModal",
      "<FilePreviewModal",
      "<DownloadButton",
    ]) expect(app).toContain(marker);
    expect(app).not.toContain("下载（F5）");
  });

  it("不提供恢复、强制删除或Office伪预览入口", () => {
    expect(modal.match(/await attachmentMutationApi\.remove/g)).toHaveLength(1);
    expect(modal).toContain("await loadLatest(true)");
    expect(app + modal).not.toContain("强制删除</button>");
    expect(modal).not.toContain("本期不提供恢复功能");
    expect(modal).not.toContain("ATTACHMENT_IN_USE；前端不会");
  });
});
