const assert = require('assert')
const knex = require('knex')
const PLV8 = require('../')

describe('plv8', () => {
  const plv8 = new PLV8(knex({
    client: 'pg',
    connection: {
      host: process.env.PLV8_HOST,
      user: process.env.PLV8_USER,
      password: process.env.PLV8_PASSWORD,
      database: process.env.PLV8_DATABASE
    }
  }))
  describe('#install', () => {
    it('should install a module', () => {
      return plv8.install({
        modulePath: 'testmodule',
        cwd: __dirname,
        moduleName: 'testmodule'
      })
      .then(moduleName => {
        assert.equal(moduleName, 'testmodule')
      })
    })
  })
  describe('#uninstall', () => {
    before(() => {
      return plv8.install({
        modulePath: 'testmodule',
        cwd: __dirname,
        moduleName: 'testmodule'
      })
      .then(moduleName => {
        assert.equal(moduleName, 'testmodule')
      })
    })
    it('should uninstall a module', () => {
      return plv8.uninstall('testmodule')
        .then(deleted => {
          assert(deleted)
        })
    })

  })
  describe('#eval', () => {
    before(() => {
      return plv8.install({
        modulePath: 'testmodule',
        cwd: __dirname,
        moduleName: 'testmodule'
      })
      .then(moduleName => {
        assert.equal(moduleName, 'testmodule')
      })
    })

    it('should eval an arbitrary function', () => {
      return plv8.eval(() => 42)
        .then(result => {
          assert.equal(result, 42)
        })
    })
    it('should invoke function on installed module', () => {
      return plv8.eval(() => {
        const tm = require('testmodule')
        return tm.hello('world')
      })
      .then(result => {
        assert.equal(result, 'helloworld')
      })
    })
  })
})

@@ -468,44 +468,39 @@
 th: 
-'testmodule',%0A        cwd: __dirname
+require.resolve('./testmodule')
 ,%0A  
@@ -739,44 +739,39 @@
 th: 
-'testmodule',%0A        cwd: __dirname
+require.resolve('./testmodule')
 ,%0A  
@@ -1163,44 +1163,39 @@
 th: 
-'testmodule',%0A        cwd: __dirname
+require.resolve('./testmodule')
 ,%0A  
@@ -1640,16 +1640,36 @@
 eturn tm
+%0A        //return tm
 .hello('
@@ -1701,32 +1701,60 @@
 hen(result =%3E %7B%0A
+        console.log(result)%0A
         assert.e

use require.resolve for modulePath