{"size": 1581, "ext": "js", "max_stars_repo_path": "scripts/test.js", "max_stars_repo_name": "mcous/resume", "max_stars_repo_stars_event_min_datetime": "2017-05-03T17:23:34.000Z", "max_stars_repo_stars_event_max_datetime": "2020-12-21T19:46:38.000Z", "max_issues_repo_path": "scripts/test.js", "max_issues_repo_name": "mcous/resume", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "scripts/test.js", "max_forks_repo_name": "mcous/resume", "max_forks_repo_forks_event_min_datetime": "2017-05-03T17:23:35.000Z", "max_forks_repo_forks_event_max_datetime": "2020-06-20T09:46:48.000Z"}
{"max_stars_count": 7, "max_issues_count": null, "max_forks_count": 5, "avg_line_length": 25.9180327869, "max_line_length": 83, "alphanum_fraction": 0.6204933586}
// test the PDF output size and page count
'use strict'

const assert = require('assert')
const path = require('path')
const fs = require('fs/promises')
const PdfParser = require('pdf2json')

const { BUILD_PARAMS } = require('./build')

// 1 pdf unit is 16pt, there are 72pts in an inch, letter is 8.5in wide x 11in tall
const EXPECTED_WIDTH = (8.5 * 72) / 16
const EXPECTED_HEIGHT = (11 * 72) / 16

function test(buildParams) {
  const parser = new PdfParser()
  const pdfPath = path.join(buildParams.outputDir, buildParams.pdfOutputName)

  return fs
    .readFile(pdfPath)
    .then((contents) => {
      return new Promise((resolve, reject) => {
        parser
          .once('pdfParser_dataError', reject)
          .once('pdfParser_dataReady', resolve)
          .parseBuffer(contents)
      })
    })
    .then((pdfData) => {
      const pageCount = pdfData.formImage.Pages.length
      const width = pdfData.formImage.Width
      const height = pdfData.formImage.Pages[0]?.Height

      assert(pageCount === 1, `Expected a 1 page PDF, got ${pageCount}`)

      assert(
        width === EXPECTED_WIDTH,
        `Expected width to be ${EXPECTED_WIDTH}, got ${width}`
      )

      assert(
        height === EXPECTED_HEIGHT,
        `Expected width to be ${EXPECTED_HEIGHT}, got ${height}`
      )
    })
}

module.exports = { test }

if (require.main === module) {
  test(BUILD_PARAMS)
    .then(() => {
      console.log('PDF size tests succeeded')
      process.exitCode = 0
    })
    .catch((error) => {
      console.error(error)
      process.exitCode = 1
    })
}
