truncate-to-width.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
  2. import { describe, expect, it } from "vitest";
  3. /**
  4. * Tests for truncateToWidth behavior with Unicode characters.
  5. *
  6. * These tests verify that truncateToWidth properly handles text with
  7. * Unicode characters that have different byte vs display widths.
  8. */
  9. describe("truncateToWidth", () => {
  10. it("should truncate messages with Unicode characters correctly", () => {
  11. // This message contains a checkmark (✔) which may have display width > 1 byte
  12. const message = '✔ script to run › dev $ concurrently "vite" "node --import tsx ./';
  13. const width = 67;
  14. const maxMsgWidth = width - 2; // Account for cursor
  15. const truncated = truncateToWidth(message, maxMsgWidth);
  16. const truncatedWidth = visibleWidth(truncated);
  17. expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth);
  18. });
  19. it("should handle emoji characters", () => {
  20. const message = "🎉 Celebration! 🚀 Launch 📦 Package ready for deployment now";
  21. const width = 40;
  22. const maxMsgWidth = width - 2;
  23. const truncated = truncateToWidth(message, maxMsgWidth);
  24. const truncatedWidth = visibleWidth(truncated);
  25. expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth);
  26. });
  27. it("should handle mixed ASCII and wide characters", () => {
  28. const message = "Hello 世界 Test 你好 More text here that is long";
  29. const width = 30;
  30. const maxMsgWidth = width - 2;
  31. const truncated = truncateToWidth(message, maxMsgWidth);
  32. const truncatedWidth = visibleWidth(truncated);
  33. expect(truncatedWidth).toBeLessThanOrEqual(maxMsgWidth);
  34. });
  35. it("should not truncate messages that fit", () => {
  36. const message = "Short message";
  37. const width = 50;
  38. const maxMsgWidth = width - 2;
  39. const truncated = truncateToWidth(message, maxMsgWidth);
  40. expect(truncated).toBe(message);
  41. expect(visibleWidth(truncated)).toBeLessThanOrEqual(maxMsgWidth);
  42. });
  43. it("should add ellipsis when truncating", () => {
  44. const message = "This is a very long message that needs to be truncated";
  45. const width = 30;
  46. const maxMsgWidth = width - 2;
  47. const truncated = truncateToWidth(message, maxMsgWidth);
  48. expect(truncated).toContain("...");
  49. expect(visibleWidth(truncated)).toBeLessThanOrEqual(maxMsgWidth);
  50. });
  51. it("should handle the exact crash case from issue report", () => {
  52. // Terminal width was 67, line had visible width 68
  53. // The problematic text contained "✔" and "›" characters
  54. const message = '✔ script to run › dev $ concurrently "vite" "node --import tsx ./server.ts"';
  55. const terminalWidth = 67;
  56. const cursorWidth = 2; // "› " or " "
  57. const maxMsgWidth = terminalWidth - cursorWidth;
  58. const truncated = truncateToWidth(message, maxMsgWidth);
  59. const finalWidth = visibleWidth(truncated);
  60. // The final line (cursor + message) must not exceed terminal width
  61. expect(finalWidth + cursorWidth).toBeLessThanOrEqual(terminalWidth);
  62. });
  63. });