require('should')
const core = require('jsreport-core')
const debug = require('../')
const express = require('jsreport-express')

describe('debug', () => {
  let reporter

  function init (options) {
    reporter = core(Object.assign({ templatingEngines: { strategy: 'in-process' } }, options))
    reporter.use(express())
    reporter.use(debug())

    return reporter.init()
  }

  afterEach(() => {
    return reporter.close()
  })

  it('should add logs to header if options.debug.logsToResponseHeader', async () => {
    await init()

    reporter.beforeRenderListeners.add('test', (req) => reporter.logger.info('test', req))

    const response = await reporter.render({
      template: {content: 'foo', engine: 'none', recipe: 'html'},
      options: {debug: {logsToResponseHeader: true}}
    })

    JSON.parse(response.meta.headers['Debug-Logs']).filter((m) => m.message === 'test').should.have.length(1)
  })

  it('should put logs to response if logsToResponse', async () => {
    await init()

    reporter.beforeRenderListeners.add('test', (req) => reporter.logger.info('test', req))

    const response = await reporter.render({
      template: {content: 'foo', engine: 'none', recipe: 'html'},
      options: {debug: {logsToResponse: true}}
    })

    response.content.toString().should.containEql('test')
  })

  it('should cut response header for options.debug.logsToResponseHeader using configuration.debug.maxLogResponseHeaderSize', async () => {
    await init({
      extensions: {
        debug: {
          maxLogResponseHeaderSize: 150
        }
      }
    })

    const response = await reporter.render({
      template: {content: 'foo', engine: 'none', recipe: 'html'},
      options: {debug: {logsToResponseHeader: true}}
    })

    const logs = JSON.parse(response.meta.headers['Debug-Logs'])
    logs.should.have.length(2)
    logs[1].message.should.containEql('cut')
  })
})

@@ -705,32 +705,33 @@
      template: %7B
+ 
 content: 'foo', 
@@ -752,32 +752,33 @@
 , recipe: 'html'
+ 
 %7D,%0D%0A      option
@@ -777,32 +777,33 @@
   options: %7B
+ 
 debug: %7B
 logsToRespon
@@ -782,32 +782,33 @@
 ions: %7B debug: %7B
+ 
 logsToResponseHe
@@ -809,33 +809,35 @@
 onseHeader: true
-%7D
+ %7D 
 %7D%0D%0A    %7D)%0D%0A%0D%0A   
@@ -1190,32 +1190,33 @@
      template: %7B
+ 
 content: 'foo', 
@@ -1237,32 +1237,33 @@
 , recipe: 'html'
+ 
 %7D,%0D%0A      option
@@ -1262,32 +1262,33 @@
   options: %7B
+ 
 debug: %7B
 logsToRespon
@@ -1267,32 +1267,33 @@
 ions: %7B debug: %7B
+ 
 logsToResponse: 
@@ -1292,25 +1292,27 @@
 sponse: true
-%7D
+ %7D 
 %7D%0D%0A    %7D)%0D%0A%0D
@@ -1710,16 +1710,17 @@
 plate: %7B
+ 
 content:
@@ -1757,16 +1757,17 @@
 : 'html'
+ 
 %7D,%0D%0A    
@@ -1778,24 +1778,25 @@
 tions: %7B
+ 
 debug: %7B
 logsToRe
@@ -1787,16 +1787,17 @@
 debug: %7B
+ 
 logsToRe
@@ -1814,17 +1814,19 @@
 er: true
-%7D
+ %7D 
 %7D%0D%0A    %7D

fix lint