const glob = require('glob')
const path = require('path')
const fs   = require('fs')
const test = require('tape')
const fixtures = require('./fixtures')
const pdf = require('../lib')

process.env.TZ = 'Europe/Berlin'

const args = process.argv.slice(2)
if (args.length) {
  run(args.map((a) => path.join(__dirname, '../', a)), true)
} else {
  glob(path.join(__dirname, '{pdfs,others}/**/*.js'), function (err, files) {
    if (err) throw err
    run(files)
  })
}

// mock current time
const _Date = Date
Date = class extends _Date {
  constructor(year, month, day, hour, minute, second) {
    if (arguments.length === 0) {
      return new _Date(2015, 1, 19, 22, 33, 26)
    } else {
      return new _Date(year, month, day, hour, minute, second)
    }
  }
}

function run(files, force) {
  const f = fixtures.create()

  for (const scriptPath of files) {
    const relativePath = path.relative(__dirname, scriptPath)
    const dirname      = path.dirname(scriptPath)
    const basename     = path.basename(scriptPath, '.js')

    // ignore tests starting with _ and named `test`
    if (!force && (basename[0] === '_' || basename === 'test')) {
      continue
    }

    if (relativePath.startsWith('others')) {
      require(scriptPath)
      continue
    }

    const pdfsPath        = path.relative(path.join(__dirname, 'pdfs'), dirname)
    const expectationPath = path.join(dirname, basename + '.pdf')
    const resultPath      = path.join(dirname, basename + '.result.pdf')

    const script = require(scriptPath)

    let doc = new pdf.Document({
      font:       f.font.afm.regular,
      padding:    script.padding >= 0 ? script.padding : 10,
      lineHeight: 1,
    })

    const newDoc = script(doc, f)
    if (newDoc) {
      doc = newDoc
    }

    test(path.join(pdfsPath, basename), function (t) {
      doc.info.id = '42'
      doc.info.creationDate = new Date(2015, 1, 19, 22, 33, 26)
      doc.info.producer = 'pdfjs tests (github.com/rkusa/pdfjs)'

      const w = fs.createWriteStream(resultPath)
      doc.pipe(w)

      doc.end().catch(err => {
        t.error(err)
      })

      w.on('close', () => {
        try {
          var result = fs.readFileSync(resultPath, 'binary')
          var expectation = fs.readFileSync(expectationPath, 'binary')
        } catch (err) {
          t.error(err)
        }

        t.ok(result === expectation, basename)
        t.end()
      })
    })
  }
}

@@ -1476,17 +1476,16 @@
 t.pdf')%0A
-%0A
     cons
@@ -1493,16 +1493,25 @@
  script 
+         
 = requir
@@ -1525,16 +1525,73 @@
 tPath)%0A%0A
+    test(path.join(pdfsPath, basename), function (t) %7B%0A  
     let 
@@ -1621,16 +1621,18 @@
 %7B%0A      
+ 
+ 
 font:   
@@ -1655,16 +1655,18 @@
 egular,%0A
+  
       pa
@@ -1718,16 +1718,18 @@
 g : 10,%0A
+  
       li
@@ -1741,28 +1741,32 @@
 ght: 1,%0A    
+  
 %7D)%0A%0A
+  
     const ne
@@ -1785,18 +1785,23 @@
 t(doc, f
-)%0A
+, t)%0A  
     if (
@@ -1806,20 +1806,46 @@
  (newDoc
+ instanceof pdf.Document
 ) %7B%0A
+  
       do
@@ -1863,65 +1863,12 @@
     
+  
 %7D%0A
-%0A    test(path.join(pdfsPath, basename), function (t) %7B
 %0A   
@@ -2091,70 +2091,8 @@
 w)%0A%0A
-      doc.end().catch(err =%3E %7B%0A        t.error(err)%0A      %7D)%0A%0A
     
@@ -2390,16 +2390,238 @@
       %7D)
+%0A%0A      const p = newDoc instanceof Promise ? newDoc : Promise.resolve()%0A      p%0A        .then(() =%3E %7B%0A          doc.end().catch(err =%3E %7B%0A            t.error(err)%0A          %7D)%0A        %7D)%0A        .catch(err =%3E t.error(err))
 %0A    %7D)%0A

add possibility for async pdf tests