import { describe, expect, it } from "vitest";
import { htmlDeclaresColors, sanitizeEditorHtml, sanitizeEmailHtml } from "../html";
describe("sanitizeEmailHtml", () => {
it("removes scripts and event handlers", () => {
const r = sanitizeEmailHtml('
hi
');
expect(r.html).not.toContain("script");
expect(r.html).not.toContain("onclick");
expect(r.html).not.toContain("iframe");
});
it("blocks remote images until allowed and maps cid", () => {
const src = '

x
';
const blocked = sanitizeEmailHtml(src, { cidMap: { "logo@x": "/api/blob/a/b/logo.png" } });
expect(blocked.remoteCount).toBe(2);
expect(blocked.html).toContain('data-ihm-blocked="1"');
expect(blocked.html).toContain("/api/blob/a/b/logo.png");
expect(blocked.html).not.toMatch(/src="https:\/\/t\.example/);
expect(blocked.html).not.toContain("url(https://t.example");
const allowed = sanitizeEmailHtml(src, { allowRemote: true, proxyRemote: true });
expect(allowed.html).toContain("/api/image?url=https%3A%2F%2Ft.example%2Fp.gif");
});
it("forces links to open in new tabs", () => {
const r = sanitizeEmailHtml('x');
expect(r.html).toContain('target="_blank"');
expect(r.html).toContain("noopener");
});
it("strips javascript: urls", () => {
const r = sanitizeEmailHtml('x');
expect(r.html).not.toContain("javascript:");
});
it("editor sanitizer keeps basic formatting", () => {
expect(sanitizeEditorHtml("x")).toBe("x");
});
});
describe("htmlDeclaresColors", () => {
it("is false for mail that brings no colours", () => {
expect(htmlDeclaresColors("Hi there
")).toBe(false);
expect(htmlDeclaresColors("bold and italic
", "font-family:Arial")).toBe(false);
expect(htmlDeclaresColors('link')).toBe(false);
expect(htmlDeclaresColors('x
')).toBe(false);
});
it("is true when the message paints itself", () => {
expect(htmlDeclaresColors('x | ')).toBe(true);
expect(htmlDeclaresColors('x')).toBe(true);
expect(htmlDeclaresColors('x
')).toBe(true);
expect(htmlDeclaresColors('x
')).toBe(true);
expect(htmlDeclaresColors("x
")).toBe(true);
expect(htmlDeclaresColors("plain
", "background:#eee")).toBe(true);
});
});