import { describe, expect, it } from "vitest";
import { blocksToPlainText, plainTextToBlocksV1, sanitizeMessageBlocks } from "./blocks";

describe("rich-text blocks", () => {
  it("preserves paragraph and hard-break boundaries in plain text", () => {
    const blocks = sanitizeMessageBlocks({ v: 1, doc: { type: "doc", content: [
      { type: "paragraph", content: [{ type: "text", text: "Hello" }, { type: "hardBreak" }, { type: "text", text: "there" }] },
      { type: "blockquote", content: [{ type: "paragraph", content: [{ type: "text", text: "Quoted" }] }] },
    ] } });
    expect(blocksToPlainText(blocks)).toBe("Hello\nthere\nQuoted");
  });

  it("drops unsafe nodes, attributes and links", () => {
    const blocks = sanitizeMessageBlocks({ v: 1, dangerous: true, doc: { type: "doc", content: [{
      type: "paragraph", onclick: "bad()", content: [
        { type: "text", text: "safe", marks: [{ type: "bold" }, { type: "link", attrs: { href: "javascript:bad()" } }] },
        { type: "image", attrs: { src: "bad" } },
      ],
    }] } });
    expect(blocks).toEqual({ v: 1, doc: { type: "doc", content: [{ type: "paragraph", content: [{ type: "text", text: "safe", marks: [{ type: "bold" }] }] }] } });
  });

  it("creates one paragraph per plain-text line", () => {
    expect(blocksToPlainText(plainTextToBlocksV1("one\ntwo"))).toBe("one\ntwo");
  });
});
