import { describe, expect, it, vi } from "vitest";
import { createStatisticsApi } from "../statisticsApi";
import { isDocumentStatistics } from "../statisticsTypes";

const statistics = {
  planDocumentCount: 6,
  mainPlanCount: 3,
  subPlanCount: 3,
  attachmentCount: 3,
  todayViewerCount: 2,
};
const result = (data: unknown) => ({
  data,
  message: "success",
  requestId: "2110a47a-f0bc-443b-9a41-088cafcdfc92",
  httpStatus: 200,
  headers: new Headers(),
});

describe("document statistics", () => {
  it("GET statistics/documents且不重复拼接/api/v1", async () => {
    const client = { get: vi.fn().mockResolvedValue(result(statistics)) };
    const signal = new AbortController().signal;
    await expect(
      createStatisticsApi(client).documents(signal),
    ).resolves.toEqual(statistics);
    expect(client.get).toHaveBeenCalledWith("statistics/documents", {
      signal,
    });
  });

  it("严格拒绝负数、小数、snake_case和未知字段", () => {
    expect(isDocumentStatistics(statistics)).toBe(true);
    expect(
      isDocumentStatistics({ ...statistics, mainPlanCount: -1 }),
    ).toBe(false);
    expect(
      isDocumentStatistics({ ...statistics, todayViewerCount: 1.5 }),
    ).toBe(false);
    expect(
      isDocumentStatistics({
        ...statistics,
        plan_document_count: statistics.planDocumentCount,
      }),
    ).toBe(false);
    expect(
      isDocumentStatistics({ ...statistics, unknownCount: 0 }),
    ).toBe(false);
  });

  it("DTO错误保留响应requestId", async () => {
    const client = {
      get: vi.fn().mockResolvedValue(
        result({ ...statistics, attachmentCount: "3" }),
      ),
    };
    await expect(
      createStatisticsApi(client).documents(),
    ).rejects.toMatchObject({
      code: "INTERNAL_ERROR",
      requestId: "2110a47a-f0bc-443b-9a41-088cafcdfc92",
    });
  });
});

