Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

notebookOutput.test.ts

66 lines2.9 KB

1import { expect, it } from "vite-plus/test";
2​
3import { notebookStreamText } from "./notebookOutput.ts";
4​
5it.each([
6 ["Processing…\r\u001b[2KDone", "Done"],
7 ["Status: waiting\r\u001b[9G\u001b[\0Kdone", "Status: done"],
8 ["Status: waiting\u001b[7\0D\u001b[Kdone", "Status: done"],
9 ["Status: waiting\r\u001b\0[9G\u001b[Kdone", "Status: done"],
10 ["Status: waiting\r\u001b[9G\u001b[Kdone", "Status: done"],
11 ["Status: waiting\u001b[7D\u001b[Kdone", "Status: done"],
12 ["Status: waiting\u001b[1;9f\u001b[Kdone", "Status: done"],
13 ["界界界界\twaiting\u001b[17G\u001b[Kdone", "界界界界 done"],
14 ["12345678waiting\r\t\u001b[Kdone", "12345678done"],
15 ["done\t\u001b[K", "done"],
16 ["\t\t\t\u001b[1CX\u001b[K", " ".repeat(25) + "X"],
17 [("\t".repeat(8) + "X\r\u001b[2K").repeat(20) + "Done", "Done"],
18 ["\t".repeat(8) + "waiting" + "\u001b[1K".repeat(20) + "\r\u001b[2KDone", "Done"],
19 ["\t".repeat(8) + "waiting" + "\rZ\u001b[72G\u001b[1K".repeat(100) + "\r\u001b[2KDone", "Done"],
20 ["\t".repeat(8) + "waiting" + "\u001b[1K".repeat(20) + "\rZ\u001b[1Kdone\u001b[K", " done"],
21 ["\u009b31mStatus: waiting\r\u009b9G\u009bKdone", "Status: done"],
22 ["HELLO\u001b[s\u001b[1G\u001b[KX\u001b[u!", "X !"],
23 ["HELLO\u001b\u00007\u001b[1G\u001b[KX\u001b\u00008!", "X !"],
24 ["def\u001b[F\u001b[Kdone", "done"],
25 ["A\u001b[31ḿ\u001b[K", "Á"],
26 ["界界界\rABC\rX\u001b[KDone", "XDone"],
27 ["界界界\rABC\rX\u001b[1K!", " !C 界"],
28 ["\uD83D\0\uDE00\u001b[2G\u001b[K!", "\uD83D!"],
29 ["keep\u001b]0;\u009b2K\u0007end", "keepend"],
30 ["keep\u001b\0]0;\u009b2K\u0007end\u001b[K", "keepend"],
31 ["keep\u001b]0;\u001b[2K\u0007end", " end"],
32 ["界".repeat(8_000) + "\rX".repeat(5_000) + "\r\u001b[2KDone", "Done"],
33 ["á".repeat(110_000) + "\u001b[K".repeat(110_000) + "\r\u001b[2KDone", "Done"],
34 [
35 "a".repeat(1_000_000) + "\u001b[1K".repeat(1_000_000) + "\r" + "a".repeat(1_000_000),
36 "a".repeat(1_000_000),
37 ],
38 ["a" + "\u001b[999999C\u001b[2Kb\u001b[1G".repeat(100_000), "a" + "b".repeat(100_000)],
39])(
40 "applies horizontal cursor movement and line erases in a notebook stream (%#)",
41 (input, expected) => {
42 expect(notebookStreamText(input)).toBe(expected);
43 },
44);
45​
46it.each([
47 ["a\tbc\rXYZW", "XYZW bc"],
48 ["abcdefghij\r\tX", "abcdefghXj"],
49 ["ab\u009b31mcdefg\th\rZ", "Zbcdefg h"],
50 ["ab\u001b]0;title\u0007cdefg\th\rZ", "Zbcdefg h"],
51 ["a\tb\rX\bY", "Y b"],
52])("uses tab stops in a carriage-return redraw (%#)", (input, expected) => {
53 expect(notebookStreamText(input)).toBe(expected);
54});
55​
56it("bounds cursor padding across many short notebook lines", () => {
57 const input = "\u001b[999999999C\u001b[Kb\n\u009b999999999G\u009bKc\n".repeat(200);
58 const output = notebookStreamText(input);
59 expect(output.length).toBeLessThanOrEqual(input.length + 400);
60 expect(
61 output
62 .trim()
63 .split("\n")
64 .map((line) => line.trimStart()),
65 ).toEqual(Array.from({ length: 200 }, () => ["b", "c"]).flat());
66});