import { describe, test, expect, beforeAll } from "vitest" import { Herb } from "@herb-tools/node-wasm" import { IdentityPrinter } from "../src/index.js" describe("IdentityPrinter", () => { beforeAll(async () => { await Herb.load() }) function expectPrintRoundTrip(input: string, description: string) { test(description, () => { const parseResult = Herb.parse(input, { track_whitespace: true }) expect(parseResult.value).toBeTruthy() const printer = new IdentityPrinter() const output = printer.print(parseResult.value!) expect(output).toBe(input) }) } describe("Basic functionality", () => { test("is defined", () => { expect(IdentityPrinter).toBeDefined() }) test("can be instantiated", () => { const printer = new IdentityPrinter() expect(printer).toBeInstanceOf(IdentityPrinter) }) test("static print method works", () => { const input = '
Hello World
' const parseResult = Herb.parse(input, { track_whitespace: true }) expect(parseResult.value).toBeTruthy() const output = IdentityPrinter.print(parseResult.value!) expect(output).toBe(input) }) test("static print method produces same output as instance method", () => { const input = '

Test paragraph

' const parseResult = Herb.parse(input, { track_whitespace: true }) expect(parseResult.value).toBeTruthy() const printer = new IdentityPrinter() const instanceOutput = printer.print(parseResult.value!) const staticOutput = IdentityPrinter.print(parseResult.value!) expect(staticOutput).toBe(instanceOutput) expect(staticOutput).toBe(input) }) test("static print method respects options", () => { const input = '
Error' const parseResult = Herb.parse(input, { track_whitespace: true }) expect(() => IdentityPrinter.print(parseResult.value!)).toThrow() const output = IdentityPrinter.print(parseResult.value!, { ignoreErrors: true }) expect(output).toBe(input) }) }) describe("HTML Structure Round-trip Verification", () => { expectPrintRoundTrip('
Hello World
', 'simple div with text') expectPrintRoundTrip('

Simple paragraph

', 'simple paragraph') expectPrintRoundTrip('Inline text', 'simple span') expectPrintRoundTrip('

Nested content

', 'nested elements') expectPrintRoundTrip('

Deep nesting

', 'deeply nested elements') expectPrintRoundTrip('', 'list with items') expectPrintRoundTrip('', 'self-closing tag with attributes') expectPrintRoundTrip('
', 'self-closing tag without attributes') expectPrintRoundTrip('Test', 'img tag with attributes') expectPrintRoundTrip('
Content
', 'element with multiple attributes') expectPrintRoundTrip('', 'input with various attributes') expectPrintRoundTrip('Link', 'anchor with attributes') }) describe("Whitespace Preservation", () => { expectPrintRoundTrip('
Hello World
', 'multiple spaces in text') expectPrintRoundTrip('
\n Hello\n World\n
', 'newlines and indentation') expectPrintRoundTrip('
\t\tTab indented\t\t
', 'tab characters') expectPrintRoundTrip('
  Preformatted\n  Text  
', 'preformatted whitespace') expectPrintRoundTrip(' const x = 5; ', 'code with spaces') expectPrintRoundTrip('
', 'element with only whitespace') expectPrintRoundTrip('
Content
', 'whitespace around elements') }) describe("HTML Comments and Special Elements", () => { expectPrintRoundTrip('', 'HTML comment') expectPrintRoundTrip('
', 'comment inside element') expectPrintRoundTrip('', 'DOCTYPE declaration') expectPrintRoundTrip('', 'DOCTYPE with document structure') }) describe("Empty and Edge Cases", () => { expectPrintRoundTrip('', 'empty input') expectPrintRoundTrip('
', 'empty div') expectPrintRoundTrip('

', 'empty paragraph') expectPrintRoundTrip('
', 'multiple empty elements') expectPrintRoundTrip('
\n
', 'element with only newline') expectPrintRoundTrip(' \n \t ', 'only whitespace') }) })