!******************************************************************************!
! Tests the AUX command.
!
! Author:  Jay Hallam & J. Czechowski IV
!******************************************************************************!
startmacro
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE
  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE

  ! save off AUX.DAT and switch to DAT with all the test files
  call setDatDir
  if sysTestDatExists then
    res origDatAux "^{aux.dat}"
    invoke nm_root nxm.sys.lib.Shell.getNmRoot()
    res aux.dat "^{nm_root}^{datDirSlash}"
  endif

  set inNeXtOpt TEST("^{reg.shell.class}",eqs,"class nxm.sys.libxm.XmShell")

  if inNeXtOpt then
    call test_basic_tests_nextopt  ! Special X-Midas compatible version (no AUX names)
  else
    call test_basic_tests
    call test_terminate_path
    call test_read_write
    call test_invalid_entry
    call test_find_subdir   ! Bug 2647 (3.3.0)
  endif

  ! requires write permissions and the write aux to be '1'
  if baseline isTrue AND AUX.1 DEXISTS then
    call test_negative_write_aux     ! compatible w/ NeXtOpt, don't need special case
  else
    INFO "Skipping. Negative write aux only tested when in /baseline and AUX.1 is set."
  endif

  ! Interactive tests (includes AUX names)
  if interAct isTrue and inNeXtOpt isFalse then
    res inst "Should see two warnings regarding 'AUX_THAT_DOES_NOT_EXIST', &
              followed by a list of the current AUX entries."

    pipe on
      panel/msgid=MAIN/setup/controls=gc

      gcontrol label macro "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemcolors {PASS=GREEN,FAIL=RED}

      gcontrol text idtext "Instructions" "^inst" 24 7 /title

      shellgui/id=idshellgui/attach
      say ""
      aux/store                                  ! Test for Bug 1079
      say "nM> AUX AUX_THAT_DOES_NOT_EXIST"      ! Test for Bug 1079
      AUX AUX_THAT_DOES_NOT_EXIST                ! Test for Bug 1079
      say ""                                     ! Test for Bug 1079
      say "nM> AUX ,, AUX_THAT_DOES_NOT_EXIST"   ! Test for Bug 1079
      AUX ,, AUX_THAT_DOES_NOT_EXIST             ! Test for Bug 1079
      aux/restore                                ! Test for Bug 1079
      say ""
      say "nM> AUX"
      AUX
    pipe off
  endif

  if sysTestDatExists then
    res aux.dat "^origDatAux"  ! restore original AUX.DAT
  endif
endmacro

!******************************************************************************!
procedure processMessage m:msg
  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Tests for AUX" msg.data eqss "PASS"
    pipe stop
  else
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return

!******************************************************************************!
! Basic functionality tests
procedure test_basic_tests
  ! Clear any saved AUX information
  remove OLDAUXWRITE OLDAUXREAD
  aux/store
  assert/text="^reg.main.name: store current WRITE aux" OLDAUXWRITE REXISTS
  assert/text="^reg.main.name: store current READ  aux" OLDAUXREAD REXISTS

  ! Prepend CWD and append HOME to the READ path
  AUX ,, CWD+ +HOME

  remove str
  result readaux "^aux.read"

  sedit readaux str RANGE 0 2 /zerobase
  assert/text="^reg.main.name: Prepend CWD to read path" str EQS "CWD"

  calc lastindex readaux.length 1 -
  remove str
  sedit readaux str RANGE -3 lastindex /zerobase
  assert/text="^reg.main.name: Append HOME to read path" str EQS "HOME"

  ! Restore the AUX structure
  aux/restore
  assert/text="^reg.main.name: restore current WRITE aux" AUX.WRITE eqs OLDAUXWRITE
  assert/text="^reg.main.name: restore current READ  aux" AUX.READ  eqs OLDAUXREAD
return

!******************************************************************************!
! Basic functionality tests (X-Midas Compatible)
procedure test_basic_tests_nextopt
  ! Clear any saved AUX information
  remove OLDAUXWRITE OLDAUXREAD
  aux/store
  assert/text="^reg.main.name: store current WRITE aux" OLDAUXWRITE REXISTS
  assert/text="^reg.main.name: store current READ  aux" OLDAUXREAD REXISTS

  ! Prepend CWD to the READ path ("CWD" -> "0" for X-Midas)
  AUX ,, CWD+

  remove str
  result readaux "^aux.read"

  ! When run from X-Midas "XMPIPE|RAM" will always be in front and "CWD" will always be listed as "0".
  sedit readaux str RANGE 0 11 /zerobase
  assert/text="^reg.main.name: Prepend CWD to read path" str EQS "XMPIPE|RAM|0"

  ! Restore the AUX structure
  aux/restore
  assert/text="^reg.main.name: restore current WRITE aux" AUX.WRITE eqs OLDAUXWRITE
  assert/text="^reg.main.name: restore current READ  aux" AUX.READ  eqs OLDAUXREAD
return

!******************************************************************************!
! Check automatic termination of file path in aux table
procedure test_terminate_path
  if aux.dat nrexists then
    warn "Skipping path termination tests... AUX.DAT not found"
  else
    SEDIT "^{aux.dat}" aux.bad_aux "RANGE" 0 -1
    assert/text="^reg.main.name: Found apenny.prm in ^{aux.dat} "      apenny.prm{AUX=DAT}     fexists
    if sysTestDatExists then
      warn "Skipping path termination tests... running with dat files nxm.sys.test.dat"
    else
      assert/text="^reg.main.name: Found apenny.prm in ^{aux.bad_aux}  " apenny.prm{AUX=BAD_AUX} fexists
    endif
    rem aux.bad_aux
  endif
return

!******************************************************************************!
! This added to test use of READ= and WRITE= added in NeXtMidas 2.3.0
! As of 2.5.0, added /WARN=NO since the AUX entries used probably do not exist,
! they are just used for this set of tests.
procedure test_read_write
  aux/store
  aux/warn=no READ=11 WRITE=12
  assert/text="^reg.main.name: Set with WRITE=11 and READ=12 - WRITE=12" AUX.WRITE EQS "12"
  assert/text="^reg.main.name: Set with WRITE=11 and READ=12 - READ=11"  AUX.READ  EQS "11"

  aux/warn=no READ=12 WRITE=11
  assert/text="^reg.main.name: Set with WRITE=12 and READ=11 - WRITE=11" AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with WRITE=12 and READ=11 - READ=12"  AUX.READ  EQS "12"

  aux/warn=no READ=11|12 WRITE=11
  assert/text="^reg.main.name: Set with WRITE=11 and READ=11|12 - WRITE=11" AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with WRITE=11 and READ=11|12 - READ=12"  AUX.READ  EQS "11|12"

  aux/warn=no WRITE=11 READ=11
  aux/warn=no READ=1
  assert/text="^reg.main.name: Set with READ=1 - WRITE=11" AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with READ=1 - READ=1"   AUX.READ  EQS "1"

  aux/warn=no WRITE=11 READ=11
  aux/warn=no WRITE=1
  assert/text="^reg.main.name: Set with WRITE=1 - WRITE=1" AUX.WRITE EQS "1"
  assert/text="^reg.main.name: Set with WRITE=1 - READ=11" AUX.READ  EQS "11"

  aux/warn=no WRITE=11 READ=11
  aux/warn=no READ=+1
  assert/text="^reg.main.name: Set with READ=+1 - WRITE=11"  AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with READ=+1 - READ=11|1" AUX.READ  EQS "11|1"

  aux/warn=no WRITE=11 READ=11
  aux/warn=no READ=1+
  assert/text="^reg.main.name: Set with READ=1+ - WRITE=11"  AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with READ=1+ - READ=1|11" AUX.READ  EQS "1|11"

  aux/warn=no WRITE=11 READ=11
  aux/warn=no ,, 1 11 12
  assert/text="^reg.main.name: Set with ,, 1 11 12 - WRITE=11"     AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with ,, 1 11 12 - READ=1|11|12" AUX.READ  EQS "1|11|12"

  aux/warn=no WRITE=11 READ=11
  aux/warn=no ,, 12+ +1
  assert/text="^reg.main.name: Set with ,, 12+ +1 - WRITE=11"     AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with ,, 12+ +1 - READ=1|11|12" AUX.READ  EQS "12|11|1"

  aux/warn=no WRITE=11 READ=11
  aux/warn=no ,, 12+ +1 -11
  assert/text="^reg.main.name: Set with ,, 12+ +1 -11 - WRITE=11"     AUX.WRITE EQS "11"
  assert/text="^reg.main.name: Set with ,, 12+ +1 -11 - READ=1|11|12" AUX.READ  EQS "12|1"
  aux/restore
return

!******************************************************************************!
! Tests for Bug 1079 to check and see that invalid AUX entries are not allowed to be added
! to read/write AUX.
procedure test_invalid_entry
  ! TEST SET 1 - Tests for:
  !   nM> aux 1 INVALID|-BOB|1|CWD|DAT|99|RAM
  !   nM> aux 1 -INVALID|-BOB|1|CWD|DAT|99|RAM
  !   ERROR: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
  aux/store

  set expWrite aux.write
  set expRead  aux.read

  ! Use /warn=no since these entries don't exist
  aux/warn=no ^{expWrite} AUX_THAT_IS_INVALID|-AUX_THAT_DOES_NOT_EXIST|^{expRead}
  assert/text="^reg.main.name: Add invalid READ entry, AUX.READ"  "AUX_THAT_IS_INVALID|^{expRead}" eqs aux.read
  assert/text="^reg.main.name: Add invalid READ entry, AUX.WRITE" "^{expWrite}" eqs aux.write

  aux/warn=no ^{expWrite} -AUX_THAT_IS_INVALID|-AUX_THAT_DOES_NOT_EXIST|^{expRead}
  assert/text="^reg.main.name: Remove invalid READ entry, AUX.READ"  expRead  eqs aux.read
  assert/text="^reg.main.name: Remove invalid READ entry, AUX.WRITE" expWrite eqs aux.write

  aux/restore

  ! TEST SET 2 - Tests for:
  !   nM> AUX CWD -WEB|-HOME
  ! make sure it removes both WEB and HOME regardless of their order on the path
  aux/store

  aux/warn=no 1   1|WEB|2|CWD|3|HOME|4
  aux/warn=no CWD -WEB|-HOME
  assert/text="^reg.main.name: Remove in any order (Test #1), AUX.READ"  "1|2|CWD|3|4" eqs aux.read
  assert/text="^reg.main.name: Remove in any order (Test #1), AUX.WRITE" "CWD"         eqs aux.write

  aux/warn=no 1   1|HOME|2|CWD|3|WEB|4
  aux/warn=no CWD -WEB|-HOME
  assert/text="^reg.main.name: Remove in any order (Test #2), AUX.READ"  "1|2|CWD|3|4" eqs aux.read
  assert/text="^reg.main.name: Remove in any order (Test #2), AUX.WRITE" "CWD"         eqs aux.write

  aux/restore
return

!******************************************************************************!
! Tests for BUG 1289 to check and see that "negative" write auxes are supported properly.
procedure test_negative_write_aux
  aux/store
  aux -1                                 ! set write aux to a negative value
  file open/n f cities.txt               ! write a file to AUX=1 that exists in DAT
  file close f
  fname filename 1 cities txt AUX
  assert/text="^reg.main.name: negative AUX (-1) created new file in AUX=1" filename fexists
  status nxm.sys.dat.cities.txt size=s       ! confirm that the file in dat is unchanged
  assert/text="^reg.main.name: negative AUX did not change existing file in DAT AUX" s gt 0

  ! cleanup
  erase/warn=no/AUX=1 cities.txt
  aux/restore
return

!******************************************************************************!
! Tests being able to find a file in a sub-dir of an AUX (Bug 2647)
procedure test_find_subdir
  if sysTestDatExists then
    info "skipping test_find_subdir as dat is under sys.test"
  else
    aux/store
    set aux.nmr "^{env.nmroot}"
    assert/text="^reg.main.name: File found in AUX subDir" nxm.sys.dat.apenny.prm feq nxm/sys/dat/apenny.prm{AUX=NMR}
    aux/restore
  endif
return

