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 { DocumentHelper } from "./DocumentHelper"; describe("DocumentHelper", () => { beforeAll(() => { vi.useFakeTimers(); 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("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); }); }); 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(/