1const t = require('tap')
2const { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
3
4/* eslint-disable max-len */
5const docsFixtures = {
6  dir1: {
7    'npm-exec.md': 'the exec command\nhelp has multiple lines of exec help\none of them references exec',
8  },
9  dir2: {
10    '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',
11    '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',
12    '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',
13  },
14  dir3: {
15    'npm-help.md': 'will run the `help-search` command if you need to run it to help you search',
16    'npm-help-search.md': 'is the help search command\nthat you get if you run help-search',
17    'npm-useless.md': 'exec\nexec',
18    'npm-more-useless.md': 'exec exec',
19    'npm-extra-useless.md': 'exec\nexec\nexec',
20  },
21}
22/* eslint-enable max-len */
23
24const execHelpSearch = async (t, exec = [], opts) => {
25  const { npm, ...rest } = await loadMockNpm(t, {
26    npm: ({ other }) => ({ npmRoot: other }),
27    // docs/content is hardcoded into the glob path in the command
28    otherDirs: {
29      docs: {
30        content: docsFixtures,
31      },
32    },
33    ...opts,
34  })
35
36  await npm.exec('help-search', exec)
37
38  return { npm, output: rest.joinedOutput(), ...rest }
39}
40
41t.test('npm help-search', async t => {
42  const { output } = await execHelpSearch(t, ['exec'])
43
44  t.match(output, /Top hits for "exec"/, 'outputs results')
45})
46
47t.test('npm help-search multiple terms', async t => {
48  const { output } = await execHelpSearch(t, ['run', 'script'])
49
50  t.match(output, /Top hits for/, 'outputs results')
51  t.match(output, /run:\d+ script:\d+/, 'shows hit counts for both terms')
52})
53
54t.test('npm help-search long output', async t => {
55  const { output } = await execHelpSearch(t, ['exec'], {
56    config: {
57      long: true,
58    },
59  })
60
61  t.match(output, /has multiple lines of exec help/, 'outputs detailed results')
62})
63
64t.test('npm help-search long output with color', async t => {
65  const { output } = await execHelpSearch(t, ['help-search'], {
66    config: {
67      long: true,
68      color: 'always',
69    },
70  })
71
72  const chalk = await import('chalk').then(v => v.default)
73
74  const highlightedText = chalk.bgBlack.red('help-search')
75  t.equal(
76    output.split('\n').some(line => line.includes(highlightedText)),
77    true,
78    'returned highlighted search terms'
79  )
80})
81
82t.test('npm help-search no args', async t => {
83  await t.rejects(execHelpSearch(t), /npm help-search/, 'outputs usage')
84})
85
86t.test('npm help-search no matches', async t => {
87  const { output } = await execHelpSearch(t, ['asdfasdf'])
88
89  t.match(output, /No matches/)
90})
91