MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 90 lines 4.8 kB view raw
1import { test, testThrows, summary } from './helpers.js'; 2import * as tty from 'node:tty'; 3 4console.log('TTY Tests\n'); 5 6test('tty module exists', typeof tty, 'object'); 7test('tty toStringTag', Object.prototype.toString.call(tty), '[object tty]'); 8test('tty.isatty is function', typeof tty.isatty, 'function'); 9test('tty.ReadStream is function', typeof tty.ReadStream, 'function'); 10test('tty.WriteStream is function', typeof tty.WriteStream, 'function'); 11 12test('tty.isatty(-1) is false', tty.isatty(-1), false); 13test('tty.isatty(0) is boolean', typeof tty.isatty(0), 'boolean'); 14test('tty.isatty(1) is boolean', typeof tty.isatty(1), 'boolean'); 15test('tty.isatty(2) is boolean', typeof tty.isatty(2), 'boolean'); 16 17test('stdin has fd', typeof process.stdin.fd, 'number'); 18test('stdout has fd', typeof process.stdout.fd, 'number'); 19test('stderr has fd', typeof process.stderr.fd, 'number'); 20 21test('stdin toStringTag', Object.prototype.toString.call(process.stdin), '[object ReadStream]'); 22test('stdout toStringTag', Object.prototype.toString.call(process.stdout), '[object WriteStream]'); 23test('stderr toStringTag', Object.prototype.toString.call(process.stderr), '[object WriteStream]'); 24 25test('stdin.setRawMode is function', typeof process.stdin.setRawMode, 'function'); 26test('stdout.clearLine is function', typeof process.stdout.clearLine, 'function'); 27test('stdout.clearScreenDown is function', typeof process.stdout.clearScreenDown, 'function'); 28test('stdout.cursorTo is function', typeof process.stdout.cursorTo, 'function'); 29test('stdout.moveCursor is function', typeof process.stdout.moveCursor, 'function'); 30test('stdout.getWindowSize is function', typeof process.stdout.getWindowSize, 'function'); 31test('stdout.getColorDepth is function', typeof process.stdout.getColorDepth, 'function'); 32test('stdout.hasColors is function', typeof process.stdout.hasColors, 'function'); 33 34const ws = process.stdout.getWindowSize(); 35test('getWindowSize returns array', Array.isArray(ws), true); 36test('getWindowSize length is 2', ws.length, 2); 37test('getWindowSize cols is number', typeof ws[0], 'number'); 38test('getWindowSize rows is number', typeof ws[1], 'number'); 39 40const depthDefault = process.stdout.getColorDepth(); 41test('getColorDepth returns number', typeof depthDefault, 'number'); 42test('getColorDepth returns valid depth', [1, 4, 8, 24].includes(depthDefault), true); 43 44const depthForced = process.stdout.getColorDepth({ FORCE_COLOR: '3' }); 45test('getColorDepth(force truecolor) = 24', depthForced, 24); 46const depthNoColor = process.stdout.getColorDepth({ NO_COLOR: '1' }); 47test('getColorDepth(NO_COLOR) = 1', depthNoColor, 1); 48 49test('hasColors() returns boolean', typeof process.stdout.hasColors(), 'boolean'); 50test('hasColors(force truecolor) supports 16m', process.stdout.hasColors(16777216, { FORCE_COLOR: '3' }), true); 51test('hasColors(NO_COLOR) rejects 16 colors', process.stdout.hasColors(16, { NO_COLOR: '1' }), false); 52 53const clearLineResult = process.stdout.clearLine(0); 54test('clearLine returns stream', clearLineResult, process.stdout); 55const clearDownResult = process.stdout.clearScreenDown(); 56test('clearScreenDown returns stream', clearDownResult, process.stdout); 57const cursorToResult = process.stdout.cursorTo(0); 58test('cursorTo returns stream', cursorToResult, process.stdout); 59const moveCursorResult = process.stdout.moveCursor(0, 0); 60test('moveCursor returns stream', moveCursorResult, process.stdout); 61 62if (process.stdin.isTTY) { 63 const stdinCtor = new tty.ReadStream(0); 64 test('new ReadStream(0) returns stdin-like stream', Object.prototype.toString.call(stdinCtor), '[object ReadStream]'); 65 test('ReadStream constructor reuses stdin stream', stdinCtor === process.stdin, true); 66 67 const rawResult = process.stdin.setRawMode(false); 68 test('stdin.setRawMode returns this', rawResult, process.stdin); 69 test('stdin.isRaw is boolean', typeof process.stdin.isRaw, 'boolean'); 70} else { 71 testThrows('new ReadStream(0) throws when stdin is not TTY', () => new tty.ReadStream(0)); 72} 73 74if (process.stdout.isTTY) { 75 const stdoutCtor = new tty.WriteStream(1); 76 test('new WriteStream(1) returns stdout-like stream', Object.prototype.toString.call(stdoutCtor), '[object WriteStream]'); 77 test('WriteStream constructor reuses stdout stream', stdoutCtor === process.stdout, true); 78} else { 79 testThrows('new WriteStream(1) throws when stdout is not TTY', () => new tty.WriteStream(1)); 80} 81 82if (process.stderr.isTTY) { 83 const stderrCtor = new tty.WriteStream(2); 84 test('new WriteStream(2) returns stderr-like stream', Object.prototype.toString.call(stderrCtor), '[object WriteStream]'); 85 test('WriteStream constructor reuses stderr stream', stderrCtor === process.stderr, true); 86} else { 87 testThrows('new WriteStream(2) throws when stderr is not TTY', () => new tty.WriteStream(2)); 88} 89 90summary();