mkry = require 'mockery';
require('../../src/util/log').logtoConsole = false;
siteCacheMock = mockClass ['init', 'has', 'get', 'set']
siteBackendMock = mockClass []
dispatcherMock = mockClass ['init']
vhMock = mock ['vhost']

enableMockery = ()->
  mkry.enable warnOnUnregistered: false, warnOnReplace: false
  mkry.registerMock '../lib/cache/sitecache', siteCacheMock.constructorFn
  mkry.registerMock '../lib/backend', siteBackendMock.constructorFn
  mkry.registerMock '../lib/dispatcher', dispatcherMock.constructorFn
  mkry.registerMock 'vhost', -> vhMock.vhost.apply vhMock, arguments
  
disableMockery = ()->
  mkry.deregisterAll();
  mkry.disable();
  siteCacheMock.reset();
  siteBackendMock.reset();
  dispatcherMock.reset();

siteConfig = ()->
  noDleaySite = require '../../src/models/site'

  theSite = new noDleaySite "www.site.com", backend: "origin.site.com"

  theSite.domain.should.eql "www.site.com"
  theSite.config.backend.should.eql "origin.site.com"

noDomain = ()->
  noDleaySite = require '../../src/models/site'
  tryExec = ->
    theSite = new noDleaySite {}
  tryExec.should.throw();
    
noBackend = ()->
  noDleaySite = require '../../src/models/site'
  tryExec = ->
    theSite = new noDleaySite domain: "www.site.com"
  tryExec.should.throw();

initSiteCache = (done)->
  siteCacheMock.modify 'init', calls: 0
  siteCacheMock.modify 'constructor', takes: ["www.site.com", "backend"]

  noDleaySite = require '../../src/models/site'
  theSite = new noDleaySite "www.site.com", 
    backend: "origin.site.com"

  theSite.backend = 'backend'

  theSite.initCache ->
    siteCacheMock.isCalled('init').should.be.ok
    done()

initSiteBackend = (done)->
  siteBackendMock.modify 'constructor',
    takes: (args)->
      args[0].config.backend.should.eql 'origin.site.com';
      return true;

  noDleaySite = require '../../src/models/site'
  theSite = new noDleaySite "www.site.com", 
    backend: "origin.site.com"

  theSite.initBackend()
  siteBackendMock.isInstantiated().should.be.ok
  done()

initSiteDispatcher = ()->
  dispatcherMock.modify 'constructor', 
    takes: (args)->
      args[0].domain.should.eql 'www.site.com'

  noDleaySite = require '../../src/models/site'
  theSite = new noDleaySite "www.site.com", 
    backend: "origin.site.com"

  theSite.cache = 'cache'
  theSite.backend = 'backend'

  theSite.initDispatcher();
  dispatcherMock.isInstantiated().should.be.ok
  dispatcherMock.isCalled('init').should.be.ok

initOrder = (done)->
  siteBackendMock.modify 'constructor', takes: undefined
  dispatcherMock.modify 'constructor', takes: undefined

  noDleaySite = require '../../src/models/site'
  theSite = new noDleaySite "www.site.com", 
    backend: "origin.site.com"

  cacheWOBackend = ->
    theSite.initCache()

  dispatcherWOCache = ->
    theSite.initDispatcher()

  dispatcherWOBackend = ->
    theSite.cache = 'cache' # dummt to test
    theSite.initDispatcher()

  cacheWOBackend.should.throw();
  dispatcherWOCache.should.throw();
  dispatcherWOBackend.should.throw();

  siteCacheMock.reset();
  dispatcherMock.reset();
  siteBackendMock.reset();

  theSite = new noDleaySite "www.site.com", 
    backend: "origin.site.com"

  siteCacheMock.modify 'constructor', takes: undefined
  theSite.init ->
    siteCacheMock.isInstantiated.should.be.ok
    siteCacheMock.isCalled('init').should.be.ok

    siteBackendMock.isInstantiated().should.be.ok

    dispatcherMock.isInstantiated().should.be.ok
    dispatcherMock.isCalled('init').should.be.ok

    done();

createVhost = ()->
  vhMock.modify 'vhost', takes:[ "www.site.com", "app" ]

  noDleaySite = require '../../src/models/site'
  theSite = new noDleaySite "www.site.com", 
    backend: "origin.site.com"

  tryExec = -> 
    theSite.vhost();
  #no Dispatcher
  tryExec.should.throw();

  #dummy dispatcher
  theSite.dispatcher = mock ['app']
  theSite.dispatcher.modify 'app', returns: 'app'
  theSite.vhost();

  vhMock.isCalled('vhost').should.be.ok
isCacheable = ()->
  noDleaySite = require '../../src/models/site'
  theSite = new noDleaySite "www.site.com", 
    backend: "origin.site.com"

  theSite.canCache('/asdf').should.be.ok
  theSite.canCache(method: 'a').should.not.be.ok
  theSite.canCache(method: 'get', url: '/asdf').should.be.ok
  theSite.config.blacklist = ['/abcd']
  theSite.canCache('/abcd').should.not.be.ok

describe 'noDelay Site', ->
  beforeEach enableMockery
  afterEach disableMockery
  it 'should hold all the configuration for site', siteConfig
  it 'should throw if the domain is not specified', noDomain
  it 'should throw if the backend is not specified', noBackend
  it 'should initialize site cache', initSiteCache
  it 'should initialize site backend', initSiteBackend
  it 'should initialize the dispatcher', initSiteDispatcher
  it 'should initialize cache, backend, and disatcher in right order', initOrder
  it 'should create the vhost', createVhost
  it 'should inform weather a resource is cachable', isCacheable
