1var Stream = require('stream') 2var tap = require('tap') 3var MS = require('../mute.js') 4 5// some marker objects 6var END = {} 7var PAUSE = {} 8var RESUME = {} 9 10function PassThrough () { 11 Stream.call(this) 12 this.readable = this.writable = true 13} 14 15PassThrough.prototype = Object.create(Stream.prototype, { 16 constructor: { 17 value: PassThrough 18 }, 19 write: { 20 value: function (c) { 21 this.emit('data', c) 22 return true 23 } 24 }, 25 end: { 26 value: function (c) { 27 if (c) this.write(c) 28 this.emit('end') 29 } 30 }, 31 pause: { 32 value: function () { 33 this.emit('pause') 34 } 35 }, 36 resume: { 37 value: function () { 38 this.emit('resume') 39 } 40 } 41}) 42 43tap.test('incoming', function (t) { 44 var ms = new MS 45 var str = new PassThrough 46 str.pipe(ms) 47 48 var expect = ['foo', 'boo', END] 49 ms.on('data', function (c) { 50 t.equal(c, expect.shift()) 51 }) 52 ms.on('end', function () { 53 t.equal(END, expect.shift()) 54 t.end() 55 }) 56 str.write('foo') 57 ms.mute() 58 str.write('bar') 59 ms.unmute() 60 str.write('boo') 61 ms.mute() 62 str.write('blaz') 63 str.end('grelb') 64}) 65 66tap.test('outgoing', function (t) { 67 var ms = new MS 68 var str = new PassThrough 69 ms.pipe(str) 70 71 var expect = ['foo', 'boo', END] 72 str.on('data', function (c) { 73 t.equal(c, expect.shift()) 74 }) 75 str.on('end', function () { 76 t.equal(END, expect.shift()) 77 t.end() 78 }) 79 80 ms.write('foo') 81 ms.mute() 82 ms.write('bar') 83 ms.unmute() 84 ms.write('boo') 85 ms.mute() 86 ms.write('blaz') 87 ms.end('grelb') 88}) 89 90tap.test('isTTY', function (t) { 91 var str = new PassThrough 92 str.isTTY = true 93 str.columns=80 94 str.rows=24 95 96 var ms = new MS 97 t.equal(ms.isTTY, false) 98 t.equal(ms.columns, undefined) 99 t.equal(ms.rows, undefined) 100 ms.pipe(str) 101 t.equal(ms.isTTY, true) 102 t.equal(ms.columns, 80) 103 t.equal(ms.rows, 24) 104 str.isTTY = false 105 t.equal(ms.isTTY, false) 106 t.equal(ms.columns, 80) 107 t.equal(ms.rows, 24) 108 str.isTTY = true 109 t.equal(ms.isTTY, true) 110 t.equal(ms.columns, 80) 111 t.equal(ms.rows, 24) 112 ms.isTTY = false 113 t.equal(ms.isTTY, false) 114 t.equal(ms.columns, 80) 115 t.equal(ms.rows, 24) 116 117 ms = new MS 118 t.equal(ms.isTTY, false) 119 str.pipe(ms) 120 t.equal(ms.isTTY, true) 121 str.isTTY = false 122 t.equal(ms.isTTY, false) 123 str.isTTY = true 124 t.equal(ms.isTTY, true) 125 ms.isTTY = false 126 t.equal(ms.isTTY, false) 127 128 t.end() 129}) 130 131tap.test('pause/resume incoming', function (t) { 132 var str = new PassThrough 133 var ms = new MS 134 str.on('pause', function () { 135 t.equal(PAUSE, expect.shift()) 136 }) 137 str.on('resume', function () { 138 t.equal(RESUME, expect.shift()) 139 }) 140 var expect = [PAUSE, RESUME, PAUSE, RESUME] 141 str.pipe(ms) 142 ms.pause() 143 ms.resume() 144 ms.pause() 145 ms.resume() 146 t.equal(expect.length, 0, 'saw all events') 147 t.end() 148}) 149 150tap.test('replace with *', function (t) { 151 var str = new PassThrough 152 var ms = new MS({replace: '*'}) 153 str.pipe(ms) 154 var expect = ['foo', '*****', 'bar', '***', 'baz', 'boo', '**', '****'] 155 156 ms.on('data', function (c) { 157 t.equal(c, expect.shift()) 158 }) 159 160 str.write('foo') 161 ms.mute() 162 str.write('12345') 163 ms.unmute() 164 str.write('bar') 165 ms.mute() 166 str.write('baz') 167 ms.unmute() 168 str.write('baz') 169 str.write('boo') 170 ms.mute() 171 str.write('xy') 172 str.write('xyzΩ') 173 174 t.equal(expect.length, 0) 175 t.end() 176}) 177 178tap.test('replace with ~YARG~', function (t) { 179 var str = new PassThrough 180 var ms = new MS({replace: '~YARG~'}) 181 str.pipe(ms) 182 var expect = ['foo', '~YARG~~YARG~~YARG~~YARG~~YARG~', 'bar', 183 '~YARG~~YARG~~YARG~', 'baz', 'boo', '~YARG~~YARG~', 184 '~YARG~~YARG~~YARG~~YARG~'] 185 186 ms.on('data', function (c) { 187 t.equal(c, expect.shift()) 188 }) 189 190 // also throw some unicode in there, just for good measure. 191 str.write('foo') 192 ms.mute() 193 str.write('ΩΩ') 194 ms.unmute() 195 str.write('bar') 196 ms.mute() 197 str.write('Ω') 198 ms.unmute() 199 str.write('baz') 200 str.write('boo') 201 ms.mute() 202 str.write('Ω') 203 str.write('ΩΩ') 204 205 t.equal(expect.length, 0) 206 t.end() 207}) 208