|
| 1 | +import {EventEmitter} from 'node:events'; |
| 2 | +import {setTimeout} from 'node:timers/promises'; |
| 3 | +import {test, stub} from 'supertape'; |
| 4 | +import {subscribe} from './subscribe.js'; |
| 5 | + |
| 6 | +const {assign} = Object; |
| 7 | + |
| 8 | +test('putout: engine-reporter: subscribe: keypress', async (t) => { |
| 9 | + const worker = new EventEmitter(); |
| 10 | + const isStop = stub(); |
| 11 | + const keypress = stub().returns({ |
| 12 | + isStop, |
| 13 | + }); |
| 14 | + |
| 15 | + const report = stub(); |
| 16 | + const createReport = stub().returns(report); |
| 17 | + const write = stub(); |
| 18 | + |
| 19 | + const args = []; |
| 20 | + const cwd = '/'; |
| 21 | + const exit = stub(); |
| 22 | + |
| 23 | + await subscribe({ |
| 24 | + write, |
| 25 | + cwd, |
| 26 | + args, |
| 27 | + worker, |
| 28 | + exit, |
| 29 | + createReport, |
| 30 | + keypress, |
| 31 | + }); |
| 32 | + |
| 33 | + t.calledWithNoArgs(keypress); |
| 34 | + t.end(); |
| 35 | +}); |
| 36 | + |
| 37 | +test('putout: engine-reporter: subscribe: createReport', async (t) => { |
| 38 | + const worker = new EventEmitter(); |
| 39 | + const isStop = stub(); |
| 40 | + const keypress = stub().returns({ |
| 41 | + isStop, |
| 42 | + }); |
| 43 | + |
| 44 | + const report = stub(); |
| 45 | + const createReport = stub().returns(report); |
| 46 | + const write = stub(); |
| 47 | + |
| 48 | + const args = []; |
| 49 | + const cwd = '/'; |
| 50 | + const exit = stub(); |
| 51 | + |
| 52 | + await subscribe({ |
| 53 | + write, |
| 54 | + cwd, |
| 55 | + args, |
| 56 | + worker, |
| 57 | + exit, |
| 58 | + createReport, |
| 59 | + keypress, |
| 60 | + }); |
| 61 | + |
| 62 | + const expected = { |
| 63 | + args, |
| 64 | + cwd, |
| 65 | + exit, |
| 66 | + }; |
| 67 | + |
| 68 | + t.calledWith(createReport, [expected]); |
| 69 | + t.end(); |
| 70 | +}); |
| 71 | + |
| 72 | +test('putout: engine-reporter: subscribe: worker: message: progress', async (t) => { |
| 73 | + const worker = new EventEmitter(); |
| 74 | + const isStop = stub(); |
| 75 | + const keypress = stub().returns({ |
| 76 | + isStop, |
| 77 | + }); |
| 78 | + |
| 79 | + const report = stub(); |
| 80 | + const createReport = stub().returns(report); |
| 81 | + const write = stub(); |
| 82 | + |
| 83 | + const args = []; |
| 84 | + const cwd = '/'; |
| 85 | + const exit = stub(); |
| 86 | + |
| 87 | + await subscribe({ |
| 88 | + write, |
| 89 | + cwd, |
| 90 | + args, |
| 91 | + worker, |
| 92 | + exit, |
| 93 | + createReport, |
| 94 | + keypress, |
| 95 | + }); |
| 96 | + |
| 97 | + const data = 'hello'; |
| 98 | + |
| 99 | + worker.emit('message', ['progress', data]); |
| 100 | + |
| 101 | + t.calledWith(report, [data]); |
| 102 | + t.end(); |
| 103 | +}); |
| 104 | + |
| 105 | +test('putout: engine-reporter: subscribe: worker: message: progress: write', async (t) => { |
| 106 | + const worker = new EventEmitter(); |
| 107 | + const isStop = stub(); |
| 108 | + const keypress = stub().returns({ |
| 109 | + isStop, |
| 110 | + }); |
| 111 | + |
| 112 | + const report = stub(); |
| 113 | + const createReport = stub().returns(report); |
| 114 | + const write = stub(); |
| 115 | + |
| 116 | + const args = []; |
| 117 | + const cwd = '/'; |
| 118 | + const exit = stub(); |
| 119 | + |
| 120 | + write.hello = 'ss'; |
| 121 | + |
| 122 | + await subscribe({ |
| 123 | + write, |
| 124 | + cwd, |
| 125 | + args, |
| 126 | + worker, |
| 127 | + exit, |
| 128 | + createReport, |
| 129 | + keypress, |
| 130 | + }); |
| 131 | + |
| 132 | + worker.emit('message', ['progress', 'hello']); |
| 133 | + |
| 134 | + await setTimeout(1); |
| 135 | + |
| 136 | + t.calledWith(write, ['']); |
| 137 | + t.end(); |
| 138 | +}); |
| 139 | + |
| 140 | +test('putout: engine-reporter: subscribe: worker: message: progress: write: line', async (t) => { |
| 141 | + const worker = new EventEmitter(); |
| 142 | + const isStop = stub(); |
| 143 | + const keypress = stub().returns({ |
| 144 | + isStop, |
| 145 | + }); |
| 146 | + |
| 147 | + const report = stub().returns('world'); |
| 148 | + const createReport = stub().returns(report); |
| 149 | + const write = stub(); |
| 150 | + |
| 151 | + const args = []; |
| 152 | + const cwd = '/'; |
| 153 | + const exit = stub(); |
| 154 | + |
| 155 | + await subscribe({ |
| 156 | + write, |
| 157 | + cwd, |
| 158 | + args, |
| 159 | + worker, |
| 160 | + exit, |
| 161 | + createReport, |
| 162 | + keypress, |
| 163 | + }); |
| 164 | + |
| 165 | + worker.emit('message', ['progress', 'hello']); |
| 166 | + |
| 167 | + await setTimeout(1); |
| 168 | + |
| 169 | + t.calledWith(write, ['world']); |
| 170 | + t.end(); |
| 171 | +}); |
| 172 | + |
| 173 | +test('putout: engine-reporter: subscribe: worker: message', async (t) => { |
| 174 | + const worker = new EventEmitter(); |
| 175 | + const isStop = stub(); |
| 176 | + const keypress = stub().returns({ |
| 177 | + isStop, |
| 178 | + }); |
| 179 | + |
| 180 | + const report = stub(); |
| 181 | + const createReport = stub().returns(report); |
| 182 | + const write = stub(); |
| 183 | + |
| 184 | + const args = []; |
| 185 | + const cwd = '/'; |
| 186 | + const exit = stub(); |
| 187 | + |
| 188 | + await subscribe({ |
| 189 | + write, |
| 190 | + cwd, |
| 191 | + args, |
| 192 | + worker, |
| 193 | + exit, |
| 194 | + createReport, |
| 195 | + keypress, |
| 196 | + }); |
| 197 | + |
| 198 | + const data = 'hello'; |
| 199 | + |
| 200 | + worker.emit('message', ['start', data]); |
| 201 | + |
| 202 | + t.notCalled(report); |
| 203 | + t.end(); |
| 204 | +}); |
| 205 | + |
| 206 | +test('putout: engine-reporter: subscribe: worker: message: progress: isStop', async (t) => { |
| 207 | + const postMessage = stub(); |
| 208 | + const worker = new EventEmitter(); |
| 209 | + |
| 210 | + assign(worker, { |
| 211 | + postMessage, |
| 212 | + }); |
| 213 | + const isStop = stub().returns(true); |
| 214 | + |
| 215 | + const keypress = stub().returns({ |
| 216 | + isStop, |
| 217 | + }); |
| 218 | + |
| 219 | + const report = stub().returns('world'); |
| 220 | + const createReport = stub().returns(report); |
| 221 | + const write = stub(); |
| 222 | + |
| 223 | + const args = []; |
| 224 | + const cwd = '/'; |
| 225 | + const exit = stub(); |
| 226 | + |
| 227 | + await subscribe({ |
| 228 | + write, |
| 229 | + cwd, |
| 230 | + args, |
| 231 | + worker, |
| 232 | + exit, |
| 233 | + createReport, |
| 234 | + keypress, |
| 235 | + }); |
| 236 | + |
| 237 | + worker.emit('message', ['progress', 'hello']); |
| 238 | + await setTimeout(1); |
| 239 | + |
| 240 | + const expected = ['stop']; |
| 241 | + |
| 242 | + t.calledWith(postMessage, [expected]); |
| 243 | + t.end(); |
| 244 | +}); |
0 commit comments