11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst localeCompare = require('@isaacs/string-locale-compare')('en')
31cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm.js')
41cb0ef41Sopenharmony_ciconst { cleanCwd } = require('../../fixtures/clean-snapshot')
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst genManPages = (obj) => {
71cb0ef41Sopenharmony_ci  const man = {}
81cb0ef41Sopenharmony_ci  const resPages = new Set()
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_ci  for (const [section, pages] of Object.entries(obj)) {
111cb0ef41Sopenharmony_ci    const num = parseInt(section, 10)
121cb0ef41Sopenharmony_ci    man[`man${num}`] = {}
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci    const sectionPages = []
151cb0ef41Sopenharmony_ci    for (const name of pages) {
161cb0ef41Sopenharmony_ci      man[`man${num}`][`${name}.${section}`] = `.TH "${name.toUpperCase()}" "${num}"`
171cb0ef41Sopenharmony_ci      sectionPages.push(name.replace(/^npm-/, ''))
181cb0ef41Sopenharmony_ci    }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    // return a sorted list of uniq pages in order to test completion
211cb0ef41Sopenharmony_ci    for (const p of sectionPages.sort(localeCompare)) {
221cb0ef41Sopenharmony_ci      resPages.add(p)
231cb0ef41Sopenharmony_ci    }
241cb0ef41Sopenharmony_ci  }
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci  // man directory name is hardcoded in the command
271cb0ef41Sopenharmony_ci  return { fixtures: { man }, pages: [...resPages.values()] }
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciconst mockHelp = async (t, {
311cb0ef41Sopenharmony_ci  man = {
321cb0ef41Sopenharmony_ci    5: ['npmrc', 'install', 'package-json'],
331cb0ef41Sopenharmony_ci    1: ['whoami', 'install', 'star', 'unstar', 'uninstall', 'unpublish'].map(p => `npm-${p}`),
341cb0ef41Sopenharmony_ci    7: ['disputes', 'config'],
351cb0ef41Sopenharmony_ci  },
361cb0ef41Sopenharmony_ci  browser = false,
371cb0ef41Sopenharmony_ci  woman = false,
381cb0ef41Sopenharmony_ci  exec: execArgs = null,
391cb0ef41Sopenharmony_ci  spawnErr,
401cb0ef41Sopenharmony_ci  ...opts
411cb0ef41Sopenharmony_ci} = {}) => {
421cb0ef41Sopenharmony_ci  const config = {
431cb0ef41Sopenharmony_ci    // always set viewer to test the same on all platforms
441cb0ef41Sopenharmony_ci    viewer: browser ? 'browser' : woman ? 'woman' : 'man',
451cb0ef41Sopenharmony_ci    ...opts.config,
461cb0ef41Sopenharmony_ci  }
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci  let args = null
491cb0ef41Sopenharmony_ci  const mockSpawn = async (...a) => {
501cb0ef41Sopenharmony_ci    args = a
511cb0ef41Sopenharmony_ci    if (spawnErr) {
521cb0ef41Sopenharmony_ci      throw spawnErr
531cb0ef41Sopenharmony_ci    }
541cb0ef41Sopenharmony_ci  }
551cb0ef41Sopenharmony_ci  mockSpawn.open = async (url) => args = [cleanCwd(decodeURI(url))]
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci  const manPages = genManPages(man)
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci  const { npm, ...rest } = await loadMockNpm(t, {
601cb0ef41Sopenharmony_ci    npm: ({ other }) => ({ npmRoot: other }),
611cb0ef41Sopenharmony_ci    mocks: { '@npmcli/promise-spawn': mockSpawn },
621cb0ef41Sopenharmony_ci    otherDirs: { ...manPages.fixtures },
631cb0ef41Sopenharmony_ci    config,
641cb0ef41Sopenharmony_ci    command: 'help',
651cb0ef41Sopenharmony_ci    exec: execArgs,
661cb0ef41Sopenharmony_ci    ...opts,
671cb0ef41Sopenharmony_ci  })
681cb0ef41Sopenharmony_ci
691cb0ef41Sopenharmony_ci  return {
701cb0ef41Sopenharmony_ci    npm,
711cb0ef41Sopenharmony_ci    manPages: manPages.pages,
721cb0ef41Sopenharmony_ci    getArgs: () => args,
731cb0ef41Sopenharmony_ci    ...rest,
741cb0ef41Sopenharmony_ci  }
751cb0ef41Sopenharmony_ci}
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_cit.test('npm help', async t => {
781cb0ef41Sopenharmony_ci  const { help, joinedOutput } = await mockHelp(t)
791cb0ef41Sopenharmony_ci  await help.exec()
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci  t.match(joinedOutput(), 'npm <command>', 'showed npm usage')
821cb0ef41Sopenharmony_ci})
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_cit.test('npm help completion', async t => {
851cb0ef41Sopenharmony_ci  const { help, manPages } = await mockHelp(t)
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  const noArgs = await help.completion({ conf: { argv: { remain: [] } } })
881cb0ef41Sopenharmony_ci  t.strictSame(noArgs, ['help', ...manPages], 'outputs available help pages')
891cb0ef41Sopenharmony_ci  const threeArgs = await help.completion({ conf: { argv: { remain: ['one', 'two', 'three'] } } })
901cb0ef41Sopenharmony_ci  t.strictSame(threeArgs, [], 'outputs no results when more than 2 args are provided')
911cb0ef41Sopenharmony_ci})
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_cit.test('npm help multiple args calls search', async t => {
941cb0ef41Sopenharmony_ci  const { joinedOutput } = await mockHelp(t, { exec: ['run', 'script'] })
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  t.match(joinedOutput(), 'No matches in help for: run script', 'calls help-search')
971cb0ef41Sopenharmony_ci})
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_cit.test('npm help no matches calls search', async t => {
1001cb0ef41Sopenharmony_ci  const { joinedOutput } = await mockHelp(t, { exec: ['asdfasdf'] })
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ci  t.match(joinedOutput(), 'No matches in help for: asdfasdf', 'passed the args to help-search')
1031cb0ef41Sopenharmony_ci})
1041cb0ef41Sopenharmony_ci
1051cb0ef41Sopenharmony_cit.test('npm help whoami', async t => {
1061cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, { exec: ['whoami'] })
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  const [spawnBin, spawnArgs] = getArgs()
1091cb0ef41Sopenharmony_ci  t.equal(spawnBin, 'man', 'calls man by default')
1101cb0ef41Sopenharmony_ci  t.equal(spawnArgs.length, 1)
1111cb0ef41Sopenharmony_ci  t.match(spawnArgs[0], /npm-whoami\.1$/)
1121cb0ef41Sopenharmony_ci})
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_cit.test('npm help 1 install', async t => {
1151cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1161cb0ef41Sopenharmony_ci    exec: ['1', 'install'],
1171cb0ef41Sopenharmony_ci    browser: true,
1181cb0ef41Sopenharmony_ci  })
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  const [url] = getArgs()
1211cb0ef41Sopenharmony_ci  t.match(url, /commands\/npm-install.html$/, 'attempts to open the correct url')
1221cb0ef41Sopenharmony_ci  t.ok(url.startsWith('file:///'), 'opens with the correct uri schema')
1231cb0ef41Sopenharmony_ci})
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_cit.test('npm help 5 install', async t => {
1261cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1271cb0ef41Sopenharmony_ci    exec: ['5', 'install'],
1281cb0ef41Sopenharmony_ci    browser: true,
1291cb0ef41Sopenharmony_ci  })
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci  const [url] = getArgs()
1321cb0ef41Sopenharmony_ci  t.match(url, /configuring-npm\/install.html$/, 'attempts to open the correct url')
1331cb0ef41Sopenharmony_ci})
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_cit.test('npm help 7 config', async t => {
1361cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1371cb0ef41Sopenharmony_ci    exec: ['7', 'config'],
1381cb0ef41Sopenharmony_ci    browser: true,
1391cb0ef41Sopenharmony_ci  })
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci  const [url] = getArgs()
1421cb0ef41Sopenharmony_ci  t.match(url, /using-npm\/config.html$/, 'attempts to open the correct url')
1431cb0ef41Sopenharmony_ci})
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_cit.test('npm help package.json redirects to package-json', async t => {
1461cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1471cb0ef41Sopenharmony_ci    exec: ['package.json'],
1481cb0ef41Sopenharmony_ci  })
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_ci  const [spawnBin, spawnArgs] = getArgs()
1511cb0ef41Sopenharmony_ci  t.equal(spawnBin, 'man', 'calls man by default')
1521cb0ef41Sopenharmony_ci  t.equal(spawnArgs.length, 1)
1531cb0ef41Sopenharmony_ci  t.match(spawnArgs[0], /package-json\.5$/)
1541cb0ef41Sopenharmony_ci})
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_cit.test('npm help ?(un)star', async t => {
1571cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1581cb0ef41Sopenharmony_ci    exec: ['?(un)star'],
1591cb0ef41Sopenharmony_ci    woman: true,
1601cb0ef41Sopenharmony_ci  })
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci  const [spawnBin, spawnArgs] = getArgs()
1631cb0ef41Sopenharmony_ci  t.equal(spawnBin, 'emacsclient', 'maps woman to emacs correctly')
1641cb0ef41Sopenharmony_ci  t.equal(spawnArgs.length, 2)
1651cb0ef41Sopenharmony_ci  t.match(spawnArgs[1], /^\(woman-find-file '/)
1661cb0ef41Sopenharmony_ci  t.match(spawnArgs[1], /npm-star.1'\)$/)
1671cb0ef41Sopenharmony_ci})
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_cit.test('npm help un*', async t => {
1701cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1711cb0ef41Sopenharmony_ci    exec: ['un*'],
1721cb0ef41Sopenharmony_ci  })
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci  const [spawnBin, spawnArgs] = getArgs()
1751cb0ef41Sopenharmony_ci  t.equal(spawnBin, 'man', 'calls man by default')
1761cb0ef41Sopenharmony_ci  t.equal(spawnArgs.length, 1)
1771cb0ef41Sopenharmony_ci  t.match(spawnArgs[0], /npm-uninstall\.1$/)
1781cb0ef41Sopenharmony_ci})
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_cit.test('npm help - prefers lowest numbered npm prefixed help pages', async t => {
1811cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1821cb0ef41Sopenharmony_ci    man: {
1831cb0ef41Sopenharmony_ci      6: ['npm-install'],
1841cb0ef41Sopenharmony_ci      1: ['npm-install'],
1851cb0ef41Sopenharmony_ci      5: ['install'],
1861cb0ef41Sopenharmony_ci      7: ['npm-install'],
1871cb0ef41Sopenharmony_ci    },
1881cb0ef41Sopenharmony_ci    exec: ['install'],
1891cb0ef41Sopenharmony_ci  })
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci  const [spawnBin, spawnArgs] = getArgs()
1921cb0ef41Sopenharmony_ci  t.equal(spawnBin, 'man', 'calls man by default')
1931cb0ef41Sopenharmony_ci  t.equal(spawnArgs.length, 1)
1941cb0ef41Sopenharmony_ci  t.match(spawnArgs[0], /npm-install\.1$/)
1951cb0ef41Sopenharmony_ci})
1961cb0ef41Sopenharmony_ci
1971cb0ef41Sopenharmony_cit.test('npm help - works in the presence of strange man pages', async t => {
1981cb0ef41Sopenharmony_ci  const { getArgs } = await mockHelp(t, {
1991cb0ef41Sopenharmony_ci    man: {
2001cb0ef41Sopenharmony_ci      '1strange': ['config'],
2011cb0ef41Sopenharmony_ci      5: ['config'],
2021cb0ef41Sopenharmony_ci      '6ssl': ['config'],
2031cb0ef41Sopenharmony_ci    },
2041cb0ef41Sopenharmony_ci    exec: ['config'],
2051cb0ef41Sopenharmony_ci  })
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci  const [spawnBin, spawnArgs] = getArgs()
2081cb0ef41Sopenharmony_ci  t.equal(spawnBin, 'man', 'calls man by default')
2091cb0ef41Sopenharmony_ci  t.equal(spawnArgs.length, 1)
2101cb0ef41Sopenharmony_ci  t.match(spawnArgs[0], /config\.5$/)
2111cb0ef41Sopenharmony_ci})
2121cb0ef41Sopenharmony_ci
2131cb0ef41Sopenharmony_cit.test('rejects with code', async t => {
2141cb0ef41Sopenharmony_ci  const { help } = await mockHelp(t, {
2151cb0ef41Sopenharmony_ci    spawnErr: Object.assign(new Error('errrrr'), { code: 'SPAWN_ERR' }),
2161cb0ef41Sopenharmony_ci  })
2171cb0ef41Sopenharmony_ci
2181cb0ef41Sopenharmony_ci  await t.rejects(help.exec(['whoami']), /help process exited with code: SPAWN_ERR/)
2191cb0ef41Sopenharmony_ci})
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_cit.test('rejects with no code', async t => {
2221cb0ef41Sopenharmony_ci  const { help } = await mockHelp(t, {
2231cb0ef41Sopenharmony_ci    spawnErr: new Error('errrrr'),
2241cb0ef41Sopenharmony_ci  })
2251cb0ef41Sopenharmony_ci
2261cb0ef41Sopenharmony_ci  await t.rejects(help.exec(['whoami']), /errrrr/)
2271cb0ef41Sopenharmony_ci})
228