import { filterLockfileByImportersAndEngine } from '@pnpm/filter-lockfile'
import test = require('tape')

test('filterByImportersAndEngine(): skip packages that are not installable', (t) => {
  const skippedPackages = new Set<string>(['/preserve-existing-skipped/1.0.0'])
  const filteredLockfile = filterLockfileByImportersAndEngine(
    {
      importers: {
        'project-1': {
          dependencies: {
            'prod-dep': '1.0.0',
          },
          devDependencies: {
            'dev-dep': '1.0.0',
          },
          optionalDependencies: {
            'not-skipped-optional': '1.0.0',
            'optional-dep': '1.0.0',
          },
          specifiers: {
            'dev-dep': '^1.0.0',
            'not-skipped-optional': '^1.0.0',
            'optional-dep': '^1.0.0',
            'prod-dep': '^1.0.0',
          },
        },
        'project-2': {
          dependencies: {
            'project-2-prod-dep': '1.0.0',
          },
          specifiers: {
            'project-2-prod-dep': '^1.0.0',
          }
        }
      },
      lockfileVersion: 5.1,
      packages: {
        '/bar/1.0.0': {
          resolution: { integrity: '' },
        },
        '/dev-dep/1.0.0': {
          dev: true,
          resolution: { integrity: '' },
        },
        '/foo/1.0.0': {
          optional: true,
          resolution: { integrity: '' },
        },
        '/not-skipped-optional/1.0.0': {
          optional: true,
          resolution: { integrity: '' },
        },
        '/optional-dep/1.0.0': {
          dependencies: {
            'bar': '1.0.0',
            'foo': '1.0.0',
          },
          engines: {
            node: '1000',
          },
          optional: true,
          resolution: { integrity: '' },
        },
        '/prod-dep-dep/1.0.0': {
          resolution: { integrity: '' },
        },
        '/prod-dep/1.0.0': {
          dependencies: {
            'bar': '1.0.0',
            'prod-dep-dep': '1.0.0',
          },
          optionalDependencies: {
            'optional-dep': '1.0.0',
          },
          resolution: { integrity: '' },
        },
        '/project-2-prod-dep/1.0.0': {
          resolution: { integrity: '' },
        },
      },
    },
    ['project-1'],
    {
      currentEngine: {
        nodeVersion: '10.0.0',
        pnpmVersion: '2.0.0',
      },
      engineStrict: true,
      failOnMissingDependencies: true,
      include: {
        dependencies: true,
        devDependencies: true,
        optionalDependencies: true,
      },
      prefix: process.cwd(),
      registries: {
        default: 'https://registry.npmjs.org/',
      },
      skipped: skippedPackages,
    },
  )

  t.deepEqual(filteredLockfile, {
    importers: {
      'project-1': {
        dependencies: {
          'prod-dep': '1.0.0',
        },
        devDependencies: {
          'dev-dep': '1.0.0',
        },
        optionalDependencies: {
          'not-skipped-optional': '1.0.0',
        },
        specifiers: {
          'dev-dep': '^1.0.0',
          'not-skipped-optional': '^1.0.0',
          'optional-dep': '^1.0.0',
          'prod-dep': '^1.0.0',
        },
      },
      'project-2': {
        dependencies: {
          'project-2-prod-dep': '1.0.0',
        },
        specifiers: {
          'project-2-prod-dep': '^1.0.0',
        }
      }
    },
    lockfileVersion: 5.1,
    packages: {
      '/bar/1.0.0': {
        resolution: { integrity: '' },
      },
      '/dev-dep/1.0.0': {
        dev: true,
        resolution: { integrity: '' },
      },
      '/not-skipped-optional/1.0.0': {
        optional: true,
        resolution: { integrity: '' },
      },
      '/prod-dep-dep/1.0.0': {
        resolution: { integrity: '' },
      },
      '/prod-dep/1.0.0': {
        dependencies: {
          'bar': '1.0.0',
          'prod-dep-dep': '1.0.0',
        },
        optionalDependencies: {
          'optional-dep': '1.0.0',
        },
        resolution: { integrity: '' },
      },
    },
  })
  t.deepEqual(Array.from(skippedPackages), ['/preserve-existing-skipped/1.0.0', '/optional-dep/1.0.0', '/foo/1.0.0'])
  t.end()
})
