11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm')
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ciconst pacote = {
51cb0ef41Sopenharmony_ci  manifest: async (spec) => {
61cb0ef41Sopenharmony_ci    return spec === 'nobugs' ? {
71cb0ef41Sopenharmony_ci      name: 'nobugs',
81cb0ef41Sopenharmony_ci      version: '1.2.3',
91cb0ef41Sopenharmony_ci    } : spec === 'nullbugs' ? {
101cb0ef41Sopenharmony_ci      name: 'nullbugs',
111cb0ef41Sopenharmony_ci      version: '1.2.3',
121cb0ef41Sopenharmony_ci      bugs: null,
131cb0ef41Sopenharmony_ci    } : spec === 'bugsurl' ? {
141cb0ef41Sopenharmony_ci      name: 'bugsurl',
151cb0ef41Sopenharmony_ci      version: '1.2.3',
161cb0ef41Sopenharmony_ci      bugs: 'https://bugzilla.localhost/bugsurl',
171cb0ef41Sopenharmony_ci    } : spec === 'bugsobj' ? {
181cb0ef41Sopenharmony_ci      name: 'bugsobj',
191cb0ef41Sopenharmony_ci      version: '1.2.3',
201cb0ef41Sopenharmony_ci      bugs: { url: 'https://bugzilla.localhost/bugsobj' },
211cb0ef41Sopenharmony_ci    } : spec === 'bugsobj-nourl' ? {
221cb0ef41Sopenharmony_ci      name: 'bugsobj-nourl',
231cb0ef41Sopenharmony_ci      version: '1.2.3',
241cb0ef41Sopenharmony_ci      bugs: { no: 'url here' },
251cb0ef41Sopenharmony_ci    } : spec === 'repourl' ? {
261cb0ef41Sopenharmony_ci      name: 'repourl',
271cb0ef41Sopenharmony_ci      version: '1.2.3',
281cb0ef41Sopenharmony_ci      repository: 'https://github.com/foo/repourl',
291cb0ef41Sopenharmony_ci    } : spec === 'repoobj' ? {
301cb0ef41Sopenharmony_ci      name: 'repoobj',
311cb0ef41Sopenharmony_ci      version: '1.2.3',
321cb0ef41Sopenharmony_ci      repository: { url: 'https://github.com/foo/repoobj' },
331cb0ef41Sopenharmony_ci    } : spec === 'mailtest' ? {
341cb0ef41Sopenharmony_ci      name: 'mailtest',
351cb0ef41Sopenharmony_ci      version: '3.7.4',
361cb0ef41Sopenharmony_ci      bugs: { email: 'hello@example.com' },
371cb0ef41Sopenharmony_ci    } : spec === 'secondmailtest' ? {
381cb0ef41Sopenharmony_ci      name: 'secondmailtest',
391cb0ef41Sopenharmony_ci      version: '0.1.1',
401cb0ef41Sopenharmony_ci      bugs: { email: 'ABC432abc@a.b.example.net' },
411cb0ef41Sopenharmony_ci    } : spec === '.' ? {
421cb0ef41Sopenharmony_ci      name: 'thispkg',
431cb0ef41Sopenharmony_ci      version: '1.2.3',
441cb0ef41Sopenharmony_ci      bugs: 'https://example.com',
451cb0ef41Sopenharmony_ci    } : null
461cb0ef41Sopenharmony_ci  },
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cit.test('usage', async (t) => {
501cb0ef41Sopenharmony_ci  const { bugs } = await loadMockNpm(t, { command: 'bugs' })
511cb0ef41Sopenharmony_ci  t.match(bugs.usage, 'bugs', 'usage has command name in it')
521cb0ef41Sopenharmony_ci})
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_cit.test('open bugs urls & emails', async t => {
551cb0ef41Sopenharmony_ci  // keep a tally of which urls got opened
561cb0ef41Sopenharmony_ci  let opened = {}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci  const openUrl = async (_, url) => {
591cb0ef41Sopenharmony_ci    opened[url] = opened[url] || 0
601cb0ef41Sopenharmony_ci    opened[url]++
611cb0ef41Sopenharmony_ci  }
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
641cb0ef41Sopenharmony_ci    mocks: {
651cb0ef41Sopenharmony_ci      pacote,
661cb0ef41Sopenharmony_ci      '{LIB}/utils/open-url.js': openUrl,
671cb0ef41Sopenharmony_ci    },
681cb0ef41Sopenharmony_ci  })
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci  const expected = {
711cb0ef41Sopenharmony_ci    '.': 'https://example.com',
721cb0ef41Sopenharmony_ci    nobugs: 'https://www.npmjs.com/package/nobugs',
731cb0ef41Sopenharmony_ci    nullbugs: 'https://www.npmjs.com/package/nullbugs',
741cb0ef41Sopenharmony_ci    'bugsobj-nourl': 'https://www.npmjs.com/package/bugsobj-nourl',
751cb0ef41Sopenharmony_ci    bugsurl: 'https://bugzilla.localhost/bugsurl',
761cb0ef41Sopenharmony_ci    bugsobj: 'https://bugzilla.localhost/bugsobj',
771cb0ef41Sopenharmony_ci    repourl: 'https://github.com/foo/repourl/issues',
781cb0ef41Sopenharmony_ci    repoobj: 'https://github.com/foo/repoobj/issues',
791cb0ef41Sopenharmony_ci    mailtest: 'mailto:hello@example.com',
801cb0ef41Sopenharmony_ci    secondmailtest: 'mailto:ABC432abc@a.b.example.net',
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci  for (const [pkg, expect] of Object.entries(expected)) {
841cb0ef41Sopenharmony_ci    await t.test(pkg, async t => {
851cb0ef41Sopenharmony_ci      await npm.exec('bugs', [pkg])
861cb0ef41Sopenharmony_ci      t.equal(opened[expect], 1, 'opened expected url', { opened })
871cb0ef41Sopenharmony_ci    })
881cb0ef41Sopenharmony_ci  }
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci  opened = {}
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  await t.test('open default package if none specified', async t => {
931cb0ef41Sopenharmony_ci    await npm.exec('bugs', [])
941cb0ef41Sopenharmony_ci    t.equal(opened['https://example.com'], 1, 'opened expected url', { opened })
951cb0ef41Sopenharmony_ci  })
961cb0ef41Sopenharmony_ci})
97