import test from 'node:test'; import assert from 'node:assert/strict'; const { resultsToHTML } = await import('../src/apisearch.js'); // The MDN-shaped definition oc ships, minus the endpoint itself: which // response fields hold the list, and what each result calls its parts. const DEF = { results: 'documents', fields: { title: 'title', url: 'mdn_url', text: 'summary' }, total: 'metadata.total.value', }; const API = 'https://developer.mozilla.org/api/v1/search?q=map'; // A miniature /api/v1/search answer: a relative URL, a nested total, and a // title that would be markup if it were ever trusted. const DATA = { documents: [ { mdn_url: '/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map', title: 'Array.prototype.map()', summary: 'Creates a new array from results of a callback.' }, { mdn_url: '/en-US/docs/Web/API/Map', title: 'Map ', summary: 'Holds key-value pairs.' }, ], metadata: { total: { value: 2696 } }, }; test('results map through the named fields and resolve against the endpoint', () => { const html = resultsToHTML(DEF, 'map', DATA, API); assert.match(html, /href="https:\/\/developer\.mozilla\.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/map"/); assert.match(html, /Array\.prototype\.map\(\)/); assert.match(html, /Creates a new array/); }); test('the site total is reported, and result count is what the page shows', () => { assert.match(resultsToHTML(DEF, 'map', DATA, API), /2696 pages match, ranked by the site's own search, top 2 shown:/); }); test('a title is response data, never markup on the results page', () => { const html = resultsToHTML(DEF, 'map', DATA, API); assert.doesNotMatch(html, /