tui-markdown.test.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { MessageRow } from "../src/app-transcript-components.js";
  4. import { palette } from "../src/app-model.js";
  5. import { detectMarkdownRenderMode, MarkdownText } from "../src/app-markdown.js";
  6. import { markdownSyntaxRules } from "../src/app-markdown-theme.js";
  7. import { DARK_TUI_THEME } from "../src/theme/index.js";
  8. test("detects the best available markdown render mode", () => {
  9. assert.equal(
  10. detectMarkdownRenderMode({ code: class Code {}, markdown: class Markdown {} }),
  11. "markdown",
  12. );
  13. assert.equal(detectMarkdownRenderMode({ code: class Code {} }), "code");
  14. assert.equal(detectMarkdownRenderMode({}), "plain");
  15. });
  16. test("maps markdown theme tokens from the TUI palette", () => {
  17. const rules = markdownSyntaxRules(DARK_TUI_THEME);
  18. const heading = rules.find((rule) => rule.scope.includes("markup.heading"));
  19. const link = rules.find((rule) => rule.scope.includes("markup.link"));
  20. const inlineCode = rules.find((rule) => rule.scope.includes("markup.raw.inline"));
  21. assert.equal(heading?.style.bold, true);
  22. assert.equal(heading?.style.foreground, DARK_TUI_THEME.markdownHeading);
  23. assert.equal(link?.style.underline, true);
  24. assert.equal(inlineCode?.style.background, DARK_TUI_THEME.backgroundPanel);
  25. });
  26. test("uses OpenTUI native markdown when available", () => {
  27. const node = MarkdownText({
  28. content: "## Title\n\nThis is **bold** and `code`.",
  29. mode: "markdown",
  30. streaming: true,
  31. });
  32. assert.equal(node.type, "markdown");
  33. assert.equal(node.props.content, "## Title\n\nThis is **bold** and `code`.");
  34. assert.equal(node.props.conceal, true);
  35. assert.equal(node.props.streaming, true);
  36. assert.ok(node.props.syntaxStyle);
  37. });
  38. test("passes underscore-delimited text through to native markdown unchanged", () => {
  39. const sessionId = "sess_workflow_activity_26c91a65-2d5a-4f7b-9954-3ff7dbe92771";
  40. const node = MarkdownText({
  41. content: `- completed · import-audit · ${sessionId}\n_keep_ underscores`,
  42. mode: "markdown",
  43. });
  44. assert.equal(node.type, "markdown");
  45. assert.equal(node.props.content, `- completed · import-audit · ${sessionId}\n_keep_ underscores`);
  46. });
  47. test("falls markdown text back to code or plain text", () => {
  48. const codeNode = MarkdownText({
  49. content: "**bold**",
  50. mode: "code",
  51. });
  52. const plainNode = MarkdownText({
  53. content: "**bold**",
  54. mode: "plain",
  55. });
  56. assert.equal(codeNode.type, "code");
  57. assert.equal(codeNode.props.filetype, "markdown");
  58. assert.equal(codeNode.props.content, "**bold**");
  59. assert.equal(plainNode.type, "text");
  60. assert.deepEqual(collectTextLines(plainNode), ["**bold**"]);
  61. });
  62. test("message rows render assistant text as native markdown", () => {
  63. const node = MessageRow({
  64. index: 0,
  65. message: {
  66. content: "Use `npm test`.",
  67. role: "agent",
  68. streaming: true,
  69. },
  70. });
  71. const markdownNodes = collectElementsOfType(node, MarkdownText);
  72. assert.equal(markdownNodes.length, 1);
  73. assert.equal(markdownNodes[0]?.props.content, "Use `npm test`.");
  74. assert.equal(markdownNodes[0]?.props.streaming, true);
  75. });
  76. test("message rows hide speaker labels and only highlight user prompts", () => {
  77. const userRow = MessageRow({
  78. index: 0,
  79. message: {
  80. content: "Show `raw` markdown",
  81. role: "user",
  82. },
  83. });
  84. const agentRow = MessageRow({
  85. index: 1,
  86. message: {
  87. content: "Use `npm test`.",
  88. role: "agent",
  89. },
  90. });
  91. assert.equal(reactElementStyle(userRow).backgroundColor, palette.userMessageBackground);
  92. assert.equal(reactElementStyle(userRow).paddingTop, 1);
  93. assert.equal(reactElementStyle(userRow).paddingBottom, 1);
  94. assert.equal(reactElementStyle(agentRow).backgroundColor, palette.background);
  95. assert.equal(reactElementStyle(agentRow).paddingTop, 0);
  96. assert.equal(reactElementStyle(agentRow).paddingBottom, 0);
  97. assert.ok(
  98. collectTextNodes(userRow).some(
  99. (node) => node.text.includes("Show `raw` markdown") && node.style.fg === palette.text,
  100. ),
  101. );
  102. assert.equal(collectTextLines(userRow).some((line) => line.includes("User:")), false);
  103. assert.equal(collectTextLines(agentRow).some((line) => line.includes("Agent:")), false);
  104. });
  105. test("message rows keep user and explicit plain text parts unformatted", () => {
  106. const userRow = MessageRow({
  107. index: 0,
  108. message: {
  109. content: "Show `raw` markdown",
  110. role: "user",
  111. },
  112. });
  113. const partRow = MessageRow({
  114. index: 0,
  115. message: {
  116. content: "",
  117. parts: [{ format: "plain", text: "**plain**", type: "text" }],
  118. role: "agent",
  119. },
  120. });
  121. assert.equal(collectElementsOfType(userRow, MarkdownText).length, 0);
  122. assert.equal(collectElementsOfType(partRow, MarkdownText).length, 0);
  123. assert.ok(collectTextLines(userRow).some((line) => line.includes("Show `raw` markdown")));
  124. assert.ok(collectTextLines(partRow).includes("**plain**"));
  125. });
  126. function collectTextLines(node: unknown): string[] {
  127. const lines: string[] = [];
  128. const visit = (value: unknown) => {
  129. if (typeof value === "string") {
  130. lines.push(value);
  131. return;
  132. }
  133. if (Array.isArray(value)) {
  134. for (const child of value) visit(child);
  135. return;
  136. }
  137. if (typeof value === "object" && value !== null && "props" in value) {
  138. const element = value as { props?: { children?: unknown } };
  139. visit(element.props?.children);
  140. }
  141. };
  142. visit(node);
  143. return lines;
  144. }
  145. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  146. const nodes: ReturnType<typeof collectTextNodes> = [];
  147. const visit = (value: unknown) => {
  148. if (Array.isArray(value)) {
  149. for (const child of value) visit(child);
  150. return;
  151. }
  152. if (typeof value !== "object" || value === null || !("props" in value)) return;
  153. const element = value as {
  154. props?: {
  155. children?: unknown;
  156. style?: Record<string, unknown>;
  157. };
  158. type?: unknown;
  159. };
  160. if (element.type === "text") {
  161. nodes.push({
  162. style: element.props?.style ?? {},
  163. text: collectTextLines(element.props?.children).join(""),
  164. });
  165. }
  166. visit(element.props?.children);
  167. };
  168. visit(node);
  169. return nodes;
  170. }
  171. function collectElementsOfType(
  172. node: unknown,
  173. type: unknown,
  174. ): Array<{ props: { style?: { attributes?: number; fg?: string } } & Record<string, unknown> }> {
  175. const elements: ReturnType<typeof collectElementsOfType> = [];
  176. const visit = (value: unknown) => {
  177. if (Array.isArray(value)) {
  178. for (const child of value) visit(child);
  179. return;
  180. }
  181. if (typeof value !== "object" || value === null || !("props" in value)) return;
  182. const element = value as {
  183. props?: {
  184. children?: unknown;
  185. } & Record<string, unknown>;
  186. type?: unknown;
  187. };
  188. if (element.type === type) {
  189. elements.push({ props: element.props ?? {} });
  190. }
  191. visit(element.props?.children);
  192. };
  193. visit(node);
  194. return elements;
  195. }
  196. function reactElementStyle(node: unknown): Record<string, unknown> {
  197. assert.ok(typeof node === "object" && node !== null && "props" in node);
  198. const element = node as { props?: { style?: Record<string, unknown> } };
  199. return element.props?.style ?? {};
  200. }