|
1 | 1 | // Msteams tests cover message handlerm media plugin behavior. |
2 | 2 | import { describe, expect, it } from "vitest"; |
| 3 | +import { |
| 4 | + sliceUtf16Safe, |
| 5 | + truncateUtf16Safe, |
| 6 | +} from "openclaw/plugin-sdk/text-utility-runtime"; |
3 | 7 | import { translateMSTeamsDmConversationIdForGraph } from "../inbound.js"; |
4 | 8 |
|
5 | 9 | describe("translateMSTeamsDmConversationIdForGraph", () => { |
@@ -53,3 +57,37 @@ describe("translateMSTeamsDmConversationIdForGraph", () => { |
53 | 57 | expect(result).toBe("a:1abc2def3"); |
54 | 58 | }); |
55 | 59 | }); |
| 60 | + |
| 61 | +// Verifies the three rawText / text / preview sites in message-handler.ts |
| 62 | +// (lines 250, 251, 505) drop a surrogate pair that straddles the truncation |
| 63 | +// boundary instead of leaving a lone high-surrogate half in the preview. |
| 64 | +describe("message-handler preview UTF-16 truncation", () => { |
| 65 | + const emoji = "🎉"; |
| 66 | + |
| 67 | + it("truncateUtf16Safe drops a surrogate pair straddling the 50-char boundary (rawText path)", () => { |
| 68 | + const input = "a".repeat(49) + emoji; |
| 69 | + const out = truncateUtf16Safe(input, 50); |
| 70 | + expect(out.length).toBe(49); |
| 71 | + expect(out).toBe("a".repeat(49)); |
| 72 | + expect(out.charCodeAt(out.length - 1)).toBeLessThan(0xd800); |
| 73 | + }); |
| 74 | + |
| 75 | + it("truncateUtf16Safe is a pass-through for plain ASCII (text path)", () => { |
| 76 | + const input = "hello world"; |
| 77 | + expect(truncateUtf16Safe(input, 50)).toBe(input); |
| 78 | + }); |
| 79 | + |
| 80 | + it("sliceUtf16Safe preserves an emoji that sits entirely before the 160-char cut (preview path)", () => { |
| 81 | + const input = emoji + "a".repeat(160); |
| 82 | + const out = sliceUtf16Safe(input, 0, 160); |
| 83 | + expect(out.startsWith(emoji)).toBe(true); |
| 84 | + expect(out.length).toBe(160); |
| 85 | + }); |
| 86 | + |
| 87 | + it("sliceUtf16Safe drops a trailing surrogate straddling the 160-char cut (preview path)", () => { |
| 88 | + const input = "a".repeat(159) + emoji; |
| 89 | + const out = sliceUtf16Safe(input, 0, 160); |
| 90 | + expect(out.length).toBe(159); |
| 91 | + expect(out.charCodeAt(out.length - 1)).toBeLessThan(0xd800); |
| 92 | + }); |
| 93 | +}); |
0 commit comments