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

const documentApi = readFileSync(new URL("../documentApi.ts", import.meta.url), "utf8");
const attachmentApi = readFileSync(new URL("../../attachments/attachmentApi.ts", import.meta.url), "utf8");
const documentState = readFileSync(new URL("../documentState.ts", import.meta.url), "utf8");
const attachmentState = readFileSync(new URL("../../attachments/attachmentState.ts", import.meta.url), "utf8");
const app = readFileSync(new URL("../../../app/App.tsx", import.meta.url), "utf8");

describe("F4 read-only scope", () => {
  it("仅封装七个批准的 GET 路径", () => {
    for (const marker of [
      '"documents"',
      "`documents/${id}`",
      "`main-plans/${mainPlanId}/sub-plans`",
      '"attachments"',
      "`attachments/${id}`",
      "`attachments/${id}/main-plans`",
      "`main-plans/${mainPlanId}/attachments`",
    ]) expect(documentApi + attachmentApi).toContain(marker);
    expect(documentApi + attachmentApi).not.toMatch(/\.(post|put|delete)\(/);
  });

  it("正向与反向挂载 DTO 明确分开", () => {
    expect(attachmentApi).toContain("isMainPlanBriefResult");
    expect(attachmentApi).toContain("isAttachmentBindingSummary");
    expect(attachmentApi).not.toContain("bindingId =");
  });

  it("F4真实页面继续运行且后续阶段路由保留角色门禁", () => {
    expect(app).toContain('page === "main" && <RealMainPage');
    expect(app).toContain(
      'page === "admin" && currentUser.roleCode === RoleCode.ADMIN',
    );
    expect(app).toContain("<RealAdminPage");
    expect(app).toContain("<AuditDashboard />");
  });

  it("列表状态具备 300ms 防抖、AbortController 和旧响应保护", () => {
    expect(documentState + attachmentState).toContain("new AbortController()");
    expect(documentState + attachmentState).toContain("window.setTimeout");
    expect(documentState).toContain("requestVersion");
    expect(documentState + attachmentState).toContain("controller.abort()");
  });
});
