const sops = require('.')
const path = require('path')
const allowedExtensions = ['yaml', 'yml', 'json']
describe('Decrypt', () => {
  beforeAll(() => {
    process.cwd = jest.fn(() => `${path.resolve('./src/test')}`)
  })

  afterAll(() => {
    jest.restoreAllMocks()
  })
  describe('checkFileFormat', () => {
    describe('when file format is invalid', () => {
      it('should throw an error', () => {
        expect(() => sops.decrypt('config/something.txt')).toThrow(
          'File not supported (txt). Only yaml and json are supported'
        )
      })
    })
    describe('when file format is valid', () => {
      allowedExtensions.forEach(extension =>
        it(`should not throw an error [${extension}]`, () => {
          expect(() =>
            sops.decrypt(`config/something.${extension}`)
          ).not.toThrow('File not supported. Only yaml and json are supported')
        })
      )
    })
    describe('when file does not exist', () => {
      it('should throw an error', () => {
        expect(() => sops.decrypt('config/something.yml')).toThrow()
      })
    })
  })
  describe('checkDecryption', () => {
    describe('when file is invalid', () => {
      it('should fail when the file is already decrypted', () => {
        expect(() =>
          sops.decrypt(path.resolve(`src/test/config/decrypted.yml`))
        ).toThrow()
      })
    })
  })
})
