#!/bin/sh
# The upstream suite for this package is browser-only (test/webtest-*), so this
# checks that the installed library loads and exports what it promises.
set -eu

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

mkdir -p node_modules/@prosemirror
for dir in /usr/share/nodejs/*; do
	[ -d "$dir" ] || continue
	name=$(basename "$dir")
	[ "$name" = "@prosemirror" ] && continue
	ln -sfn "$dir" "node_modules/$name"
done
for dir in /usr/share/nodejs/@prosemirror/*; do
	ln -sfn "$dir" "node_modules/@prosemirror/$(basename "$dir")"
done

cat > check.mjs <<'EOF'
import {EditorView, Decoration, DecorationSet} from "prosemirror-view"

if (typeof EditorView !== "function") {
  throw new Error("EditorView is not a constructor")
}
if (typeof EditorView.prototype.updateState !== "function") {
  throw new Error("EditorView.updateState is missing")
}
if (typeof Decoration.widget !== "function") {
  throw new Error("Decoration.widget is missing")
}
if (typeof DecorationSet !== "function") {
  throw new Error("DecorationSet is not a constructor")
}
console.log("prosemirror-view loaded")
EOF

node check.mjs
