const {generateApprovalObserverScript} = require('../out/observer-script');
let s = generateApprovalObserverScript(18080);

// Extract the terminal prompt regex from generated code
let idx = s.indexOf('_termPromptMatch');
let reCtx = s.substring(idx, s.indexOf(');', idx) + 1);
console.log('v32 code:', reCtx.substring(0, 80));

// Extract regex
let reMatch = reCtx.match(/\/(.+?)\//);
let termRe = new RegExp(reMatch[1]);
console.log('v32 regex:', termRe);

let stripRe = /\s*(content_copy|content_paste|play_arrow)\s*$/;

let tests = [
  // Should MATCH (terminal commands)
  ['\u276f gravity_control > Start-Sleep 12', true, 'Start-Sleep'],
  ['\u276f gravity_control > npm.cmd run compile', true, 'npm compile'],
  ['\u276f gravity_control > $logFile = Join-Path $env:USERPROFILE', true, 'PS variable (had JUNK match)'],
  ['\u276f gravity_control > git add -A; git commit -m "test"', true, 'git commit'],
  ['\u276f gravity_control > node -e "const {gen}=require(\'./out\')"', true, 'node with const (was JUNK)'],
  ['\u276f extension > npm.cmd run compile', true, 'extension npm'],
  ['\u276f gravity_control > Start-Sleep 12 content_copy', true, 'with icon (strip)'],
  ['\u276f gravity_control > Get-Content f.txt | Select-Object -Last 5', true, 'Get-Content'],
  // Should NOT match (prompt only, no command)
  ['\u276f gravity_control > ', false, 'prompt only'],
  ['\u276f extension > ', false, 'prompt only ext'],
  // Should NOT match (not terminal - JS code)
  ['function test() { return 1; }', false, 'JS function'],
  ['const x = require("fs")', false, 'JS const'],
  ['import { foo } from "bar"', false, 'JS import'],
  // Should NOT match (no prompt marker)
  ['gravity_control > dir', false, 'no ❯ marker'],
];

console.log('\n=== v32 TERMINAL PROMPT REGEX TESTS ===');
let pass = 0, fail = 0;
for (let [text, shouldMatch, desc] of tests) {
  let m = termRe.exec(text);
  let matched = false;
  let cmdV = null;
  if (m && m[1] && m[1].trim().length > 2) {
    cmdV = m[1].trim().replace(stripRe, '').trim();
    matched = cmdV.length > 2;
  }
  let ok = matched === shouldMatch;
  if (ok) pass++; else fail++;
  console.log((ok ? 'PASS' : 'FAIL') + ' | ' + (matched ? 'MATCH' : 'SKIP ').padEnd(5) + ' | ' + desc);
  if (matched && cmdV) console.log('       cmd: "' + cmdV + '"');
}
console.log('\nResult: ' + pass + '/' + (pass+fail) + ' passed' + (fail > 0 ? ' (' + fail + ' FAILED!)' : ' ALL OK'));
