From ce1f96ac6190b57026ed096d8d68b023ac3d3549 Mon Sep 17 00:00:00 2001 From: ansuz Date: Tue, 23 Feb 2021 14:23:34 +0530 Subject: [PATCH] clean up /assert/ scripts and create a new instance checkup page --- www/assert/assertions.js | 48 ++++++++++++++ www/assert/index.html | 6 +- www/assert/main.js | 134 ++++++++------------------------------- www/checkup/index.html | 29 +++++++++ www/checkup/main.js | 49 ++++++++++++++ 5 files changed, 158 insertions(+), 108 deletions(-) create mode 100644 www/assert/assertions.js create mode 100644 www/checkup/index.html create mode 100644 www/checkup/main.js diff --git a/www/assert/assertions.js b/www/assert/assertions.js new file mode 100644 index 000000000..8ea0a2638 --- /dev/null +++ b/www/assert/assertions.js @@ -0,0 +1,48 @@ +define([], function () { + return function () { + var failMessages = []; + var passed = 0; + var ASSERTS = []; + + var assert = function (test, msg) { + ASSERTS.push(function (cb, i) { + test(function (result) { + if (result === true) { + passed++; + cb(); + } else { + cb({ + test: i, + message: msg, + output: result, + }); + } + }); + }); + }; + + assert.run = function (cb) { + var count = ASSERTS.length; + var done = function (err) { + count--; + if (err) { failMessages.push(err); } + if (count === 0) { + cb({ + total: ASSERTS.length, + passed: passed, + errors: failMessages, + }); + } + }; + + ASSERTS.forEach(function (f, index) { + f(function (err) { + //console.log("test " + index); + done(err, index); + }, index); + }); + }; + + return assert; + }; +}); diff --git a/www/assert/index.html b/www/assert/index.html index 10af165a9..a760bef97 100644 --- a/www/assert/index.html +++ b/www/assert/index.html @@ -24,11 +24,15 @@ width: auto; border: 3px solid black; } + .hidden { + display: none; + } + diff --git a/www/assert/main.js b/www/assert/main.js index f40a8e006..07f532f05 100644 --- a/www/assert/main.js +++ b/www/assert/main.js @@ -11,57 +11,19 @@ define([ '/common/flat-dom.js', '/common/media-tag.js', '/common/outer/login-block.js', + '/api/config', + '/assert/assertions.js', + '/common/hyperscript.js', + '/customize/messages.js', '/bower_components/tweetnacl/nacl-fast.min.js', 'less!/customize/src/less2/pages/page-assert.less', -], function ($, Hyperjson, Sortify, Drive, Test, Hash, Util, Thumb, Wire, Flat, MediaTag, Block) { +], function ($, Hyperjson, Sortify, Drive, Test, Hash, Util, Thumb, Wire, Flat, MediaTag, Block, ApiConfig, Assertions, h, Messages) { window.Hyperjson = Hyperjson; window.Sortify = Sortify; var Nacl = window.nacl; - var assertions = 0; - var failed = false; - var failedOn; - var failMessages = []; - - var ASSERTS = []; - var runASSERTS = function (cb) { - var count = ASSERTS.length; - var successes = 0; - - var done = function (err) { - count--; - if (err) { failMessages.push(err); } - else { successes++; } - if (count === 0) { cb(); } - }; - - ASSERTS.forEach(function (f, index) { - f(function (err) { - //console.log("test " + index); - done(err, index); - }, index); - }); - }; - - var assert = function (test, msg) { - ASSERTS.push(function (cb, i) { - test(function (result) { - if (result === true) { - assertions++; - cb(); - } else { - failed = true; - failedOn = assertions; - cb({ - test: i, - message: msg, - output: result, - }); - } - }); - }); - }; + var assert = Assertions(); var HJSON_list = [ '["DIV",{"id":"target"},[["P",{"class":" alice bob charlie has.dot","id":"bang"},["pewpewpew"]]]]', @@ -536,69 +498,27 @@ define([ cb(dom.outerHTML === bodyText); }); - var swap = function (str, dict) { - return str.replace(/\{\{(.*?)\}\}/g, function (all, key) { - return typeof dict[key] !== 'undefined'? dict[key] : all; - }); - }; - - var multiline = function (f) { - var str; - f.toString().replace(/\/\*([\s\S]*)\*\//g, function (all, out) { - str = out; - }); - return str || ''; - }; - - var formatFailures = function () { - var template = multiline(function () { /* -

-Failed on test number {{test}} with error message: -"{{message}}" - -

-

-The test returned: -{{output}} -

- -
- -*/}); - return failMessages.map(function (obj) { - console.log(obj); - return swap(template, obj); - }).join("\n"); - }; - - runASSERTS(function () { - $("body").html(function (i, val) { - var dict = { - previous: val, - totalAssertions: ASSERTS.length, - passedAssertions: assertions, - plural: (assertions === 1? '' : 's'), - failMessages: formatFailures() - }; - - var SUCCESS = swap(multiline(function(){/* -
{{passedAssertions}} / {{totalAssertions}} test{{plural}} passed. - -{{failMessages}} - -
- - -{{previous}} - */}), dict); - - var report = SUCCESS; - - return report; - }); - - var $report = $('.report'); - $report.addClass(failed?'failure':'success'); + assert.run(function (state) { + var errors = state.errors; + var failed = errors.length; + + Messages.assert_numberOfTestsPassed = "{0} / {1} tests passed."; + + var statusClass = failed? 'failure': 'success'; + $('body').prepend(h('div.report.' + statusClass, [ + Messages._getKey('assert_numberOfTestsPassed', [ + state.passed, + state.total + ]), + h('div.failures', errors.map(function (obj) { + return h('p.error', [ + h('p', "Test number: " + obj.test), + h('p', "Error message: " + obj.message), + h('p', "Returned value: " + obj.output), + h('br'), + ]); + })), + ])); if (failed) { Test.failed(); diff --git a/www/checkup/index.html b/www/checkup/index.html new file mode 100644 index 000000000..2309ffd56 --- /dev/null +++ b/www/checkup/index.html @@ -0,0 +1,29 @@ + + + + + + + + + + + + diff --git a/www/checkup/main.js b/www/checkup/main.js new file mode 100644 index 000000000..9097894fc --- /dev/null +++ b/www/checkup/main.js @@ -0,0 +1,49 @@ +define([ + 'jquery', + '/api/config', + '/assert/assertions.js', + '/common/hyperscript.js', + '/customize/messages.js', + + '/bower_components/tweetnacl/nacl-fast.min.js', + 'less!/customize/src/less2/pages/page-assert.less', +], function ($, ApiConfig, Assertions, h, Messages) { + var assert = Assertions(); + + assert(function (cb) { + var c = ApiConfig; + cb(Boolean(c.httpUnsafeOrigin && c.httpSafeOrigin)); + }, "Sandbox configuration: ensure that both httpUnsafeOrigin and httpSafeOrigin are defined"); // XXX + + assert(function (cb) { + var c = ApiConfig; + return void cb(c.httpUnsafeOrigin !== c.httpSafeOrigin); + }, 'Sandbox configuration: httpUnsafeOrigin !== httpSafeOrigin'); // XXX + + assert(function (cb) { + cb((window.location.origin + '/') === ApiConfig.httpUnsafeOrigin); + }, 'Sandbox configuration: loading via httpUnsafeOrigin'); // XXX + + assert.run(function (state) { + var errors = state.errors; + var failed = errors.length; + + Messages.assert_numberOfTestsPassed = "{0} / {1} tests passed."; + + var statusClass = failed? 'failure': 'success'; + $('body').prepend(h('div.report.' + statusClass, [ + Messages._getKey('assert_numberOfTestsPassed', [ + state.passed, + state.total + ]), + h('div.failures', errors.map(function (obj) { + return h('p.error', [ + h('p', "Test number: " + obj.test), + h('p', "Error message: " + obj.message), + h('p', "Returned value: " + obj.output), + h('br'), + ]); + })), + ])); + }); +});