; Mosis Designer Navigation Test
; AutoHotkey v2
; Tests: Click on app icons and verify navigation occurs

#Requires AutoHotkey v2.0
#Include "lib\utils.ahk"

; Test configuration
testsPassed := 0
testsFailed := 0

; Main test function
RunNavigationTests() {
    global testsPassed, testsFailed

    LogMessage("========================================")
    LogMessage("Starting Navigation Tests")
    LogMessage("========================================")

    ; Clean up any previous runs
    KillDesigner()
    ClearLogs()

    ; Start the designer
    pid := StartDesigner()
    if (!pid) {
        LogMessage("FATAL: Could not start designer")
        return false
    }

    ; Wait for window to appear
    LogMessage("Waiting for designer window...")
    hwnd := WaitForDesignerWindow()
    if (!hwnd) {
        LogMessage("FATAL: Designer window did not appear")
        KillDesigner()
        return false
    }
    LogMessage("Designer window found: " . hwnd)

    ; Wait for document to load
    Sleep(2000)  ; Give time for initial document load

    ; Check log for successful load
    if (!WaitForLog("Document loaded successfully", 5000)) {
        LogMessage("WARNING: Did not detect document load confirmation")
    }

    ; Run individual tests
    TestClickDialer(hwnd)
    TestClickMessages(hwnd)
    TestClickContacts(hwnd)
    TestClickSettings(hwnd)

    ; Clean up
    LogMessage("Closing designer...")
    CloseDesigner(hwnd)
    Sleep(500)

    ; Report results
    LogMessage("========================================")
    LogMessage("Navigation Tests Complete")
    LogMessage("Passed: " . testsPassed)
    LogMessage("Failed: " . testsFailed)
    LogMessage("========================================")

    return (testsFailed = 0)
}

; Test: Click on Phone/Dialer app
TestClickDialer(hwnd) {
    global testsPassed, testsFailed

    LogMessage("--- Test: Click Dialer ---")

    ; First go home to ensure clean state
    ; (In future, could send Home key)
    Sleep(500)

    ; Click the phone app
    if (!ClickApp("phone", hwnd)) {
        testsFailed++
        return
    }

    Sleep(1000)

    ; Check log for navigation
    ; Note: The log might say "dialer" because that's the screen name
    result := CheckLogFor("Navigated to: dialer")
    if (result) {
        LogMessage("Navigation confirmed: " . result)
        testsPassed++
    } else {
        ; Also check if screen was loaded
        result := CheckLogFor("Loaded screen: apps/dialer")
        if (result) {
            LogMessage("Screen load confirmed: " . result)
            testsPassed++
        } else {
            LogMessage("Navigation not detected in log")
            testsFailed++
        }
    }
}

; Test: Click on Messages app
TestClickMessages(hwnd) {
    global testsPassed, testsFailed

    LogMessage("--- Test: Click Messages ---")

    ; We're likely on dialer now, need to go back first
    ; For now, just click messages dock item
    Sleep(500)

    if (!ClickDock("messages", hwnd)) {
        testsFailed++
        return
    }

    Sleep(1000)

    result := CheckLogFor("Loaded screen: apps/messages")
    if (result) {
        LogMessage("Screen load confirmed: " . result)
        testsPassed++
    } else {
        LogMessage("Navigation not detected in log")
        testsFailed++
    }
}

; Test: Click on Contacts app
TestClickContacts(hwnd) {
    global testsPassed, testsFailed

    LogMessage("--- Test: Click Contacts ---")
    Sleep(500)

    if (!ClickDock("contacts", hwnd)) {
        testsFailed++
        return
    }

    Sleep(1000)

    result := CheckLogFor("Loaded screen: apps/contacts")
    if (result) {
        LogMessage("Screen load confirmed: " . result)
        testsPassed++
    } else {
        LogMessage("Navigation not detected in log")
        testsFailed++
    }
}

; Test: Click on Settings app
TestClickSettings(hwnd) {
    global testsPassed, testsFailed

    LogMessage("--- Test: Click Settings ---")
    Sleep(500)

    ; Settings is on the main grid, not dock
    ; But we're on contacts now, so we need to navigate
    ; For this test, we'll just verify the click mechanics work

    if (!ClickApp("settings", hwnd)) {
        ; This will likely fail since we're not on home screen
        ; But let's see what happens
        LogMessage("Click attempted (may fail if not on home)")
        testsFailed++
        return
    }

    Sleep(1000)

    result := CheckLogFor("Loaded screen: apps/settings")
    if (result) {
        LogMessage("Screen load confirmed: " . result)
        testsPassed++
    } else {
        LogMessage("Navigation not detected (expected if not on home screen)")
        testsFailed++
    }
}

; Run the tests
RunNavigationTests()

; Keep script alive briefly for logging
Sleep(1000)
ExitApp()
