import assert from 'node:assert/strict'; import test from 'node:test'; import { Window } from 'happy-dom'; import { initSheetFilter, parseState, rowMatches, serializeState, } from '../src/scripts/sheet-filter.ts'; const row = (over = {}) => ({ text: 'certipy esc1 adcs kerberos impacket', tags: ['adcs', 'kerberos'], tools: ['certipy', 'impacket'], diff: 'advanced', sec: 'adcs-certificates', ...over, }); const state = (over = {}) => ({ q: '', tags: [], tool: '', diff: '', sec: '', ...over }); test('filter state survives a round trip through the query string', () => { const s = state({ q: 'c++ a&b 100%', tags: ['adcs', 'kerberos'], tool: 'john the ripper', diff: 'advanced', sec: 'acl-abuse' }); assert.deepEqual(parseState(`?${serializeState(s)}`), s); assert.equal(serializeState(state()), ''); assert.deepEqual(parseState(''), state()); }); test('query values are normalised: tags and tools lower-cased, repeats and commas both accepted', () => { assert.deepEqual(parseState('?tag=ADCS,Kerberos&tag=adcs&tool=Impacket').tags, ['adcs', 'kerberos']); assert.equal(parseState('?tool=Impacket').tool, 'impacket'); assert.equal(parseState('?diff=nonsense').diff, ''); }); test('a row has to satisfy every active filter', () => { assert.equal(rowMatches(row(), state()), true); assert.equal(rowMatches(row(), state({ q: 'ESC1 certipy' })), true, 'terms AND together, any case'); assert.equal(rowMatches(row(), state({ q: 'esc1 nmap' })), false); assert.equal(rowMatches(row(), state({ q: 'c++' })), false, 'metacharacters are plain text'); assert.equal(rowMatches(row({ text: 'c++ notes' }), state({ q: 'c++' })), true); assert.equal(rowMatches(row(), state({ tags: ['adcs', 'kerberos'] })), true); assert.equal(rowMatches(row(), state({ tags: ['adcs', 'ntlm'] })), false, 'tags AND together'); assert.equal(rowMatches(row(), state({ tool: 'impacket' })), true); assert.equal(rowMatches(row(), state({ tool: 'nmap' })), false); assert.equal(rowMatches(row(), state({ diff: 'beginner' })), false); assert.equal(rowMatches(row(), state({ diff: 'beginner' }), 'diff'), true, 'skip ignores its own facet'); assert.equal(rowMatches(row(), state({ sec: 'acl-abuse' })), false); assert.equal(rowMatches(row(), state({ sec: 'acl-abuse' }), 'sec'), true); }); const PAGE = ` adcs 02 ntlm 01 + 1 more Clear 01 02 02 01 Nothing matches. Clear filters One Two ESC1 Relay Nmap `; function mount(url) { const window = new Window({ url }); globalThis.window = window; globalThis.document = window.document; document.body.innerHTML = PAGE; initSheetFilter(); const $ = (sel) => document.querySelector(sel); const visible = () => [...document.querySelectorAll('[data-sf-row]')].filter((r) => !r.hidden).map((r) => r.id); const type = (value) => { $('[data-sf-q]').value = value; $('[data-sf-q]').dispatchEvent(new window.Event('input', { bubbles: true })); }; return { window, $, visible, type }; } test('typing narrows the rows, hides empty sections and their jump links, and repaints the counts', () => { const { window, $, visible, type } = mount('https://example.test/active-directory'); assert.deepEqual(visible(), ['a', 'b', 'c']); assert.equal($('[data-sf-count]').textContent, '3 shown'); type('adcs'); assert.deepEqual(visible(), ['a', 'b']); assert.equal($('[data-sf-count]').textContent, '2 shown'); assert.equal($('[data-sf-section="two"]').hidden, true); assert.equal($('[data-sf-jump="two"]').hidden, true); assert.equal($('[data-sf-section="one"]').hidden, false); assert.equal($('[data-sf-n="diff:beginner"]').textContent, '00'); assert.equal($('[data-sf-n="diff:advanced"]').textContent, '02'); assert.equal($('[data-sf-grow="diff:beginner"]').style.flexGrow, '0'); assert.equal(window.location.search, '?q=adcs'); assert.equal($('[data-sf-chips] [data-sf-clear]').hidden, false); }); test('chips toggle, AND together, and every control for one tag stays in step', () => { const { window, $, visible } = mount('https://example.test/active-directory'); const [chip, bar] = document.querySelectorAll('[data-sf-tag="adcs"]'); bar.click(); assert.deepEqual(visible(), ['a', 'b']); assert.equal(chip.getAttribute('aria-pressed'), 'true'); assert.equal(bar.getAttribute('aria-pressed'), 'true'); assert.equal($('[data-sf-fill="tag:adcs"]').style.width, '100%'); $('[data-sf-more]').click(); assert.equal($('[data-sf-tag="ntlm"]').hidden, false); $('[data-sf-tag="ntlm"]').click(); assert.deepEqual(visible(), ['b']); assert.equal(window.location.search, '?tag=adcs&tag=ntlm'); chip.click(); assert.deepEqual(visible(), ['b'], 'ntlm alone still selects the relay sheet'); assert.equal(window.location.search, '?tag=ntlm'); }); test('difficulty and section are single-select and count as if their own facet were off', () => { const { $, visible } = mount('https://example.test/active-directory'); $('[data-sf-diff="beginner"]').click(); assert.deepEqual(visible(), ['c']); assert.equal($('[data-sf-n="diff:advanced"]').textContent, '02', 'the other difficulty stays countable'); assert.equal($('[data-sf-n="sec:one"]').textContent, '00'); $('[data-sf-diff="advanced"]').click(); assert.deepEqual(visible(), ['a', 'b']); assert.equal($('[data-sf-diff="beginner"]').getAttribute('aria-pressed'), 'false'); $('[data-sf-diff="advanced"]').click(); assert.deepEqual(visible(), ['a', 'b', 'c'], 'clicking the active one clears it'); }); test('a filter with no control on the page still applies and gets a removable chip', () => { const { window, $, visible } = mount('https://example.test/sheets?tool=Impacket&tag=kerberos&tag=ntlm'); assert.deepEqual(visible(), [], 'kerberos AND ntlm AND impacket matches nothing'); assert.equal($('[data-sf-empty]').hidden, false); assert.equal($('[data-sf-count]').textContent, '0 shown'); assert.equal($('[data-sf-tag="ntlm"]').hidden, false, 'an active extra chip is revealed'); const made = [...document.querySelectorAll('[data-sf-dynamic]')]; assert.deepEqual(made.map((b) => b.dataset.sfTool ?? b.dataset.sfTag).sort(), ['impacket', 'kerberos']); $('[data-sf-dynamic][data-sf-tag="kerberos"]').click(); assert.deepEqual(visible(), ['b']); assert.equal(document.querySelector('[data-sf-dynamic][data-sf-tag="kerberos"]'), null); $('[data-sf-dynamic][data-sf-tool="impacket"]').click(); assert.equal(window.location.search, '?tag=ntlm'); }); test('clear resets everything, from the bar or from the empty state', () => { const { window, $, visible, type } = mount('https://example.test/active-directory?diff=beginner#sec-two'); type('zzz'); assert.deepEqual(visible(), []); $('[data-sf-empty] [data-sf-clear]').click(); assert.deepEqual(visible(), ['a', 'b', 'c']); assert.equal($('[data-sf-q]').value, ''); assert.equal($('[data-sf-empty]').hidden, true); assert.equal($('[data-sf-chips] [data-sf-clear]').hidden, true); assert.equal(window.location.search, ''); assert.equal(window.location.hash, '#sec-two', 'the anchor is left alone'); });
Nothing matches. Clear filters