! Runs the confidence tests to test NeXtMidas running as an X-Midas option tree.
! To run NeXtOpt confidence test in X-Midas:
!  xM> homepath   $NMROOT/nxm/sys/test
!  --OR-- in xm-4.6.x and later
!  xM> homepath test nxm
!
!  xM> mtranslate %confidence
!  xM> %confidence
!  --or-- to run baseline test
!  xM> %confidence/baseline
!
! @author  J. Czechowski IV / Neon Ngo
!
startmacro
  local testCount,failCount,temp
  local initialAuxList
  local baseln       ! Baseline testing?
  local interAct     ! Interactive?
  local statemachine ! Use state machine mode for RMIF tests?

  switch BASELINE    baseln GET                 ! Baseline testing?
  switch INTERACTIVE interAct GET               ! Interactive?
  switch STATEM      statemachine GET           ! Use state machine mode for RMIF tests?
  switch XM2NMPIPE   xm2nmpipe GET              ! XM to NM piping test
  if baseln     eq -1 then res baseln   0       ! switch not specified, default to no Baseline testing
  if interAct   eq -1 then res interAct ^baseln ! switch not specified, default to ^baseline
  if statemachine eq -1 then res statemachine 0 ! switch not specified, default to no state machine mode
  if xm2nmpipe  eq -1 then res xm2nmpipe 0      ! switch not specified, default to no XM2NM pipe testing

  res testCount 0
  res failCount 0

  aux/save=initialAuxList

  ! Build all the required test macros
  mtrans %test_results
  mtrans %test_xmmessage
  mtrans %test_xmmessage_translated
  mtrans %test_genmsgtbl
  mtrans %test_nm_jvmoptions
  mtrans %test_rmsg
  mtrans %test_xm_piped_data

  if interAct le 0 then
    say "INFO: Run %confidence/baseline or with /interactive to enable additional tests that requires X11 display"
  endif

  if xm2nmpipe ge 1 then
    %test_xm_piped_data/baseline=^baseln/interactive=^interAct
  endif

  ! Run the individual tests.
  %test_results/baseline=^baseln/interactive=^interAct numTested numFailed
  calc/quiet testCount testCount numTested +
  calc/quiet failCount failCount numFailed +

  say "INFO: Testing send/recv messages to/from a NeXtMidas Macro..."
  %test_xmmessage/baseline=^baseln/interactive=^interAct numTested numFailed
  calc/quiet testCount testCount numTested +
  calc/quiet failCount failCount numFailed +

  say "INFO: Testing send/recv messages to/from a NeXtMidas Primitive..."
  %test_xmmessage/prim/baseline=^baseln/interactive=^interAct numTested numFailed
  calc/quiet testCount testCount numTested +
  calc/quiet failCount failCount numFailed +

  %test_xmmessage_translated/baseline=^baseln/interactive=^interAct numTested numFailed
  calc/quiet testCount testCount numTested +
  calc/quiet failCount failCount numFailed +

  %test_genmsgtbl/baseline=^baseln/interactive=^interAct numTested numFailed
  calc/quiet testCount testCount numTested +
  calc/quiet failCount failCount numFailed +

  %test_nm_jvmoptions/baseline=^baseln/interactive=^interAct numTested numFailed
  calc/quiet testCount testCount numTested +
  calc/quiet failCount failCount numFailed +

  %test_rmsg/baseline=^baseln/interactive=^interAct numTested numFailed
  calc/quiet testCount testCount numTested +
  ! NTN: it is ok for this test case to fail, just print warning and continue
  if numFailed gt 0 then
    warn "RMSG ^numFailed of ^numTested tests failed."
  endif
  say " "

  ! Output the results and run the NeXtMidas tests if everything is good thus far.
  if failCount eq 0 then
    info "Ran ^{testCount} X-Midas<->NeXtMidas tests, ^{failCount} failed"
    say ""
    say ""
    info "Running NeXtMidas Confidence Tests"
    fname temp dat ,,, nxm
    res AUX_101 temp
    aux 1 1|101

    ! make sure current messages queue is down before calling message up
    message down

    message up

    ! Set FIPS flags
    ! and read in user JVM flags (NM_USER_JVM_FLAGS env var)
    ! since NeXtMidas 4.2.1
    res nmFipsFlags "-Dcom.redhat.fips=false -Dredhat.crypto-policies=false"
    import/nowarn NM_USER_JVM_FLAGS nmUserJVMFlags
    res jvmOptions "^{nmFipsFlags} ^{nmUserJVMFlags}"
    sedit jvmOptions jvmOptions strim ! trim leading and trailing spaces as they can cause jvm args to not be read in
    nm/on/bg/id=999/jvmoptions="^{jvmOptions}"

    ! Run regular NeXtMidas confidence (java) tests. Not passing /baseline and /interactive
    ! switches down since we don't want to do all the NeXtMidas interactive tests here
    res confSwitches " "
    if statemachine then res confSwitches "^{confSwitches}/statem"
    nm confidence^{confSwitches} sys

    nm/off

    rem/quiet nmFipsFlags
    rem/quiet nmUserJVMFlags
    rem/quiet jvmOptions
    message down
    aux ,, -101
    rem/quiet AUX_101
  else
    info "Ran ^{testCount} X-Midas<->NeXtMidas tests, ^{failCount} failed"
    say ""
    say ""
    warn "Skipping remaining tests after ^{failCount} tests failed"
  endif

  rem/quiet numTested
  rem/quiet numFailed

  aux/load=initialAuxList

endmacro
