diff --git a/extension/background.js b/extension/background.js index b0f937d..4caaaf0 100644 --- a/extension/background.js +++ b/extension/background.js @@ -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(); } diff --git a/test/Makefile b/test/Makefile index 060a265..9659a23 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,4 +1,5 @@ run-test: test + node test.js ./test test: test.c diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..31460f9 --- /dev/null +++ b/test/README.md @@ -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. diff --git a/test/test-page.html b/test/test-resources/test-page.html similarity index 100% rename from test/test-page.html rename to test/test-resources/test-page.html diff --git a/test/test-script.js b/test/test-resources/test-script.js similarity index 100% rename from test/test-script.js rename to test/test-resources/test-script.js diff --git a/test/test-textarea.html b/test/test-resources/test-textarea.html similarity index 100% rename from test/test-textarea.html rename to test/test-resources/test-textarea.html diff --git a/test/test.c b/test/test.c index e18197c..4ad3dd3 100644 --- a/test/test.c +++ b/test/test.c @@ -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); diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..1288bf4 --- /dev/null +++ b/test/test.js @@ -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']); +})();