import { describe, expect, it } from "vitest";
import { buildDocumentSavePayload } from "@/lib/documents/save-contract";

describe("buildDocumentSavePayload", () => {
  it("优先规范 editorDocument 并从它派生 legacy content", () => {
    const payload = buildDocumentSavePayload({
      documentId: "  doc_1  ",
      workspaceId: "  ws_1  ",
      revision: 3,
      editorDocument: {
        documentId: "other_doc",
        rootBlockIds: ["block_1"],
        blocks: [
          {
            blockId: "block_1",
            blockType: "paragraph",
            contentNodes: [{ type: "text", text: "新正文" }],
            childBlockIds: [],
          },
        ],
      },
      content: [{ id: "legacy_1", type: "paragraph", content: "旧正文" }],
      conflictDetectionKey: "  conflict_1  ",
    });

    expect(payload).toEqual({
      documentId: "doc_1",
      workspaceId: "ws_1",
      revision: 3,
      editorDocument: {
        documentId: "doc_1",
        rootBlockIds: ["block_1"],
        blocks: [
          {
            blockId: "block_1",
            blockType: "paragraph",
            props: {},
            contentNodes: [{ type: "text", text: "新正文", marks: [] }],
            childBlockIds: [],
          },
        ],
      },
      content: [{ id: "block_1", type: "paragraph", props: undefined, content: "新正文" }],
      tiptapDocument: null,
      conflictDetectionKey: "conflict_1",
      snapshotCapturedAt: null,
      blockCount: null,
    });
  });

  it("兼容仅 content 的旧 payload，并补齐 editorDocument", () => {
    const payload = buildDocumentSavePayload({
      documentId: "doc_1",
      workspaceId: "",
      content: [{ id: "heading_1", type: "heading", props: { level: 2 }, content: "章节标题" }],
    });

    expect(payload).toEqual({
      documentId: "doc_1",
      workspaceId: null,
      revision: null,
      editorDocument: {
        documentId: "doc_1",
        rootBlockIds: ["heading_1"],
        blocks: [
          {
            blockId: "heading_1",
            blockType: "heading",
            props: { headingLevel: 2 },
            contentNodes: [{ type: "text", text: "章节标题", marks: [] }],
            childBlockIds: [],
          },
        ],
      },
      content: [{ id: "heading_1", type: "heading", props: { level: 2 }, content: "章节标题" }],
      tiptapDocument: null,
      conflictDetectionKey: null,
      snapshotCapturedAt: null,
      blockCount: null,
    });
  });

  it("支持将 tiptapDocument 适配为 editorDocument 与兼容 content", () => {
    const payload = buildDocumentSavePayload({
      documentId: "doc_1",
      workspaceId: "ws_1",
      tiptapDocument: {
        type: "doc",
        content: [
          {
            type: "paragraph",
            attrs: { blockId: "p_1" },
            content: [{ type: "text", text: "来自 tiptap" }],
          },
        ],
      },
    });

    expect(payload).toEqual({
      documentId: "doc_1",
      workspaceId: "ws_1",
      revision: null,
      editorDocument: {
        documentId: "doc_1",
        rootBlockIds: ["p_1"],
        blocks: [
          {
            blockId: "p_1",
            blockType: "paragraph",
            props: {},
            contentNodes: [{ type: "text", text: "来自 tiptap", marks: [] }],
            childBlockIds: [],
          },
        ],
      },
      content: [{ id: "p_1", type: "paragraph", props: undefined, content: "来自 tiptap" }],
      tiptapDocument: {
        type: "doc",
        content: [
          {
            type: "paragraph",
            attrs: { blockId: "p_1" },
            content: [{ type: "text", text: "来自 tiptap" }],
          },
        ],
      },
      conflictDetectionKey: null,
      snapshotCapturedAt: null,
      blockCount: null,
    });
  });

  it("保留正文快照采集元数据", () => {
    const payload = buildDocumentSavePayload({
      documentId: "doc_1",
      workspaceId: "ws_1",
      revision: 4,
      content: [{ id: "block_1" }],
      conflictDetectionKey: "doc_1:4",
      snapshotCapturedAt: "2026-04-14T14:30:00.000Z",
      blockCount: 1,
    });

    expect(payload.revision).toBe(4);
    expect(payload.conflictDetectionKey).toBe("doc_1:4");
    expect(payload.snapshotCapturedAt).toBe("2026-04-14T14:30:00.000Z");
    expect(payload.blockCount).toBe(1);
  });
});
