#!/usr/bin/env bash
set -euo pipefail

root=`dirname "$0"`
root=`node -r fs -p "fs.realpathSync(process.argv[1]);" "$root"`

. "$root/../node_modules/faucet-pipeline-core/test/cli_harness.sh"

begin "$root/test_basic"
	faucet
	assert_identical "./dist/bundle.css" "./expected.css"
	assert_missing "./expected.json"
end

begin "$root/test_import_scss"
	faucet
	assert_identical "./dist/bundle.css" "./expected.css"
end

begin "$root/test_import_css"
	faucet
	assert_identical "./dist/bundle.css" "./expected.css"
end

begin "$root/test_errors"
	faucet || echo "Crashed successfully"
	assert_identical "./dist/bundle.css" "./expected.css"
end

begin "$root/test_fingerprinting"
	faucet --fingerprint
	assert_identical "./dist/fingerprint/bundle-0c1a0954ab49f1a615bd68bb8844fe5f3650afca726aa6008218bd34cb9dc56f.css" "./expected.css"
	assert_identical "./dist/no-fingerprint/bundle.css" "./expected.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_multi"
	faucet
	assert_identical "./dist/foo.css" "./expected_foo.css"
	assert_identical "./dist/bar.css" "./expected_bar.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_compact"
	faucet --compact
	assert_identical "./dist/bundle.css" "./expected.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_prefix"
	faucet
	assert_identical "./dist/bundle.css" "./expected.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_no_prefix"
	faucet
	assert_identical "./dist/bundle.css" "./expected.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_prefix_with_different_browserslist"
	faucet
	assert_identical "./dist/bundle.css" "./expected.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_base_uri"
	faucet
	assert_identical "./dist/bundle.css" "./expected.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_assets_integration"
	faucet --fingerprint
	assert_identical "./dist/bundle-2fb534e51296210a6e7693deb06357de2a8a769c4595812885423013a6f1ed1d.css" "./expected.css"
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_key_config"
	faucet --fingerprint
	assert_json "./dist/manifest.json" "./expected.json"
end

begin "$root/test_sourcemap"
	faucet --sourcemaps
	assert_identical_sourcemap "./dist/bundle.css" "./expected.css" "./expected.css.map"
end

echo; echo "SUCCESS: all tests passed"
