CacheUpdater = require '../../src/lib/cache/updater'

cacheMock = mock [ 'set', 'keyFor' ]
backendMock = mock [ 'download' ]

checkIp = ()->
  tryExec = ->
    theUpdater = new CacheUpdater
  tryExec.should.throw();

  tryExec = ->
    theUpdater = new CacheUpdater "cache"
  tryExec.should.throw();

  tryExec = ->
    theUpdater = new CacheUpdater "cache", "backend"
  tryExec.should.not.throw();

addUpdateCache = (done)->
  theUpdater = new CacheUpdater cacheMock, backendMock

  backendMock.modify 'download',
    takes: ['/', ->]
    calls: 1
    with: ["resource"]

  cacheMock.modify "keyFor", takes: ['/'], returns: "somekey"
  cacheMock.modify "set", takes: ['somekey','resource']

  theUpdater.update '/', ->
    backendMock.isCalled('download').should.be.ok
    cacheMock.isCalled('keyFor').should.be.ok
    cacheMock.isCalled('set').should.be.ok
    done();

describe 'Cache Updater', ->
  it 'should check for cache and backend', checkIp
  it 'should add/update the cache for given resource', addUpdateCache