truncate.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import { describe, expect, it } from "vitest";
  2. import { truncateHead, truncateTail } from "../../src/harness/utils/truncate.ts";
  3. const encoder = new TextEncoder();
  4. function byteLength(content: string): number {
  5. return encoder.encode(content).length;
  6. }
  7. function bufferTail(content: string, maxBytes: number): string {
  8. const bytes = Buffer.from(content, "utf8");
  9. if (bytes.length <= maxBytes) return content;
  10. let start = bytes.length - maxBytes;
  11. while (start < bytes.length && (bytes[start] & 0xc0) === 0x80) start++;
  12. return bytes.subarray(start).toString("utf8");
  13. }
  14. function assertMatchesBufferTail(input: string, maxByteValues?: readonly number[]): void {
  15. const totalBytes = Buffer.byteLength(input, "utf8");
  16. const values = maxByteValues ?? Array.from({ length: totalBytes + 5 }, (_, maxBytes) => maxBytes);
  17. for (const maxBytes of values) {
  18. const result = truncateTail(input, { maxBytes, maxLines: 10 });
  19. const expected = bufferTail(input, maxBytes);
  20. if (result.content !== expected) {
  21. throw new Error(
  22. `tail mismatch input=${JSON.stringify(input)} maxBytes=${maxBytes} expected=${JSON.stringify(expected)} actual=${JSON.stringify(result.content)}`,
  23. );
  24. }
  25. const outputBytes = Buffer.byteLength(result.content, "utf8");
  26. if (outputBytes > maxBytes) {
  27. throw new Error(
  28. `tail output exceeded byte limit input=${JSON.stringify(input)} maxBytes=${maxBytes} outputBytes=${outputBytes}`,
  29. );
  30. }
  31. }
  32. }
  33. function sampledByteLimits(input: string): number[] {
  34. const totalBytes = Buffer.byteLength(input, "utf8");
  35. const candidates = [
  36. 0,
  37. 1,
  38. 2,
  39. 3,
  40. 4,
  41. 5,
  42. 8,
  43. Math.floor(totalBytes / 2) - 1,
  44. Math.floor(totalBytes / 2),
  45. Math.floor(totalBytes / 2) + 1,
  46. totalBytes - 8,
  47. totalBytes - 5,
  48. totalBytes - 4,
  49. totalBytes - 3,
  50. totalBytes - 2,
  51. totalBytes - 1,
  52. totalBytes,
  53. totalBytes + 1,
  54. totalBytes + 4,
  55. ];
  56. return [...new Set(candidates.filter((value) => value >= 0))].sort((a, b) => a - b);
  57. }
  58. describe("truncate utilities", () => {
  59. it("counts UTF-8 bytes without Node Buffer", () => {
  60. const content = "aé🙂\nb";
  61. const result = truncateHead(content, { maxBytes: 100, maxLines: 10 });
  62. expect(result.truncated).toBe(false);
  63. expect(result.totalBytes).toBe(byteLength(content));
  64. expect(result.outputBytes).toBe(byteLength(content));
  65. expect(result.totalBytes).toBe(9);
  66. });
  67. it("truncates head on UTF-8 byte limits without partial lines", () => {
  68. const content = "éé\nabc";
  69. const result = truncateHead(content, { maxBytes: 4, maxLines: 10 });
  70. expect(result.content).toBe("éé");
  71. expect(result.truncated).toBe(true);
  72. expect(result.truncatedBy).toBe("bytes");
  73. expect(result.outputBytes).toBe(4);
  74. expect(result.firstLineExceedsLimit).toBe(false);
  75. });
  76. it("reports head truncation when the first line exceeds the byte limit", () => {
  77. const result = truncateHead("éé\nabc", { maxBytes: 3, maxLines: 10 });
  78. expect(result.content).toBe("");
  79. expect(result.truncated).toBe(true);
  80. expect(result.truncatedBy).toBe("bytes");
  81. expect(result.firstLineExceedsLimit).toBe(true);
  82. });
  83. it("truncates tail on UTF-8 boundaries when only a partial last line fits", () => {
  84. const result = truncateTail("aé🙂b", { maxBytes: 5, maxLines: 10 });
  85. expect(result.content).toBe("🙂b");
  86. expect(result.truncated).toBe(true);
  87. expect(result.truncatedBy).toBe("bytes");
  88. expect(result.lastLinePartial).toBe(true);
  89. expect(result.outputBytes).toBe(5);
  90. });
  91. it("truncates an oversized single line with a trailing newline", () => {
  92. const input = `${"X".repeat(300_000)}\n`;
  93. const result = truncateTail(input, { maxBytes: 1024, maxLines: 100 });
  94. expect(result.content).toBe("X".repeat(1024));
  95. expect(result.outputBytes).toBe(1024);
  96. expect(result.outputLines).toBe(1);
  97. expect(result.lastLinePartial).toBe(true);
  98. expect(result.truncatedBy).toBe("bytes");
  99. });
  100. it("drops an oversized trailing character when it cannot fit in tail byte limit", () => {
  101. const result = truncateTail("abc🙂", { maxBytes: 3, maxLines: 10 });
  102. expect(result.content).toBe("");
  103. expect(result.truncated).toBe(true);
  104. expect(result.truncatedBy).toBe("bytes");
  105. expect(result.lastLinePartial).toBe(true);
  106. expect(result.outputBytes).toBe(0);
  107. });
  108. it("matches Buffer tail truncation semantics for surrogate edge cases", () => {
  109. const inputs = ["a\ud83d", "\ude42b", "a\ude42b", "\ud83d\ud83d\ude42", "\ud83d\ude42\ude42", "👩‍💻"];
  110. for (const input of inputs) assertMatchesBufferTail(input);
  111. });
  112. it("matches Buffer tail truncation semantics across deterministic fuzz cases", () => {
  113. const alphabet = [
  114. "a",
  115. "\u007f",
  116. "\u0080",
  117. "é",
  118. "\u07ff",
  119. "\u0800",
  120. "中",
  121. "\ud7ff",
  122. "\ud800",
  123. "\ud83d",
  124. "\udc00",
  125. "\ude42",
  126. "🙂",
  127. "\ue000",
  128. "\uffff",
  129. ];
  130. function checkExhaustive(prefix: string, depth: number): void {
  131. assertMatchesBufferTail(prefix, sampledByteLimits(prefix));
  132. if (depth === 0) return;
  133. for (const character of alphabet) checkExhaustive(prefix + character, depth - 1);
  134. }
  135. checkExhaustive("", 3);
  136. let seed = 0x12345678;
  137. function random(): number {
  138. seed = (seed * 1664525 + 1013904223) >>> 0;
  139. return seed / 0x100000000;
  140. }
  141. for (let i = 0; i < 1_000; i++) {
  142. let input = "";
  143. const length = Math.floor(random() * 80);
  144. for (let j = 0; j < length; j++) input += alphabet[Math.floor(random() * alphabet.length)];
  145. assertMatchesBufferTail(input, sampledByteLimits(input));
  146. }
  147. });
  148. });