export-html-whitespace.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import type { Component } from "@earendil-works/pi-tui";
  2. import { readFileSync } from "fs";
  3. import { describe, expect, it } from "vitest";
  4. import { ansiLinesToHtml } from "../src/core/export-html/ansi-to-html.ts";
  5. import { createToolHtmlRenderer } from "../src/core/export-html/tool-renderer.ts";
  6. import type { ToolDefinition } from "../src/core/extensions/types.ts";
  7. import type { Theme } from "../src/modes/interactive/theme/theme.ts";
  8. describe("export HTML tool output whitespace", () => {
  9. it("preserves whitespace for plain-text tool output lines without preserving template whitespace", () => {
  10. const css = readFileSync(new URL("../src/core/export-html/template.css", import.meta.url), "utf-8");
  11. expect(css).toMatch(
  12. /\.output-preview > div:not\(\.expand-hint\),\s*\.output-full > div:not\(\.expand-hint\) \{[\s\S]*?white-space:\s*pre-wrap;/,
  13. );
  14. expect(css).toMatch(/\.ansi-line\s*\{[\s\S]*?white-space:\s*pre;/);
  15. expect(css).not.toMatch(/\.output-preview,\s*\.output-full\s*\{[\s\S]*?white-space:\s*pre-wrap;/);
  16. });
  17. it("does not insert source whitespace between ANSI-rendered lines", () => {
  18. expect(ansiLinesToHtml(["one", "two"])).toBe('<div class="ansi-line">one</div><div class="ansi-line">two</div>');
  19. });
  20. it("trims TUI spacing lines from custom tool result HTML", () => {
  21. const component: Component = { render: () => ["", "\u001b[31mone\u001b[0m", "two", ""], invalidate: () => {} };
  22. const tool = {
  23. name: "custom",
  24. label: "custom",
  25. description: "custom",
  26. renderResult: () => component,
  27. } as unknown as ToolDefinition;
  28. const renderer = createToolHtmlRenderer({
  29. getToolDefinition: () => tool,
  30. theme: {} as Theme,
  31. cwd: "/tmp",
  32. });
  33. expect(renderer.renderResult("id", "custom", [], undefined, false)?.expanded).toBe(
  34. '<div class="ansi-line"><span style="color:#800000">one</span></div><div class="ansi-line">two</div>',
  35. );
  36. });
  37. });