forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-repl-multiline-navigation.js
More file actions
321 lines (276 loc) Β· 8.78 KB
/
test-repl-multiline-navigation.js
File metadata and controls
321 lines (276 loc) Β· 8.78 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
'use strict';
// Flags: --expose-internals
const common = require('../common');
const assert = require('assert');
const repl = require('internal/repl');
const stream = require('stream');
class ActionStream extends stream.Stream {
run(data) {
const _iter = data[Symbol.iterator]();
const doAction = () => {
const next = _iter.next();
if (next.done) {
// Close the repl. Note that it must have a clean prompt to do so.
this.emit('keypress', '', { ctrl: true, name: 'd' });
return;
}
const action = next.value;
if (typeof action === 'object') {
this.emit('keypress', '', action);
}
setImmediate(doAction);
};
doAction();
}
write(chunk) {
const chunkLines = chunk.toString('utf8').split('\n');
this.lines[this.lines.length - 1] += chunkLines[0];
if (chunkLines.length > 1) {
this.lines.push(...chunkLines.slice(1));
}
this.emit('line', this.lines[this.lines.length - 1]);
return true;
}
resume() {}
pause() {}
}
ActionStream.prototype.readable = true;
const tmpdir = require('../common/tmpdir');
tmpdir.refresh();
{
const historyPath = tmpdir.resolve(`.${Math.floor(Math.random() * 10000)}`);
// Make sure the cursor is at the right places.
// If the cursor is at the end of a long line and the down key is pressed,
// Move the cursor to the end of the next line, if shorter.
const checkResults = common.mustSucceed((r) => {
r.write('let str = `');
r.input.run([{ name: 'enter' }]);
r.write('111');
r.input.run([{ name: 'enter' }]);
r.write('22222222222222');
r.input.run([{ name: 'enter' }]);
r.write('3`');
r.input.run([{ name: 'enter' }]);
r.input.run([{ name: 'up' }]);
assert.strictEqual(r.cursor, 33);
r.input.run([{ name: 'up' }]);
assert.strictEqual(r.cursor, 18);
for (let i = 0; i < 5; i++) {
r.input.run([{ name: 'right' }]);
}
r.input.run([{ name: 'up' }]);
assert.strictEqual(r.cursor, 15);
r.input.run([{ name: 'up' }]);
for (let i = 0; i < 4; i++) {
r.input.run([{ name: 'right' }]);
}
assert.strictEqual(r.cursor, 11);
r.input.run([{ name: 'down' }]);
assert.strictEqual(r.cursor, 15);
r.input.run([{ name: 'down' }]);
assert.strictEqual(r.cursor, 27);
});
repl.createInternalRepl(
{ NODE_REPL_HISTORY: historyPath },
{
terminal: true,
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
next();
}
}),
},
checkResults
);
}
{
const historyPath = tmpdir.resolve(`.${Math.floor(Math.random() * 10000)}`);
// Make sure the cursor is at the right places.
// This is testing cursor clamping and restoring when moving up and down from long lines.
const checkResults = common.mustSucceed((r) => {
r.write('let ddd = `000');
r.input.run([{ name: 'enter' }]);
r.write('1111111111111');
r.input.run([{ name: 'enter' }]);
r.write('22222');
r.input.run([{ name: 'enter' }]);
r.write('2222');
r.input.run([{ name: 'enter' }]);
r.write('22222');
r.input.run([{ name: 'enter' }]);
r.write('33333333`');
r.input.run([{ name: 'up' }]);
assert.strictEqual(r.cursor, 45);
r.input.run([{ name: 'up' }]);
assert.strictEqual(r.cursor, 39);
r.input.run([{ name: 'up' }]);
assert.strictEqual(r.cursor, 34);
r.input.run([{ name: 'up' }]);
assert.strictEqual(r.cursor, 24);
r.input.run([{ name: 'right' }]);
// This is to reach a cursor pos which is much higher than the line we want to go to,
// So we can check that the cursor is clamped to the end of the line.
r.input.run([{ name: 'right' }]);
r.input.run([{ name: 'down' }]);
assert.strictEqual(r.cursor, 34);
r.input.run([{ name: 'down' }]);
assert.strictEqual(r.cursor, 39);
r.input.run([{ name: 'down' }]);
assert.strictEqual(r.cursor, 45);
r.input.run([{ name: 'down' }]);
assert.strictEqual(r.cursor, 55);
});
repl.createInternalRepl(
{ NODE_REPL_HISTORY: historyPath },
{
terminal: true,
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
next();
}
}),
},
checkResults
);
}
{
const historyPath = tmpdir.resolve(`.${Math.floor(Math.random() * 10000)}`);
// If the last command errored and the user is trying to edit it,
// The errored line should be removed from history
const checkResults = common.mustSucceed((r) => {
r.write('let lineWithMistake = `I have some');
r.input.run([{ name: 'enter' }]);
r.write('problem with` my syntax\'');
r.input.run([{ name: 'enter' }]);
r.input.run([{ name: 'up' }]);
r.input.run([{ name: 'backspace' }]);
r.write('`');
for (let i = 0; i < 11; i++) {
r.input.run([{ name: 'left' }]);
}
r.input.run([{ name: 'backspace' }]);
r.input.run([{ name: 'enter' }]);
assert.strictEqual(r.history.length, 1);
// Check that the line is properly set in the history structure
assert.strictEqual(r.history[0], 'problem with my syntax`\rlet lineWithMistake = `I have some');
assert.strictEqual(r.line, '');
r.input.run([{ name: 'up' }]);
// Check that the line is properly displayed
assert.strictEqual(r.line, 'let lineWithMistake = `I have some\nproblem with my syntax`');
});
repl.createInternalRepl(
{ NODE_REPL_HISTORY: historyPath },
{
terminal: true,
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
next();
}
}),
},
checkResults
);
}
{
const historyPath = tmpdir.resolve(`.${Math.floor(Math.random() * 10000)}`);
const outputBuffer = [];
// Test that the REPL preview is properly shown on multiline commands
// And deleted when enter is pressed
const checkResults = common.mustSucceed((r) => {
r.write('Array(100).fill(');
r.input.run([{ name: 'enter' }]);
r.write('123');
r.input.run([{ name: 'enter' }]);
r.write(')');
r.input.run([{ name: 'enter' }]);
r.input.run([{ name: 'up' }]);
r.input.run([{ name: 'up' }]);
assert.deepStrictEqual(r.last, new Array(100).fill(123));
r.input.run([{ name: 'enter' }]);
assert.strictEqual(outputBuffer.includes('[\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123, 123, 123, 123, 123, 123, 123, 123, 123, 123, 123,\n' +
' 123\n' +
']\n'), true);
});
repl.createInternalRepl(
{ NODE_REPL_HISTORY: historyPath },
{
preview: true,
terminal: true,
input: new ActionStream(),
output: new stream.Writable({
write(chunk, _, next) {
// Store each chunk in the buffer
outputBuffer.push(chunk.toString());
next();
}
}),
},
checkResults
);
}
{
const historyPath = tmpdir.resolve(`.${Math.floor(Math.random() * 10000)}`);
// Test for issue #59938: REPL cursor navigation in viewport overflow
// This test just verifies cursor position consistency when input exceeds
// terminal height. The actual screen rendering fix is harder to test
// but can be manually verified.
class MockOutput extends stream.Writable {
constructor() {
super({
write(chunk, _, next) {
next();
}
});
this.columns = 80;
this.rows = 20;
}
}
const checkResults = common.mustSucceed((r) => {
r.write('const arr = [');
r.input.run([{ name: 'enter' }]);
for (let i = 1; i <= 25; i++) {
r.write(` ${i},`);
r.input.run([{ name: 'enter' }]);
}
r.write('];');
const endCursor = r.cursor;
assert.ok(endCursor > 0);
for (let i = 0; i < 27; i++) {
r.input.run([{ name: 'up' }]);
}
assert.strictEqual(r.cursor, 0);
const prevRowsAtTop = r.prevRows;
assert.ok(prevRowsAtTop !== undefined);
for (let i = 0; i < 10; i++) {
r.input.run([{ name: 'down' }]);
}
const midCursor = r.cursor;
assert.ok(midCursor > 0 && midCursor < endCursor);
// Navigate back to end
for (let i = 0; i < 17; i++) {
r.input.run([{ name: 'down' }]);
}
assert.strictEqual(r.cursor, endCursor);
});
repl.createInternalRepl(
{ NODE_REPL_HISTORY: historyPath },
{
terminal: true,
input: new ActionStream(),
output: new MockOutput(),
},
checkResults
);
}