import should from 'should'
import createMiddlewares from '../src/createMiddlewares'

describe('createMiddlewares', function() {
  it('sync return', () => {
    const middlewares = createMiddlewares()

    middlewares.use((ctx) => {
      ctx.body = ctx.inputs.number + 1
      return ctx
    })

    const ctx = {inputs: {number: 1}}
    const result = middlewares(ctx).then(ctx => ctx.body)
    should(result.isFulfilled).be.true()
    should(result.value).equal(2)
  })

  it('async return', async () => {
    const middlewares = createMiddlewares()

    middlewares.use(async (ctx) => {
      ctx.body = await new Promise(resolve => {
        setTimeout(() => {
          resolve('Http Body')
        }, 10)
      })
    })

    const result = await middlewares().then(ctx => ctx.body)
    should(!!result.isFulfilled).be.false()
    should(result).equal('Http Body')
  })

  it('path regexp', () => {
    const middlewares = createMiddlewares()

    middlewares.use('lesson', (ctx) => {
      ctx.body = ctx.inputs.number + 1
      return ctx
    })

    const ctx = {
      url: 'lesson?courseId=7bLXN46m&branchId=bD0n20Wn',
      inputs: {number: 1},
    }
    const result = middlewares(ctx).then(ctx => ctx.body)
    should(result.value).equal(2)

    should(middlewares({
      url: 'lesson/bD0n20Wn',
    }).value.body).null()
  })
})

@@ -890,16 +890,22 @@
 regexp',
+ async
  () =%3E %7B
@@ -984,295 +984,322 @@
 n', 
-(ctx) =%3E %7B%0A      ctx.body = ctx.inputs.number + 1%0A      return ctx%0A    %7D)%0A%0A    const ctx = %7B%0A      url: 'lesson?courseId=7bLXN46m&branchId=bD0n20Wn',%0A      inputs: %7Bnumber: 1%7D,%0A    %7D%0A    const result = middlewares(ctx).then(ctx =%3E ctx.body)%0A    should(result.value).equal(2)%0A%0A    should(
+async (ctx, next) =%3E %7B%0A      await next()%0A      ctx.body = ctx.body + ' World'%0A    %7D)%0A    middlewares.use(ctx =%3E %7B%0A      ctx.body = 'Hello'%0A    %7D)%0A%0A    should((%0A      await middlewares(%7B%0A        url: 'lesson?courseId=7bLXN46m&branchId=bD0n20Wn',%0A      %7D)%0A    ).body).equal('Hello World')%0A%0A    should((%0A      await 
 midd
@@ -1304,24 +1304,26 @@
 ddlewares(%7B%0A
+  
       url: '
@@ -1348,28 +1348,38 @@
     
-%7D).value.body).null(
+  %7D)%0A    ).body).equal('Hello'
 )%0A  

add test