#!/bin/sh
# Run by dh_auto_test in the source tree and by autopkgtest from the installed
# package, so use whichever of the two locations exists.
set -eu

lib=/usr/share/nodejs/svg-parser
[ -d "$lib" ] || lib=$PWD

work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

cat > "$work/umd.cjs" <<'JS'
const { parse } = require(process.env.SVG_PARSER_LIB);
const root = parse('<svg width="10" height="5"><g id="a"><circle r="2"/></g></svg>');
const svg = root.children[0];
if (root.type !== "root" || svg.tagName !== "svg") throw new Error("bad root element");
if (svg.properties.width !== 10 || svg.properties.height !== 5) {
  throw new Error("attributes not parsed: " + JSON.stringify(svg.properties));
}
const g = svg.children[0];
if (g.tagName !== "g" || g.properties.id !== "a") throw new Error("bad child element");
const circle = g.children[0];
if (circle.tagName !== "circle" || circle.properties.r !== 2) throw new Error("bad nested element");
JS

cp "$lib/dist/svg-parser.esm.js" "$work/esm.mjs"
cat > "$work/check.mjs" <<'JS'
import { parse } from "./esm.mjs";
const root = parse('<rect x="1" y="2"/>');
const rect = root.children[0];
if (rect.tagName !== "rect" || rect.properties.x !== 1) throw new Error("esm build broken");
JS

SVG_PARSER_LIB="$lib" node "$work/umd.cjs"
node "$work/check.mjs"
echo "svg-parser smoke test passed ($lib)"
