Skip to content

Commit 47618a1

Browse files
miorbnliclaude
andcommitted
test(matrix): fix inbound-preview test for lint and lib target
- Avoid String.prototype.isWellFormed (ES2024 lib): use the encodeURIComponent well-formedness probe instead, which works on the project's TS lib target. - Replace literal string concatenation with template strings to satisfy the no-useless-concat lint rule. No behavior change in the assertions. Co-Authored-By: Claude <[email protected]>
1 parent 9d2d398 commit 47618a1

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

extensions/matrix/src/matrix/monitor/handler.inbound-preview.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ import { truncateUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
55
// other supplementary-plane character straddles position 200.
66
import { describe, expect, it } from "vitest";
77

8+
// A string containing a lone surrogate throws when passed to encodeURIComponent,
9+
// which is a runtime-safe well-formedness check that does not need ES2024 lib.
810
function isWellFormed(value: string): boolean {
9-
if (typeof value.isWellFormed === "function") {
10-
return value.isWellFormed();
11-
}
1211
try {
1312
encodeURIComponent(value);
1413
return true;
@@ -18,19 +17,21 @@ function isWellFormed(value: string): boolean {
1817
}
1918

2019
describe("matrix inbound verbose preview truncation", () => {
21-
// Same limit used in handler.ts for the inbound preview.
20+
// Same limit used in handler.ts for the inbound preview. 😀 = \uD83D\uDE00;
21+
// placing it after 199 'x' chars makes the 200th code unit a high surrogate.
2222
const PREVIEW_LIMIT = 200;
23+
const highSurrogate = "\uD83D";
24+
const lowSurrogate = "\uDE00";
25+
const body = `${"x".repeat(199)}${highSurrogate}${lowSurrogate} tail`;
2326

2427
it("keeps the preview well-formed when an emoji straddles the boundary", () => {
25-
const body = "x".repeat(199) + "\uD83D\uDE00" + " tail";
2628
const preview = truncateUtf16Safe(body, PREVIEW_LIMIT).replace(/\n/g, "\\n");
2729
expect(isWellFormed(preview)).toBe(true);
2830
// Naive slice would split the pair and end with the lone high surrogate.
29-
expect(preview.endsWith("\uD83D")).toBe(false);
31+
expect(preview.endsWith(highSurrogate)).toBe(false);
3032
});
3133

3234
it("reproduces the old slice bug splitting the pair", () => {
33-
const body = "x".repeat(199) + "\uD83D\uDE00" + " tail";
3435
const buggy = body.slice(0, PREVIEW_LIMIT);
3536
// Documents why the helper is needed: the unguarded slice is malformed.
3637
expect(isWellFormed(buggy)).toBe(false);
@@ -42,7 +43,7 @@ describe("matrix inbound verbose preview truncation", () => {
4243
});
4344

4445
it("leaves short input unchanged", () => {
45-
const body = "hello \uD83D\uDE00";
46-
expect(truncateUtf16Safe(body, PREVIEW_LIMIT)).toBe(body);
46+
const short = `hello ${highSurrogate}${lowSurrogate}`;
47+
expect(truncateUtf16Safe(short, PREVIEW_LIMIT)).toBe(short);
4748
});
4849
});

0 commit comments

Comments
 (0)