|
3 | 3 | * Covers strict env parsing and compact session labels. |
4 | 4 | */ |
5 | 5 | import { afterEach, describe, expect, it, vi } from "vitest"; |
6 | | -import { deriveSessionName, readEnvInt } from "./bash-tools.shared.js"; |
| 6 | +import { chunkString, deriveSessionName, readEnvInt } from "./bash-tools.shared.js"; |
7 | 7 |
|
8 | 8 | describe("readEnvInt", () => { |
9 | 9 | afterEach(() => { |
@@ -70,3 +70,42 @@ describe("deriveSessionName", () => { |
70 | 70 | } |
71 | 71 | }); |
72 | 72 | }); |
| 73 | + |
| 74 | +describe("chunkString", () => { |
| 75 | + it("preserves surrogate pairs at chunk boundaries", () => { |
| 76 | + // 8191 'a' + emoji + "b" = 8194 code units. With chunk limit 8192, |
| 77 | + // raw slice(0, 8192) would cut between the emoji's high surrogate at |
| 78 | + // index 8191 and low surrogate at index 8192 → lone surrogate in chunk 1. |
| 79 | + // sliceUtf16Safe backs out the boundary → both surrogates stay together. |
| 80 | + const input = "a".repeat(8191) + "🚀b"; |
| 81 | + const chunks = chunkString(input, 8192); |
| 82 | + expect(chunks.length).toBe(2); |
| 83 | + for (const chunk of chunks) { |
| 84 | + const rt = new TextDecoder().decode(new TextEncoder().encode(chunk)); |
| 85 | + expect(rt).not.toContain("�"); |
| 86 | + } |
| 87 | + // The emoji must not be lost across chunks. |
| 88 | + const rejoined = chunks.join(""); |
| 89 | + expect(rejoined).toBe(input); |
| 90 | + }); |
| 91 | + |
| 92 | + it("returns single chunk for input smaller than limit", () => { |
| 93 | + const chunks = chunkString("hello", 8192); |
| 94 | + expect(chunks).toEqual(["hello"]); |
| 95 | + }); |
| 96 | + |
| 97 | + it("splits cleanly when chunk boundary aligns with surrogate pair", () => { |
| 98 | + // 2 'a' + emoji + 2 'b' = 6 code units. chunk limit 2 splits at |
| 99 | + // index 2 (inside the emoji). sliceUtf16Safe backs out to index 2 |
| 100 | + // at chunk end and advances to index 3 at chunk start → both halves |
| 101 | + // preserved, data not lost. |
| 102 | + const input = "aa🚀bb"; |
| 103 | + const chunks = chunkString(input, 2); |
| 104 | + const rejoined = chunks.join(""); |
| 105 | + expect(rejoined).toBe(input); |
| 106 | + for (const chunk of chunks) { |
| 107 | + const rt = new TextDecoder().decode(new TextEncoder().encode(chunk)); |
| 108 | + expect(rt).not.toContain("�"); |
| 109 | + } |
| 110 | + }); |
| 111 | +}); |
0 commit comments