File
Blob: src/workerd/api/node/tests/tty-nodejs-test.js
| 1 | // Copyright (c) 2025 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | import * as tty from 'node:tty'; |
| 5 | import { strictEqual, deepStrictEqual } from 'node:assert'; |
| 6 | |
| 7 | export const ttyTest = { |
| 8 | test() { |
| 9 | // Note: test was generated by claude with manual tweaks |
| 10 | |
| 11 | // Test that required exports exist |
| 12 | strictEqual(typeof tty.isatty, 'function'); |
| 13 | strictEqual(typeof tty.ReadStream, 'function'); |
| 14 | strictEqual(typeof tty.WriteStream, 'function'); |
| 15 | |
| 16 | // Test isatty function - should always return false |
| 17 | strictEqual(tty.isatty(0), false); // stdin |
| 18 | strictEqual(tty.isatty(1), false); // stdout |
| 19 | strictEqual(tty.isatty(2), false); // stderr |
| 20 | strictEqual(tty.isatty(3), false); // arbitrary fd |
| 21 | strictEqual(tty.isatty(-1), false); // invalid fd |
| 22 | strictEqual(tty.isatty(999), false); // high fd number |
| 23 | |
| 24 | // Test ReadStream constructor - should succeed and store fd |
| 25 | const readStream = new tty.ReadStream(0); |
| 26 | strictEqual(readStream.fd, 0); |
| 27 | strictEqual(readStream.isRaw, false); |
| 28 | strictEqual(readStream.isTTY, false); |
| 29 | |
| 30 | const readStream2 = new tty.ReadStream(1, {}); |
| 31 | strictEqual(readStream2.fd, 1); |
| 32 | |
| 33 | const readStream3 = new tty.ReadStream(2, { readable: true }); |
| 34 | strictEqual(readStream3.fd, 2); |
| 35 | |
| 36 | // Test WriteStream constructor - should succeed and store fd |
| 37 | const writeStream = new tty.WriteStream(1); |
| 38 | strictEqual(writeStream.fd, 1); |
| 39 | strictEqual(writeStream.columns, 0); |
| 40 | strictEqual(writeStream.rows, 0); |
| 41 | strictEqual(writeStream.isTTY, true); |
| 42 | |
| 43 | const writeStream2 = new tty.WriteStream(2, {}); |
| 44 | strictEqual(writeStream2.fd, 2); |
| 45 | |
| 46 | const writeStream3 = new tty.WriteStream(1, { writable: true }); |
| 47 | strictEqual(writeStream3.fd, 1); |
| 48 | |
| 49 | // Test ReadStream prototype methods and properties |
| 50 | strictEqual(typeof tty.ReadStream.prototype.setRawMode, 'function'); |
| 51 | |
| 52 | // Test WriteStream prototype properties and methods |
| 53 | strictEqual(tty.WriteStream.prototype.isTTY, true); |
| 54 | strictEqual(typeof tty.WriteStream.prototype.getColorDepth, 'function'); |
| 55 | strictEqual(typeof tty.WriteStream.prototype.hasColors, 'function'); |
| 56 | strictEqual(typeof tty.WriteStream.prototype._refreshSize, 'function'); |
| 57 | strictEqual(typeof tty.WriteStream.prototype.cursorTo, 'function'); |
| 58 | strictEqual(typeof tty.WriteStream.prototype.moveCursor, 'function'); |
| 59 | strictEqual(typeof tty.WriteStream.prototype.clearLine, 'function'); |
| 60 | strictEqual(typeof tty.WriteStream.prototype.clearScreenDown, 'function'); |
| 61 | strictEqual(typeof tty.WriteStream.prototype.getWindowSize, 'function'); |
| 62 | |
| 63 | // Test WriteStream instance methods behavior |
| 64 | // Test getColorDepth - should return 8 |
| 65 | const colorDepth = writeStream.getColorDepth(); |
| 66 | strictEqual(colorDepth, 8); |
| 67 | |
| 68 | // Test hasColors - should return false |
| 69 | const hasColors = writeStream.hasColors(); |
| 70 | strictEqual(hasColors, false); |
| 71 | |
| 72 | // Test _refreshSize - should be a no-op (not throw) |
| 73 | writeStream._refreshSize(); |
| 74 | |
| 75 | // Test getWindowSize - should return [columns, rows] |
| 76 | const windowSize = writeStream.getWindowSize(); |
| 77 | deepStrictEqual(windowSize, [0, 0]); |
| 78 | |
| 79 | // Test cursorTo - should be a no-op and return false |
| 80 | strictEqual(writeStream.cursorTo(10, 5), false); |
| 81 | let callbackCalled = false; |
| 82 | strictEqual( |
| 83 | writeStream.cursorTo(0, 0, () => { |
| 84 | callbackCalled = true; |
| 85 | }), |
| 86 | false |
| 87 | ); |
| 88 | strictEqual(callbackCalled, true); |
| 89 | |
| 90 | // Test cursorTo with callback as second argument |
| 91 | callbackCalled = false; |
| 92 | strictEqual( |
| 93 | writeStream.cursorTo(0, () => { |
| 94 | callbackCalled = true; |
| 95 | }), |
| 96 | false |
| 97 | ); |
| 98 | strictEqual(callbackCalled, true); |
| 99 | |
| 100 | // Test moveCursor - should be a no-op and return false |
| 101 | strictEqual(writeStream.moveCursor(1, 1), false); |
| 102 | callbackCalled = false; |
| 103 | strictEqual( |
| 104 | writeStream.moveCursor(-1, 0, () => { |
| 105 | callbackCalled = true; |
| 106 | }), |
| 107 | false |
| 108 | ); |
| 109 | strictEqual(callbackCalled, true); |
| 110 | |
| 111 | // Test clearLine - should be a no-op and return false |
| 112 | strictEqual(writeStream.clearLine(0), false); |
| 113 | callbackCalled = false; |
| 114 | strictEqual( |
| 115 | writeStream.clearLine(1, () => { |
| 116 | callbackCalled = true; |
| 117 | }), |
| 118 | false |
| 119 | ); |
| 120 | strictEqual(callbackCalled, true); |
| 121 | |
| 122 | // Test clearScreenDown - should be a no-op and return false |
| 123 | strictEqual(writeStream.clearScreenDown(), false); |
| 124 | callbackCalled = false; |
| 125 | strictEqual( |
| 126 | writeStream.clearScreenDown(() => { |
| 127 | callbackCalled = true; |
| 128 | }), |
| 129 | false |
| 130 | ); |
| 131 | strictEqual(callbackCalled, true); |
| 132 | |
| 133 | // Test ReadStream setRawMode - should set isRaw and return this |
| 134 | const setRawModeResult = readStream.setRawMode(true); |
| 135 | strictEqual(setRawModeResult, readStream); |
| 136 | strictEqual(readStream.isRaw, true); |
| 137 | |
| 138 | const setRawModeFalseResult = readStream.setRawMode(false); |
| 139 | strictEqual(setRawModeFalseResult, readStream); |
| 140 | strictEqual(readStream.isRaw, false); |
| 141 | |
| 142 | // Test edge cases for isatty |
| 143 | strictEqual(tty.isatty(Number.MAX_SAFE_INTEGER), false); |
| 144 | strictEqual(tty.isatty(Number.MIN_SAFE_INTEGER), false); |
| 145 | strictEqual(tty.isatty(0.5), false); // Non-integer |
| 146 | strictEqual(tty.isatty(NaN), false); |
| 147 | strictEqual(tty.isatty(Infinity), false); |
| 148 | strictEqual(tty.isatty(-Infinity), false); |
| 149 | |
| 150 | // Test that constructors are functions |
| 151 | strictEqual(typeof tty.ReadStream, 'function'); |
| 152 | strictEqual(typeof tty.WriteStream, 'function'); |
| 153 | |
| 154 | // Test default export |
| 155 | strictEqual(typeof tty.default, 'object'); |
| 156 | strictEqual(tty.default.isatty, tty.isatty); |
| 157 | strictEqual(tty.default.ReadStream, tty.ReadStream); |
| 158 | strictEqual(tty.default.WriteStream, tty.WriteStream); |
| 159 | |
| 160 | // Test hasColors with different arguments (should always return false) |
| 161 | strictEqual(writeStream.hasColors(), false); |
| 162 | strictEqual(writeStream.hasColors(8), false); |
| 163 | strictEqual(writeStream.hasColors(256), false); |
| 164 | |
| 165 | // Test getColorDepth with different arguments (should always return 8) |
| 166 | strictEqual(writeStream.getColorDepth(), 8); |
| 167 | strictEqual(writeStream.getColorDepth({}), 8); |
| 168 | |
| 169 | // Test setRawMode with different argument types |
| 170 | strictEqual(readStream.setRawMode(true), readStream); |
| 171 | strictEqual(readStream.setRawMode(false), readStream); |
| 172 | strictEqual(readStream.setRawMode(1), readStream); |
| 173 | strictEqual(readStream.setRawMode(0), readStream); |
| 174 | strictEqual(readStream.setRawMode('true'), readStream); |
| 175 | }, |
| 176 | }; |