ansi-utils.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import { describe, expect, it } from "vitest";
  2. import { stripAnsi } from "../src/utils/ansi.ts";
  3. function referenceAnsiRegex(): RegExp {
  4. const ST = "(?:\\u0007|\\u001B\\u005C|\\u009C)";
  5. const osc = `(?:\\u001B\\][\\s\\S]*?${ST})`;
  6. const csi = "[\\u001B\\u009B][[\\]()#;?]*(?:\\d{1,4}(?:[;:]\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]";
  7. return new RegExp(`${osc}|${csi}`, "g");
  8. }
  9. const referenceRegex = referenceAnsiRegex();
  10. function referenceStripAnsi(value: string): string {
  11. if (!value.includes("\u001B") && !value.includes("\u009B")) {
  12. return value;
  13. }
  14. return value.replace(referenceRegex, "");
  15. }
  16. function getCompatibilityInputs(): string[] {
  17. const inputs = [
  18. "plain",
  19. "a\x1b[31mred\x1b[0mz",
  20. "a\x1b]8;;https://example.com\x07link\x1b]8;;\x07z",
  21. "a\x1b]unterminated",
  22. "a\x1b]funterminated",
  23. "a\x1bPabc\x1b\\z",
  24. "a\x1b^abc\x07z",
  25. "a\x1b_abc\x9cz",
  26. "a\x90abc\x9cz",
  27. "a\x9dabc\x9cz",
  28. "a\x9b31mred",
  29. "a\x1b(0x",
  30. "a\x1b*0x",
  31. "a\x1b+c",
  32. "a\x1b/0x",
  33. "a\x1bcok",
  34. "a\x1b\\ok",
  35. ];
  36. const chars = [
  37. "a",
  38. "f",
  39. "0",
  40. "1",
  41. ";",
  42. ":",
  43. "[",
  44. "]",
  45. "(",
  46. ")",
  47. "#",
  48. "?",
  49. "m",
  50. "P",
  51. "_",
  52. "\\",
  53. "\x07",
  54. "\x1b",
  55. "\x9b",
  56. "\x9c",
  57. "\x90",
  58. "\x9d",
  59. ];
  60. for (const char of chars) {
  61. inputs.push(`x\x1b${char}y`);
  62. inputs.push(`x\x9b${char}y`);
  63. for (let index = 0; index < chars.length; index += 3) {
  64. inputs.push(`x\x1b${char}${chars[index]}y`);
  65. }
  66. }
  67. return inputs;
  68. }
  69. describe("stripAnsi", () => {
  70. it("matches chalk strip-ansi for generated compatibility inputs", () => {
  71. for (const input of getCompatibilityInputs()) {
  72. expect(stripAnsi(input)).toBe(referenceStripAnsi(input));
  73. }
  74. });
  75. it("throws the same TypeError as chalk strip-ansi for non-string values", () => {
  76. const stripAnsiUnknown = stripAnsi as (value: unknown) => string;
  77. for (const value of [undefined, null, 123, {}, Object("x")]) {
  78. const message = `Expected a \`string\`, got \`${typeof value}\``;
  79. expect(() => stripAnsiUnknown(value)).toThrow(TypeError);
  80. expect(() => stripAnsiUnknown(value)).toThrow(message);
  81. }
  82. });
  83. it("strips RIS without leaking the final byte", () => {
  84. expect(stripAnsi("\x1bcdone")).toBe("done");
  85. });
  86. it("strips single-byte ESC sequences without leaking final bytes", () => {
  87. for (let code = "g".charCodeAt(0); code <= "m".charCodeAt(0); code++) {
  88. expect(stripAnsi(`\x1b${String.fromCharCode(code)}ok`)).toBe("ok");
  89. }
  90. for (let code = "r".charCodeAt(0); code <= "t".charCodeAt(0); code++) {
  91. expect(stripAnsi(`\x1b${String.fromCharCode(code)}ok`)).toBe("ok");
  92. }
  93. });
  94. it("strips common ANSI sequences used in tool output", () => {
  95. const input = "a\x1b[31mred\x1b[0m\x1b]8;;https://example.com\x07link\x1b]8;;\x07z";
  96. expect(stripAnsi(input)).toBe("aredlinkz");
  97. });
  98. });