const makeEgTunnel = require('../scripts/run-execution-grid-tunnel')
const fetch = require('node-fetch')

async function main() {
  const {cleanupFunction, port} = await makeEgTunnel()
  
  // Create tunnel
  const response = await fetch(`http://0.0.0.0:${port}/tunnels`, {
    method: 'post',
    headers: {
      'x-eyes-api-key': process.env.APPLITOOLS_API_KEY,
      'x-eyes-server-url': process.env.APPLITOOLS_EYES_URL, 
    },
  })

  const tunnelId = await response.json()
  
  // Stop tunnel
  await fetch(`http://0.0.0.0:${port}/tunnels/${tunnelId}`, {
    method: 'delete',
    headers: {
      'x-eyes-api-key': process.env.APPLITOOLS_API_KEY,
      'x-eyes-server-url': process.env.APPLITOOLS_EYES_URL,
    },
  })

  await cleanupFunction()
}


main()