import Revision from "@server/models/Revision"; import { buildCollection, buildDocument } from "@server/test/factories"; import { ChangesetHelper } from "@shared/editor/lib/ChangesetHelper"; import { EditorStyleHelper } from "@shared/editor/styles/EditorStyleHelper"; import type { ProsemirrorData } from "@shared/types"; import { HeadingPrefixStyle, TextEditMode } from "@shared/types"; import { DocumentHelper } from "./DocumentHelper"; import { ProsemirrorHelper } from "./ProsemirrorHelper"; describe("DocumentHelper", () => { beforeAll(() => { // Fake only Date (for a deterministic system time); leave real timers so // toHTML's React effect flush, which awaits setTimeout, is not deadlocked. vi.useFakeTimers({ toFake: ["Date"] }); vi.setSystemTime(Date.parse("2021-01-01T00:00:00.000Z")); }); afterAll(() => { vi.useRealTimers(); }); describe("replaceInternalUrls", () => { it("should replace internal urls", async () => { const document = await buildDocument({ text: `[link](/doc/internal-123)`, }); const result = await DocumentHelper.toJSON(document, { internalUrlBase: "/s/share-123", }); expect(result).toEqual({ content: [ { content: [ { marks: [ { attrs: { href: "/s/share-123/doc/internal-123", title: null, }, type: "link", }, ], text: "link", type: "text", }, ], type: "paragraph", }, ], type: "doc", }); }); it("should not duplicate share path for URLs that already contain it", async () => { const document = await buildDocument({ text: `[link](/s/testbugpage001/doc/test-page-2-2xIDEXYlib)`, }); const result = await DocumentHelper.toJSON(document, { internalUrlBase: "/s/testbugpage001", }); expect(result).toEqual({ content: [ { content: [ { marks: [ { attrs: { href: "/s/testbugpage001/doc/test-page-2-2xIDEXYlib", title: null, }, type: "link", }, ], text: "link", type: "text", }, ], type: "paragraph", }, ], type: "doc", }); }); }); describe("toProsemirror", () => { const linkContent = () => ({ type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "link", marks: [{ type: "link", attrs: { href: "/doc/internal-123" } }], }, ], }, ], }); it("should memoize the node for the same content", async () => { const document = await buildDocument({ content: linkContent() }); const node = DocumentHelper.toProsemirror(document); expect(DocumentHelper.toProsemirror(document)).toBe(node); }); it("should parse again when the content is replaced", async () => { const document = await buildDocument({ content: linkContent() }); const node = DocumentHelper.toProsemirror(document); document.content = linkContent(); expect(DocumentHelper.toProsemirror(document)).not.toBe(node); }); it("should not be changed by transforms of its JSON", async () => { const document = await buildDocument({ content: linkContent() }); const node = DocumentHelper.toProsemirror(document); ProsemirrorHelper.replaceInternalUrls(node, "/s/share-123"); ProsemirrorHelper.replaceDocumentReferences( node, new Map([["internal-123", { id: "other", path: "/doc/other" }]]) ); const text = DocumentHelper.toProsemirror(document).firstChild?.firstChild; expect(text?.marks[0].attrs.href).toBe("/doc/internal-123"); }); }); describe("toJSON", () => { it("should return content directly if no transformation required", async () => { const document = await buildDocument(); const result = await DocumentHelper.toJSON(document); expect(result === document.content).toBe(true); }); it("should remove marks and replace internal urls together", async () => { const document = await buildDocument({ text: "link", content: { type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "link", marks: [ { type: "comment", attrs: { id: "comment-123", userId: "user-123" }, }, { type: "link", attrs: { href: "/doc/internal-123" } }, ], }, ], }, ], }, }); const result = await DocumentHelper.toJSON(document, { removeMarks: ["comment"], internalUrlBase: "/s/share-123", }); const marks = result.content?.[0].content?.[0].marks; expect(marks?.map((mark) => mark.type)).toEqual(["link"]); expect(marks?.[0].attrs?.href).toBe("/s/share-123/doc/internal-123"); }); }); describe("toStaticHTML", () => { it("should remove comment marks", async () => { const document = await buildDocument({ text: "hello", content: { type: "doc", content: [ { type: "paragraph", content: [ { type: "text", text: "hello", marks: [ { type: "comment", attrs: { id: "comment-123", userId: "user-123" }, }, ], }, ], }, ], }, }); const html = await DocumentHelper.toStaticHTML(document); expect(html).toContain("hello"); expect(html).not.toContain("comment-123"); expect(html).not.toContain("user-123"); }); }); describe("toHTML", () => { it("should return html", async () => { const document = await buildDocument({ text: "This is a test paragraph", }); const result = await DocumentHelper.toHTML(document, { includeTitle: false, includeStyles: false, }); expect(result).toContain('
This is a test paragraph
'); }); it("should apply the cspNonce to the injected mermaid script", async () => { const document = await buildDocument({ text: "```mermaid\ngraph TD;\nA-->B;\n```", }); const result = await DocumentHelper.toHTML(document, { includeTitle: false, includeStyles: false, includeMermaid: true, cspNonce: "test-nonce-123", }); expect(result).toMatch(/