<commit_before>import Storage, { canUseStorage, buildCustomStorage, buildCustomStoragesMap } from '../src/Storage'

const expect = require('chai').expect
let __global__ = {}

describe('Storage', function() {
  let newStorage = buildCustomStorage('fileSystem', (p, v)=>{__global__[p]=v}, (p)=>{return __global__[p]}, (p)=>{__global__[p] = undefined})
  let customStoragesMap = buildCustomStoragesMap('fileSystem', newStorage)

  describe('#buildCustomStorage()', function() {
    it('should build the custom storage', function() {
        expect(newStorage).to.have.property('fileSystem')
        expect(newStorage.fileSystem).to.have.property('setItem')
        expect(newStorage.fileSystem.setItem).to.be.a('function')
        expect(newStorage.fileSystem).to.have.property('getItem')
        expect(newStorage.fileSystem.getItem).to.be.a('function')
        expect(newStorage.fileSystem).to.have.property('removeItem')
        expect(newStorage.fileSystem.setItem).to.be.a('function')
    });
  });

  describe('#buildCustomStoragesMap()', function() {
    it('should build the custom storage map', function() {
        expect(customStoragesMap).to.have.property('fileSystem')
        expect(customStoragesMap).to.have.property('cookie')
        expect(customStoragesMap.fileSystem.setItem).to.be.a('function')
    });
  });

  describe('#StorageInstance', function(){
    let storage = new Storage('fileSystem', undefined, customStoragesMap)
    it('should not check custom storage', function(){
      let _testStorage = buildCustomStorage('disabledStorage', (p, v)=>{throw new Error('can not set')}, (p)=>{throw new Error('can not get')}, (p)=>{throw new Error('can not remove')})
      let _testCustomStoragesMap = buildCustomStoragesMap('disabledStorage', newStorage)
      expect(canUseStorage('disabledStorage', undefined, _testCustomStoragesMap)).to.be.equal(false)
    })

    it('should check custom storage', function(){
      expect(canUseStorage('fileSystem', undefined, customStoragesMap)).to.be.equal(true)
    })

    it('should not check standard local storage disabled', function(){
      let _mockedLocalStorage = {
        setItem: () => {throw new Error('disabled')},
        getItem: () => {throw new Error('disabled')},
        removeItem: () => {throw new Error('disabled')}
      }
      expect(canUseStorage('fileSystem', _mockedLocalStorage)).to.be.equal(false)
    })

    it('should use fallback with standard local storage disabled', function(){
      let _mockedLocalStorage = {
        setItem: () => {throw new Error('disabled')},
        getItem: () => {throw new Error('disabled')},
        removeItem: () => {throw new Error('disabled')}
      }
      let _storageDisabled = new Storage('STORAGE', _mockedLocalStorage)
      _storageDisabled.setItem('a', 1)
      expect(_storageDisabled.getItem('a')).to.be.a('number')
    })

    it('should get the types map', function(){
      expect(Storage.getTypesMap()).to.have.all.keys('STORAGE', 'COOKIE')
    })

    it('should add custom storage to Storage instance', function(){
      expect(storage.STORAGES_MAP).to.have.property('fileSystem')
    })

    it('should set and get a value', function(){
      storage.setItem('a', 1)
      expect(storage.getItem('a')).to.be.a('number')
      storage.setItem('b', 'may be the good one')
      expect(storage.getItem('b')).to.be.equal('may be the good one')
    })

    it('should remove a value', function(){
      storage.setItem('a', 1)
      expect(storage.getItem('a')).to.be.a('number')
      storage.removeItem('b')
      expect(storage.getItem('b')).to.be.equal(undefined)
    })<commit_msg>test grantedOptions and callbackOnDisabled for fallback storage<commit_after>import Storage, { canUseStorage, buildCustomStorage, buildCustomStoragesMap } from '../src/Storage'

const expect = require('chai').expect
const assert = require('chai').assert
let __global__ = {}

describe('Storage', function() {
  let newStorage = buildCustomStorage('fileSystem', (p, v)=>{__global__[p]=v}, (p)=>{return __global__[p]}, (p)=>{__global__[p] = undefined})
  let customStoragesMap = buildCustomStoragesMap('fileSystem', newStorage)

  describe('#buildCustomStorage()', function() {
    it('should build the custom storage', function() {
        expect(newStorage).to.have.property('fileSystem')
        expect(newStorage.fileSystem).to.have.property('setItem')
        expect(newStorage.fileSystem.setItem).to.be.a('function')
        expect(newStorage.fileSystem).to.have.property('getItem')
        expect(newStorage.fileSystem.getItem).to.be.a('function')
        expect(newStorage.fileSystem).to.have.property('removeItem')
        expect(newStorage.fileSystem.setItem).to.be.a('function')
    });
  });

  describe('#buildCustomStoragesMap()', function() {
    it('should build the custom storage map', function() {
        expect(customStoragesMap).to.have.property('fileSystem')
        expect(customStoragesMap).to.have.property('cookie')
        expect(customStoragesMap.fileSystem.setItem).to.be.a('function')
    });
  });

  describe('#StorageInstance', function(){
    let storage = new Storage('fileSystem', undefined, customStoragesMap)
    it('should not check custom storage', function(){
      let _testStorage = buildCustomStorage('disabledStorage', (p, v)=>{throw new Error('can not set')}, (p)=>{throw new Error('can not get')}, (p)=>{throw new Error('can not remove')})
      let _testCustomStoragesMap = buildCustomStoragesMap('disabledStorage', newStorage)
      expect(canUseStorage('disabledStorage', undefined, _testCustomStoragesMap)).to.be.equal(false)
    })

    it('should check custom storage', function(){
      expect(canUseStorage('fileSystem', undefined, customStoragesMap)).to.be.equal(true)
    })

    it('should not check standard local storage disabled', function(){
      let _mockedLocalStorage = {
        setItem: () => {throw new Error('disabled')},
        getItem: () => {throw new Error('disabled')},
        removeItem: () => {throw new Error('disabled')}
      }
      expect(canUseStorage('fileSystem', _mockedLocalStorage)).to.be.equal(false)
    })

    it('should not use fallback with standard local storage disabled,no fallbackStorage options and no callbackOnDisabled', function(){
      let _mockedLocalStorage = {
        setItem: () => {throw new Error('disabled')},
        getItem: () => {throw new Error('disabled')},
        removeItem: () => {throw new Error('disabled')}
      }
      let _storageDisabled = new Storage('STORAGE', _mockedLocalStorage)
      try {
          _storageDisabled.setItem('a', 1)
      } catch(err){
        assert.ok(true);
      }
    })

    it('should use callbackOnDisabled configured on standard local storage disabled', function(){
      let _mockedLocalStorage = {
        setItem: () => {throw new Error('disabled')},
        getItem: () => {throw new Error('disabled')},
        removeItem: () => {throw new Error('disabled')}
      }
      let _storageDisabled = new Storage('STORAGE', _mockedLocalStorage, undefined, {'callbackOnDisabled': () => {__global__['callbackOnDisabled'] = true}})
      try {
          _storageDisabled.setItem('a', 1)
      } catch(err){
        expect(__global__['callbackOnDisabled']).to.be.equal(true)
        delete __global__['callbackOnDisabled']
      }
    })

    it('should use fallback with standard local storage disabled and fallbackStorage option grantedProps', function(){
      let _mockedLocalStorage = {
        setItem: () => {throw new Error('disabled')},
        getItem: () => {throw new Error('disabled')},
        removeItem: () => {throw new Error('disabled')}
      }
      let _storageDisabled = new Storage('STORAGE', _mockedLocalStorage, undefined, {'grantedProps': ['a']})
      _storageDisabled.setItem('a', 1)
      expect(_storageDisabled.getItem('a')).to.be.a('number')
    })

    it('should get the types map', function(){
      expect(Storage.getTypesMap()).to.have.all.keys('STORAGE', 'COOKIE')
    })

    it('should add custom storage to Storage instance', function(){
      expect(storage.STORAGES_MAP).to.have.property('fileSystem')
    })

    it('should set and get a value', function(){
      storage.setItem('a', 1)
      expect(storage.getItem('a')).to.be.a('number')
      storage.setItem('b', 'may be the good one')
      expect(storage.getItem('b')).to.be.equal('may be the good one')
    })

    it('should set and get a value for localStorage', function(){
      let _mockedLocalStorage = {
        setItem: (p, v) => {__global__[p] = v},
        getItem: (p) => {return __global__[p]},
        removeItem: (p) => {}
      }
      let _storageDisabled = new Storage('STORAGE', _mockedLocalStorage, undefined)
      storage.setItem('a', 1)
      expect(storage.getItem('a')).to.be.a('number')
    })

    it('should remove a value', function(){
      storage.setItem('a', 1)
      expect(storage.getItem('a')).to.be.a('number')
      storage.removeItem('b')
      expect(storage.getItem('b')).to.be.equal(undefined)
    })
{'fraction_non_alphanumeric': 0.15981836305238675, 'fraction_numerical': 0.0009967881271458633, 'mean_word_length': 3.8916576381365116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '570625', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2616, 'n_words': 536}