import Joi from 'joi'
import { createServiceTester } from '../tester.js'
import {
  twoVulnerabilitiesSvg,
  zeroVulnerabilitiesSvg,
} from './snyk-test-helpers.js'
export const t = await createServiceTester()

t.create('valid repo').get('/snyk/snyk.json').timeout(20000).expectBadge({
  label: 'vulnerabilities',
  message: Joi.number().required(),
})

t.create('non existent repo')
  .get('/badges/not-real.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'repo or manifest not found',
  })

t.create('valid target manifest path')
  .get('/snyk/snyk-resolve-deps-fixtures/node_modules/debug/package.json.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('invalid target manifest path')
  .get('/badges/shields/badge-maker/requirements.txt.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'repo or manifest not found',
  })

t.create('repo has no vulnerabilities')
  .get('/badges/shields.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .reply(200, zeroVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '0',
    color: 'brightgreen',
  })

t.create('repo has vulnerabilities')
  .get('/badges/shields.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .reply(200, twoVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '2',
    color: 'red',
  })

t.create('target manifest file has no vulnerabilities')
  .get('/badges/shields/badge-maker/package.json.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .query({
        targetFile: 'badge-maker/package.json',
      })
      .reply(200, zeroVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '0',
    color: 'brightgreen',
  })

t.create('target manifest file has vulnerabilities')
  .get('/badges/shields/badge-maker/package.json.json')
  .intercept(nock =>
    nock('https://snyk.io/test/github/badges/shields')
      .get('/badge.svg')
      .query({
        targetFile: 'badge-maker/package.json',
      })
      .reply(200, twoVulnerabilitiesSvg)
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: '2',
    color: 'red',
  })
