#!/bin/sh
set -e

if [ "${NRGM_BUILD:-}" = 1 ]; then
  # Upstream tests use the package self-reference and exercise the
  # freshly built dist/ tree.  Run them only during the build.
  tap --no-coverage --timeout=300 test/*.ts
  exit 0
fi

# autopkgtest: exercise the installed module.
node <<'EOF'
const assert = require('assert')
const { Matcher } = require('@isaacs/nonrecursive-glob-match')

const m = new Matcher('a*a!(y|z)')
assert.strictEqual(m.test('abcay'), false)
assert.strictEqual(m.test('abcax'), true)
assert.strictEqual(m.test('aa'), true)
assert.throws(() => new Matcher('a/b'), /single path portion/)
assert.throws(() => m.test('a/b'), /single path portion/)
assert.strictEqual(m.testUnchecked('abcax'), true)

assert.strictEqual(new Matcher('*.js').test('a.js'), true)
assert.strictEqual(new Matcher('*.js').test('.a.js'), false)
assert.strictEqual(new Matcher('*.js', { dot: true }).test('.a.js'), true)
assert.strictEqual(new Matcher('A', { nocase: true }).test('a'), true)

// numeric range matching, as used by minimatch for brace ranges
const M = ''
const r = new Matcher(`file-${M}001..500${M}.txt`, { rangeMark: M })
assert.strictEqual(r.test('file-042.txt'), true)
assert.strictEqual(r.test('file-42.txt'), false)
assert.strictEqual(r.test('file-501.txt'), false)

console.log('ok')
EOF
