#!/bin/sh
set -e

if [ "${BRACE_EXPANSION_BUILD:-}" = 1 ]; then
  # Upstream test suite: it uses the package self-reference, which points
  # to the freshly built dist/ directory.  Run it only during the build.
  tap --no-coverage --timeout=300 test/index.js
  exit 0
fi

# autopkgtest: exercise the installed module.
node <<'EOF'
const assert = require('assert')
const m = require('brace-expansion')

assert.strictEqual(typeof m.expand, 'function')
assert.strictEqual(typeof m.default, 'function')
assert.strictEqual(m.default, m.expand)

assert.deepStrictEqual(m.expand('a{b,c}d'), ['abd', 'acd'])
assert.deepStrictEqual(m.expand('{1..3}'), ['1', '2', '3'])
assert.deepStrictEqual(m.expand('{a..c}'), ['a', 'b', 'c'])
assert.deepStrictEqual(m.expand('{x,*}/{0..2}'), [
  'x/0',
  'x/1',
  'x/2',
  '*/0',
  '*/1',
  '*/2',
])

// escaped braces must stay literal, and an escaped backslash before a
// brace group must keep its backslash (CVE-2026-140006)
assert.deepStrictEqual(m.expand('a\\{b,c}'), ['a{b,c}'])
assert.deepStrictEqual(m.expand('a\\\\{b,c}'), ['a\\b', 'a\\c'])

assert.deepStrictEqual(m.expand('${1..3}'), ['${1..3}'])
assert.deepStrictEqual(m.expand('a{b,c'), ['a{b,c'])
assert.deepStrictEqual(m.expand('{}'), ['{}'])
assert.strictEqual(m.EXPANSION_MAX, 100000)

console.log('ok')
EOF
