11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst mockNpm = require('../../fixtures/mock-npm.js')
31cb0ef41Sopenharmony_ciconst { sep } = require('path')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst fixture = {
61cb0ef41Sopenharmony_ci  'package.json': JSON.stringify({
71cb0ef41Sopenharmony_ci    name: 'thispkg',
81cb0ef41Sopenharmony_ci    version: '1.2.3',
91cb0ef41Sopenharmony_ci    repository: 'https://example.com/thispkg.git',
101cb0ef41Sopenharmony_ci  }),
111cb0ef41Sopenharmony_ci  norepo: {
121cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
131cb0ef41Sopenharmony_ci      name: 'norepo',
141cb0ef41Sopenharmony_ci      version: '1.2.3',
151cb0ef41Sopenharmony_ci    }),
161cb0ef41Sopenharmony_ci  },
171cb0ef41Sopenharmony_ci  'repoobbj-nourl': {
181cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
191cb0ef41Sopenharmony_ci      name: 'repoobj-nourl',
201cb0ef41Sopenharmony_ci      repository: { no: 'url' },
211cb0ef41Sopenharmony_ci    }),
221cb0ef41Sopenharmony_ci  },
231cb0ef41Sopenharmony_ci  hostedgit: {
241cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
251cb0ef41Sopenharmony_ci      repository: 'git://github.com/foo/hostedgit',
261cb0ef41Sopenharmony_ci    }),
271cb0ef41Sopenharmony_ci  },
281cb0ef41Sopenharmony_ci  hostedgitat: {
291cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
301cb0ef41Sopenharmony_ci      repository: 'git@github.com:foo/hostedgitat',
311cb0ef41Sopenharmony_ci    }),
321cb0ef41Sopenharmony_ci  },
331cb0ef41Sopenharmony_ci  hostedssh: {
341cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
351cb0ef41Sopenharmony_ci      repository: 'ssh://git@github.com/foo/hostedssh',
361cb0ef41Sopenharmony_ci    }),
371cb0ef41Sopenharmony_ci  },
381cb0ef41Sopenharmony_ci  hostedgitssh: {
391cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
401cb0ef41Sopenharmony_ci      repository: 'git+ssh://git@github.com/foo/hostedgitssh',
411cb0ef41Sopenharmony_ci    }),
421cb0ef41Sopenharmony_ci  },
431cb0ef41Sopenharmony_ci  hostedgithttp: {
441cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
451cb0ef41Sopenharmony_ci      repository: 'git+http://github.com/foo/hostedgithttp',
461cb0ef41Sopenharmony_ci    }),
471cb0ef41Sopenharmony_ci  },
481cb0ef41Sopenharmony_ci  hostedgithttps: {
491cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
501cb0ef41Sopenharmony_ci      repository: 'git+https://github.com/foo/hostedgithttps',
511cb0ef41Sopenharmony_ci    }),
521cb0ef41Sopenharmony_ci  },
531cb0ef41Sopenharmony_ci  hostedgitobj: {
541cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
551cb0ef41Sopenharmony_ci      repository: { url: 'git://github.com/foo/hostedgitobj' },
561cb0ef41Sopenharmony_ci    }),
571cb0ef41Sopenharmony_ci  },
581cb0ef41Sopenharmony_ci  hostedgitatobj: {
591cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
601cb0ef41Sopenharmony_ci      repository: { url: 'git@github.com:foo/hostedgitatobj' },
611cb0ef41Sopenharmony_ci    }),
621cb0ef41Sopenharmony_ci  },
631cb0ef41Sopenharmony_ci  hostedsshobj: {
641cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
651cb0ef41Sopenharmony_ci      repository: { url: 'ssh://git@github.com/foo/hostedsshobj' },
661cb0ef41Sopenharmony_ci    }),
671cb0ef41Sopenharmony_ci  },
681cb0ef41Sopenharmony_ci  hostedgitsshobj: {
691cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
701cb0ef41Sopenharmony_ci      repository: { url: 'git+ssh://git@github.com/foo/hostedgitsshobj' },
711cb0ef41Sopenharmony_ci    }),
721cb0ef41Sopenharmony_ci  },
731cb0ef41Sopenharmony_ci  hostedgithttpobj: {
741cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
751cb0ef41Sopenharmony_ci      repository: { url: 'git+http://github.com/foo/hostedgithttpobj' },
761cb0ef41Sopenharmony_ci    }),
771cb0ef41Sopenharmony_ci  },
781cb0ef41Sopenharmony_ci  hostedgithttpsobj: {
791cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
801cb0ef41Sopenharmony_ci      repository: { url: 'git+https://github.com/foo/hostedgithttpsobj' },
811cb0ef41Sopenharmony_ci    }),
821cb0ef41Sopenharmony_ci  },
831cb0ef41Sopenharmony_ci  unhostedgit: {
841cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
851cb0ef41Sopenharmony_ci      repository: 'git://gothib.com/foo/unhostedgit',
861cb0ef41Sopenharmony_ci    }),
871cb0ef41Sopenharmony_ci  },
881cb0ef41Sopenharmony_ci  unhostedgitat: {
891cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
901cb0ef41Sopenharmony_ci      repository: 'git@gothib.com:foo/unhostedgitat',
911cb0ef41Sopenharmony_ci    }),
921cb0ef41Sopenharmony_ci  },
931cb0ef41Sopenharmony_ci  unhostedssh: {
941cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
951cb0ef41Sopenharmony_ci      repository: 'ssh://git@gothib.com/foo/unhostedssh',
961cb0ef41Sopenharmony_ci    }),
971cb0ef41Sopenharmony_ci  },
981cb0ef41Sopenharmony_ci  unhostedgitssh: {
991cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1001cb0ef41Sopenharmony_ci      repository: 'git+ssh://git@gothib.com/foo/unhostedgitssh',
1011cb0ef41Sopenharmony_ci    }),
1021cb0ef41Sopenharmony_ci  },
1031cb0ef41Sopenharmony_ci  unhostedgithttp: {
1041cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1051cb0ef41Sopenharmony_ci      repository: 'git+http://gothib.com/foo/unhostedgithttp',
1061cb0ef41Sopenharmony_ci    }),
1071cb0ef41Sopenharmony_ci  },
1081cb0ef41Sopenharmony_ci  unhostedgithttps: {
1091cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1101cb0ef41Sopenharmony_ci      repository: 'git+https://gothib.com/foo/unhostedgithttps',
1111cb0ef41Sopenharmony_ci    }),
1121cb0ef41Sopenharmony_ci  },
1131cb0ef41Sopenharmony_ci  unhostedgitobj: {
1141cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1151cb0ef41Sopenharmony_ci      repository: { url: 'git://gothib.com/foo/unhostedgitobj' },
1161cb0ef41Sopenharmony_ci    }),
1171cb0ef41Sopenharmony_ci  },
1181cb0ef41Sopenharmony_ci  unhostedgitatobj: {
1191cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1201cb0ef41Sopenharmony_ci      repository: { url: 'git@gothib.com:foo/unhostedgitatobj' },
1211cb0ef41Sopenharmony_ci    }),
1221cb0ef41Sopenharmony_ci  },
1231cb0ef41Sopenharmony_ci  unhostedsshobj: {
1241cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1251cb0ef41Sopenharmony_ci      repository: { url: 'ssh://git@gothib.com/foo/unhostedsshobj' },
1261cb0ef41Sopenharmony_ci    }),
1271cb0ef41Sopenharmony_ci  },
1281cb0ef41Sopenharmony_ci  unhostedgitsshobj: {
1291cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1301cb0ef41Sopenharmony_ci      repository: { url: 'git+ssh://git@gothib.com/foo/unhostedgitsshobj' },
1311cb0ef41Sopenharmony_ci    }),
1321cb0ef41Sopenharmony_ci  },
1331cb0ef41Sopenharmony_ci  unhostedgithttpobj: {
1341cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1351cb0ef41Sopenharmony_ci      repository: { url: 'git+http://gothib.com/foo/unhostedgithttpobj' },
1361cb0ef41Sopenharmony_ci    }),
1371cb0ef41Sopenharmony_ci  },
1381cb0ef41Sopenharmony_ci  unhostedgithttpsobj: {
1391cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1401cb0ef41Sopenharmony_ci      repository: { url: 'git+https://gothib.com/foo/unhostedgithttpsobj' },
1411cb0ef41Sopenharmony_ci    }),
1421cb0ef41Sopenharmony_ci  },
1431cb0ef41Sopenharmony_ci  directory: {
1441cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1451cb0ef41Sopenharmony_ci      repository: {
1461cb0ef41Sopenharmony_ci        type: 'git',
1471cb0ef41Sopenharmony_ci        url: 'git+https://github.com/foo/test-repo-with-directory.git',
1481cb0ef41Sopenharmony_ci        directory: 'some/directory',
1491cb0ef41Sopenharmony_ci      },
1501cb0ef41Sopenharmony_ci    }),
1511cb0ef41Sopenharmony_ci  },
1521cb0ef41Sopenharmony_ci}
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ciconst workspaceFixture = {
1551cb0ef41Sopenharmony_ci  'package.json': JSON.stringify({
1561cb0ef41Sopenharmony_ci    name: 'workspaces-test',
1571cb0ef41Sopenharmony_ci    version: '1.2.3-test',
1581cb0ef41Sopenharmony_ci    workspaces: ['workspace-a', 'workspace-b', 'workspace-c'],
1591cb0ef41Sopenharmony_ci    repository: 'https://github.com/npm/workspaces-test',
1601cb0ef41Sopenharmony_ci  }),
1611cb0ef41Sopenharmony_ci  'workspace-a': {
1621cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1631cb0ef41Sopenharmony_ci      name: 'workspace-a',
1641cb0ef41Sopenharmony_ci      version: '1.2.3-a',
1651cb0ef41Sopenharmony_ci      repository: 'http://repo.workspace-a/',
1661cb0ef41Sopenharmony_ci    }),
1671cb0ef41Sopenharmony_ci  },
1681cb0ef41Sopenharmony_ci  'workspace-b': {
1691cb0ef41Sopenharmony_ci    'package.json': JSON.stringify({
1701cb0ef41Sopenharmony_ci      name: 'workspace-b',
1711cb0ef41Sopenharmony_ci      version: '1.2.3-n',
1721cb0ef41Sopenharmony_ci      repository: 'https://github.com/npm/workspace-b',
1731cb0ef41Sopenharmony_ci    }),
1741cb0ef41Sopenharmony_ci  },
1751cb0ef41Sopenharmony_ci  'workspace-c': JSON.stringify({
1761cb0ef41Sopenharmony_ci    'package.json': {
1771cb0ef41Sopenharmony_ci      name: 'workspace-n',
1781cb0ef41Sopenharmony_ci      version: '1.2.3-n',
1791cb0ef41Sopenharmony_ci    },
1801cb0ef41Sopenharmony_ci  }),
1811cb0ef41Sopenharmony_ci}
1821cb0ef41Sopenharmony_ci
1831cb0ef41Sopenharmony_ciconst loadMockNpm = async (t, prefixDir, config = {}) => {
1841cb0ef41Sopenharmony_ci  // keep a tally of which urls got opened
1851cb0ef41Sopenharmony_ci  const opened = {}
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ci  const mock = await mockNpm(t, {
1881cb0ef41Sopenharmony_ci    command: 'repo',
1891cb0ef41Sopenharmony_ci    mocks: {
1901cb0ef41Sopenharmony_ci      '{LIB}/utils/open-url.js': async (_, url) => {
1911cb0ef41Sopenharmony_ci        opened[url] = opened[url] || 0
1921cb0ef41Sopenharmony_ci        opened[url]++
1931cb0ef41Sopenharmony_ci      },
1941cb0ef41Sopenharmony_ci    },
1951cb0ef41Sopenharmony_ci    config,
1961cb0ef41Sopenharmony_ci    prefixDir,
1971cb0ef41Sopenharmony_ci  })
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci  return {
2001cb0ef41Sopenharmony_ci    ...mock,
2011cb0ef41Sopenharmony_ci    opened,
2021cb0ef41Sopenharmony_ci  }
2031cb0ef41Sopenharmony_ci}
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_cit.test('open repo urls', async t => {
2061cb0ef41Sopenharmony_ci  const { repo, opened } = await loadMockNpm(t, fixture)
2071cb0ef41Sopenharmony_ci  const expect = {
2081cb0ef41Sopenharmony_ci    hostedgit: 'https://github.com/foo/hostedgit',
2091cb0ef41Sopenharmony_ci    hostedgitat: 'https://github.com/foo/hostedgitat',
2101cb0ef41Sopenharmony_ci    hostedssh: 'https://github.com/foo/hostedssh',
2111cb0ef41Sopenharmony_ci    hostedgitssh: 'https://github.com/foo/hostedgitssh',
2121cb0ef41Sopenharmony_ci    hostedgithttp: 'https://github.com/foo/hostedgithttp',
2131cb0ef41Sopenharmony_ci    hostedgithttps: 'https://github.com/foo/hostedgithttps',
2141cb0ef41Sopenharmony_ci    hostedgitobj: 'https://github.com/foo/hostedgitobj',
2151cb0ef41Sopenharmony_ci    hostedgitatobj: 'https://github.com/foo/hostedgitatobj',
2161cb0ef41Sopenharmony_ci    hostedsshobj: 'https://github.com/foo/hostedsshobj',
2171cb0ef41Sopenharmony_ci    hostedgitsshobj: 'https://github.com/foo/hostedgitsshobj',
2181cb0ef41Sopenharmony_ci    hostedgithttpobj: 'https://github.com/foo/hostedgithttpobj',
2191cb0ef41Sopenharmony_ci    hostedgithttpsobj: 'https://github.com/foo/hostedgithttpsobj',
2201cb0ef41Sopenharmony_ci    unhostedgit: 'https://gothib.com/foo/unhostedgit',
2211cb0ef41Sopenharmony_ci    unhostedssh: 'https://gothib.com/foo/unhostedssh',
2221cb0ef41Sopenharmony_ci    unhostedgitssh: 'https://gothib.com/foo/unhostedgitssh',
2231cb0ef41Sopenharmony_ci    unhostedgithttp: 'http://gothib.com/foo/unhostedgithttp',
2241cb0ef41Sopenharmony_ci    unhostedgithttps: 'https://gothib.com/foo/unhostedgithttps',
2251cb0ef41Sopenharmony_ci    unhostedgitobj: 'https://gothib.com/foo/unhostedgitobj',
2261cb0ef41Sopenharmony_ci    unhostedsshobj: 'https://gothib.com/foo/unhostedsshobj',
2271cb0ef41Sopenharmony_ci    unhostedgitsshobj: 'https://gothib.com/foo/unhostedgitsshobj',
2281cb0ef41Sopenharmony_ci    unhostedgithttpobj: 'http://gothib.com/foo/unhostedgithttpobj',
2291cb0ef41Sopenharmony_ci    unhostedgithttpsobj: 'https://gothib.com/foo/unhostedgithttpsobj',
2301cb0ef41Sopenharmony_ci    directory: 'https://github.com/foo/test-repo-with-directory/tree/HEAD/some/directory',
2311cb0ef41Sopenharmony_ci    '.': 'https://example.com/thispkg',
2321cb0ef41Sopenharmony_ci  }
2331cb0ef41Sopenharmony_ci  for (const [pkg, url] of Object.entries(expect)) {
2341cb0ef41Sopenharmony_ci    await repo.exec([['.', pkg].join(sep)])
2351cb0ef41Sopenharmony_ci    t.equal(opened[url], 1, `opened ${url}`)
2361cb0ef41Sopenharmony_ci  }
2371cb0ef41Sopenharmony_ci})
2381cb0ef41Sopenharmony_ci
2391cb0ef41Sopenharmony_cit.test('fail if cannot figure out repo url', async t => {
2401cb0ef41Sopenharmony_ci  const { repo } = await loadMockNpm(t, fixture)
2411cb0ef41Sopenharmony_ci
2421cb0ef41Sopenharmony_ci  const cases = [
2431cb0ef41Sopenharmony_ci    'norepo',
2441cb0ef41Sopenharmony_ci    'repoobbj-nourl',
2451cb0ef41Sopenharmony_ci    'unhostedgitat',
2461cb0ef41Sopenharmony_ci    'unhostedgitatobj',
2471cb0ef41Sopenharmony_ci  ]
2481cb0ef41Sopenharmony_ci
2491cb0ef41Sopenharmony_ci  for (const pkg of cases) {
2501cb0ef41Sopenharmony_ci    await t.rejects(
2511cb0ef41Sopenharmony_ci      repo.exec([['.', pkg].join(sep)]),
2521cb0ef41Sopenharmony_ci      { pkgid: pkg }
2531cb0ef41Sopenharmony_ci    )
2541cb0ef41Sopenharmony_ci  }
2551cb0ef41Sopenharmony_ci})
2561cb0ef41Sopenharmony_ci
2571cb0ef41Sopenharmony_cit.test('open default package if none specified', async t => {
2581cb0ef41Sopenharmony_ci  const { repo, opened } = await loadMockNpm(t, fixture)
2591cb0ef41Sopenharmony_ci  await repo.exec([])
2601cb0ef41Sopenharmony_ci  t.equal(opened['https://example.com/thispkg'], 1, 'opened expected url', { opened })
2611cb0ef41Sopenharmony_ci})
2621cb0ef41Sopenharmony_ci
2631cb0ef41Sopenharmony_cit.test('workspaces', async t => {
2641cb0ef41Sopenharmony_ci  const mockWorkspaces = (t, config) => loadMockNpm(t, workspaceFixture, config)
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ci  t.test('include workspace root', async (t) => {
2671cb0ef41Sopenharmony_ci    const { opened, repo } = await mockWorkspaces(t, {
2681cb0ef41Sopenharmony_ci      workspaces: true,
2691cb0ef41Sopenharmony_ci      'include-workspace-root': true,
2701cb0ef41Sopenharmony_ci    })
2711cb0ef41Sopenharmony_ci    await repo.exec([])
2721cb0ef41Sopenharmony_ci    t.match({
2731cb0ef41Sopenharmony_ci      'https://github.com/npm/workspaces-test': 1,
2741cb0ef41Sopenharmony_ci      'https://repo.workspace-a/': 1, // Gets translated to https!
2751cb0ef41Sopenharmony_ci      'https://github.com/npm/workspace-b': 1,
2761cb0ef41Sopenharmony_ci    }, opened, 'opened two valid repo urls')
2771cb0ef41Sopenharmony_ci  })
2781cb0ef41Sopenharmony_ci
2791cb0ef41Sopenharmony_ci  t.test('all workspaces', async (t) => {
2801cb0ef41Sopenharmony_ci    const { opened, repo } = await mockWorkspaces(t, {
2811cb0ef41Sopenharmony_ci      workspaces: true,
2821cb0ef41Sopenharmony_ci    })
2831cb0ef41Sopenharmony_ci    await repo.exec([])
2841cb0ef41Sopenharmony_ci    t.match({
2851cb0ef41Sopenharmony_ci      'https://repo.workspace-a/': 1, // Gets translated to https!
2861cb0ef41Sopenharmony_ci      'https://github.com/npm/workspace-b': 1,
2871cb0ef41Sopenharmony_ci    }, opened, 'opened two valid repo urls')
2881cb0ef41Sopenharmony_ci  })
2891cb0ef41Sopenharmony_ci
2901cb0ef41Sopenharmony_ci  t.test('one workspace', async (t) => {
2911cb0ef41Sopenharmony_ci    const { opened, repo } = await mockWorkspaces(t, {
2921cb0ef41Sopenharmony_ci      workspace: ['workspace-a'],
2931cb0ef41Sopenharmony_ci    })
2941cb0ef41Sopenharmony_ci    await repo.exec([])
2951cb0ef41Sopenharmony_ci    t.match({
2961cb0ef41Sopenharmony_ci      'https://repo.workspace-a/': 1,
2971cb0ef41Sopenharmony_ci    }, opened, 'opened one requested repo urls')
2981cb0ef41Sopenharmony_ci  })
2991cb0ef41Sopenharmony_ci
3001cb0ef41Sopenharmony_ci  t.test('invalid workspace', async (t) => {
3011cb0ef41Sopenharmony_ci    const { opened, repo } = await mockWorkspaces(t, {
3021cb0ef41Sopenharmony_ci      workspace: ['workspace-x'],
3031cb0ef41Sopenharmony_ci    })
3041cb0ef41Sopenharmony_ci    await t.rejects(
3051cb0ef41Sopenharmony_ci      repo.exec([]),
3061cb0ef41Sopenharmony_ci      /workspace-x/
3071cb0ef41Sopenharmony_ci    )
3081cb0ef41Sopenharmony_ci    t.match({}, opened, 'opened no repo urls')
3091cb0ef41Sopenharmony_ci  })
3101cb0ef41Sopenharmony_ci
3111cb0ef41Sopenharmony_ci  t.test('package arg and workspace', async (t) => {
3121cb0ef41Sopenharmony_ci    const { opened, repo } = await mockWorkspaces(t, {
3131cb0ef41Sopenharmony_ci      workspace: ['workspace-x'],
3141cb0ef41Sopenharmony_ci    })
3151cb0ef41Sopenharmony_ci    await repo.exec(['.'])
3161cb0ef41Sopenharmony_ci    t.match({
3171cb0ef41Sopenharmony_ci      'https://github.com/npm/workspaces-test': 1,
3181cb0ef41Sopenharmony_ci    }, opened, 'opened url for package arg, not workspace')
3191cb0ef41Sopenharmony_ci  })
3201cb0ef41Sopenharmony_ci})
321