import { describe, expect, it } from "vitest";
import {
  isRecycleBinPage,
  isRestoreDocumentResult,
} from "../recycleBinTypes";
import {
  recycleItem,
  restoredDocument,
  restoreResult,
} from "./fixtures";

const page = {
  items: [recycleItem],
  page: 1,
  pageSize: 20,
  total: 1,
  totalPages: 1,
};

describe("F8 strict DTO guards", () => {
  it("接受字符串ID、UTC删除时间和完整deletedBy", () => {
    expect(isRecycleBinPage(page)).toBe(true);
  });

  it("接受deletedBy及其字段为null", () => {
    expect(
      isRecycleBinPage({
        ...page,
        items: [
          {
            ...recycleItem,
            categoryId: null,
            categoryName: null,
            updatedBy: null,
            updatedByName: null,
            deletedBy: null,
          },
          {
            ...recycleItem,
            deletedBy: {
              userId: null,
              username: null,
              realName: null,
              organizationName: null,
            },
          },
        ],
        total: 2,
      }),
    ).toBe(true);
  });

  it.each([
    { ...recycleItem, id: 10 },
    { ...recycleItem, deletedAt: "2026-07-26 11:58:52" },
    { ...recycleItem, documentType: "FILE" },
    { ...recycleItem, securityLevel: "PRIVATE" },
    { ...recycleItem, documentStatus: "DELETED" },
    { ...recycleItem, rowVersion: 1.5 },
    { ...recycleItem, deleted_at: recycleItem.deletedAt },
    { ...recycleItem, fileRelativePath: "original/a.docx" },
  ])("拒绝number ID、非UTC、未知枚举、snake_case和未知字段", (item) => {
    expect(isRecycleBinPage({ ...page, items: [item] })).toBe(false);
  });

  it("严格校验分页", () => {
    expect(isRecycleBinPage({ ...page, page: 0 })).toBe(false);
    expect(isRecycleBinPage({ ...page, pageSize: 101 })).toBe(false);
    expect(isRecycleBinPage({ ...page, total: -1 })).toBe(false);
    expect(isRecycleBinPage({ ...page, unknown: true })).toBe(false);
  });

  it("接受不含fileHash的专用恢复文档及四个计数", () => {
    expect(isRestoreDocumentResult(restoreResult)).toBe(true);
    expect("fileHash" in restoreResult.document).toBe(false);
  });

  it("共享附件恢复结果允许正式mountedPlanCount", () => {
    expect(
      isRestoreDocumentResult({
        ...restoreResult,
        document: {
          ...restoredDocument,
          documentType: "ATTACHMENT",
          securityLevel: "PUBLIC",
          visibilityType: "ALL_AUTHENTICATED",
          categoryId: null,
          categoryName: null,
          categoryPath: null,
          attachmentType: "OTHER",
          mountedPlanCount: 0,
        },
      }),
    ).toBe(true);
  });

  it.each([
    { ...restoreResult, restoredPermissionCount: -1 },
    { ...restoreResult, skippedPermissionCount: 0.5 },
    { ...restoreResult, unknownCount: 0 },
    {
      ...restoreResult,
      document: { ...restoredDocument, fileHash: "a".repeat(64) },
    },
    {
      ...restoreResult,
      document: { ...restoredDocument, id: 10 },
    },
    {
      ...restoreResult,
      document: { ...restoredDocument, updatedAt: "not-utc" },
    },
  ])("拒绝非法恢复计数和未知或错误文档字段", (value) => {
    expect(isRestoreDocumentResult(value)).toBe(false);
  });
});

