#!/bin/sh
set -e

# During dpkg-buildpackage, debian/rules sets MINIMATCH_MODULE=. so that the
# freshly built package is tested.  In autopkgtest, require the installed
# "minimatch" module instead.
MINIMATCH_MODULE="${MINIMATCH_MODULE:-minimatch}" node <<'EOF'
const assert = require('assert')
const spec = process.env.MINIMATCH_MODULE
const m = spec === '.' ? require(process.cwd()) : require(spec)

// basic smoke test
assert(m('foo.js', '*.js'))
assert(!m('foo.js', '*.ts'))
assert(m('bar/baz/quux.js', '**/*.js'))

// CVE-2026-140006: escape() output must only match the literal input
const posix = {}
const win = { windowsPathsNoEscape: true }
for (const o of [posix, win]) {
  const bang = m.escape('!secret.txt', o)
  assert.strictEqual(m('!secret.txt', bang, o), true)
  assert.strictEqual(m('passwd', bang, o), false)
  assert.strictEqual(m('secret.txt', bang, o), false)
  assert.strictEqual(m('x', bang, o), false)
  assert.strictEqual(m('!secret.txt', m.unescape(bang, o), o), true)

  const hash = m.escape('#notes.md', o)
  assert.strictEqual(m('#notes.md', hash, o), true)
  assert.strictEqual(m('notes.md', hash, o), false)
}

// an escaped backslash must survive brace expansion
const p = m.escape('x\\*') + '.{js,ts}'
assert.strictEqual(m('x\\*.js', p), true)
assert.strictEqual(m('x\\*.ts', p), true)
assert.strictEqual(m('x\\anything.js', p), false)

const p2 = m.escape('x\\*{,}')
assert.strictEqual(m('x\\anything', p2), false)
assert.strictEqual(m('x\\*', p2), true)

assert.deepStrictEqual(m.braceExpand('a\\{b,c}'), ['a{b,c}'])
assert.deepStrictEqual(m.braceExpand('a\\\\{b,c}'), ['a\\\\b', 'a\\\\c'])
assert.strictEqual(m('a\\b', 'a\\\\{b,c}'), true)
assert.strictEqual(m('ab', 'a\\\\{b,c}'), false)

// side effect documented in the advisory: next to a brace group, an
// escaped backslash no longer loses a backslash
assert.deepStrictEqual(m.braceExpand('{src,lib}/a\\\\b.js'), [
  'src/a\\\\b.js',
  'lib/a\\\\b.js',
])
assert.strictEqual(m('src/a\\b.js', '{src,lib}/a\\\\b.js'), true)
assert.strictEqual(m('src/ab.js', '{src,lib}/a\\\\b.js'), false)

// in windowsPathsNoEscape mode, a leading ! is encoded as @(!);
// with noext, that encoding fails closed
const pn = m.escape('!a', win)
assert.strictEqual(m('b', pn, { ...win, noext: true }), false)
assert.strictEqual(m('a', pn, { ...win, noext: true }), false)

// CVE-2026-118764: **/.. preprocessing must stay bounded
const p3 = Array(30).fill('**/../a/b').join('/')
const capped = new m.Minimatch(p3, {
  optimizationLevel: 2,
  braceExpandMax: 64,
})
assert.strictEqual(capped.set.length, 64)

const small = new m.Minimatch('**/../a/b/**/../c/d', {
  optimizationLevel: 2,
})
assert.strictEqual(small.set.length, 4)
assert.strictEqual(small.match('../a/b/../c/d'), true)
assert.strictEqual(small.match('x/a/b/y/c/d'), true)

const dedupe = new m.Minimatch('{x,*}/{0..999}', { optimizationLevel: 2 })
assert.strictEqual(dedupe.set.length, 2000)
assert.strictEqual(dedupe.match('x/5'), true)
assert.strictEqual(dedupe.match('y/999'), true)
assert.strictEqual(dedupe.match('.y/5'), false)

console.log('ok')
EOF
