wrap-ansi.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import { visibleWidth, wrapTextWithAnsi } from "../src/utils.ts";
  4. describe("wrapTextWithAnsi", () => {
  5. describe("underline styling", () => {
  6. it("should not apply underline style before the styled text", () => {
  7. const underlineOn = "\x1b[4m";
  8. const underlineOff = "\x1b[24m";
  9. const url = "https://example.com/very/long/path/that/will/wrap";
  10. const text = `read this thread ${underlineOn}${url}${underlineOff}`;
  11. const wrapped = wrapTextWithAnsi(text, 40);
  12. // First line should NOT contain underline code - it's just "read this thread"
  13. assert.strictEqual(wrapped[0], "read this thread");
  14. // Second line should start with underline, have URL content
  15. assert.strictEqual(wrapped[1].startsWith(underlineOn), true);
  16. assert.ok(wrapped[1].includes("https://"));
  17. });
  18. it("should not have whitespace before underline reset code", () => {
  19. const underlineOn = "\x1b[4m";
  20. const underlineOff = "\x1b[24m";
  21. const textWithUnderlinedTrailingSpace = `${underlineOn}underlined text here ${underlineOff}more`;
  22. const wrapped = wrapTextWithAnsi(textWithUnderlinedTrailingSpace, 18);
  23. assert.ok(!wrapped[0].includes(` ${underlineOff}`));
  24. });
  25. it("should not bleed underline to padding - each line should end with reset for underline only", () => {
  26. const underlineOn = "\x1b[4m";
  27. const underlineOff = "\x1b[24m";
  28. const url = "https://example.com/very/long/path/that/will/definitely/wrap";
  29. const text = `prefix ${underlineOn}${url}${underlineOff} suffix`;
  30. const wrapped = wrapTextWithAnsi(text, 30);
  31. // Middle lines (with underlined content) should end with underline-off, not full reset
  32. // Line 1 and 2 contain underlined URL parts
  33. for (let i = 1; i < wrapped.length - 1; i++) {
  34. const line = wrapped[i];
  35. if (line.includes(underlineOn)) {
  36. // Should end with underline off, NOT full reset
  37. assert.strictEqual(line.endsWith(underlineOff), true);
  38. assert.strictEqual(line.endsWith("\x1b[0m"), false);
  39. }
  40. }
  41. });
  42. });
  43. describe("background color preservation", () => {
  44. it("should preserve background color across wrapped lines without full reset", () => {
  45. const bgBlue = "\x1b[44m";
  46. const reset = "\x1b[0m";
  47. const text = `${bgBlue}hello world this is blue background text${reset}`;
  48. const wrapped = wrapTextWithAnsi(text, 15);
  49. // Each line should have background color
  50. for (const line of wrapped) {
  51. assert.ok(line.includes(bgBlue));
  52. }
  53. // Middle lines should NOT end with full reset (kills background for padding)
  54. for (let i = 0; i < wrapped.length - 1; i++) {
  55. assert.strictEqual(wrapped[i].endsWith("\x1b[0m"), false);
  56. }
  57. });
  58. it("should reset underline but preserve background when wrapping underlined text inside background", () => {
  59. const underlineOn = "\x1b[4m";
  60. const underlineOff = "\x1b[24m";
  61. const reset = "\x1b[0m";
  62. const text = `\x1b[41mprefix ${underlineOn}UNDERLINED_CONTENT_THAT_WRAPS${underlineOff} suffix${reset}`;
  63. const wrapped = wrapTextWithAnsi(text, 20);
  64. // All lines should have background color 41 (either as \x1b[41m or combined like \x1b[4;41m)
  65. for (const line of wrapped) {
  66. const hasBgColor = line.includes("[41m") || line.includes(";41m") || line.includes("[41;");
  67. assert.ok(hasBgColor);
  68. }
  69. // Lines with underlined content should use underline-off at end, not full reset
  70. for (let i = 0; i < wrapped.length - 1; i++) {
  71. const line = wrapped[i];
  72. // If this line has underline on, it should end with underline off (not full reset)
  73. if (
  74. (line.includes("[4m") || line.includes("[4;") || line.includes(";4m")) &&
  75. !line.includes(underlineOff)
  76. ) {
  77. assert.strictEqual(line.endsWith(underlineOff), true);
  78. assert.strictEqual(line.endsWith("\x1b[0m"), false);
  79. }
  80. }
  81. });
  82. });
  83. describe("basic wrapping", () => {
  84. it("should wrap plain text correctly", () => {
  85. const text = "hello world this is a test";
  86. const wrapped = wrapTextWithAnsi(text, 10);
  87. assert.ok(wrapped.length > 1);
  88. for (const line of wrapped) {
  89. assert.ok(visibleWidth(line) <= 10);
  90. }
  91. });
  92. it("should break CJK runs at grapheme boundaries after Latin text", () => {
  93. const text = "This is an example 中文汉字测试段落内容中文汉字测试段落内容.";
  94. const wrapped = wrapTextWithAnsi(text, 40);
  95. assert.deepStrictEqual(wrapped, ["This is an example 中文汉字测试段落内容", "中文汉字测试段落内容."]);
  96. for (const line of wrapped) {
  97. assert.ok(visibleWidth(line) <= 40);
  98. }
  99. });
  100. it("should preserve color codes when wrapping CJK runs", () => {
  101. const red = "\x1b[31m";
  102. const reset = "\x1b[0m";
  103. const text = `${red}This is an example 中文汉字测试段落内容中文汉字测试段落内容.${reset}`;
  104. const wrapped = wrapTextWithAnsi(text, 40);
  105. assert.strictEqual(wrapped.length, 2);
  106. assert.strictEqual(wrapped[0], `${red}This is an example 中文汉字测试段落内容`);
  107. assert.strictEqual(wrapped[1], `${red}中文汉字测试段落内容.${reset}`);
  108. for (const line of wrapped) {
  109. assert.ok(visibleWidth(line) <= 40);
  110. }
  111. });
  112. it("should ignore OSC 133 semantic markers in visible width", () => {
  113. const text = "\x1b]133;A\x07hello\x1b]133;B\x07";
  114. assert.strictEqual(visibleWidth(text), 5);
  115. });
  116. it("should ignore OSC sequences terminated with ST in visible width", () => {
  117. const text = "\x1b]133;A\x1b\\hello\x1b]133;B\x1b\\";
  118. assert.strictEqual(visibleWidth(text), 5);
  119. });
  120. it("should treat isolated regional indicators as width 2", () => {
  121. assert.strictEqual(visibleWidth("🇨"), 2);
  122. assert.strictEqual(visibleWidth("🇨🇳"), 2);
  123. });
  124. it("should truncate trailing whitespace that exceeds width", () => {
  125. const twoSpacesWrappedToWidth1 = wrapTextWithAnsi(" ", 1);
  126. assert.ok(visibleWidth(twoSpacesWrappedToWidth1[0]) <= 1);
  127. });
  128. it("should preserve color codes across wraps", () => {
  129. const red = "\x1b[31m";
  130. const reset = "\x1b[0m";
  131. const text = `${red}hello world this is red${reset}`;
  132. const wrapped = wrapTextWithAnsi(text, 10);
  133. // Each continuation line should start with red code
  134. for (let i = 1; i < wrapped.length; i++) {
  135. assert.strictEqual(wrapped[i].startsWith(red), true);
  136. }
  137. // Middle lines should not end with full reset
  138. for (let i = 0; i < wrapped.length - 1; i++) {
  139. assert.strictEqual(wrapped[i].endsWith("\x1b[0m"), false);
  140. }
  141. });
  142. });
  143. });
  144. describe("wrapTextWithAnsi with OSC 8 hyperlinks", () => {
  145. it("re-emits OSC 8 open at the start of continuation lines", () => {
  146. // A hyperlink whose text is long enough to wrap
  147. const url = "https://example.com";
  148. // OSC 8 open + text that is 10 visible chars + OSC 8 close
  149. const input = `\x1b]8;;${url}\x1b\\0123456789\x1b]8;;\x1b\\`;
  150. const lines = wrapTextWithAnsi(input, 6);
  151. // Every line that contains visible text from inside the hyperlink
  152. // should start with the OSC 8 open sequence (or be preceded by it).
  153. for (const line of lines) {
  154. // If the line has visible content it must begin with the OSC 8 re-open
  155. // OR it is the line where the close appeared with no following content.
  156. const stripped = line.replace(/\x1b\]8;;[^\x1b\x07]*\x1b\\/g, "").replace(/\x1b\[[0-9;]*m/g, "");
  157. if (stripped.trim().length > 0) {
  158. assert.ok(
  159. line.startsWith(`\x1b]8;;${url}\x1b\\`) || line.includes(`\x1b]8;;${url}\x1b\\`),
  160. `Line "${line}" has visible text but no OSC 8 re-open`,
  161. );
  162. }
  163. }
  164. });
  165. it("closes OSC 8 before each line break", () => {
  166. const url = "https://example.com";
  167. const input = `\x1b]8;;${url}\x1b\\0123456789\x1b]8;;\x1b\\`;
  168. const lines = wrapTextWithAnsi(input, 6);
  169. for (let i = 0; i < lines.length - 1; i++) {
  170. const line = lines[i];
  171. // Every non-final line that is inside a hyperlink should end with the close
  172. if (line.includes(`\x1b]8;;${url}\x1b\\`)) {
  173. assert.ok(
  174. line.endsWith("\x1b]8;;\x1b\\"),
  175. `Non-final line "${line}" is inside a hyperlink but does not close it`,
  176. );
  177. }
  178. }
  179. });
  180. it("preserves BEL terminators when wrapping OAuth-style hyperlinks", () => {
  181. const url = `https://example.com/oauth/${"a".repeat(32)}`;
  182. const input = `\x1b]8;;${url}\x07${url}\x1b]8;;\x07`;
  183. const lines = wrapTextWithAnsi(input, 20);
  184. assert.ok(lines.length > 1);
  185. for (const line of lines) {
  186. assert.ok(line.includes(`\x1b]8;;${url}\x07`), `Line "${line}" does not reopen the hyperlink with BEL`);
  187. assert.ok(!line.includes(`\x1b]8;;${url}\x1b\\`), `Line "${line}" reopens the hyperlink with ST`);
  188. }
  189. for (const line of lines.slice(0, -1)) {
  190. assert.ok(line.endsWith("\x1b]8;;\x07"), `Line "${line}" does not close the hyperlink with BEL`);
  191. }
  192. });
  193. it("does not emit OSC 8 sequences on lines that are outside the hyperlink", () => {
  194. const url = "https://example.com";
  195. const input = `before \x1b]8;;${url}\x1b\\link\x1b]8;;\x1b\\ after`;
  196. const lines = wrapTextWithAnsi(input, 80);
  197. // With width 80 everything fits on one line; there should be exactly one
  198. // OSC 8 open and one OSC 8 close.
  199. assert.strictEqual(lines.length, 1);
  200. const openCount = (lines[0].match(/\x1b\]8;;https:[^\x1b]+\x1b\\/g) ?? []).length;
  201. const closeCount = (lines[0].match(/\x1b\]8;;\x1b\\/g) ?? []).length;
  202. assert.strictEqual(openCount, 1);
  203. assert.strictEqual(closeCount, 1);
  204. });
  205. });