All files / scripts/lib/docs extractTesting.js

87.87% Statements 29/33
65.21% Branches 30/46
100% Functions 6/6
92.3% Lines 24/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 525x     29x 29x 29x 61x 17x 26x 13x 13x 2x 1x     61x 17x 19x 19x 5x         29x                         29x 29x 29x 25x 4x   29x             5x  
const DEBUG_FETCH = process.env.DEBUG_FETCH === '1' || process.env.DEBUG_FETCH === 'true';
 
function findCoverageInAst(ast, ctx) {
  const links = [];
  Iif (!ast || !Array.isArray(ast.children)) return links;
  for (const n of ast.children) {
    if (n.type === 'paragraph') {
      for (const ch of (n.children || [])) {
        if (ch.type !== 'link' || !ch.url) continue;
        const label = (ch.children || []).map(c => c.value || '').join('').trim();
        if (!/Test\s+Coverage/i.test(label)) continue;
        const desc = (n.children || []).filter(c => c.type === 'text').map(c => c.value).join(' ').trim();
        links.push({ title: label || 'Test Coverage', link: ch.url, description: ctx.strip(desc) });
      }
    }
    if (n.type === 'list') {
      for (const li of (n.children || [])) {
        const flat = String(ctx.parseReadme.flattenNodeText(li || ''));
        for (const m of flat.matchAll(/\[([^\]]*Test\s+Coverage[^\]]*)\]\(([^)]+)\)/ig)) {
          links.push({ title: (m[1] || 'Test Coverage').trim(), link: (m[2] || '').trim(), description: '' });
        }
      }
    }
  }
  return links;
}
 
/**
 * Finds "Test Coverage" links in the README. Scans AST nodes first, then falls
 * back to a raw-text regex. Returns a `{ coverage: [...] }` object or null.
 *
 * @param {object} ast - Parsed README AST
 * @param {string} readmeText - Raw README string (fallback)
 * @param {object} ctx - Shared context: { toRawGithub, parseReadme, strip }
 * @returns {{ coverage: Array }|null}
 */
function extractTestingLinks(ast, readmeText, ctx) {
  try {
    let links = findCoverageInAst(ast, ctx);
    if (links.length === 0) {
      links = [...readmeText.matchAll(/\[([^\]]*Test\s+Coverage[^\]]*)\]\(([^)]+)\)/ig)]
        .map(m => ({ title: (m[1] || 'Test Coverage').trim(), link: (m[2] || '').trim(), description: '' }));
    }
    return links.length > 0 ? { coverage: links } : null;
  } catch (e) {
    if (DEBUG_FETCH) console.log('extractTestingLinks error', e && e.message);
    return null;
  }
}
 
module.exports = { extractTestingLinks };