const test = require('ava')
const supertest = require('supertest')
const app = require('../../../app')
const config = require('../../../config')

const server = app.listen()
const request = supertest.agent(server)

const newTag = {
  tid: 'newTag',
  list: [1001, 1004]
}
const newList = [1002, 1003]

test.before('Login', async t => {
  const login = await request
    .post('/api/session')
    .send({
      uid: 'admin',
      pwd: config.deploy.adminInitPwd
    })

  t.is(login.status, 200)
})

test('Tag list', async t => {
  const list = await request
    .get('/api/tag/list')

  t.is(list.status, 200)
  t.is(list.type, 'application/json')
  t.truthy(Array.isArray(list.body.list))
})

test.serial('create a new tag', async t => {
  const create = await request
    .post('/api/tag')
    .send(newTag)

  t.is(create.status, 200)

  Promise.all(
    newTag.list.map(async (item) => {
      const res = await request.get(`/api/problem/${item}`)
      t.true(res.body.problem.tags.includes(item))
    })
  )
})

test.serial('Update a tag', async t => {
  const update = await request
    .put(`/api/tag/${newTag.tid}`)
    .send({ list: newList })

  t.is(update.status, 200)

  const find = await request
    .get(`/api/tag/${newTag.tid}`)

  t.is(find.status, 200)
  t.deepEqual(find.body.tag.list, newList)

  Promise.all(
    newTag.list.map(async (item) => {
      const res = await request.get(`/api/problem/${item}`)
      t.false(res.body.problem.tags.includes(item))
    }),
    newList.map(async (item) => {
      const res = await request.get(`/api/problem/${item}`)
      t.true(res.body.problem.tags.includes(item))
    })
  )
})

test.serial('Delete a tag', async t => {
  const del = await request
    .delete(`/api/tag/${newTag.tid}`)

  t.is(del.status, 200)

  const find = await request
    .get(`/api/tag/${newTag.tid}`)

  t.is(find.status, 400)

  Promise.all(
    newList.map(async (item) => {
      const res = await request.get(`/api/problem/${item}`)
      t.false(res.body.problem.tags.includes(item))
    })
  )
})

test.after.always('close server', t => {
  server.close()
})

@@ -684,24 +684,528 @@
 .list))%0A%7D)%0A%0A
+test('Fails to create a tag -- tid is too short', async t =%3E %7B%0A  const create = await request%0A    .post('/api/tag')%0A    .send(%7B%0A      tid: '1',%0A      list: %5B%5D%0A    %7D)%0A%0A  t.is(create.status, 400)%0A  t.truthy(create.body.error)%0A%7D)%0A%0Atest('Fails to create a tag -- tid is too long', async t =%3E %7B%0A  const create = await request%0A    .post('/api/tag')%0A    .send(%7B%0A      tid: Array.from(%7Blength: 100%7D, (_, i) =%3E i + '').join(''),%0A      list: %5B%5D%0A    %7D)%0A%0A  t.is(create.status, 400)%0A  t.truthy(create.body.error)%0A%7D)%0A%0A
 test.serial(
@@ -1229,32 +1229,32 @@
 ', async t =%3E %7B%0A
-
   const create =
@@ -1335,24 +1335,31 @@
 us, 200)%0A%0A  
+return 
 Promise.all(
@@ -1493,36 +1493,42 @@
 m.tags.includes(
-item
+newTag.tid
 ))%0A    %7D)%0A  )%0A%7D)
@@ -1826,24 +1826,31 @@
 newList)%0A%0A  
+return 
 Promise.all(
@@ -1985,36 +1985,42 @@
 m.tags.includes(
-item
+newTag.tid
 ))%0A    %7D),%0A    n
@@ -2144,36 +2144,42 @@
 m.tags.includes(
-item
+newTag.tid
 ))%0A    %7D)%0A  )%0A%7D)
@@ -2406,16 +2406,23 @@
 400)%0A%0A  
+return 
 Promise.
@@ -2565,20 +2565,26 @@
 ncludes(
-item
+newTag.tid
 ))%0A    %7D

test on tags