select-list.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import { SelectList } from "../src/components/select-list.ts";
  4. import { visibleWidth } from "../src/utils.ts";
  5. const testTheme = {
  6. selectedPrefix: (text: string) => text,
  7. selectedText: (text: string) => text,
  8. description: (text: string) => text,
  9. scrollInfo: (text: string) => text,
  10. noMatch: (text: string) => text,
  11. };
  12. const visibleIndexOf = (line: string, text: string): number => {
  13. const index = line.indexOf(text);
  14. assert.notEqual(index, -1);
  15. return visibleWidth(line.slice(0, index));
  16. };
  17. describe("SelectList", () => {
  18. it("normalizes multiline descriptions to single line", () => {
  19. const items = [
  20. {
  21. value: "test",
  22. label: "test",
  23. description: "Line one\nLine two\nLine three",
  24. },
  25. ];
  26. const list = new SelectList(items, 5, testTheme);
  27. const rendered = list.render(100);
  28. assert.ok(rendered.length > 0);
  29. assert.ok(!rendered[0].includes("\n"));
  30. assert.ok(rendered[0].includes("Line one Line two Line three"));
  31. });
  32. it("keeps descriptions aligned when the primary text is truncated", () => {
  33. const items = [
  34. { value: "short", label: "short", description: "short description" },
  35. {
  36. value: "very-long-command-name-that-needs-truncation",
  37. label: "very-long-command-name-that-needs-truncation",
  38. description: "long description",
  39. },
  40. ];
  41. const list = new SelectList(items, 5, testTheme);
  42. const rendered = list.render(80);
  43. assert.equal(visibleIndexOf(rendered[0], "short description"), visibleIndexOf(rendered[1], "long description"));
  44. });
  45. it("uses the configured minimum primary column width", () => {
  46. const items = [
  47. { value: "a", label: "a", description: "first" },
  48. { value: "bb", label: "bb", description: "second" },
  49. ];
  50. const list = new SelectList(items, 5, testTheme, {
  51. minPrimaryColumnWidth: 12,
  52. maxPrimaryColumnWidth: 20,
  53. });
  54. const rendered = list.render(80);
  55. assert.equal(rendered[0].indexOf("first"), 14);
  56. assert.equal(rendered[1].indexOf("second"), 14);
  57. });
  58. it("uses the configured maximum primary column width", () => {
  59. const items = [
  60. {
  61. value: "very-long-command-name-that-needs-truncation",
  62. label: "very-long-command-name-that-needs-truncation",
  63. description: "first",
  64. },
  65. { value: "short", label: "short", description: "second" },
  66. ];
  67. const list = new SelectList(items, 5, testTheme, {
  68. minPrimaryColumnWidth: 12,
  69. maxPrimaryColumnWidth: 20,
  70. });
  71. const rendered = list.render(80);
  72. assert.equal(visibleIndexOf(rendered[0], "first"), 22);
  73. assert.equal(visibleIndexOf(rendered[1], "second"), 22);
  74. });
  75. it("allows overriding primary truncation while preserving description alignment", () => {
  76. const items = [
  77. {
  78. value: "very-long-command-name-that-needs-truncation",
  79. label: "very-long-command-name-that-needs-truncation",
  80. description: "first",
  81. },
  82. { value: "short", label: "short", description: "second" },
  83. ];
  84. const list = new SelectList(items, 5, testTheme, {
  85. minPrimaryColumnWidth: 12,
  86. maxPrimaryColumnWidth: 12,
  87. truncatePrimary: ({ text, maxWidth }) => {
  88. if (text.length <= maxWidth) {
  89. return text;
  90. }
  91. return `${text.slice(0, Math.max(0, maxWidth - 1))}…`;
  92. },
  93. });
  94. const rendered = list.render(80);
  95. assert.ok(rendered[0].includes("…"));
  96. assert.equal(visibleIndexOf(rendered[0], "first"), visibleIndexOf(rendered[1], "second"));
  97. });
  98. });