import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { loadTs } from './_loadts.mjs';
const { buildBlock, buildBreakdown, renderBreakdown, breakdownValues, catalogHref, ledger, facetLabel, QUICK_STARTS, CATALOG_BREAKDOWN, DECK_BREAKDOWN } = await loadTs('src/lib/insights.ts');
const { filterTechniques, EMPTY_FILTERS, filtersFromSearch, routeSourceOf } = await loadTs('src/lib/techniques.ts');
const { totalFacetCounts, renderFacets } = await loadTs('src/lib/render-row.ts');
const { DECK_ALPHABET, deckLetter, sigMatches } = await loadTs('src/lib/deck.ts');
const rows = JSON.parse(readFileSync('src/data/techniques.json'));
test('single-valued facets draw as a split, multi-valued ones as a ranking', () => {
const counts = { context: { sudo: 3, suid: 1 }, capability: { Execution: 5, 'File Read': 2, Compile: 1 } };
const split = buildBlock('context', counts);
assert.equal(split.kind, 'split'); assert.equal(split.sum, 4);
assert.deepEqual(split.items.map(i => i.value), ['sudo', 'suid']);
const rank = buildBlock('capability', counts, { limit: 2 });
assert.equal(rank.kind, 'rank'); assert.equal(rank.max, 5);
assert.deepEqual(rank.items.map(i => i.value), ['Execution', 'File Read']);
});
test('a block needs something to compare, and a pressed value is never cut off', () => {
assert.equal(buildBlock('context', { context: { sudo: 3 } }), null);
assert.equal(buildBlock('context', { context: { sudo: 3 } }, { min: 1 }).items.length, 1);
const counts = { capability: { Execution: 5, 'File Read': 2, Compile: 1 } };
const pressed = buildBlock('capability', counts, { limit: 1, active: { capability: ['Compile'] } });
assert.deepEqual(pressed.items.map(i => i.value), ['Execution', 'Compile']);
});
test('a hue belongs to the value, not to its rank', () => {
const hue = (counts) => Object.fromEntries(buildBlock('context', counts, { min: 1 }).items.map(i => [i.value, i.hue]));
assert.equal(hue({ context: { sudo: 9, suid: 1 } }).suid, hue({ context: { suid: 9 } }).suid);
});
test('the left column takes the first facets that actually vary', () => {
const b = buildBreakdown({ platform: { Linux: 4 }, context: { sudo: 3, suid: 1 }, source: { GTFOBins: 4 }, capability: { Execution: 4, 'File Read': 1 } }, DECK_BREAKDOWN);
assert.deepEqual(b.left.map(x => x.key), ['context']); assert.equal(b.right.key, 'capability');
assert.deepEqual(breakdownValues(b), { context: ['sudo', 'suid'], capability: ['Execution', 'File Read'] });
});
test('button marks carry the facet and pressed state; link marks open the catalog', () => {
const b = buildBreakdown({ context: { sudo: 3, suid: 1 }, capability: { 'Reverse/Bind Shell': 4, Execution: 1 } }, { left: ['context'], right: 'capability' });
const buttons = renderBreakdown(b, { mode: 'button', active: { context: ['sudo'] } });
assert.match(buttons, /class="bd-key" aria-pressed="true" data-facet="context" data-value="sudo"/);
assert.match(buttons, /class="bd-bar" aria-pressed="false" data-facet="capability"/);
assert.match(buttons, /data-narrowed/);
const links = renderBreakdown(b, { mode: 'link' });
assert.doesNotMatch(links, /<button/);
assert.match(links, /href="\/catalog\?c=Reverse%2FBind\+Shell"/);
assert.equal(renderBreakdown({ left: [], right: null }, { mode: 'link' }), '');
});
test('labels and numerals read the way the site writes them', () => {
assert.equal(ledger(6), '06'); assert.equal(ledger(42), '42'); assert.equal(ledger(1451), '1,451');
assert.equal(facetLabel('platform', 'ActiveDirectory'), 'Active Directory');
assert.equal(facetLabel('source', 'DAEMON'), 'DÆMON');
assert.equal(catalogHref({}), '/catalog');
});
test('every quick start round-trips through the URL and finds something', () => {
for (const q of QUICK_STARTS) {
const f = { ...EMPTY_FILTERS, ...q.filters };
assert.deepEqual(filtersFromSearch(new URL(catalogHref(q.filters), 'https://x').search), f, q.label);
assert.ok(filterTechniques(rows, f).length > 0, `${q.label} matches nothing`);
}
});
test('the catalog panel and the rail agree on the committed dataset', () => {
const counts = totalFacetCounts(rows);
const b = buildBreakdown(counts, CATALOG_BREAKDOWN);
assert.deepEqual(b.left.map(x => x.key), ['platform', 'context']);
assert.equal(b.right.items.length, 6);
assert.equal(b.right.items[0].n, counts.capability[b.right.items[0].value]);
const rail = renderFacets(EMPTY_FILTERS, counts);
assert.equal((rail.match(/<details class="cat-facet"/g) || []).length, 9);
assert.match(rail, /data-value="ActiveDirectory"[^>]*>\s*<span class="cat-chip__lbl">Active Directory</);
});
test('every collection fits the signature alphabet', () => {
for (const ns of ['gtfo', 'lolbas', 'wadcoms', 'loobins', 'daemon']) {
const deck = rows.filter(t => t.toolId.startsWith(ns + ':'));
const counts = totalFacetCounts(deck);
const b = buildBreakdown(counts, DECK_BREAKDOWN);
const size = [...b.left.map(x => x.key), 'capability'].reduce((n, k) => n + Object.keys(counts[k] || {}).length, 0);
assert.ok(size <= DECK_ALPHABET.length, `${ns}: ${size} values`);
assert.ok(b.right, `${ns} has no capability ranking`);
}
assert.equal(new Set(DECK_ALPHABET).size, DECK_ALPHABET.length);
assert.equal(routeSourceOf('wadcoms:Certipy'), 'WADComs');
});
test('a tool matches only when one technique satisfies every pressed facet', () => {
// A = sudo, B = suid, C = File Read, D = Execution
const tool = ['AC', 'BD'];
assert.equal(sigMatches(tool, []), true);
assert.equal(sigMatches(tool, ['A', 'C']), true);
assert.equal(sigMatches(tool, ['A', 'D']), false); // sudo and Execution are different techniques
assert.equal(sigMatches(tool, ['AB', 'D']), true); // any within a facet
assert.equal(deckLetter('7z'), '#'); assert.equal(deckLetter('Vim'), 'v');
});