// app.pedshub.com/#resources opens My Resources. The app has no routes and is // not getting any; a hash is read once, stored as the last tab, and dropped. const test = require('node:test'); const assert = require('node:assert/strict'); const fs = require('node:fs'); const path = require('node:path'); const src = fs.readFileSync(path.join(__dirname, '..', 'public/js/app.js'), 'utf8'); const start = src.indexOf('var TAB_ALIASES'); const end = src.indexOf('try { openTabFromHash(window'); const openTabFromHash = new Function(src.slice(start, end) + '; return openTabFromHash;')(); function world(hash, tabs) { const stored = {}; const replaced = []; return { win: { location: { hash, pathname: '/', search: '?sso=ok' }, history: { replaceState: (_s, _t, url) => replaced.push(url) } }, doc: { querySelector: sel => tabs.some(t => sel === '.tab-btn[data-tab="' + t + '"]') ? {} : null }, storage: { setItem: (k, v) => { stored[k] = v; } }, stored, replaced }; } test('#resources stores My Resources as the tab to open and clears the hash', () => { const w = world('#resources', ['encounter', 'myresources']); assert.equal(openTabFromHash(w.win, w.doc, w.storage), 'myresources'); assert.equal(w.stored.ped_last_tab, 'myresources'); assert.deepEqual(w.replaced, ['/?sso=ok'], 'the query survives, the hash goes'); }); test('#share= sets the token aside and opens My Resources', () => { const w = world('#share=abc.def-123', ['encounter', 'myresources']); assert.equal(openTabFromHash(w.win, w.doc, w.storage), 'myresources'); assert.equal(w.stored.ped_pending_share, 'abc.def-123'); assert.equal(w.stored.ped_last_tab, 'myresources'); }); test('a tab that does not exist, or no hash at all, changes nothing', () => { for (const hash of ['#nonsense', '', '#']) { const w = world(hash, ['encounter']); assert.equal(openTabFromHash(w.win, w.doc, w.storage), null); assert.deepEqual(w.stored, {}); assert.deepEqual(w.replaced, []); } }); test('the hash is honoured by the same code that restores the last tab after sign-in', () => { const auth = fs.readFileSync(path.join(__dirname, '..', 'public/js/auth.js'), 'utf8'); assert.match(auth, /localStorage\.getItem\('ped_last_tab'\)/); assert.match(src, /try \{ openTabFromHash\(window, document, window\.localStorage\); \} catch/); });