tui-slash-suggestions.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import assert from "node:assert/strict";
  2. import test from "node:test";
  3. import { SlashSuggestionPanel } from "../src/app-components.js";
  4. import { palette } from "../src/app-model.js";
  5. const narrowCommands = [
  6. {
  7. name: "compact",
  8. summary: "Compact the current conversation with optional instructions.",
  9. usage: "/compact",
  10. },
  11. {
  12. name: "mcp",
  13. summary: "Show or manage configured MCP servers.",
  14. usage: "/mcp",
  15. },
  16. ];
  17. test("renders slash command suggestions with wrapping-friendly layout", () => {
  18. const panel = SlashSuggestionPanel({
  19. commands: narrowCommands,
  20. contentWidth: 34,
  21. selectedIndex: 0,
  22. });
  23. assert.ok(panel);
  24. const panelStyle = elementStyle(panel);
  25. assert.equal(typeof panelStyle.height, "number");
  26. assert.equal((panelStyle.height as number) > narrowCommands.length + 2, true);
  27. assert.equal(panelStyle.width, "100%");
  28. const lines = collectTextNodes(panel);
  29. assert.deepEqual(
  30. lines.map((line) => line.text),
  31. [
  32. "> ",
  33. "/compact",
  34. " ",
  35. "Compact the current conversation with optional instructions.",
  36. " ",
  37. "/mcp",
  38. " ",
  39. "Show or manage configured MCP servers.",
  40. ],
  41. );
  42. assert.equal(lines[1]?.style.fg, palette.accent);
  43. assert.equal(lines[3]?.style.fg, palette.muted);
  44. assert.equal(lines[3]?.style.wrapMode, "word");
  45. assert.equal(lines[5]?.style.fg, palette.text);
  46. assert.equal(lines[7]?.style.fg, palette.muted);
  47. assert.equal(lines[7]?.style.wrapMode, "word");
  48. });
  49. function collectTextNodes(node: unknown): Array<{ style: Record<string, unknown>; text: string }> {
  50. const lines: ReturnType<typeof collectTextNodes> = [];
  51. const visit = (value: unknown) => {
  52. if (Array.isArray(value)) {
  53. for (const child of value) visit(child);
  54. return;
  55. }
  56. if (typeof value !== "object" || value === null || !("props" in value)) return;
  57. const element = value as {
  58. props?: {
  59. children?: unknown;
  60. style?: Record<string, unknown>;
  61. };
  62. type?: unknown;
  63. };
  64. if (element.type === "text") {
  65. const text = collectTextLines(element.props?.children).join("");
  66. lines.push({ style: element.props?.style ?? {}, text });
  67. }
  68. visit(element.props?.children);
  69. };
  70. visit(node);
  71. return lines;
  72. }
  73. function collectTextLines(node: unknown): string[] {
  74. const lines: string[] = [];
  75. const visit = (value: unknown) => {
  76. if (typeof value === "string") {
  77. lines.push(value);
  78. return;
  79. }
  80. if (Array.isArray(value)) {
  81. for (const child of value) visit(child);
  82. return;
  83. }
  84. if (typeof value === "object" && value !== null && "props" in value) {
  85. const element = value as { props?: { children?: unknown } };
  86. visit(element.props?.children);
  87. }
  88. };
  89. visit(node);
  90. return lines;
  91. }
  92. function elementStyle(node: unknown): Record<string, unknown> {
  93. if (typeof node !== "object" || node === null || !("props" in node)) return {};
  94. const element = node as {
  95. props?: {
  96. style?: Record<string, unknown>;
  97. };
  98. };
  99. return element.props?.style ?? {};
  100. }