# Upstream's own test suite is not part of the source tree (test/ is not
# tracked, and the demo website that src/ drives is not built), so smoke-test
# the built artifacts instead.
#
# This script runs twice: from dh_auto_test inside the build tree, and from
# pkg-js-autopkgtest inside the installed module directory.  In both cases a
# "dist" directory built from the same sources is present, so the assertions
# below can address it by relative path.

test -f dist/prosemirror-math.js
test -f dist/prosemirror-math.d.ts
test -f dist/prosemirror-math.css

# The bundle is an ES module, and its peer dependencies (katex and the
# prosemirror-* modules) are resolved at load time.
node --input-type=module -e '
const mod = await import("./dist/prosemirror-math.js");
const expected = [
  "MathView",
  "createMathSchema",
  "createMathView",
  "defaultBlockMathParseRules",
  "defaultInlineMathParseRules",
  "insertMathCmd",
  "makeBlockMathInputRule",
  "makeInlineMathInputRule",
  "mathBackspaceCmd",
  "mathPlugin",
  "mathSchemaSpec",
  "mathSelectPlugin",
  "mathSerializer",
];
const missing = expected.filter((name) => !(name in mod));
if (missing.length) {
  throw new Error("missing exports: " + missing.join(", "));
}
'

# The declarations must still reference katex's own types.  Debian has no
# node-types-katex, so this depends on the copy shipped in
# debian/build_modules/@types/katex: without it, katexOptions silently
# degrades from KatexOptions to any.
grep -q KatexOptions dist/prosemirror-math.d.ts

# The IME composition fixes in debian/patches/ must actually be in the bundle.
grep -Fq 'includes("Gecko")' dist/prosemirror-math.js
