const nock = require('nock')

const dbs = require('../../lib/dbs')
const removeIfExists = require('../helpers/remove-if-exists')
const { cleanCache } = require('../helpers/module-cache-helpers')

nock.disableNetConnect()
nock.enableNetConnect('localhost')

describe('create initial branch', () => {
  beforeEach(() => {
    delete process.env.IS_ENTERPRISE
    delete process.env.BADGES_HOST
    cleanCache('../../lib/env')
    jest.resetModules()
  })

  beforeAll(async () => {
    const { installations, payments } = await dbs()

    await installations.put({
      _id: '123',
      installation: 137,
      plan: 'free'
    })

    await payments.put({
      _id: '123',
      stripeSubscriptionId: 'stripe123',
      plan: 'personal'
    })
  })

  afterAll(async () => {
    const { installations, repositories, payments } = await dbs()
    await Promise.all([
      removeIfExists(installations, '123'),
      removeIfExists(payments, '123'),
      removeIfExists(repositories, '42', '43', '44', '45', '46', '47', '48', '49', '42:branch:1234abcd', '47:branch:1234abcd', '48:branch:1234abcd', '49:branch:1234abcd')
    ])
  })

  test('create pull request for monorepo with one non-root package.json', async () => {
    const ignore = ['eslint', 'lodash']
    const packagePaths = ['package.json', 'frontend/package.json']
    const packageJsonContents = [{ devDependencies: {'@finnpauls/dep': '1.0.0'} }]
    const registryGet = () => {}
    // const log = {
    //   info: () => {}
    // }
    const log = console

    const { repositories } = await dbs()
    await repositories.put({
      _id: '49',
      accountId: '123',
      fullName: 'finnp/test'
    })

    // jest.mock('path', () => {
    //   const path = require.requireActual('path')
    //   path.jobPath = (job) => {
    //     throw new Error('not implemented')
    //   }
    //   return path
    // })
    // const path = require('path')

    // mock relative dependencies
    jest.mock('../../utils/initial-branch-utils', () => ({ignore, log, packagePaths, packageJsonContents, registryGet}) => {
      // const utils = require.requireActual('../../utils/initial-branch-utils')
      const utils = {}
      utils.getUpdatedDependenciesForFiles = require.requireActual('../../utils/initial-branch-utils').getUpdatedDependenciesForFiles
      console.log('### utils', utils)
      utils.addNPMPackageData = async(dependencyInfo, registryGet, log) => {
        return [{
          name: '@finnpauls/dep',
          version: '1.0.0',
          type: 'devDependencies',
          data: {
            'dist-tags': {
              latest: '3.0.0-rc1'
            },
            versions: {
              '2.0.0-rc1': true,
              '2.0.0-rc2': true,
              '2.0.0': true,
              '3.0.0-rc1': true,
              '1.0.0': true
            }
          }
        }]
      }
      utils.getDependenciesFromPackageFiles = (packagePaths, packageJsonContents) => {
        return [{
          name: '@finnpauls/dep',
          version: '1.0.0',
          type: 'devDependencies'
        }]
      }
      return utils
    })
    const initialBranchUtils = require('../../utils/initial-branch-utils')
    console.log('### initialBranchUtils', initialBranchUtils)
    const updatedDependencies = initialBranchUtils.getUpdatedDependenciesForFiles({ignore, log, packagePaths, packageJsonContents, registryGet})
    console.log('### updatedDependencies', updatedDependencies)
  })
})

@@ -1670,253 +1670,8 @@
 %7D)%0A%0A
-    // jest.mock('path', () =%3E %7B%0A    //   const path = require.requireActual('path')%0A    //   path.jobPath = (job) =%3E %7B%0A    //     throw new Error('not implemented')%0A    //   %7D%0A    //   return path%0A    // %7D)%0A    // const path = require('path')%0A%0A
     
@@ -1759,83 +1759,16 @@
 ) =%3E
- (%7Bignore, log, packagePaths, packageJsonContents, registryGet%7D) =%3E
  %7B%0A     
  // 
@@ -1763,19 +1763,16 @@
  %7B%0A     
- //
  const u
@@ -1840,203 +1840,8 @@
 s')%0A
-      const utils = %7B%7D%0A      utils.getUpdatedDependenciesForFiles = require.requireActual('../../utils/initial-branch-utils').getUpdatedDependenciesForFiles%0A      console.log('### utils', utils)%0A
     
@@ -2623,32 +2623,44 @@
 hUtils = require
+.requireMock
 ('../../utils/in
@@ -2684,70 +2684,8 @@
 s')%0A
-    console.log('### initialBranchUtils', initialBranchUtils)%0A
     
@@ -2711,16 +2711,22 @@
 encies =
+ await
  initial
@@ -2839,25 +2839,15 @@
     
-console.log('### 
+expect(
 upda
@@ -2865,30 +2865,39 @@
 cies
-', updatedDependencies
+%5B0%5D.newVersion).toEqual('2.0.0'
 )%0A  

fix mocks