import { describe, expect, it } from "vitest";
import { extractDataUriImages } from "./imap";
const TINY_PNG_B64 = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
describe("extractDataUriImages", () => {
it("extracts a data:image src into a real attachment and rewrites the HTML to reference it", async () => {
const html = `
see
`;
const { html: out, attachments } = await extractDataUriImages(html);
expect(attachments).toHaveLength(1);
expect(attachments[0].mimeType).toBe("image/png");
expect(attachments[0].isInline).toBe(true);
expect(attachments[0].contentId).toBeNull();
expect(out).toContain(`/api/attachments/${attachments[0].id}`);
expect(out).not.toContain("base64");
});
it("dedupes the same image embedded twice into a single attachment", async () => {
const src = `data:image/png;base64,${TINY_PNG_B64}`;
const html = `
middle
`;
const { html: out, attachments } = await extractDataUriImages(html);
expect(attachments).toHaveLength(1);
const occurrences = out.split(`/api/attachments/${attachments[0].id}`).length - 1;
expect(occurrences).toBe(2);
});
it("leaves HTML with no data: images untouched", async () => {
const html = 'hi
';
const { html: out, attachments } = await extractDataUriImages(html);
expect(attachments).toHaveLength(0);
expect(out).toBe(html);
});
});