mkry = require 'mockery'
pmMock = mockClass ['getItems', 'updateItem']
cacheMock = mockClass ['init', 'has', 'set', 'get']
updaterMock = mockClass ['update']

enableMockery = ()->
  mkry.enable warnOnUnregistered: false, warnOnReplace: false, useCleanCache: true
  mkry.registerMock './fs_persist', pmMock.constructorFn
  mkry.registerMock './basic', cacheMock.constructorFn
  mkry.registerMock './updater', updaterMock.constructorFn
  Log = require '../../src/util/log'
  Log.logtoConsole = false;
  
disableMockery = ()->
  mkry.deregisterAll();
  mkry.disable();

checkDomainAndBackend = ()->
  SiteCache = require '../../src/lib/cache/sitecache'

  tryExec = ->
    siteCache = new SiteCache
  tryExec.should.throw();

  tryExec = ->
    siteCache = new SiteCache "www.site.com"
  tryExec.should.throw();

  tryExec = ->
    siteCache = new SiteCache "www.site.com", "backend"
  tryExec.should.not.throw();

generateUniqueKey = ()->
  SiteCache = require '../../src/lib/cache/sitecache'
  siteCache1 = new SiteCache("www.site.com", "backend");
  siteCache2 = new SiteCache("www.site.com", "backend");
  siteCache3 = new SiteCache("www.site1.com", "backend");

  siteCache1.cacheKey.should.eql(siteCache2.cacheKey);
  siteCache1.cacheKey.should.not.eql(siteCache3.cacheKey);

initPersistantMedium = (done)->
  SiteCache = require '../../src/lib/cache/sitecache'

  pmMock.modify 'constructor', 
    takes: ["www.site.com"]

  cacheMock.modify 'init', calls: 0

  siteCache = new SiteCache("www.site.com", "backend");
  siteCache.init ->
    pmMock.isInstantiated().should.be.ok
    cacheMock.isCalled('init').should.be.ok
    done()

initCacheObj = (done)->
  SiteCache = require '../../src/lib/cache/sitecache'
  cacheMock.modify 'init', calls: 0

  cacheMock.modify 'constructor', 
    takes: [pmMock.mock]

  siteCache = new SiteCache("www.site.com", "backend");
  siteCache.init ->
    pmMock.isInstantiated().should.be.ok
    cacheMock.isInstantiated().should.be.ok
    cacheMock.isCalled('init').should.be.ok
    done()

initUpdater = ()->
  SiteCache = require '../../src/lib/cache/sitecache'

  siteCache = new SiteCache "www.site.com", "backend"

  updaterMock.modify 'constructor',
    takes: [{}, "backend"]

  siteCache.initUpdater();

  updaterMock.isInstantiated().should.be.ok

proxyCache = (done)->
  SiteCache = require '../../src/lib/cache/sitecache'
  siteCache = new SiteCache("www.site.com", "backend");

  cacheMock.modify 'get',
    takes: ['some_key']
  
  cacheMock.modify 'has',
    takes: ['some_key']
  
  cacheMock.modify 'set',
    takes: ['some_key', 'some_val']


  siteCache.init ->
    siteCache.get("some_key")
    cacheMock.isCalled('get').should.be.ok
    siteCache.has("some_key")
    cacheMock.isCalled('has').should.be.ok
    siteCache.set("some_key","some_val")
    cacheMock.isCalled('set').should.be.ok
    done();

proxyUpdater = ()->
  SiteCache = require '../../src/lib/cache/sitecache'
  siteCache = new SiteCache "www.site.com", "backend"

  updaterMock.modify 'update',
    takes: [ "/some-resource" ]

  siteCache.initUpdater()
  siteCache.update "/some-resource"
  updaterMock.isCalled('update').should.be.ok

createKey = ()->
  SiteCache = require '../../src/lib/cache/sitecache'
  siteCache = new SiteCache("www.site.com", "backend");

  siteCache.keyFor("/some-url").should.eql(
    siteCache.keyFor("/some-url"))

  siteCache.keyFor("/some-url").should.not.eql(
    siteCache.keyFor("/some-url1"))

  /\W/.test(siteCache.keyFor("/some-url")).should.not.be.ok

describe 'Site Cache', ->
  beforeEach enableMockery
  afterEach disableMockery

  it 'should check for domain and backend', checkDomainAndBackend
  it 'generate unique key for given domain', generateUniqueKey
  it 'should initialize cache from persistance medium', initPersistantMedium
  it 'should initialize cache object', initCacheObj
  it 'should initialize cache updater', initUpdater
  it 'should pass has, get and set to cache object', proxyCache
  it 'should pass update to updater object', proxyUpdater
  it 'should create a unique key for given url', createKey