import { describe, expect, test } from "bun:test"; import { buildHtmlFilename, buildStandaloneHtmlDocument, createEmptyHtmlImageEmbedResult, createHtmlFile, escapeHtml, getHtmlImageEmbedNoticeKind, } from "../apps/web/src/lib/note-html-export"; describe("single-note HTML export", () => { test("creates a UTF-8 HTML file with the current content", async () => { const file = createHtmlFile( "

当前未保存内容

", "项目 / 计划", "Untitled note", ); expect(file.filename).toBe("项目 - 计划.html"); expect(file.blob.type).toBe("text/html;charset=utf-8"); expect(await file.blob.text()).toContain("当前未保存内容"); }); test("sanitizes unsafe and reserved filenames", () => { expect(buildHtmlFilename("CON", "Untitled note")).toBe("_CON.html"); expect(buildHtmlFilename('<>:"/\\|?*', "Untitled note")).toBe("---------.html"); expect(buildHtmlFilename("...", "Untitled note")).toBe("Untitled note.html"); expect(buildHtmlFilename("README.html", "Untitled note")).toBe("README.html"); expect(buildHtmlFilename("notes.htm", "Untitled note")).toBe("notes.html"); }); test("escapes HTML special characters", () => { expect(escapeHtml(`&'`)).toBe( "<script>alert("x")</script>&'", ); }); test("builds a standalone HTML document with metadata and styles", () => { const html = buildStandaloneHtmlDocument({ title: 'Release & "docs"', notebook: "Product", tags: ["ship", "html"], updatedAt: "2026-08-11 12:00", language: "zh-CN", bodyHtml: "

Hello world

", styles: "body { color: #111; }", }); expect(html).toContain(""); expect(html).toContain('lang="zh-CN"'); expect(html).toContain("Release <notes> & "docs""); expect(html).toContain('

Release <notes> & "docs"

'); expect(html).toContain("Product · 2026-08-11 12:00 · #ship · #html"); expect(html).toContain("

Hello world

"); expect(html).toContain("body { color: #111; }"); expect(html).toContain('meta name="generator" content="EdgeEver"'); }); test("omits empty metadata row", () => { const html = buildStandaloneHtmlDocument({ title: "Untitled", bodyHtml: "

body

", }); expect(html).not.toContain("edgeever-html-meta"); expect(html).toContain("

body

"); }); test("only warns about image embedding when some or all images fail", () => { expect(getHtmlImageEmbedNoticeKind(createEmptyHtmlImageEmbedResult())).toBe("none"); expect(getHtmlImageEmbedNoticeKind({ total: 3, embedded: 3, failed: 0 })).toBe("none"); expect(getHtmlImageEmbedNoticeKind({ total: 3, embedded: 2, failed: 1 })).toBe("partial"); expect(getHtmlImageEmbedNoticeKind({ total: 2, embedded: 0, failed: 2 })).toBe("failed-all"); }); });