import assert from "node:assert/strict"; import { createRequire } from "node:module"; import * as path from "node:path"; import { fileURLToPath } from "node:url"; import { describe, test } from "vitest"; const __dirname = path.dirname(fileURLToPath(import.meta.url)); const require = createRequire(import.meta.url); const addonDir = path.resolve( __dirname, "..", "..", "..", "..", "rust-engine", "addon", ); const platformTag = `${process.platform}-${process.arch}`; const candidates = [ path.join(addonDir, `forge_engine.${platformTag}.node`), path.join(addonDir, "forge_engine.dev.node"), ]; let native; for (const candidate of candidates) { try { native = require(candidate); break; } catch { // try next } } if (!native) { console.error( "Native addon not found. Run `npm run build:native -w @singularity-forge/native` first.", ); process.exit(1); } describe("native html: htmlToMarkdown()", () => { test("converts basic HTML to markdown", () => { const html = "

Hello

World

"; const result = native.htmlToMarkdown(html); assert.ok(result.includes("Hello"), "Should contain heading text"); assert.ok(result.includes("World"), "Should contain paragraph text"); }); test("converts links to markdown links", () => { const html = '

Visit Example

'; const result = native.htmlToMarkdown(html); assert.ok( result.includes("[Example]"), "Should contain markdown link text", ); assert.ok( result.includes("(https://example.com)"), "Should contain markdown link URL", ); }); test("converts lists to markdown", () => { const html = ""; const result = native.htmlToMarkdown(html); assert.ok(result.includes("First"), "Should contain first item"); assert.ok(result.includes("Second"), "Should contain second item"); assert.ok(result.includes("Third"), "Should contain third item"); }); test("converts bold and italic", () => { const html = "

bold and italic

"; const result = native.htmlToMarkdown(html); assert.ok( result.includes("**bold**") || result.includes("__bold__"), "Should contain bold", ); assert.ok( result.includes("*italic*") || result.includes("_italic_"), "Should contain italic", ); }); test("handles empty HTML", () => { const result = native.htmlToMarkdown(""); assert.equal(typeof result, "string"); }); test("handles plain text", () => { const result = native.htmlToMarkdown("Just plain text"); assert.ok(result.includes("Just plain text"), "Should preserve plain text"); }); test("accepts skipImages option", () => { const html = '

Title

Content with photo image

'; const result = native.htmlToMarkdown(html, { skipImages: true }); assert.ok(result.includes("Title"), "Should contain heading"); assert.ok(result.includes("Content"), "Should contain paragraph text"); }); test("accepts cleanContent option", () => { const html = '

Article

Body text.

'; const result = native.htmlToMarkdown(html, { cleanContent: true }); assert.ok( result.includes("Article") || result.includes("Body text"), "Should contain main content", ); }); test("converts code blocks", () => { const html = "
const x = 1;
"; const result = native.htmlToMarkdown(html); assert.ok(result.includes("const x = 1;"), "Should contain code content"); }); test("converts complex nested HTML", () => { const html = '

Section

Text with bold link.

'; const result = native.htmlToMarkdown(html); assert.ok(result.includes("Section"), "Should contain heading"); assert.ok(result.includes("example.com"), "Should contain link"); assert.ok(result.includes("one"), "Should contain list items"); }); });