# Run ON AD2: Tests NAS SSH operations
# Deploy: scp test-nas-v2.ps1 AD2:C:\Shares\testdatadb\
# Run: powershell -NoProfile -ExecutionPolicy Bypass -File C:\Shares\testdatadb\test-nas-v2.ps1
$SSH = "C:\Program Files\OpenSSH\ssh.exe"
$SSH_KEY = "C:\Users\sysadmin\.ssh\id_ed25519"

Write-Host "=== Test A: echo ==="
$t = Get-Date
$r = & $SSH -i $SSH_KEY -o BatchMode=yes -o ConnectTimeout=10 -o StrictHostKeyChecking=accept-new root@192.168.0.9 "echo NAS_OK" 2>&1
Write-Host "  Result: $r  ($(((Get-Date)-$t).TotalSeconds)s)"

Write-Host "=== Test B: ls data dir ==="
$t = Get-Date
$r = & $SSH -i $SSH_KEY -o BatchMode=yes -o ConnectTimeout=10 root@192.168.0.9 "ls /data/test/ | head -5" 2>&1
foreach ($l in $r) { Write-Host "  $l" }
Write-Host "  ($(((Get-Date)-$t).TotalSeconds)s)"

Write-Host "=== Test C: find with wc -l only ==="
$t = Get-Date
$r = & $SSH -i $SSH_KEY -o BatchMode=yes -o ConnectTimeout=10 root@192.168.0.9 "find /data/test/TS-*/LOGS -name '*.DAT' -type f -mmin -1440 2>/dev/null | wc -l" 2>&1
Write-Host "  Count: $r  ($(((Get-Date)-$t).TotalSeconds)s)"

Write-Host "=== Test D: find to temp file ==="
$t = Get-Date
& $SSH -i $SSH_KEY -o BatchMode=yes -o ConnectTimeout=10 root@192.168.0.9 "find /data/test/TS-*/LOGS -name '*.DAT' -type f -mmin -1440 > /tmp/test-list.txt 2>/dev/null" *> $null
Write-Host "  ($(((Get-Date)-$t).TotalSeconds)s)"

Write-Host "=== Test E: check temp file ==="
$r = & $SSH -i $SSH_KEY -o BatchMode=yes -o ConnectTimeout=10 root@192.168.0.9 "wc -l /tmp/test-list.txt; rm -f /tmp/test-list.txt" 2>&1
Write-Host "  $r"

Write-Host "=== ALL DONE ==="
