truncated-text.test.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import { Chalk } from "chalk";
  4. import { TruncatedText } from "../src/components/truncated-text.ts";
  5. import { visibleWidth } from "../src/utils.ts";
  6. // Force full color in CI so ANSI assertions are deterministic
  7. const chalk = new Chalk({ level: 3 });
  8. describe("TruncatedText component", () => {
  9. it("pads output lines to exactly match width", () => {
  10. const text = new TruncatedText("Hello world", 1, 0);
  11. const lines = text.render(50);
  12. // Should have exactly one content line (no vertical padding)
  13. assert.strictEqual(lines.length, 1);
  14. // Line should be exactly 50 visible characters
  15. const visibleLen = visibleWidth(lines[0]);
  16. assert.strictEqual(visibleLen, 50);
  17. });
  18. it("pads output with vertical padding lines to width", () => {
  19. const text = new TruncatedText("Hello", 0, 2);
  20. const lines = text.render(40);
  21. // Should have 2 padding lines + 1 content line + 2 padding lines = 5 total
  22. assert.strictEqual(lines.length, 5);
  23. // All lines should be exactly 40 characters
  24. for (const line of lines) {
  25. assert.strictEqual(visibleWidth(line), 40);
  26. }
  27. });
  28. it("truncates long text and pads to width", () => {
  29. const longText = "This is a very long piece of text that will definitely exceed the available width";
  30. const text = new TruncatedText(longText, 1, 0);
  31. const lines = text.render(30);
  32. assert.strictEqual(lines.length, 1);
  33. // Should be exactly 30 characters
  34. assert.strictEqual(visibleWidth(lines[0]), 30);
  35. // Should contain ellipsis
  36. const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, "");
  37. assert.ok(stripped.includes("..."));
  38. });
  39. it("preserves ANSI codes in output and pads correctly", () => {
  40. const styledText = `${chalk.red("Hello")} ${chalk.blue("world")}`;
  41. const text = new TruncatedText(styledText, 1, 0);
  42. const lines = text.render(40);
  43. assert.strictEqual(lines.length, 1);
  44. // Should be exactly 40 visible characters (ANSI codes don't count)
  45. assert.strictEqual(visibleWidth(lines[0]), 40);
  46. // Should preserve the color codes
  47. assert.ok(lines[0].includes("\x1b["));
  48. });
  49. it("truncates styled text and adds reset code before ellipsis", () => {
  50. const longStyledText = chalk.red("This is a very long red text that will be truncated");
  51. const text = new TruncatedText(longStyledText, 1, 0);
  52. const lines = text.render(20);
  53. assert.strictEqual(lines.length, 1);
  54. // Should be exactly 20 visible characters
  55. assert.strictEqual(visibleWidth(lines[0]), 20);
  56. // Should contain reset code before ellipsis
  57. assert.ok(lines[0].includes("\x1b[0m..."));
  58. });
  59. it("handles text that fits exactly", () => {
  60. // With paddingX=1, available width is 30-2=28
  61. // "Hello world" is 11 chars, fits comfortably
  62. const text = new TruncatedText("Hello world", 1, 0);
  63. const lines = text.render(30);
  64. assert.strictEqual(lines.length, 1);
  65. assert.strictEqual(visibleWidth(lines[0]), 30);
  66. // Should NOT contain ellipsis
  67. const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, "");
  68. assert.ok(!stripped.includes("..."));
  69. });
  70. it("handles empty text", () => {
  71. const text = new TruncatedText("", 1, 0);
  72. const lines = text.render(30);
  73. assert.strictEqual(lines.length, 1);
  74. assert.strictEqual(visibleWidth(lines[0]), 30);
  75. });
  76. it("stops at newline and only shows first line", () => {
  77. const multilineText = "First line\nSecond line\nThird line";
  78. const text = new TruncatedText(multilineText, 1, 0);
  79. const lines = text.render(40);
  80. assert.strictEqual(lines.length, 1);
  81. assert.strictEqual(visibleWidth(lines[0]), 40);
  82. // Should only contain "First line"
  83. const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, "").trim();
  84. assert.ok(stripped.includes("First line"));
  85. assert.ok(!stripped.includes("Second line"));
  86. assert.ok(!stripped.includes("Third line"));
  87. });
  88. it("truncates first line even with newlines in text", () => {
  89. const longMultilineText = "This is a very long first line that needs truncation\nSecond line";
  90. const text = new TruncatedText(longMultilineText, 1, 0);
  91. const lines = text.render(25);
  92. assert.strictEqual(lines.length, 1);
  93. assert.strictEqual(visibleWidth(lines[0]), 25);
  94. // Should contain ellipsis and not second line
  95. const stripped = lines[0].replace(/\x1b\[[0-9;]*m/g, "");
  96. assert.ok(stripped.includes("..."));
  97. assert.ok(!stripped.includes("Second line"));
  98. });
  99. });