import { describe, expect, it } from "vitest"; import { sanitizeTrainingHtml, trainingBodyToHtml } from "./rich-text"; describe("sanitizeTrainingHtml", () => { it("keeps editor formatting and removes scripts and event handlers", () => { const html = sanitizeTrainingHtml( '

Gain staging

Set gain before the service.

', ); expect(html).toContain("

Gain staging

"); expect(html).toContain("gain"); expect(html).toContain("the service"); expect(html).not.toContain("onclick"); expect(html).not.toContain(" { const allowed = sanitizeTrainingHtml( 'Board', ); expect(allowed).toContain('src="/api/training/files/abc"'); expect(allowed).toContain('alt="Board"'); expect(allowed).toContain('width="50%"'); expect(allowed).toContain('data-align="right"'); expect(allowed).not.toContain("onerror"); const dataUrl = sanitizeTrainingHtml( 'pasted', ); expect(dataUrl).not.toContain(" { const external = sanitizeTrainingHtml( 'Manual', ); expect(external).toContain('target="_blank"'); expect(external).toContain('rel="noopener noreferrer nofollow"'); const internal = sanitizeTrainingHtml('Training'); expect(internal).toContain('href="/training"'); expect(internal).not.toContain('target="_blank"'); }); }); describe("trainingBodyToHtml", () => { it("turns legacy plain text into paragraphs", () => { expect(trainingBodyToHtml("Step one\nStep two")).toBe( "

Step one

Step two

", ); }); it("escapes legacy text instead of interpreting it as markup", () => { expect(trainingBodyToHtml("Use carefully")).toBe( "

Use <b> carefully

", ); }); it("sanitizes already-authored HTML", () => { expect(trainingBodyToHtml("

Hello

")).toBe( "

Hello

", ); }); });