summaryrefslogtreecommitdiff
path: root/test/pseudo-tty/test-tty-window-size.js
blob: 2a9891bd19e68edccffdaeaa4ba446f139810a3f (plain)
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
// Flags: --expose-internals --no-warnings
'use strict';
const common = require('../common');
const assert = require('assert');
const { WriteStream } = require('tty');
const { internalBinding } = require('internal/test/binding');
const { TTY } = internalBinding('tty_wrap');
const getWindowSize = TTY.prototype.getWindowSize;

function monkeyPatchGetWindowSize(fn) {
  TTY.prototype.getWindowSize = function() {
    TTY.prototype.getWindowSize = getWindowSize;
    return fn.apply(this, arguments);
  };
}

{
  // tty.WriteStream constructor does not define columns and rows if an error
  // occurs while retrieving the window size from the handle.
  monkeyPatchGetWindowSize(function() {
    return -1;
  });

  const stream = WriteStream(1);

  assert(stream instanceof WriteStream);
  assert.strictEqual(stream.columns, undefined);
  assert.strictEqual(stream.rows, undefined);
}

{
  // _refreshSize() emits an error if an error occurs while retrieving the
  // window size from the handle.
  const stream = WriteStream(1);

  stream.on('error', common.mustCall((err) => {
    assert.strictEqual(err.syscall, 'getWindowSize');
  }));

  monkeyPatchGetWindowSize(function() {
    return -1;
  });

  stream._refreshSize();
}

{
  // _refreshSize() emits a 'resize' event when the window size changes.
  monkeyPatchGetWindowSize(function(size) {
    size[0] = 80;
    size[1] = 24;
    return 0;
  });

  const stream = WriteStream(1);

  stream.on('resize', common.mustCall(() => {
    assert.strictEqual(stream.columns, 82);
    assert.strictEqual(stream.rows, 26);
  }));

  assert.strictEqual(stream.columns, 80);
  assert.strictEqual(stream.rows, 24);

  monkeyPatchGetWindowSize(function(size) {
    size[0] = 82;
    size[1] = 26;
    return 0;
  });

  stream._refreshSize();
}

{
  // WriteStream.prototype.getWindowSize() returns the current columns and rows.
  monkeyPatchGetWindowSize(function(size) {
    size[0] = 99;
    size[1] = 32;
    return 0;
  });

  const stream = WriteStream(1);

  assert.strictEqual(stream.columns, 99);
  assert.strictEqual(stream.rows, 32);
  assert.deepStrictEqual(stream.getWindowSize(), [99, 32]);
}