test: separate tests from background.js, clean up and document a bit

also makes the test more lenient (subset instead of equality for tab
entries)
This commit is contained in:
Omar Rizwan 2021-03-14 21:33:23 -07:00
parent 00caad5228
commit 11cf48259a
8 changed files with 43 additions and 15 deletions

View File

@ -82,7 +82,6 @@ async function detachDebugger(tabId) {
}));
}
const TabManager = (function() {
if (TESTING) return;
if (chrome.debugger) chrome.debugger.onEvent.addListener((source, method, params) => {
console.log(source, method, params);
if (method === "Page.frameStartedLoading") {
@ -614,17 +613,6 @@ for (let i = 10; i >= 0; i--) {
// so you could patch more routes in at runtime, but I need to think
// a bit about how to make that work with wildcards.
}
if (TESTING) { // I wish I could color this section with... a pink background, or something.
const assert = require('assert');
(async () => {
assert.deepEqual(await router['/tabs/by-id/*'].readdir(), { entries: ['.', '..', 'url.txt', 'title.txt', 'text.txt', 'window', 'control', 'debugger'] });
assert.deepEqual(await router['/'].readdir(), { entries: ['.', '..', 'windows', 'extensions', 'tabs', 'runtime'] });
assert.deepEqual(await router['/tabs'].readdir(), { entries: ['.', '..', 'create', 'by-id', 'by-title', 'last-focused'] });
assert.deepEqual(findRoute('/tabs/by-id/TABID/url.txt'), router['/tabs/by-id/*/url.txt']);
})()
}
// fill in default implementations of fs ops
for (let key in router) {
@ -773,6 +761,12 @@ function tryConnect() {
port.onDisconnect.addListener(p => {console.log('disconnect', p)});
}
if (!TESTING) {
if (typeof process === 'object') {
// we're running in node (as part of a test)
// return everything they might want to test
module.exports = {router, findRoute};
} else {
tryConnect();
}

View File

@ -1,4 +1,5 @@
run-test: test
node test.js
./test
test: test.c

12
test/README.md Normal file
View File

@ -0,0 +1,12 @@
# test
Two separate test 'suites': one in `test.js` that uses node, and one
in `test.c` that is an integration test that actually tests against
the extension in browser.
Right now, you need to have Chrome open (I haven't tried Firefox or
Safari), and you'll want to make sure a window other than the
extension console is focused (the console is non-debuggable, so it
breaks the test).
Run `make` in this folder.

View File

@ -55,7 +55,7 @@ int main() {
}
{
assert(system("echo file://$(pwd)/test-page.html > ../fs/mnt/tabs/create") == 0);
assert(system("echo file://$(pwd)/test-resources/test-page.html > ../fs/mnt/tabs/create") == 0);
assert(file_contents_equal("../fs/mnt/tabs/last-focused/title.txt", "Title of Test Page"));
assert(file_contents_equal("../fs/mnt/tabs/last-focused/text.txt", "Body Text of Test Page"));
@ -87,7 +87,7 @@ int main() {
}
{
assert(system("echo file://$(pwd)/test-textarea.html > ../fs/mnt/tabs/create") == 0);
assert(system("echo file://$(pwd)/test-resources/test-textarea.html > ../fs/mnt/tabs/create") == 0);
{
assert(system("echo \"document.getElementById('ta').value\" > ../fs/mnt/tabs/last-focused/evals/ta.js") == 0);

21
test/test.js Normal file
View File

@ -0,0 +1,21 @@
const assert = require('assert');
// mock chrome namespace
global.chrome = {};
// run background.js
const {router, findRoute} = require('../extension/background');
(async () => {
const tabRoute = await router['/tabs/by-id/*'].readdir();
assert(['.', '..', 'url.txt', 'title.txt', 'text.txt']
.every(file => tabRoute.entries.includes(file)));
assert.deepEqual(await router['/'].readdir(),
{ entries: ['.', '..', 'windows', 'extensions', 'tabs', 'runtime'] });
assert.deepEqual(await router['/tabs'].readdir(),
{ entries: ['.', '..', 'create',
'by-id', 'by-title', 'last-focused'] });
assert.deepEqual(findRoute('/tabs/by-id/TABID/url.txt'),
router['/tabs/by-id/*/url.txt']);
})();