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 package latest version')
  .get('/commander.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('valid scoped package latest version')
  .get('/@babel/core.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('non existent package')
  .get('/mochaabcdef.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'npm package is invalid or does not exist',
  })

t.create('valid package specific version')
  .get('/commander@2.20.0.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: Joi.number().required(),
  })

t.create('non existent package version')
  .get('/gh-badges@0.3.4.json')
  .timeout(20000)
  .expectBadge({
    label: 'vulnerabilities',
    message: 'npm package is invalid or does not exist',
  })

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

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

t.create('package not found')
  .get('/not-mocha-fake-ish@13.0.0.json')
  .intercept(nock =>
    nock('https://snyk.io/test/npm/not-mocha-fake-ish@13.0.0')
      .get('/badge.svg')
      .reply(200, '<html>foo</html>')
  )
  .expectBadge({
    label: 'vulnerabilities',
    message: 'npm package is invalid or does not exist',
    color: 'red',
  })
