set -e

# The upstream test suite is written for vitest and imports the TypeScript
# sources directly. Rewrite it to run on node:test against the module as
# installed, then compile it: Debian's nodejs has no type stripping.
rm -rf out-src out-tests
mkdir -p out-src
cp -r tests out-src/tests
# tests/fixtures/node-semver/invalid-versions.ts reads MAX_LENGTH from the
# sources, so they have to keep their position relative to the tests
cp -r src out-src/src
cp debian/tests/pkg-js/vitest-shim.ts out-src/vitest-shim.ts
# bundle-size compares verkit with semver once bundled: needs rolldown
rm -f out-src/tests/bundle-size.test.ts
rm -rf out-src/tests/fixtures/bundle-size

find out-src -name '*.ts' -exec sed -i \
	-e "s|from '\.\./src/[a-z.-]*\.ts'|from 'verkit'|" \
	-e "s|from 'vitest'|from '../vitest-shim.ts'|" {} +

# do not depend on the package.json of the tree the tests are copied into
echo '{"type":"module"}' > out-src/package.json

# resolve "verkit" to the module root: the source tree during build, the
# installed files that the autopkgtest runner links here during autopkgtest
[ -e node_modules/verkit ] || { mkdir -p node_modules; ln -s .. node_modules/verkit; }

tsc -p debian/tests/pkg-js/tsconfig.json
echo '{"type":"module"}' > out-tests/package.json
node --test out-tests/tests/*.test.js
