11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci/* eslint-disable max-len */
51cb0ef41Sopenharmony_ciconst docsFixtures = {
61cb0ef41Sopenharmony_ci  dir1: {
71cb0ef41Sopenharmony_ci    'npm-exec.md': 'the exec command\nhelp has multiple lines of exec help\none of them references exec',
81cb0ef41Sopenharmony_ci  },
91cb0ef41Sopenharmony_ci  dir2: {
101cb0ef41Sopenharmony_ci    'npm-something.md': 'another\ncommand you run\nthat\nreferences exec\nand has multiple lines\nwith no matches\nthat will be ignored\nand another line\nthat does have exec as well',
111cb0ef41Sopenharmony_ci    'npm-run-script.md': 'the scripted run-script command runs scripts\nand has lines\nsome of which dont match the string run\nor script\nscript',
121cb0ef41Sopenharmony_ci    'npm-install.md': 'does a thing in a script\nif a thing does not exist in a thing you run\nto install it and run it maybe in a script',
131cb0ef41Sopenharmony_ci  },
141cb0ef41Sopenharmony_ci  dir3: {
151cb0ef41Sopenharmony_ci    'npm-help.md': 'will run the `help-search` command if you need to run it to help you search',
161cb0ef41Sopenharmony_ci    'npm-help-search.md': 'is the help search command\nthat you get if you run help-search',
171cb0ef41Sopenharmony_ci    'npm-useless.md': 'exec\nexec',
181cb0ef41Sopenharmony_ci    'npm-more-useless.md': 'exec exec',
191cb0ef41Sopenharmony_ci    'npm-extra-useless.md': 'exec\nexec\nexec',
201cb0ef41Sopenharmony_ci  },
211cb0ef41Sopenharmony_ci}
221cb0ef41Sopenharmony_ci/* eslint-enable max-len */
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciconst execHelpSearch = async (t, exec = [], opts) => {
251cb0ef41Sopenharmony_ci  const { npm, ...rest } = await loadMockNpm(t, {
261cb0ef41Sopenharmony_ci    npm: ({ other }) => ({ npmRoot: other }),
271cb0ef41Sopenharmony_ci    // docs/content is hardcoded into the glob path in the command
281cb0ef41Sopenharmony_ci    otherDirs: {
291cb0ef41Sopenharmony_ci      docs: {
301cb0ef41Sopenharmony_ci        content: docsFixtures,
311cb0ef41Sopenharmony_ci      },
321cb0ef41Sopenharmony_ci    },
331cb0ef41Sopenharmony_ci    ...opts,
341cb0ef41Sopenharmony_ci  })
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  await npm.exec('help-search', exec)
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci  return { npm, output: rest.joinedOutput(), ...rest }
391cb0ef41Sopenharmony_ci}
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cit.test('npm help-search', async t => {
421cb0ef41Sopenharmony_ci  const { output } = await execHelpSearch(t, ['exec'])
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ci  t.match(output, /Top hits for "exec"/, 'outputs results')
451cb0ef41Sopenharmony_ci})
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_cit.test('npm help-search multiple terms', async t => {
481cb0ef41Sopenharmony_ci  const { output } = await execHelpSearch(t, ['run', 'script'])
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci  t.match(output, /Top hits for/, 'outputs results')
511cb0ef41Sopenharmony_ci  t.match(output, /run:\d+ script:\d+/, 'shows hit counts for both terms')
521cb0ef41Sopenharmony_ci})
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cit.test('npm help-search long output', async t => {
551cb0ef41Sopenharmony_ci  const { output } = await execHelpSearch(t, ['exec'], {
561cb0ef41Sopenharmony_ci    config: {
571cb0ef41Sopenharmony_ci      long: true,
581cb0ef41Sopenharmony_ci    },
591cb0ef41Sopenharmony_ci  })
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci  t.match(output, /has multiple lines of exec help/, 'outputs detailed results')
621cb0ef41Sopenharmony_ci})
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cit.test('npm help-search long output with color', async t => {
651cb0ef41Sopenharmony_ci  const { output } = await execHelpSearch(t, ['help-search'], {
661cb0ef41Sopenharmony_ci    config: {
671cb0ef41Sopenharmony_ci      long: true,
681cb0ef41Sopenharmony_ci      color: 'always',
691cb0ef41Sopenharmony_ci    },
701cb0ef41Sopenharmony_ci  })
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  const chalk = await import('chalk').then(v => v.default)
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  const highlightedText = chalk.bgBlack.red('help-search')
751cb0ef41Sopenharmony_ci  t.equal(
761cb0ef41Sopenharmony_ci    output.split('\n').some(line => line.includes(highlightedText)),
771cb0ef41Sopenharmony_ci    true,
781cb0ef41Sopenharmony_ci    'returned highlighted search terms'
791cb0ef41Sopenharmony_ci  )
801cb0ef41Sopenharmony_ci})
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cit.test('npm help-search no args', async t => {
831cb0ef41Sopenharmony_ci  await t.rejects(execHelpSearch(t), /npm help-search/, 'outputs usage')
841cb0ef41Sopenharmony_ci})
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_cit.test('npm help-search no matches', async t => {
871cb0ef41Sopenharmony_ci  const { output } = await execHelpSearch(t, ['asdfasdf'])
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci  t.match(output, /No matches/)
901cb0ef41Sopenharmony_ci})
91