70160
Ce
cel-tui / files
cel-tui / packages / clew / src / index.test.ts
23.2 KB
1import { describe, expect, test } from "bun:test"; 2import { 3 type ClewPatch, 4 type ClewToken, 5 clew, 6 clewSupportsLanguage, 7} from "./index.js"; 8 9function joinedText(tokens: readonly { text: string }[]): string { 10 return tokens.map((token) => token.text).join(""); 11} 12 13function applyPatch( 14 current: readonly ClewToken[], 15 patch: ClewPatch, 16): ClewToken[] { 17 return [ 18 ...current.filter((token) => token.end <= patch.from), 19 ...patch.tokens, 20 ...current.filter((token) => token.start >= patch.to), 21 ]; 22} 23 24function chunkBySize(content: string, size: number): string[] { 25 const chunks: string[] = []; 26 for (let start = 0; start < content.length; start += size) { 27 chunks.push(content.slice(start, start + size)); 28 } 29 return chunks; 30} 31 32function expectStreamPatchesReplay( 33 content: string, 34 language: string, 35 chunks: readonly string[], 36): void { 37 const stream = clew("", { lang: language, stability: "stable" }); 38 let replayed: ClewToken[] = []; 39 40 stream.onChunk((patch) => { 41 replayed = applyPatch(replayed, patch); 42 }); 43 stream.onCorrection((patch) => { 44 replayed = applyPatch(replayed, patch); 45 }); 46 47 for (const chunk of chunks) { 48 stream.write(chunk); 49 } 50 51 const final = stream.end(); 52 expect(replayed).toEqual(final.tokens); 53 expect(joinedText(replayed)).toBe(content); 54 expect(final).toEqual(clew(content, { lang: language, stream: false })); 55} 56 57function expectFullCoverage(content: string, language: string): void { 58 const output = clew(content, { lang: language, stream: false }); 59 60 expect(joinedText(output.tokens)).toBe(content); 61 62 if (content.length === 0) { 63 expect(output.tokens).toEqual([]); 64 return; 65 } 66 67 expect(output.tokens[0]?.start).toBe(0); 68 expect(output.tokens.at(-1)?.end).toBe(content.length); 69 70 for (let i = 0; i < output.tokens.length; i++) { 71 const token = output.tokens[i]; 72 expect(token).toBeDefined(); 73 if (!token) { 74 throw new Error(`Missing token at index ${i}`); 75 } 76 77 expect(token.start).toBeLessThan(token.end); 78 expect(token.text).toBe(content.slice(token.start, token.end)); 79 80 if (i > 0) { 81 const previous = output.tokens[i - 1]; 82 expect(previous).toBeDefined(); 83 if (!previous) { 84 throw new Error(`Missing token at index ${i - 1}`); 85 } 86 expect(previous.end).toBe(token.start); 87 } 88 } 89} 90 91function expectTokenType( 92 language: string, 93 content: string, 94 tokenText: string, 95 type: string, 96): void { 97 const output = clew(content, { lang: language, stream: false }); 98 expect(output.tokens.find((token) => token.text === tokenText)?.type).toBe( 99 type, 100 ); 101} 102 103function expectTokenScopes( 104 language: string, 105 content: string, 106 tokenText: string, 107 scopes: readonly string[], 108): void { 109 const output = clew(content, { lang: language, stream: false }); 110 expect( 111 output.tokens.find((token) => token.text === tokenText)?.scopes, 112 ).toEqual(scopes); 113} 114 115describe("clew", () => { 116 test("reports supported languages through the registry", () => { 117 expect(clewSupportsLanguage("typescript")).toBe(true); 118 expect(clewSupportsLanguage("javascript")).toBe(true); 119 expect(clewSupportsLanguage("python")).toBe(true); 120 expect(clewSupportsLanguage("py")).toBe(true); 121 expect(clewSupportsLanguage("bash")).toBe(true); 122 expect(clewSupportsLanguage("json")).toBe(true); 123 expect(clewSupportsLanguage("markdown")).toBe(true); 124 expect(clewSupportsLanguage("diff")).toBe(true); 125 expect(clewSupportsLanguage("patch")).toBe(true); 126 expect(clewSupportsLanguage("tsx")).toBe(false); 127 expect(clewSupportsLanguage("jsx")).toBe(false); 128 expect(clewSupportsLanguage("shell")).toBe(false); 129 expect(clewSupportsLanguage("zsh")).toBe(false); 130 expect(clewSupportsLanguage("md")).toBe(false); 131 }); 132 133 test("tokenizes supported languages with full coverage", () => { 134 expectFullCoverage('type Phase = "idle"\n// done', "typescript"); 135 expectFullCoverage("const value = 1n", "javascript"); 136 expectFullCoverage( 137 '@dataclass\nclass Job:\n def render(self):\n print("hi")\n', 138 "python", 139 ); 140 expectFullCoverage( 141 'if true; then echo "$HOME"; fi\ncat <<EOF\nhello\nEOF\n', 142 "bash", 143 ); 144 expectFullCoverage('{"name":"cel","ok":true,"items":[1,null]}', "json"); 145 expectFullCoverage( 146 "# Title\n- item with `code`\n> quote [link](https://example.com)\n", 147 "markdown", 148 ); 149 expectFullCoverage( 150 "diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-old\n+new\n", 151 "diff", 152 ); 153 expectFullCoverage( 154 "Index: a.txt\n===================================================================\n--- a.txt\n+++ a.txt\n@@ -1,1 +1,1 @@\n-old\n+new\n", 155 "patch", 156 ); 157 }); 158 159 test("tokenizes richer TypeScript declaration, property, function, and literal scopes", () => { 160 const source = [ 161 "@sealed", 162 'type Phase = "idle" | "running";', 163 "interface Job {", 164 " status: Phase;", 165 " execute(input: string): Promise<void>;", 166 "}", 167 "class Runner<T> {", 168 " constructor(private value: T) {}", 169 " get current(): T { return this.value; }", 170 "}", 171 "const render = (job: Job) => console.log(job.status, config.port);", 172 'const config = { port: 3000, "display-name": "cel", ok: true };', 173 "const count = 1n;", 174 "const empty = null;", 175 'enum Mode { Idle = "idle" }', 176 ].join("\n"); 177 178 expectTokenType("typescript", source, "type", "keyword"); 179 expectTokenType("typescript", source, '"idle"', "string"); 180 expectTokenScopes("typescript", source, "sealed", [ 181 "meta", 182 "meta.decorator", 183 ]); 184 expectTokenScopes("typescript", source, "Phase", ["type", "type.alias"]); 185 expectTokenScopes("typescript", source, "Job", ["type", "type.interface"]); 186 expectTokenScopes("typescript", source, "status", ["property"]); 187 expectTokenScopes("typescript", source, "execute", ["function"]); 188 expectTokenScopes("typescript", source, "input", [ 189 "variable", 190 "variable.parameter", 191 ]); 192 expectTokenScopes("typescript", source, "string", [ 193 "type", 194 "type.primitive", 195 ]); 196 expectTokenScopes("typescript", source, "Promise", ["type"]); 197 expectTokenScopes("typescript", source, "void", ["type", "type.primitive"]); 198 expectTokenScopes("typescript", source, "Runner", ["type", "type.class"]); 199 expectTokenScopes("typescript", source, "T", ["type", "type.parameter"]); 200 expectTokenScopes("typescript", source, "value", ["property"]); 201 expectTokenScopes("typescript", source, "current", ["property"]); 202 expectTokenType("typescript", source, "render", "function"); 203 expectTokenType("typescript", source, "console", "builtin"); 204 expectTokenType("typescript", source, "log", "function"); 205 expectTokenScopes("typescript", source, "port", ["property"]); 206 expectTokenScopes("typescript", source, '"display-name"', [ 207 "string", 208 "property", 209 ]); 210 expectTokenScopes("typescript", source, "true", [ 211 "keyword", 212 "constant.boolean", 213 ]); 214 expectTokenScopes("typescript", source, "1n", ["number", "number.bigint"]); 215 expectTokenScopes("typescript", source, "null", [ 216 "keyword", 217 "constant.null", 218 ]); 219 expectTokenScopes("typescript", source, "Mode", ["type", "type.enum"]); 220 expectTokenScopes("typescript", source, "Idle", [ 221 "property", 222 "constant.enum", 223 ]); 224 }); 225 226 test("tokenizes python decorators, definitions, builtins, and properties", () => { 227 const source = [ 228 "@dataclass", 229 "class Job:", 230 " retries: int = 0", 231 " def render(self, item):", 232 ' print(item.value, f"{self.retries}")', 233 " return True", 234 ].join("\n"); 235 236 expectTokenScopes("python", source, "@dataclass", [ 237 "meta", 238 "meta.decorator", 239 ]); 240 expectTokenType("python", source, "class", "keyword"); 241 expectTokenType("python", source, "Job", "type"); 242 expectTokenType("python", source, "int", "type"); 243 expectTokenType("python", source, "def", "keyword"); 244 expectTokenType("python", source, "render", "function"); 245 expectTokenType("python", source, "print", "builtin"); 246 expectTokenScopes("python", source, "value", ["property"]); 247 expectTokenScopes("python", source, "True", [ 248 "keyword", 249 "constant.boolean", 250 ]); 251 }); 252 253 test("tokenizes bash control flow, substitutions, and heredocs", () => { 254 const parameterExpansion = "$" + "{name:-guest}"; 255 const source = [ 256 'if true; then echo "$HOME"; fi', 257 'name="world"', 258 `echo $(pwd) $((count + 1)) ${parameterExpansion}`, 259 "cat <<EOF", 260 "hello", 261 "EOF", 262 ].join("\n"); 263 264 expectTokenType("bash", source, "if", "keyword"); 265 expectTokenType("bash", source, "then", "keyword"); 266 expectTokenType("bash", source, "fi", "keyword"); 267 expectTokenType("bash", source, "echo", "builtin"); 268 expectTokenScopes("bash", source, "$HOME", ["variable"]); 269 expectTokenScopes("bash", source, "name", ["variable"]); 270 expectTokenScopes("bash", source, "$(pwd)", [ 271 "meta", 272 "meta.substitution.command", 273 ]); 274 expectTokenScopes("bash", source, "$((count + 1))", [ 275 "meta", 276 "meta.substitution.arithmetic", 277 ]); 278 expectTokenScopes("bash", source, parameterExpansion, [ 279 "variable", 280 "variable.expansion", 281 ]); 282 expect( 283 clew(source, { lang: "bash", stream: false }).tokens.filter( 284 (token) => token.text === "EOF", 285 ), 286 ).toHaveLength(2); 287 }); 288 289 test("treats a hash inside a bash word as literal text", () => { 290 const content = "echo foo#bar"; 291 const output = clew(content, { lang: "bash", stream: false }); 292 const commentOutput = clew("echo foo #bar", { 293 lang: "bash", 294 stream: false, 295 }); 296 297 expect(joinedText(output.tokens)).toBe(content); 298 expect(output.tokens.some((token) => token.type === "comment")).toBe(false); 299 expect(output.tokens.find((token) => token.text === "foo#bar")?.type).toBe( 300 "text", 301 ); 302 expect( 303 commentOutput.tokens.find((token) => token.text === "#bar")?.type, 304 ).toBe("comment"); 305 }); 306 307 test("always terminates with full coverage for deterministic bash fuzz inputs", () => { 308 const alphabet = [ 309 "a", 310 "b", 311 "c", 312 "X", 313 "Y", 314 "Z", 315 "0", 316 "9", 317 "#", 318 " ", 319 "$", 320 "'", 321 '"', 322 ";", 323 "|", 324 "&", 325 "(", 326 ")", 327 "{", 328 "}", 329 "<", 330 ">", 331 "\n", 332 "\t", 333 ]; 334 let seed = 0x9e3779b9; 335 336 const next = (): number => { 337 seed ^= seed << 13; 338 seed ^= seed >>> 17; 339 seed ^= seed << 5; 340 return seed >>> 0; 341 }; 342 343 const samples = ["echo foo#bar", "printf %s foo#bar#baz"]; 344 for (let sampleIndex = 0; sampleIndex < 128; sampleIndex++) { 345 let sample = ""; 346 for (let index = 0; index < 64; index++) { 347 sample += alphabet[next() % alphabet.length] ?? ""; 348 } 349 samples.push(sample); 350 } 351 352 for (const sample of samples) { 353 expectFullCoverage(sample, "bash"); 354 } 355 }); 356 357 test("tokenizes json properties, literals, and numbers", () => { 358 const source = '{"name":"cel","ok":true,"count":2,"items":[null]}'; 359 360 expectTokenType("json", source, '"name"', "string"); 361 expectTokenScopes("json", source, '"name"', ["string", "property"]); 362 expectTokenType("json", source, '"cel"', "string"); 363 expectTokenType("json", source, "true", "keyword"); 364 expectTokenScopes("json", source, "true", ["keyword", "constant.boolean"]); 365 expectTokenType("json", source, "2", "number"); 366 expectTokenScopes("json", source, "null", ["keyword", "constant.null"]); 367 }); 368 369 test("tokenizes markdown headings, markers, links, and code", () => { 370 const source = [ 371 "# Title", 372 "- item with `code`", 373 "> quote [link](https://example.com)", 374 "```ts", 375 "const value = 1", 376 "```", 377 ].join("\n"); 378 379 expectTokenScopes("markdown", source, "# ", [ 380 "meta", 381 "markup.heading.marker", 382 ]); 383 expectTokenScopes("markdown", source, "Title", ["meta", "markup.heading"]); 384 expectTokenScopes("markdown", source, "- ", ["meta", "markup.list"]); 385 expectTokenScopes("markdown", source, "`code`", ["string", "markup.code"]); 386 expectTokenScopes("markdown", source, "> ", ["meta", "markup.quote"]); 387 expectTokenScopes("markdown", source, "[link](https://example.com)", [ 388 "link", 389 "markup.link", 390 ]); 391 expectTokenScopes("markdown", source, "```ts", [ 392 "meta", 393 "markup.code.fence", 394 ]); 395 expectTokenScopes("markdown", source, "const value = 1", [ 396 "string", 397 "markup.code", 398 ]); 399 }); 400 401 test("tokenizes unified and git diff line classes", () => { 402 const source = [ 403 "diff --git a/src/file.ts b/src/file.ts", 404 "index 83db48f..f735c2d 100644", 405 "old mode 100644", 406 "new mode 100755", 407 "deleted file mode 100644", 408 "new file mode 100644", 409 "similarity index 96%", 410 "dissimilarity index 12%", 411 "rename from src/old.ts", 412 "rename to src/new.ts", 413 "copy from src/source.ts", 414 "copy to src/copy.ts", 415 "--- /dev/null", 416 "+++ b/src/file.ts", 417 "@@ -0,0 +1,3 @@ optional section", 418 "+added line", 419 "-removed line", 420 " context line", 421 "\\ No newline at end of file", 422 "plain fallback", 423 "", 424 ].join("\n"); 425 426 expectTokenScopes( 427 "diff", 428 source, 429 "diff --git a/src/file.ts b/src/file.ts", 430 ["meta", "diff.header"], 431 ); 432 expectTokenScopes("diff", source, "index 83db48f..f735c2d 100644", [ 433 "meta", 434 "diff.header", 435 ]); 436 expectTokenScopes("diff", source, "old mode 100644", [ 437 "meta", 438 "diff.header", 439 ]); 440 expectTokenScopes("diff", source, "new mode 100755", [ 441 "meta", 442 "diff.header", 443 ]); 444 expectTokenScopes("diff", source, "deleted file mode 100644", [ 445 "meta", 446 "diff.header", 447 ]); 448 expectTokenScopes("diff", source, "new file mode 100644", [ 449 "meta", 450 "diff.header", 451 ]); 452 expectTokenScopes("diff", source, "similarity index 96%", [ 453 "meta", 454 "diff.header", 455 ]); 456 expectTokenScopes("diff", source, "dissimilarity index 12%", [ 457 "meta", 458 "diff.header", 459 ]); 460 expectTokenScopes("diff", source, "rename from src/old.ts", [ 461 "meta", 462 "diff.header", 463 ]); 464 expectTokenScopes("diff", source, "rename to src/new.ts", [ 465 "meta", 466 "diff.header", 467 ]); 468 expectTokenScopes("diff", source, "copy from src/source.ts", [ 469 "meta", 470 "diff.header", 471 ]); 472 expectTokenScopes("diff", source, "copy to src/copy.ts", [ 473 "meta", 474 "diff.header", 475 ]); 476 expectTokenScopes("diff", source, "--- /dev/null", [ 477 "meta", 478 "diff.file.old", 479 ]); 480 expectTokenScopes("diff", source, "+++ b/src/file.ts", [ 481 "meta", 482 "diff.file.new", 483 ]); 484 expectTokenScopes("diff", source, "@@ -0,0 +1,3 @@ optional section", [ 485 "meta", 486 "diff.hunk", 487 ]); 488 expectTokenScopes("diff", source, "added line", [ 489 "string", 490 "diff.inserted", 491 ]); 492 expectTokenScopes("diff", source, "removed line", [ 493 "comment", 494 "diff.deleted", 495 ]); 496 expectTokenScopes("diff", source, "context line", ["text", "diff.context"]); 497 expectTokenScopes("diff", source, "\\ No newline at end of file", [ 498 "comment", 499 "diff.no-newline", 500 ]); 501 expectTokenType("diff", source, "plain fallback", "text"); 502 }); 503 504 test("tokenizes npm diff createPatch-style headers", () => { 505 const source = [ 506 "Index: file.txt", 507 "===================================================================", 508 "--- file.txt", 509 "+++ file.txt", 510 "@@ -1,2 +1,2 @@", 511 " line", 512 "-before", 513 "+after", 514 "", 515 ].join("\n"); 516 517 expectTokenScopes("patch", source, "Index: file.txt", [ 518 "meta", 519 "diff.header", 520 ]); 521 expectTokenScopes( 522 "patch", 523 source, 524 "===================================================================", 525 ["meta", "diff.header"], 526 ); 527 expectTokenScopes("patch", source, "--- file.txt", [ 528 "meta", 529 "diff.file.old", 530 ]); 531 expectTokenScopes("patch", source, "+++ file.txt", [ 532 "meta", 533 "diff.file.new", 534 ]); 535 expectTokenScopes("patch", source, "before", ["comment", "diff.deleted"]); 536 expectTokenScopes("patch", source, "after", ["string", "diff.inserted"]); 537 }); 538 539 test("streams diff final output consistently across chunk boundaries", () => { 540 const content = 541 "diff --git a/a.txt b/a.txt\n--- a/a.txt\n+++ b/a.txt\n@@ -1 +1 @@\n-old\n+new\n"; 542 const direct = clew(content, { lang: "diff", stream: false }); 543 const stream = clew("", { lang: "diff" }); 544 545 for (const chunk of [ 546 "diff --git a/a", 547 ".txt b/a.txt\n--- a/a.txt\n+", 548 "++ b/a.txt\n@@ -1 +1 @@\n-o", 549 "ld\n+new\n", 550 ]) { 551 stream.write(chunk); 552 } 553 554 expect(stream.end()).toEqual(direct); 555 }); 556 557 test("streams append-only chunks eagerly by default", () => { 558 const stream = clew("", { lang: "ts" }); 559 const chunks: Array<{ from: number; to: number; text: string }> = []; 560 const corrections: Array<{ from: number; to: number; text: string }> = []; 561 562 stream.onChunk((chunk) => { 563 chunks.push({ 564 from: chunk.from, 565 to: chunk.to, 566 text: joinedText(chunk.tokens), 567 }); 568 }); 569 stream.onCorrection((correction) => { 570 corrections.push({ 571 from: correction.from, 572 to: correction.to, 573 text: joinedText(correction.tokens), 574 }); 575 }); 576 577 stream.write("const"); 578 stream.write(" value = 1"); 579 580 expect(corrections).toEqual([]); 581 expect(chunks).toEqual([ 582 { from: 0, to: 0, text: "const" }, 583 { from: 5, to: 5, text: " value = 1" }, 584 ]); 585 expect(joinedText(stream.snapshot().tokens)).toBe("const value = 1"); 586 }); 587 588 test("emits corrections when later TypeScript input changes an earlier token", () => { 589 const stream = clew("", { lang: "typescript" }); 590 const corrections: Array<{ from: number; to: number; text: string }> = []; 591 592 stream.onCorrection((correction) => { 593 corrections.push({ 594 from: correction.from, 595 to: correction.to, 596 text: joinedText(correction.tokens), 597 }); 598 }); 599 600 stream.write('const message = "hel'); 601 stream.write('lo"'); 602 603 expect(corrections).toHaveLength(1); 604 expect(corrections[0]).toEqual({ 605 from: "const message = ".length, 606 to: 'const message = "hel'.length, 607 text: '"hello"', 608 }); 609 expect(joinedText(stream.end().tokens)).toBe('const message = "hello"'); 610 }); 611 612 test("emits corrections when later python input closes a string", () => { 613 const stream = clew("", { lang: "python" }); 614 const corrections: Array<{ from: number; to: number; text: string }> = []; 615 616 stream.onCorrection((correction) => { 617 corrections.push({ 618 from: correction.from, 619 to: correction.to, 620 text: joinedText(correction.tokens), 621 }); 622 }); 623 624 stream.write('print("hel'); 625 stream.write('lo")'); 626 627 expect(corrections).toEqual([ 628 { 629 from: "print(".length, 630 to: 'print("hel'.length, 631 text: '"hello")', 632 }, 633 ]); 634 expect(joinedText(stream.end().tokens)).toBe('print("hello")'); 635 }); 636 637 test("emits corrections when later bash input extends a variable expansion", () => { 638 const stream = clew("", { lang: "bash" }); 639 const corrections: Array<{ from: number; to: number; text: string }> = []; 640 641 stream.onCorrection((correction) => { 642 corrections.push({ 643 from: correction.from, 644 to: correction.to, 645 text: joinedText(correction.tokens), 646 }); 647 }); 648 649 stream.write('echo "$HO'); 650 stream.write('ME"'); 651 652 expect(corrections).toEqual([ 653 { 654 from: 'echo "'.length, 655 to: 'echo "$HO'.length, 656 text: '$HOME"', 657 }, 658 ]); 659 expect(joinedText(stream.end().tokens)).toBe('echo "$HOME"'); 660 }); 661 662 test("emits corrections when later json input closes a string value", () => { 663 const stream = clew("", { lang: "json" }); 664 const corrections: Array<{ from: number; to: number; text: string }> = []; 665 666 stream.onCorrection((correction) => { 667 corrections.push({ 668 from: correction.from, 669 to: correction.to, 670 text: joinedText(correction.tokens), 671 }); 672 }); 673 674 stream.write('{"message":"hel'); 675 stream.write('lo"}'); 676 677 expect(corrections).toEqual([ 678 { 679 from: '{"message":'.length, 680 to: '{"message":"hel'.length, 681 text: '"hello"}', 682 }, 683 ]); 684 expect(joinedText(stream.end().tokens)).toBe('{"message":"hello"}'); 685 }); 686 687 test("holds back unterminated trailing lines in stable mode and flushes on end", () => { 688 const stream = clew("", { lang: "bash", stability: "stable" }); 689 const chunks: Array<{ from: number; to: number; text: string }> = []; 690 691 stream.onChunk((chunk) => { 692 chunks.push({ 693 from: chunk.from, 694 to: chunk.to, 695 text: joinedText(chunk.tokens), 696 }); 697 }); 698 699 stream.write("echo hi"); 700 expect(chunks).toEqual([]); 701 702 stream.write("\ncat <<EOF\nhello"); 703 expect(chunks).toEqual([{ from: 0, to: 0, text: "echo hi\ncat <<EOF\n" }]); 704 705 stream.end(); 706 expect(chunks).toEqual([ 707 { from: 0, to: 0, text: "echo hi\ncat <<EOF\n" }, 708 { 709 from: "echo hi\ncat <<EOF\n".length, 710 to: "echo hi\ncat <<EOF\n".length, 711 text: "hello", 712 }, 713 ]); 714 }); 715 716 test("replays stable patches for tokens spanning line boundaries", () => { 717 const cases = [ 718 { 719 content: 'value = """alpha\nbeta\ngamma"""', 720 language: "python", 721 }, 722 { 723 content: "printf '%s' 'alpha\nbeta\ngamma'", 724 language: "bash", 725 }, 726 { 727 content: '{"value":"alpha\nbeta\ngamma"}', 728 language: "json", 729 }, 730 ]; 731 732 for (const { content, language } of cases) { 733 expectStreamPatchesReplay(content, language, [content]); 734 735 for (let split = 1; split < content.length; split++) { 736 expectStreamPatchesReplay(content, language, [ 737 content.slice(0, split), 738 content.slice(split), 739 ]); 740 } 741 742 for (const size of [1, 2, 3, 5, 8]) { 743 expectStreamPatchesReplay( 744 content, 745 language, 746 chunkBySize(content, size), 747 ); 748 } 749 } 750 }); 751 752 test("produces the same final output regardless of bash chunk boundaries", () => { 753 const content = 'if true; then echo "$HOME"; fi\ncat <<EOF\nhello\nEOF\n'; 754 const direct = clew(content, { lang: "bash", stream: false }); 755 const stream = clew("", { lang: "bash" }); 756 757 for (const chunk of [ 758 "if true", 759 "; then ", 760 'echo "$H', 761 'OME"; fi\n', 762 "cat <<EOF\nhe", 763 "llo\nEOF\n", 764 ]) { 765 stream.write(chunk); 766 } 767 768 expect(stream.end()).toEqual(direct); 769 }); 770 771 test("produces the same final output regardless of markdown chunk boundaries", () => { 772 const content = [ 773 "# Title", 774 "- item with `code`", 775 "> quote [link](https://example.com)", 776 "```ts", 777 "const value = 1", 778 "```", 779 "", 780 ].join("\n"); 781 const direct = clew(content, { lang: "markdown", stream: false }); 782 const stream = clew("", { lang: "markdown" }); 783 784 for (const chunk of [ 785 "# Tit", 786 "le\n- item ", 787 "with `co", 788 "de`\n> quote ", 789 "[link](https://example.com)\n```ts\nconst", 790 " value = 1\n```\n", 791 ]) { 792 stream.write(chunk); 793 } 794 795 expect(stream.end()).toEqual(direct); 796 }); 797});