import { leafValues, matchRoute, } from "../src/content/sw.exported.js"; const test_leafValues = t => { t.start("leafValues()"); t.test("noop on empty object", () => { t.assert.deepEqual(leafValues({}), []); }); t.test("flat array for flat tree", () => { }); t.test("array of empty arrays for leafless tree", () => { t.assert.deepEqual(leafValues({ a: {} }), [[]]); }); }; 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, ];