|
| 1 | +const Koa = require('koa'); |
| 2 | +//const cors = require('koa2-cors'); |
| 3 | +const bodyParser = require('koa-bodyparser'); |
| 4 | +// const Vue = require('vue'); |
| 5 | +const {getRequestFromURL, App} = require('./main'); |
| 6 | +// const { createBundleRenderer } = require('vue-server-renderer') |
| 7 | + |
| 8 | +const JSONResponse = require('../apijson/JSONResponse'); |
| 9 | +const StringUtil = require('../apijson/StringUtil'); |
| 10 | + |
| 11 | +var isCrossEnabled = true; // false; |
| 12 | +var isLoading = false; |
| 13 | +var startTime = 0; |
| 14 | +var endTime = 0; |
| 15 | +var message = ''; |
| 16 | +var error = null; |
| 17 | +var timeMsg = ''; |
| 18 | +var progressMsg = ''; |
| 19 | + |
| 20 | +var progress = 0; |
| 21 | +var accountProgress = 0; |
| 22 | +var testCaseProgress = 0; |
| 23 | +var deepProgress = 0; |
| 24 | +var randomProgress = 0; |
| 25 | + |
| 26 | +function update() { |
| 27 | + if (isLoading != true) { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + if (error != null) { |
| 32 | + isLoading = false; |
| 33 | + } |
| 34 | + |
| 35 | + var curTime = isLoading ? (new Date()).getTime() : null; |
| 36 | + if (endTime <= 0 || isLoading) { |
| 37 | + endTime = curTime; |
| 38 | + } |
| 39 | + var duration = endTime - startTime; |
| 40 | + var dd = new Date(duration + 60000*(new Date().getTimezoneOffset())); |
| 41 | + |
| 42 | + timeMsg = '\n\nStart Time: ' + startTime + ' = ' + new Date(startTime).toLocaleString() |
| 43 | + + (isLoading ? '; \nCurrent' : '\nEnd') + ' Time: ' + endTime + ' = ' + new Date(endTime).toLocaleString() |
| 44 | + + '; \nTime Spent: ' + duration + ' = ' + dd.getHours() + ":" + dd.getMinutes() + ":" + dd.getSeconds() + "." + dd.getMilliseconds(); |
| 45 | + |
| 46 | + var accountDoneCount = App.currentAccountIndex + 1; |
| 47 | + var accountAllCount = App.accounts.length; |
| 48 | + |
| 49 | + accountProgress = isCrossEnabled != true || accountAllCount <= 0 || accountDoneCount >= accountAllCount ? 1 : (accountDoneCount/accountAllCount).toFixed(2); |
| 50 | + testCaseProgress = App.doneCount >= App.allCount ? 1 : (App.doneCount/App.allCount).toFixed(2); |
| 51 | + deepProgress = App.deepDoneCount >= App.deepAllCount ? 1 : (App.deepDoneCount/App.deepAllCount).toFixed(2); |
| 52 | + randomProgress = App.randomDoneCount >= App.randomAllCount ? 1 : (App.randomDoneCount/App.randomAllCount).toFixed(2); |
| 53 | + // progress = accountProgress*testCaseProgress*deepProgress*randomProgress; |
| 54 | + progress = accountProgress >= 1 ? 1 : (accountProgress + (accountAllCount <= 0 ? 1 : 1/accountAllCount*(testCaseProgress |
| 55 | + + (App.allCount <= 0 ? 1 : 1/App.allCount*(deepProgress + (App.deepAllCount <= 0 ? 1 : 1/App.deepAllCount*randomProgress)))))); |
| 56 | + |
| 57 | + if (progress >= 1) { |
| 58 | + isLoading = false; |
| 59 | + } |
| 60 | + |
| 61 | + progressMsg = '\n\nProgress: ' + (100*progress) + '%' |
| 62 | + + '\nTest Account: ' + accountDoneCount + ' / ' + accountAllCount + ' = ' + (100*accountProgress) + '%' |
| 63 | + + '\nTest Case: ' + App.doneCount + ' / ' + App.allCount + ' = ' + (100*testCaseProgress) + '%' |
| 64 | + + '\nDeep Test Case: ' + App.deepDoneCount + ' / ' + App.deepAllCount + ' = ' + (100*deepProgress) + '%' |
| 65 | + + '\nRandom & Order: ' + App.randomDoneCount + ' / ' + App.randomAllCount + ' = ' + (100*randomProgress) + '%'; |
| 66 | +}; |
| 67 | + |
| 68 | +const PORT = 3000; |
| 69 | + |
| 70 | +var done = false; |
| 71 | +const app = new Koa(); |
| 72 | +// app.use(bodyParser()); |
| 73 | +app.use(async ctx => { |
| 74 | + console.log(ctx); |
| 75 | + var origin = ctx.get('Origin') || ctx.get('origin'); |
| 76 | + console.log('origin = ' + origin); |
| 77 | + ctx.set('Access-Control-Max-Age', "1000000"); // "-1"); |
| 78 | + ctx.set('Access-Control-Allow-Origin', origin); |
| 79 | + ctx.set('Access-Control-Allow-Headers', "*"); |
| 80 | + ctx.set('Access-Control-Allow-Credentials', 'true'); |
| 81 | + ctx.set('Access-Control-Allow-Methods', 'GET,HEAD,POST,PUT,DELETE,OPTIONS,TRACE'); |
| 82 | +// ctx.set('Access-Control-Expose-Headers', "*"); |
| 83 | + |
| 84 | + if (ctx.method == null || ctx.method.toUpperCase() == 'OPTIONS') { |
| 85 | + ctx.status = 200; |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + if (ctx.path == '/test/start' || (isLoading != true && ctx.path == '/test')) { |
| 90 | + if (isLoading && ctx.path == '/test/start') { |
| 91 | + ctx.status = 200; |
| 92 | + ctx.body = ctx.response.body = 'Already started auto testing in node, please wait for minutes...'; |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + App.isCrossEnabled = isCrossEnabled; // isCrossEnabled = App.isCrossEnabled; |
| 97 | + if (isCrossEnabled) { |
| 98 | + App.currentAccountIndex = -1; |
| 99 | + } |
| 100 | + |
| 101 | + isLoading = true; |
| 102 | + startTime = (new Date()).getTime(); |
| 103 | + endTime = startTime; |
| 104 | + message = ''; |
| 105 | + error = null; |
| 106 | + timeMsg = ''; |
| 107 | + progressMsg = ''; |
| 108 | + |
| 109 | + progress = 0; |
| 110 | + accountProgress = 0; |
| 111 | + testCaseProgress = 0; |
| 112 | + deepProgress = 0; |
| 113 | + randomProgress = 0; |
| 114 | + |
| 115 | + update(); |
| 116 | + |
| 117 | + App.key = ctx.query.key; |
| 118 | + if (StringUtil.isNotEmpty(App.key, true)) { |
| 119 | + App.testCaseCount = App.data.testCaseCount = 1000; |
| 120 | + App.randomCount = App.data.randomCount = 200; |
| 121 | + App.randomSubCount = App.data.randomSubCount = 500; |
| 122 | + } |
| 123 | + |
| 124 | + App.autoTest(function (msg, err) { |
| 125 | + message = msg; |
| 126 | + error = err; |
| 127 | + update(); |
| 128 | + console.log('autoTest callback(' + msg + ')' + timeMsg + progressMsg); |
| 129 | + return Number.isNaN(progress) != true && progress >= 1; |
| 130 | + }); |
| 131 | + isLoading = true; |
| 132 | + isCrossEnabled = App.isCrossEnabled; |
| 133 | + |
| 134 | + ctx.status = ctx.response.status = 200; // 302; |
| 135 | + ctx.body = ctx.response.body = JSON.stringify({ |
| 136 | + 'code': 200, |
| 137 | + 'msg': 'Auto testing in node...' |
| 138 | + }); |
| 139 | + |
| 140 | + // setTimeout(function () { // 延迟无效 |
| 141 | + ctx.redirect('/test/status'); |
| 142 | + // }, 1000) |
| 143 | + } |
| 144 | + else if (ctx.path == '/test/status' || (isLoading && ctx.path == '/test')) { |
| 145 | + update(); |
| 146 | + if (isLoading) { |
| 147 | + // ctx.response.header['refresh'] = "1"; |
| 148 | + // ctx.redirect('/status'); |
| 149 | + } |
| 150 | + |
| 151 | + var server = App.server; |
| 152 | + var ind = server == null ? -1 : server.indexOf('?'); |
| 153 | + |
| 154 | + ctx.status = ctx.response.status = 200; // progress >= 1 ? 200 : 302; |
| 155 | + ctx.body = ctx.response.body = JSON.stringify({ |
| 156 | + 'code': 200, |
| 157 | + 'msg': (message || (progress < 1 || isLoading ? 'Auto testing in node...' : 'Done auto testing in node.')) + timeMsg + progressMsg, |
| 158 | + 'progress': progress, |
| 159 | + 'reportId': App.reportId, |
| 160 | + 'link': server + (ind < 0 ? '?' : '&') + 'reportId=' + App.reportId |
| 161 | + }); |
| 162 | + } |
| 163 | + else if (ctx.path == '/test/compare' || ctx.path == '/test/ml') { |
| 164 | + done = false; |
| 165 | +// var json = ''; |
| 166 | +// ctx.req.addListener('data', (data) => { |
| 167 | +// json += data; |
| 168 | +// }) |
| 169 | +// ctx.req.addListener('end', function() { |
| 170 | +// console.log(json); |
| 171 | + var body = ctx.body || ctx.req.body || ctx.request.body || {} // || JSON.parse(json) || {}; |
| 172 | + console.log(body); |
| 173 | + var isML = ctx.path == '/test/ml' || body.isML; |
| 174 | + var res = body.response; |
| 175 | + var stdd = body.standard; |
| 176 | + var response = typeof res != 'string' ? res : (StringUtil.isEmpty(res, true) ? null : JSON.parse(res)); |
| 177 | + var standard = typeof stdd != 'string' ? stdd : (StringUtil.isEmpty(stdd, true) ? null : JSON.parse(stdd)); |
| 178 | + console.log('\n\nresponse = ' + JSON.stringify(response)); |
| 179 | + console.log('\n\nstdd = ' + JSON.stringify(stdd)); |
| 180 | + var compare = JSONResponse.compareResponse(null, standard, response || {}, '', isML, null, null, false) || {} |
| 181 | + |
| 182 | + if (body.newStandard) { |
| 183 | + compare.newStandard = JSONResponse.updateFullStandard(standard, response, isML) |
| 184 | + } |
| 185 | + console.log('\n\ncompare = ' + JSON.stringify(compare)); |
| 186 | + |
| 187 | + ctx.status = ctx.response.status = 200; |
| 188 | + ctx.body = ctx.response.body = compare == null ? '' : JSON.stringify(compare); |
| 189 | + done = true; |
| 190 | +// }) |
| 191 | +// while (true) { |
| 192 | +// if (done) { |
| 193 | +// break; |
| 194 | +// } |
| 195 | +// } |
| 196 | + } |
| 197 | +}); |
| 198 | + |
| 199 | +app.listen(PORT); |
| 200 | + |
| 201 | +console.log(`已启动 Node HTTP 服务,可以 |
| 202 | +GET http://localhost:${PORT}/test/start |
| 203 | +来启动后台回归测试,或者 |
| 204 | +GET http://localhost:${PORT}/test/status |
| 205 | +来查询测试进度。`); |
0 commit comments