|
| 1 | +var fs = require('fs'); |
| 2 | +var path = require('path'); |
| 3 | +var verify = require('adventure-verify'); |
| 4 | +var concat = require('concat-stream'); |
| 5 | +var http = require('http'); |
| 6 | +var shoe = require('shoe'); |
| 7 | +var ecstatic = require('ecstatic')(path.join(__dirname, 'static')); |
| 8 | +var split = require('split'); |
| 9 | +var through = require('through2'); |
| 10 | +var colorname = require('colornames'); |
| 11 | + |
| 12 | +exports.problem = fs.createReadStream(__dirname + '/problem.txt'); |
| 13 | +exports.solution = fs.createReadStream(__dirname + '/solution.txt'); |
| 14 | + |
| 15 | +exports.verify = verify({ modeReset: true }, function (args, t) { |
| 16 | + var expected = [ |
| 17 | + [ [ 'msg', 'howdee pardner' ], 'emits the message event' ], |
| 18 | + [ [ 'bg', 'purple' ], 'textarea background set to purple' ], |
| 19 | + [ [ 'fg', 'yellow' ], 'textarea foreground set to yellow' ], |
| 20 | + [ [ 'style', true ], 'using a style tag' ] |
| 21 | + ]; |
| 22 | + t.plan(expected.length); |
| 23 | + process.stdin.pipe(concat(function (body) { |
| 24 | + createServer(body, t).pipe(through.obj(function (row, enc, next) { |
| 25 | + var xex = expected.shift(); |
| 26 | + var ex = xex[0], desc = xex[1]; |
| 27 | + |
| 28 | + if (row[0] === 'fg' || row[0] === 'bg') { |
| 29 | + if (ex[1] !== row[1] && /^#/.test(row[1])) { |
| 30 | + ex[1] = colorname(ex[1]); |
| 31 | + } |
| 32 | + else if (ex[1] !== row[1] && /^rgb/.test(row[1])) { |
| 33 | + var rgb = colorname(ex[1]).match(/\w{2}/g); |
| 34 | + ex[1] = 'rgb(' + rgb.map(function (s) { |
| 35 | + return parseInt(s, 16); |
| 36 | + }).join(',') + ')'; |
| 37 | + row[1] = row[1].replace(/\s+/g, ''); |
| 38 | + } |
| 39 | + } |
| 40 | + t.deepEqual(row, ex, desc); |
| 41 | + next(); |
| 42 | + })); |
| 43 | + })); |
| 44 | +}); |
| 45 | + |
| 46 | +function createServer (body, t) { |
| 47 | + var output = through.obj(); |
| 48 | + var server = http.createServer(function (req, res) { |
| 49 | + if (req.url === '/code.js') { |
| 50 | + res.end(body); |
| 51 | + } |
| 52 | + else ecstatic(req, res) |
| 53 | + }); |
| 54 | + server.listen(55500, function () { |
| 55 | + console.log('Web server running. Visit this URL:' |
| 56 | + + ' http://localhost:' + server.address().port |
| 57 | + ); |
| 58 | + }); |
| 59 | + var sock = shoe(function (stream) { |
| 60 | + stream.pipe(split()).pipe(through.obj(function (buf, enc, next) { |
| 61 | + var line = buf.toString('utf8'); |
| 62 | + try { var row = JSON.parse(line) } |
| 63 | + catch (err) { |
| 64 | + if (t) return t.fail(err) |
| 65 | + else console.error(err); |
| 66 | + } |
| 67 | + |
| 68 | + this.push(row); |
| 69 | + next(); |
| 70 | + })).pipe(output); |
| 71 | + |
| 72 | + if (t) t.once('end', function () { stream.end() }); |
| 73 | + }); |
| 74 | + |
| 75 | + if (t) t.once('end', function () { |
| 76 | + server.close(); |
| 77 | + setTimeout(function () { |
| 78 | + process.exit(); |
| 79 | + }, 100); |
| 80 | + }); |
| 81 | + sock.install(server, '/sock'); |
| 82 | + return output; |
| 83 | +} |
| 84 | + |
| 85 | +exports.run = function (args) { |
| 86 | + process.stdin.pipe(concat(function (body) { |
| 87 | + createServer(body).pipe(through.obj(function (row, enc, next) { |
| 88 | + console.log(row); |
| 89 | + next(); |
| 90 | + })); |
| 91 | + })); |
| 92 | +}; |
0 commit comments