import _ from 'lodash'
import { stripIndent } from 'common-tags'
import stripAnsi from 'strip-ansi'
import { getRunner } from '../../../lib/util/chrome_policy_check'

import '../../spec_helper'

describe('lib/util/chrome_policy_check', () => {
  describe('.getRunner returns a function', () => {
    it('calls callback with an error if policies are found', () => {
      const run = getRunner({
        enumerateValues (hkey, key) {
        // mock a registry with a couple of policies
          return _.get({
            'HKEY_LOCAL_MACHINE': {
              'Software\\Policies\\Google\\Chrome': [
                { name: 'ProxyServer' },
              ],
            },
            'HKEY_CURRENT_USER': {
              'Software\\Policies\\Google\\Chromium': [
                { name: 'ExtensionSettings' },
              ],
            },
          }, `${hkey}.${key}`, [])
        },
      })

      const cb = sinon.stub()

      run(cb)

      expect(cb).to.be.calledOnce

      expect(stripAnsi(cb.getCall(0).args[0].message)).to.eq(stripIndent(`\
Cypress detected policy settings on your computer that may cause issues.

The following policies were detected that may prevent Cypress from automating Chrome:

 - HKEY_LOCAL_MACHINE\\Software\\Policies\\Google\\Chrome\\ProxyServer
 - HKEY_CURRENT_USER\\Software\\Policies\\Google\\Chromium\\ExtensionSettings

For more information, see https://on.cypress.io/bad-browser-policy\
`))
    })

    it('does not call callback if no policies are found', () => {
      const run = getRunner({
        enumerateValues: _.constant([]),
      })

      const cb = sinon.stub()

      run(cb)

      expect(cb).to.not.be.called
    })

    it('fails silently if enumerateValues throws', () => {
      const run = getRunner({
        enumerateValues () {
          throw new Error('blah')
        },
      })

      const cb = sinon.stub()

      run(cb)

      expect(cb).to.not.be.called
    })
  })
})
