• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-internals --no-warnings
2'use strict';
3const common = require('../common');
4const assert = require('assert');
5const { WriteStream } = require('tty');
6const { internalBinding } = require('internal/test/binding');
7const { TTY } = internalBinding('tty_wrap');
8const getWindowSize = TTY.prototype.getWindowSize;
9
10function monkeyPatchGetWindowSize(fn) {
11  TTY.prototype.getWindowSize = function() {
12    TTY.prototype.getWindowSize = getWindowSize;
13    return fn.apply(this, arguments);
14  };
15}
16
17{
18  // tty.WriteStream constructor does not define columns and rows if an error
19  // occurs while retrieving the window size from the handle.
20  monkeyPatchGetWindowSize(function() {
21    return -1;
22  });
23
24  const stream = WriteStream(1);
25
26  assert(stream instanceof WriteStream);
27  assert.strictEqual(stream.columns, undefined);
28  assert.strictEqual(stream.rows, undefined);
29}
30
31{
32  // _refreshSize() emits an error if an error occurs while retrieving the
33  // window size from the handle.
34  const stream = WriteStream(1);
35
36  stream.on('error', common.mustCall((err) => {
37    assert.strictEqual(err.syscall, 'getWindowSize');
38  }));
39
40  monkeyPatchGetWindowSize(function() {
41    return -1;
42  });
43
44  stream._refreshSize();
45}
46
47{
48  // _refreshSize() emits a 'resize' event when the window size changes.
49  monkeyPatchGetWindowSize(function(size) {
50    size[0] = 80;
51    size[1] = 24;
52    return 0;
53  });
54
55  const stream = WriteStream(1);
56
57  stream.on('resize', common.mustCall(() => {
58    assert.strictEqual(stream.columns, 82);
59    assert.strictEqual(stream.rows, 26);
60  }));
61
62  assert.strictEqual(stream.columns, 80);
63  assert.strictEqual(stream.rows, 24);
64
65  monkeyPatchGetWindowSize(function(size) {
66    size[0] = 82;
67    size[1] = 26;
68    return 0;
69  });
70
71  stream._refreshSize();
72}
73
74{
75  // WriteStream.prototype.getWindowSize() returns the current columns and rows.
76  monkeyPatchGetWindowSize(function(size) {
77    size[0] = 99;
78    size[1] = 32;
79    return 0;
80  });
81
82  const stream = WriteStream(1);
83
84  assert.strictEqual(stream.columns, 99);
85  assert.strictEqual(stream.rows, 32);
86  assert.deepStrictEqual(stream.getWindowSize(), [99, 32]);
87}
88