Skip to content

Commit f8d5393

Browse files
fix(text): bound compactText for tiny caps (#576)
* fix(text): harden string helpers against small/degenerate inputs Four internal string helpers violated their invariants on inputs outside their current call sites (all callers pass safe values, so these are latent, surfaced by property-testing with einsiedlerkrebs): - trimMiddle: maxLength < 120 made `edge` negative — slices read the wrong ends (`slice(-0)` returns the whole string) and the "truncated N" count exceeded the input length. Fall back to a simple head truncation. - compactText: maxLength <= 2 returned the bare "..." (length 3), overflowing the cap. Drop the ellipsis when there is no room for it. - slug: the length slice ran after the trailing-dash trim, so slicing on a "-" boundary re-introduced a trailing dash — not idempotent. Re-trim post-slice. - normalizeGitHubActor: stripped only one trailing [bot], leaving a residual on a doubled suffix — not idempotent. Strip all trailing [bot]. Adds regression + unregression tests for each. Closes #571 Closes #572 Closes #573 Closes #574 * test(repair): keep mutation actor matching fail-closed * test(text): pin helper boundary semantics * fix(text): narrow hardening to compact text bounds Co-authored-by: Cameron Beeley <[email protected]> --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 0bb5964 commit f8d5393

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

‎src/repair/text-utils.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ export function compactText(value: unknown, maxLength: number) {
33
.replace(/\s+/g, " ")
44
.trim();
55
if (text.length <= maxLength) return text;
6-
if (maxLength <= 16) return `${text.slice(0, Math.max(0, maxLength - 3))}...`;
6+
// Below the ellipsis width there's no room for "..."; slicing to `maxLength-3` and always
7+
// appending "..." overflowed the cap (e.g. maxLength 2 returned "..." of length 3).
8+
if (maxLength <= 16) {
9+
if (maxLength < 3) return text.slice(0, Math.max(0, maxLength));
10+
return `${text.slice(0, maxLength - 3)}...`;
11+
}
712

813
const marker = " ... ";
914
const available = Math.max(0, maxLength - marker.length);

‎test/repair/text-utils.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import assert from "node:assert/strict";
2+
import test from "node:test";
3+
4+
import { compactText } from "../../dist/repair/text-utils.js";
5+
6+
test("compactText never exceeds maxLength, even for tiny caps", () => {
7+
for (const n of [0, 1, 2, 3, 5, 16]) {
8+
assert.ok(
9+
compactText("hello world this is long", n).length <= n,
10+
`compactText(..., ${n}) must fit within ${n}`,
11+
);
12+
}
13+
// Regression: maxLength 2 used to return the bare "..." (length 3).
14+
assert.equal(compactText("hello world", 2), "he");
15+
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 3), "...");
16+
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 4), "a...");
17+
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 16), "abcdefghijklm...");
18+
assert.equal(compactText("abcdefghijklmnopqrstuvwxyz", 17), "abcdef ... uvwxyz");
19+
});

0 commit comments

Comments
 (0)