import { describe, expect, it } from "vitest";
import { buildPageAggregateFromDocumentPayloads } from "@/lib/documents/page-aggregate-builder";

describe("page-aggregate-builder", () => {
  it("会把 documents meta 与 content 收口为统一 page aggregate", () => {
    const aggregate = buildPageAggregateFromDocumentPayloads({
      meta: {
        id: "doc_1",
        workspace_id: "ws_1",
        title: " 页面标题 ",
        updated_at: "2026-04-22T12:00:00.000Z",
        can_edit: false,
        disable_download: true,
        disable_copy: false,
        wide_layout: true,
        use_small_text: true,
        show_heading_numbers: false,
        show_toc: true,
        show_structure: false,
        protect_editing: false,
        show_word_count: true,
        collapse_backlinks: true,
        page_font: "default",
        layout_density: "compact",
        hide_child_pages: true,
        show_block_ref_count: true,
        embed_default_block_id: "block_1",
        word_count: 20,
        character_count: 40,
        block_count: 2,
        todo_total: 3,
        todo_done: 1,
      },
      contentPayload: {
        content: [{ id: "block_1", type: "paragraph", content: [] }],
        revision: 8,
        conflict_detection_key: "doc_1:8",
        page_subtree: null,
      },
    });

    expect(aggregate.identity).toEqual({
      documentId: "doc_1",
      workspaceId: "ws_1",
    });
    expect(aggregate.head).toEqual({
      title: "页面标题",
      updatedAt: "2026-04-22T12:00:00.000Z",
      permissions: {
        readOnly: true,
        disableDownload: true,
        disableCopy: false,
      },
    });
    expect(aggregate.layout.pageOptions).toMatchObject({
      wideLayout: true,
      smallText: true,
      showHeadingNumbers: false,
      showToc: true,
      collapseBacklinks: true,
      layoutDensity: "compact",
      hideChildPages: true,
      showBlockRefCount: true,
      embedDefaultBlockId: "block_1",
    });
    expect(aggregate.body).toEqual({
      content: [{ id: "block_1", type: "paragraph", content: [] }],
      revision: 8,
      conflictDetectionKey: "doc_1:8",
    });
    expect(aggregate.stats).toEqual({
      wordCount: 20,
      characterCount: 40,
      blockCount: 2,
      todoTotal: 3,
      todoDone: 1,
    });
  });
});
