-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathterm.test.ts
More file actions
262 lines (235 loc) · 7.85 KB
/
Copy pathterm.test.ts
File metadata and controls
262 lines (235 loc) · 7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import { beforeEach, describe, expect, it } from "./suite.ts";
import { createTerm, type Term } from "../term.ts";
import { close, fixed, grow, open, rgba, text } from "../ops.ts";
import { print } from "./print.ts";
const decode = (bytes: Uint8Array) => new TextDecoder().decode(bytes);
const trim = (s: string) => s.split("\n").map((l) => l.trimEnd()).join("\n");
describe("term", () => {
let term: Term;
beforeEach(async () => {
term = await createTerm({ width: 40, height: 10 });
});
it("renders hello world", () => {
let out = print(
decode(
term.render([
open("root", {
layout: { width: grow(), height: grow(), direction: "ttb" },
}),
text("Hello, World!"),
close(),
]).output,
),
40,
10,
);
expect(out).toContain("Hello, World!");
});
it("text inherits parent background", () => {
let ansi = decode(
term.render([
open("root", {
layout: { width: grow(), height: grow(), direction: "ttb" },
bg: rgba(255, 0, 0),
}),
text("hi"),
close(),
]).output,
);
// the SGR active when "h" is emitted should include the
// parent's red background (48;2;255;0;0), not terminal default
let before = ansi.slice(0, ansi.indexOf("h"));
expect(before).toContain("\x1b[48;2;255;0;0");
});
it("renders borders and padding", () => {
let out = print(
decode(
term.render([
open("box", {
layout: {
width: grow(),
height: grow(),
padding: { left: 5, top: 5 },
direction: "ttb",
},
border: {
color: rgba(0, 255, 0),
left: 1,
right: 1,
top: 1,
bottom: 1,
},
cornerRadius: { tl: 1, tr: 1, bl: 1, br: 1 },
}),
text("padded"),
close(),
]).output,
),
40,
10,
);
expect(out).toEqual(`
╭──────────────────────────────────────╮
│ │
│ │
│ │
│ │
│ padded │
│ │
│ │
│ │
╰──────────────────────────────────────╯`.trim());
});
describe("line mode", () => {
let box = (msg: string) => [
open("root", {
layout: { width: grow(), height: grow(), direction: "ttb" },
}),
open("box", {
layout: {
width: grow(),
height: grow(),
direction: "ttb",
padding: { left: 1, top: 1 },
},
border: {
color: rgba(255, 255, 255),
left: 1,
right: 1,
top: 1,
bottom: 1,
},
}),
text(msg),
close(),
close(),
];
it("renders with newlines instead of CUP sequences", async () => {
let term = await createTerm({ width: 20, height: 5 });
let out = decode(
term.render(box("hello world"), { mode: "line" }).output,
);
// deno-lint-ignore no-control-regex
expect(out).not.toMatch(/\x1b\[\d+;\d+H/);
expect(out.split("\n").length).toBe(5);
expect(trim(print(out, 20, 5))).toEqual(`
┌──────────────────┐
│hello world │
│ │
│ │
└──────────────────┘`.trim());
if (Deno.build.os === "windows") {
expect(out.startsWith("\x1b[?7l")).toBe(true);
expect(out.endsWith("\x1b[?7h")).toBe(true);
}
});
it("primes front buffer for subsequent diff render", async () => {
let term = await createTerm({ width: 20, height: 5 });
let first = decode(
term.render(box("hello world"), { mode: "line" }).output,
);
let second = decode(term.render(box("goodbye")).output);
expect(trim(print(first + second, 20, 5))).toEqual(`
┌──────────────────┐
│goodbye │
│ │
│ │
└──────────────────┘`.trim());
if (Deno.build.os === "windows") {
expect(second.startsWith("\x1b[?7l\x1b[H")).toBe(true);
expect(second).toContain("\r\n");
expect(second.endsWith("\x1b[?7h")).toBe(true);
} else {
expect(second.length).toBeLessThan(first.length);
}
});
});
describe("info", () => {
it("returns bounds for named elements", async () => {
let term = await createTerm({ width: 40, height: 10 });
let result = term.render([
open("root", {
layout: { width: grow(), height: grow(), direction: "ttb" },
}),
open("child", {
layout: { width: fixed(20), height: fixed(5) },
}),
close(),
close(),
]);
let root = result.info.get("root");
expect(root).toBeDefined();
expect(root!.bounds).toEqual({ x: 0, y: 0, width: 40, height: 10 });
let child = result.info.get("child");
expect(child).toBeDefined();
expect(child!.bounds).toEqual({ x: 0, y: 0, width: 20, height: 5 });
});
it("returns undefined for unknown ids", async () => {
let term = await createTerm({ width: 20, height: 5 });
term.render([
open("root", { layout: { width: grow(), height: grow() } }),
close(),
]);
let result = term.render([
open("root", { layout: { width: grow(), height: grow() } }),
close(),
]);
expect(result.info.get("nonexistent")).toBeUndefined();
expect(result.info.get("")).toBeUndefined();
});
});
describe("row offset", () => {
it("renders two frames at the offset position", async () => {
let term = await createTerm({ width: 20, height: 5 });
let box = (msg: string) => [
open("root", {
layout: { width: grow(), height: grow(), direction: "ttb" },
}),
open("box", {
layout: {
width: grow(),
height: grow(),
direction: "ttb",
padding: { left: 1, top: 1 },
},
border: {
color: rgba(255, 255, 255),
left: 1,
right: 1,
top: 1,
bottom: 1,
},
}),
text(msg),
close(),
close(),
];
let header = await createTerm({ width: 20, height: 5 });
let banner = decode(header.render(box("hello")).output);
let first = decode(term.render(box("world"), { row: 6 }).output);
expect(print(banner + first, 20, 10)).toEqual(`\
┌──────────────────┐
│hello │
│ │
│ │
└──────────────────┘
┌──────────────────┐
│world │
│ │
│ │
└──────────────────┘`);
let second = decode(term.render(box("universe"), { row: 6 }).output);
expect(print(banner + first + second, 20, 10)).toEqual(`\
┌──────────────────┐
│hello │
│ │
│ │
└──────────────────┘
┌──────────────────┐
│universe │
│ │
│ │
└──────────────────┘`);
});
});
});