11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst path = require('path')
31cb0ef41Sopenharmony_ciconst fs = require('fs')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst { load: loadMockNpm } = require('../../fixtures/mock-npm')
61cb0ef41Sopenharmony_ciconst MockRegistry = require('@npmcli/mock-registry')
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cit.test('should throw in global mode', async (t) => {
91cb0ef41Sopenharmony_ci  const { npm } = await loadMockNpm(t, {
101cb0ef41Sopenharmony_ci    config: {
111cb0ef41Sopenharmony_ci      global: true,
121cb0ef41Sopenharmony_ci    },
131cb0ef41Sopenharmony_ci  })
141cb0ef41Sopenharmony_ci  t.rejects(
151cb0ef41Sopenharmony_ci    npm.exec('dedupe', []),
161cb0ef41Sopenharmony_ci    { code: 'EDEDUPEGLOBAL' },
171cb0ef41Sopenharmony_ci    'throws EDEDUPEGLOBALE'
181cb0ef41Sopenharmony_ci  )
191cb0ef41Sopenharmony_ci})
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciconst treeWithDupes = {
221cb0ef41Sopenharmony_ci  'package.json': JSON.stringify({
231cb0ef41Sopenharmony_ci    name: 'test-top',
241cb0ef41Sopenharmony_ci    version: '1.0.0',
251cb0ef41Sopenharmony_ci    dependencies: {
261cb0ef41Sopenharmony_ci      'test-dep-a': '*',
271cb0ef41Sopenharmony_ci      'test-dep-b': '*',
281cb0ef41Sopenharmony_ci    },
291cb0ef41Sopenharmony_ci  }),
301cb0ef41Sopenharmony_ci  node_modules: {
311cb0ef41Sopenharmony_ci    'test-dep-a': {
321cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
331cb0ef41Sopenharmony_ci        name: 'test-dep-a',
341cb0ef41Sopenharmony_ci        version: '1.0.1',
351cb0ef41Sopenharmony_ci        dependencies: { 'test-sub': '*' },
361cb0ef41Sopenharmony_ci      }),
371cb0ef41Sopenharmony_ci      node_modules: {
381cb0ef41Sopenharmony_ci        'test-sub': {
391cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
401cb0ef41Sopenharmony_ci            name: 'test-sub',
411cb0ef41Sopenharmony_ci            version: '1.0.0',
421cb0ef41Sopenharmony_ci          }),
431cb0ef41Sopenharmony_ci        },
441cb0ef41Sopenharmony_ci      },
451cb0ef41Sopenharmony_ci    },
461cb0ef41Sopenharmony_ci    'test-dep-b': {
471cb0ef41Sopenharmony_ci      'package.json': JSON.stringify({
481cb0ef41Sopenharmony_ci        name: 'test-dep-b',
491cb0ef41Sopenharmony_ci        version: '1.0.0',
501cb0ef41Sopenharmony_ci        dependencies: { 'test-sub': '*' },
511cb0ef41Sopenharmony_ci      }),
521cb0ef41Sopenharmony_ci      node_modules: {
531cb0ef41Sopenharmony_ci        'test-sub': {
541cb0ef41Sopenharmony_ci          'package.json': JSON.stringify({
551cb0ef41Sopenharmony_ci            name: 'test-sub',
561cb0ef41Sopenharmony_ci            version: '1.0.0',
571cb0ef41Sopenharmony_ci          }),
581cb0ef41Sopenharmony_ci        },
591cb0ef41Sopenharmony_ci      },
601cb0ef41Sopenharmony_ci    },
611cb0ef41Sopenharmony_ci  },
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_cit.test('dedupe', async (t) => {
651cb0ef41Sopenharmony_ci  const { npm, joinedOutput } = await loadMockNpm(t, {
661cb0ef41Sopenharmony_ci    prefixDir: treeWithDupes,
671cb0ef41Sopenharmony_ci  })
681cb0ef41Sopenharmony_ci  const registry = new MockRegistry({
691cb0ef41Sopenharmony_ci    tap: t,
701cb0ef41Sopenharmony_ci    registry: npm.config.get('registry'),
711cb0ef41Sopenharmony_ci  })
721cb0ef41Sopenharmony_ci  const manifestSub = registry.manifest({
731cb0ef41Sopenharmony_ci    name: 'test-sub',
741cb0ef41Sopenharmony_ci    packuments: [{ version: '1.0.0' }],
751cb0ef41Sopenharmony_ci  })
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ci  await registry.package({
781cb0ef41Sopenharmony_ci    manifest: manifestSub,
791cb0ef41Sopenharmony_ci    tarballs: {
801cb0ef41Sopenharmony_ci      '1.0.0': path.join(npm.prefix, 'node_modules', 'test-dep-a', 'node_modules', 'test-sub'),
811cb0ef41Sopenharmony_ci    },
821cb0ef41Sopenharmony_ci  })
831cb0ef41Sopenharmony_ci  await npm.exec('dedupe', [])
841cb0ef41Sopenharmony_ci  t.match(joinedOutput(), /added 1 package, and removed 2 packages/)
851cb0ef41Sopenharmony_ci  t.ok(
861cb0ef41Sopenharmony_ci    fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')),
871cb0ef41Sopenharmony_ci    'test-sub was hoisted'
881cb0ef41Sopenharmony_ci  )
891cb0ef41Sopenharmony_ci  t.notOk(
901cb0ef41Sopenharmony_ci    fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-a', 'node_modules', 'test-sub')),
911cb0ef41Sopenharmony_ci    'test-dep-a/test-sub was removed'
921cb0ef41Sopenharmony_ci  )
931cb0ef41Sopenharmony_ci  t.notOk(
941cb0ef41Sopenharmony_ci    fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-b', 'node_modules', 'test-sub')),
951cb0ef41Sopenharmony_ci    'test-dep-b/test-sub was removed')
961cb0ef41Sopenharmony_ci})
97