diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/browser-driver.js | 1 | ||||
-rw-r--r-- | tests/driver.html | 12 | ||||
-rw-r--r-- | tests/driver.js | 20 | ||||
-rw-r--r-- | tests/node-driver.js | 43 | ||||
-rw-r--r-- | tests/papo.js | 50 | ||||
-rw-r--r-- | tests/sw.js | 118 |
6 files changed, 190 insertions, 54 deletions
diff --git a/tests/browser-driver.js b/tests/browser-driver.js index 529d824..aa02c07 100644 --- a/tests/browser-driver.js +++ b/tests/browser-driver.js @@ -38,6 +38,7 @@ const conf = { }, }; + const TEST_PATHS = [ "tests/papo.js", "tests/sw.js", diff --git a/tests/driver.html b/tests/driver.html index e4508ec..687d704 100644 --- a/tests/driver.html +++ b/tests/driver.html @@ -4,7 +4,7 @@ <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="shortcut icon" type="image/svg+xml" href="../src/content/img/favicon.svg" /> - <title>Papo | webapp tests</title> + <title>Papo | chat.papo.im test suite</title> <style> :root { --color-fg: black; @@ -12,15 +12,17 @@ --color-red: red; --color-green: green; --color-yellow: goldenrod; + --color-pre-bg: #eee; } - @media(prefers-color-scheme: dark) { + @media (prefers-color-scheme: dark) { :root { --color-fg: white; --color-bg: black; --color-red: orangered; --color-green: lightgreen; --color-yellow: yellow; + --color-pre-bg: #111; } } @@ -42,6 +44,12 @@ .yellow { color: var(--color-yellow); } + + pre, code { + background-color: var(--color-pre-bg); + border-radius: 5px; + padding: 5px; + } </style> <script type="module"> import { main } from "./browser-driver.js"; diff --git a/tests/driver.js b/tests/driver.js index d273c5d..d3c1f7b 100644 --- a/tests/driver.js +++ b/tests/driver.js @@ -1,9 +1,8 @@ -const isAsync = f => - typeof f.then === 'function' && - f[Symbol.toStringTag] === 'Promise'; - -const t = ({ colors, err, assert }) => ({ - assert, +const t = ({ colors, err, assert: { equal, deepEqual }}) => ({ + assert: { + equal, + deepEqual, + }, tap: x => { err(`tap: ${x}\n`); return x; @@ -14,11 +13,7 @@ const t = ({ colors, err, assert }) => ({ test: async (msg, fn) => { err(`${colors.yellow("testing")}: ${msg}... `); try { - if (isAsync(fn)) { - await fn(); - } else { - fn(); - } + await fn(); } catch (e) { err(`${colors.red("ERR")}.\n`); throw e; @@ -28,7 +23,8 @@ const t = ({ colors, err, assert }) => ({ }); export const runTests = async (conf, tests) => { + const tFinal = t(conf); for (const testFn of tests) { - await testFn(t(conf)); + await testFn(tFinal); } }; diff --git a/tests/node-driver.js b/tests/node-driver.js index 3393dca..5da5526 100644 --- a/tests/node-driver.js +++ b/tests/node-driver.js @@ -1,50 +1,29 @@ import assert from "node:assert/strict"; import process from "node:process"; +import { runTests } from "./driver.js"; + +const err = x => process.stderr.write(x); + const red = s => `\x1b[31m${s}\x1b[0m`; const green = s => `\x1b[32m${s}\x1b[0m`; const yellow = s => `\x1b[33m${s}\x1b[0m`; -const isAsync = f => - typeof f.then === 'function' && - f[Symbol.toStringTag] === 'Promise'; - -const t = { +const conf = { + err, assert, - tap: x => { - process.stderr.write(`tap: ${x}\n`); - return x; - }, - start: msg => { - process.stderr.write(`${msg}:\n`); + colors: { + red, + green, + yellow, }, - test: async (msg, fn) => { - process.stderr.write(`${yellow("testing")}: ${msg}... `); - try { - if (isAsync(fn)) { - await fn(); - } else { - fn(); - } - } catch (e) { - process.stderr.write(`${red("ERR")}.\n}`); - throw e; - } - process.stderr.write(`${green("OK")}.\n`); - }, -}; - -const runTests = async tests => { - for (const testFn of tests) { - await testFn(t); - } }; const main = async (path = process.argv[2]) => { const module = await import("../" + path); - await runTests(module.allTests); + await runTests(conf, module.allTests); }; diff --git a/tests/papo.js b/tests/papo.js index 188ca67..2a4f2e6 100644 --- a/tests/papo.js +++ b/tests/papo.js @@ -1,19 +1,55 @@ import { - f1, + configureStorage, } from "../src/content/papo.exported.js"; -const test_f1 = async t => { - t.start("f1()"); - t.test("addition", () => { - t.assert.equal(f1(1), 2); - // t.assert.equal(f1(1), 3); +const test_configureStorage = async t => { + t.start("configureStorage()"); + + const contents = []; + const err = x => contents.push(x); + + const navigator = { + storage: { + persist: () => Promise.resolve(true), + }, + }; + + const env = { + err, + navigator, + }; + + + return + t.test("noop when successful", async () => { + console.log(1); + await configureStorage(env); + console.log(2); + t.assert.deepEqual(contents, []); + }); + + t.test("noop if not available", async () => { + delete env.navigator.storage.persist; + console.log(3); + await configureStorage(env); + console.log(4); + t.assert.deepEqual(contents, [ + "Persistent storage is not supported", + ]); + + delete env.navigator.storage; + await configureStorage(env); + t.assert.deepEqual(contents, [ + "Persistent storage is not supported", + "Persistent storage is not supported", + ]); }); }; export const allTests = [ - test_f1, + test_configureStorage, ]; diff --git a/tests/sw.js b/tests/sw.js index cff37bc..25ef93e 100644 --- a/tests/sw.js +++ b/tests/sw.js @@ -1,10 +1,11 @@ import { leafValues, + matchRoute, } from "../src/content/sw.exported.js"; -const test_leafValues = async t => { +const test_leafValues = t => { t.start("leafValues()"); t.test("noop on empty object", () => { @@ -19,8 +20,123 @@ const test_leafValues = async t => { }); }; +const test_buildTable = t => { + t.start("buildTable()"); + + t.test("", () => { + }); +}; + +const test_matchRoutes = t => { + t.start("matchRoutes()"); + + t.test("null on empty values", () => { + t.assert.equal(matchRoute([""], {}), null); + + t.assert.equal(matchRoute(["", ""], {}), null); + t.assert.equal(matchRoute(["some", ""], {}), null); + t.assert.equal(matchRoute(["path", ""], undefined), null); + + t.assert.equal(matchRoute([""], { k: "v" }), null); + t.assert.equal(matchRoute([""], {some:{nested:"values"}}), null); + }); + + t.test("shallow routes match returns the matched value", () => { + const table = { + boolean: { "": "values" }, + true: { "": true }, + false: { "": false }, + string: { "": "a-string" }, + number: { "": 123 }, + }; + + t.assert.equal(matchRoute(["boolean", ""], table), "values"); + t.assert.equal(matchRoute(["true", ""], table), true); + t.assert.equal(matchRoute(["false", ""], table), false); + t.assert.equal(matchRoute(["string", ""], table), "a-string"); + t.assert.equal(matchRoute(["number", ""], table), 123); + }); + + t.test("special case for `null` provides equivalent value", () => { + // given that `null` us an object + t.assert.equal(typeof null, typeof {}); + t.assert.equal(typeof null, "object"); + + // the else case of the recursion base returns a differenc null + // from the one in the `routes` object. + const table = { + path: { + "": null, + }, + }; + t.assert.equal(matchRoute(["path", ""], table), null); + }); + + t.test("we can match nested routes", () => { + const table = { + p0: { + p1: { + "": "match", + }, + }, + }; + t.assert.equal(matchRoute(["not", "valid", ""], table), null); + t.assert.equal(matchRoute(["p0", ""], table), null); + t.assert.equal(matchRoute(["p0", "p1", ""], table), "match"); + t.assert.equal(matchRoute([ "p1", ""], table), null); + }); + + t.test("longer routes have more priority than shorter ones", () => { + // FIXME + // /root/* => catchall + // /root/skip/leaf => + const table = { + root: { + "": "lower", + "*": { + "": "catchall", + }, + skip: { + leaf: { + "": "higher", + }, + }, + }, + }; + t.assert.equal( + matchRoute(["root", "skip", "leaf", ""], table), + "higher", + ); + t.assert.equal( + matchRoute(["root", "skip", ""], table), + "catchall", + ); + t.assert.equal( + matchRoute(["root", ""], table), + "lower", + ); + }); + + t.test("catchall at root captures everything", () => { + const routes = { + "*": { + "": "all", + }, + }; + t.assert.equal( + matchRoute(["anything", "goes", "here", ""], routes), + "all", + ); + t.assert.equal(matchRoute(["here", ""], routes), "all"); + t.assert.equal(matchRoute(["here", ""], routes), "all"); + t.assert.equal(matchRoute([""], routes), "all"); + }); +}; + export const allTests = [ test_leafValues, + test_buildTable, + test_matchRoutes, ]; |