#!/bin/bash
# The test suite is only executed in autopkgtest, so we can just `npm install`
# the world, as there are too many missing dependencies.
set -e

# Ensure we have a writable home.
export HOME=/tmp/home
mkdir -p $HOME

# I really have no idea what I am doing here, but without this, imports fail.
export NODE_PRESERVE_SYMLINKS=1

# This can be SUCH a source of headches, and I have not found a way to make it
# work reusing the system installed modules.
rm -rf package-lock.json node_modules/.package-lock.json node_modules
mkdir node_modules
ln -s ../devtools-protocol node_modules/

PUPPETEER_SKIP_DOWNLOAD=1 NPM_CONFIG_CACHE=/tmp/npm_cache \
    npm install

ts-node-script scripts/ensure-correct-devtools-protocol-package

# Binary to use for tests.
export BINARY=/usr/bin/chromium

# But a couple of tests still try to find a downloaded chromium unless I set
# this too.
export PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium
#export CHROME_DEVEL_SANDBOX=/usr/lib/chromium/chrome-sandbox

# Even in headless mode, some tests fail unless run with an X server.
#xvfb-run mocha --config mocha-config/puppeteer-unit-tests.js -f 'should work with no default arguments'
xvfb-run mocha --config mocha-config/puppeteer-unit-tests.js
