clean up /assert/ scripts and create a new instance checkup page

pull/1/head
ansuz 4 years ago
parent d4291fb3ed
commit ce1f96ac61

@ -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;
};
});

@ -24,11 +24,15 @@
width: auto; width: auto;
border: 3px solid black; border: 3px solid black;
} }
.hidden {
display: none;
}
</style> </style>
</head> </head>
<body> <body>
<div class="hidden">
<h1>Serialization tests</h1> <h1>Serialization tests</h1>
<h2>Test 1</h2> <h2>Test 1</h2>
@ -53,4 +57,4 @@
<hr> <hr>
<img id="thumb-orig" src="/customize/alt-favicon.png" /> <img id="thumb-orig" src="/customize/alt-favicon.png" />
</div>

@ -11,57 +11,19 @@ define([
'/common/flat-dom.js', '/common/flat-dom.js',
'/common/media-tag.js', '/common/media-tag.js',
'/common/outer/login-block.js', '/common/outer/login-block.js',
'/api/config',
'/assert/assertions.js',
'/common/hyperscript.js',
'/customize/messages.js',
'/bower_components/tweetnacl/nacl-fast.min.js', '/bower_components/tweetnacl/nacl-fast.min.js',
'less!/customize/src/less2/pages/page-assert.less', '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.Hyperjson = Hyperjson;
window.Sortify = Sortify; window.Sortify = Sortify;
var Nacl = window.nacl; var Nacl = window.nacl;
var assertions = 0; var assert = Assertions();
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 HJSON_list = [ var HJSON_list = [
'["DIV",{"id":"target"},[["P",{"class":" alice bob charlie has.dot","id":"bang"},["pewpewpew"]]]]', '["DIV",{"id":"target"},[["P",{"class":" alice bob charlie has.dot","id":"bang"},["pewpewpew"]]]]',
@ -536,69 +498,27 @@ define([
cb(dom.outerHTML === bodyText); cb(dom.outerHTML === bodyText);
}); });
var swap = function (str, dict) { assert.run(function (state) {
return str.replace(/\{\{(.*?)\}\}/g, function (all, key) { var errors = state.errors;
return typeof dict[key] !== 'undefined'? dict[key] : all; var failed = errors.length;
});
}; Messages.assert_numberOfTestsPassed = "{0} / {1} tests passed.";
var multiline = function (f) { var statusClass = failed? 'failure': 'success';
var str; $('body').prepend(h('div.report.' + statusClass, [
f.toString().replace(/\/\*([\s\S]*)\*\//g, function (all, out) { Messages._getKey('assert_numberOfTestsPassed', [
str = out; state.passed,
}); state.total
return str || ''; ]),
}; h('div.failures', errors.map(function (obj) {
return h('p.error', [
var formatFailures = function () { h('p', "Test number: " + obj.test),
var template = multiline(function () { /* h('p', "Error message: " + obj.message),
<p class="error"> h('p', "Returned value: " + obj.output),
Failed on test number {{test}} with error message: h('br'),
"{{message}}" ]);
})),
</p> ]));
<p>
The test returned:
{{output}}
</p>
<br>
*/});
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(){/*
<div class="report">{{passedAssertions}} / {{totalAssertions}} test{{plural}} passed.
{{failMessages}}
</div>
{{previous}}
*/}), dict);
var report = SUCCESS;
return report;
});
var $report = $('.report');
$report.addClass(failed?'failure':'success');
if (failed) { if (failed) {
Test.failed(); Test.failed();

@ -0,0 +1,29 @@
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<script data-bootload="main.js" data-main="/common/boot.js" src="/bower_components/requirejs/require.js"></script>
<style>
.report {
font-size: 40px;
}
.success {
border: 3px solid green;
}
.failure {
border: 3px solid red;
}
.error {
border: 1px solid red;
}
.hidden {
display: none;
}
</style>
</head>
<body>

@ -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'),
]);
})),
]));
});
});
Loading…
Cancel
Save