ScreenInfrastructureTest.groovy 23.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
/*
 * This software is in the public domain under CC0 1.0 Universal plus a
 * Grant of Patent License.
 *
 * To the extent possible under law, the author(s) have dedicated all
 * copyright and related and neighboring rights to this software to the
 * public domain worldwide. This software is distributed without any
 * warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication
 * along with this software (see the LICENSE.md file). If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
 */

import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
import java.util.concurrent.TimeUnit

/**
 * Screen Infrastructure Test for MCP
 * 
 * Tests screen-based functionality following Moqui patterns:
 * - Screen discovery and navigation
 * - Form-list and form-single execution
 * - Transition testing
 * - Parameter handling
 * - Subscreen navigation
 * - Security and permissions
 */
class ScreenInfrastructureTest {
    
    static void main(String[] args) {
        def test = new ScreenInfrastructureTest()
        test.runAllTests()
    }
    
    def jsonSlurper = new JsonSlurper()
    def testResults = [:]
    def startTime = System.currentTimeMillis()
    
    void runAllTests() {
        println "🖥️  Screen Infrastructure Test for MCP"
        println "=================================="
        
        try {
            // Initialize MCP session
            def sessionId = initializeSession()
            
            // Run screen infrastructure tests
            testScreenDiscovery(sessionId)
            testScreenNavigation(sessionId)
            testFormListExecution(sessionId)
            testFormSingleExecution(sessionId)
            testTransitionExecution(sessionId)
            testParameterHandling(sessionId)
            testSubscreenNavigation(sessionId)
            testScreenSecurity(sessionId)
            
            // Generate report
            generateReport()
            
        } catch (Exception e) {
            println "❌ Test failed with exception: ${e.message}"
            e.printStackTrace()
        }
    }
    
    String initializeSession() {
        println "\n🚀 Initializing MCP session for screen test..."
        
        def initResult = callMcpService("org.moqui.mcp.McpTestServices.initialize#Session", [:])
        if (initResult?.sessionId) {
            println "✅ Session initialized: ${initResult.sessionId}"
            return initResult.sessionId
        } else {
            throw new RuntimeException("Failed to initialize session")
        }
    }
    
    void testScreenDiscovery(String sessionId) {
        println "\n🔍 Test 1: Screen Discovery"
        println "============================="
        
        try {
            // Test tool discovery for screen-related tools
            def tools = callMcpService("org.moqui.mcp.McpServices.get#AvailableTools", [:])
            def screenTools = tools?.tools?.findAll { it.name?.contains('screen') || it.name?.contains('Screen') }
            
            if (screenTools && screenTools.size() > 0) {
                println "✅ Found ${screenTools.size()} screen-related tools"
                screenTools.each { tool ->
                    println "   - ${tool.name}: ${tool.description}"
                }
                testResults.screenDiscovery = true
            } else {
                println "❌ No screen tools found"
                testResults.screenDiscovery = false
            }
            
            // Test screen path resolution
            def screenPaths = [
                "SimpleScreens/Order/FindOrder",
                "SimpleScreens/Catalog/Product", 
                "SimpleScreens/Customer/FindCustomer",
                "PopCommerceAdmin/Catalog"
            ]
            
            def validScreens = []
            screenPaths.each { path ->
                try {
                    def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                        screenPath: path,
                        parameters: [:]
                    ])
                    if (result && !result.error) {
                        validScreens << path
                        println "   ✅ Screen accessible: ${path}"
                    }
                } catch (Exception e) {
                    println "   ⚠️  Screen not accessible: ${path} - ${e.message}"
                }
            }
            
            testResults.screenDiscoveryValid = validScreens.size() > 0
            println "✅ Valid screens found: ${validScreens.size()}/${screenPaths.size()}"
            
        } catch (Exception e) {
            println "❌ Screen discovery test failed: ${e.message}"
            testResults.screenDiscovery = false
        }
    }
    
    void testScreenNavigation(String sessionId) {
        println "\n🧭 Test 2: Screen Navigation"
        println "=============================="
        
        try {
            // Test navigation to known screens
            def navigationTests = [
                [
                    name: "Order Find Screen",
                    path: "SimpleScreens/Order/FindOrder",
                    expectedElements: ["OrderList", "CreateSalesOrderDialog"]
                ],
                [
                    name: "Product Catalog", 
                    path: "SimpleScreens/Catalog/Product",
                    expectedElements: ["ProductList", "CreateProductDialog"]
                ],
                [
                    name: "Customer Find",
                    path: "SimpleScreens/Customer/FindCustomer", 
                    expectedElements: ["CustomerList", "CreateCustomerDialog"]
                ]
            ]
            
            def passedTests = 0
            navigationTests.each { test ->
                try {
                    def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                        screenPath: test.path,
                        parameters: [:]
                    ])
                    
                    if (result && !result.error) {
                        def foundElements = 0
                        test.expectedElements.each { element ->
                            if (result.content?.toString()?.contains(element)) {
                                foundElements++
                            }
                        }
                        
                        if (foundElements > 0) {
                            println "   ✅ ${test.name}: ${foundElements}/${test.expectedElements.size()} elements found"
                            passedTests++
                        } else {
                            println "   ⚠️  ${test.name}: No expected elements found"
                        }
                    } else {
                        println "   ❌ ${test.name}: ${result?.error ?: 'Unknown error'}"
                    }
                } catch (Exception e) {
                    println "   ❌ ${test.name}: ${e.message}"
                }
            }
            
            testResults.screenNavigation = passedTests > 0
            println "✅ Navigation tests passed: ${passedTests}/${navigationTests.size()}"
            
        } catch (Exception e) {
            println "❌ Screen navigation test failed: ${e.message}"
            testResults.screenNavigation = false
        }
    }
    
    void testFormListExecution(String sessionId) {
        println "\n📋 Test 3: Form-List Execution"
        println "================================="
        
        try {
            // Test form-list with search parameters
            def formListTests = [
                [
                    name: "Order List Search",
                    screenPath: "SimpleScreens/Order/FindOrder",
                    transition: "actions",
                    parameters: [
                        orderId: "",
                        partStatusId: "OrderPlaced,OrderApproved",
                        entryDate_poffset: "-7",
                        entryDate_period: "d"
                    ]
                ],
                [
                    name: "Product List Search",
                    screenPath: "SimpleScreens/Catalog/Product", 
                    transition: "actions",
                    parameters: [
                        productName: "",
                        productCategoryId: ""
                    ]
                ]
            ]
            
            def passedTests = 0
            formListTests.each { test ->
                try {
                    def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                        screenPath: test.screenPath,
                        transition: test.transition,
                        parameters: test.parameters
                    ])
                    
                    if (result && !result.error) {
                        // Check if we got list data back
                        if (result.content || result.data || result.list) {
                            println "   ✅ ${test.name}: Form-list executed successfully"
                            passedTests++
                        } else {
                            println "   ⚠️  ${test.name}: No list data returned"
                        }
                    } else {
                        println "   ❌ ${test.name}: ${result?.error ?: 'Unknown error'}"
                    }
                } catch (Exception e) {
                    println "   ❌ ${test.name}: ${e.message}"
                }
            }
            
            testResults.formListExecution = passedTests > 0
            println "✅ Form-list tests passed: ${passedTests}/${formListTests.size()}"
            
        } catch (Exception e) {
            println "❌ Form-list execution test failed: ${e.message}"
            testResults.formListExecution = false
        }
    }
    
    void testFormSingleExecution(String sessionId) {
        println "\n📝 Test 4: Form-Single Execution"
        println "==================================="
        
        try {
            // Test form-single for data creation
            def formSingleTests = [
                [
                    name: "Create Product Form",
                    screenPath: "SimpleScreens/Catalog/Product",
                    transition: "createProduct",
                    parameters: [
                        productName: "TEST-SCREEN-PRODUCT-${System.currentTimeMillis()}",
                        productTypeId: "FinishedGood",
                        internalName: "Test Screen Product"
                    ]
                ],
                [
                    name: "Create Customer Form", 
                    screenPath: "SimpleScreens/Customer/FindCustomer",
                    transition: "createCustomer",
                    parameters: [
                        firstName: "Test",
                        lastName: "Screen",
                        partyTypeEnumId: "Person"
                    ]
                ]
            ]
            
            def passedTests = 0
            formSingleTests.each { test ->
                try {
                    def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                        screenPath: test.screenPath,
                        transition: test.transition,
                        parameters: test.parameters
                    ])
                    
                    if (result && !result.error) {
                        if (result.productId || result.partyId || result.success) {
                            println "   ✅ ${test.name}: Form-single executed successfully"
                            passedTests++
                        } else {
                            println "   ⚠️  ${test.name}: No confirmation returned"
                        }
                    } else {
                        println "   ❌ ${test.name}: ${result?.error ?: 'Unknown error'}"
                    }
                } catch (Exception e) {
                    println "   ❌ ${test.name}: ${e.message}"
                }
            }
            
            testResults.formSingleExecution = passedTests > 0
            println "✅ Form-single tests passed: ${passedTests}/${formSingleTests.size()}"
            
        } catch (Exception e) {
            println "❌ Form-single execution test failed: ${e.message}"
            testResults.formSingleExecution = false
        }
    }
    
    void testTransitionExecution(String sessionId) {
        println "\n🔄 Test 5: Transition Execution"
        println "================================="
        
        try {
            // Test specific transitions
            def transitionTests = [
                [
                    name: "Order Detail Transition",
                    screenPath: "SimpleScreens/Order/FindOrder",
                    transition: "orderDetail",
                    parameters: [orderId: "TEST-ORDER"]
                ],
                [
                    name: "Edit Party Transition",
                    screenPath: "SimpleScreens/Customer/FindCustomer", 
                    transition: "editParty",
                    parameters: [partyId: "TEST-PARTY"]
                ]
            ]
            
            def passedTests = 0
            transitionTests.each { test ->
                try {
                    def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                        screenPath: test.screenPath,
                        transition: test.transition,
                        parameters: test.parameters
                    ])
                    
                    // Transitions might redirect or return URLs
                    if (result && (!result.error || result.url || result.redirect)) {
                        println "   ✅ ${test.name}: Transition executed"
                        passedTests++
                    } else {
                        println "   ⚠️  ${test.name}: ${result?.error ?: 'No clear result'}"
                    }
                } catch (Exception e) {
                    println "   ❌ ${test.name}: ${e.message}"
                }
            }
            
            testResults.transitionExecution = passedTests > 0
            println "✅ Transition tests passed: ${passedTests}/${transitionTests.size()}"
            
        } catch (Exception e) {
            println "❌ Transition execution test failed: ${e.message}"
            testResults.transitionExecution = false
        }
    }
    
    void testParameterHandling(String sessionId) {
        println "\n📊 Test 6: Parameter Handling"
        println "================================"
        
        try {
            // Test parameter passing and validation
            def parameterTests = [
                [
                    name: "Search Parameters",
                    screenPath: "SimpleScreens/Order/FindOrder",
                    parameters: [
                        orderId: "TEST%",
                        partStatusId: "OrderPlaced",
                        entryDate_poffset: "-1",
                        entryDate_period: "d"
                    ]
                ],
                [
                    name: "Date Range Parameters",
                    screenPath: "SimpleScreens/Order/FindOrder", 
                    parameters: [
                        entryDate_from: "2024-01-01",
                        entryDate_thru: "2024-12-31"
                    ]
                ],
                [
                    name: "Dropdown Parameters",
                    screenPath: "SimpleScreens/Order/FindOrder",
                    parameters: [
                        orderType: "Sales",
                        salesChannelEnumId: "ScWebStore"
                    ]
                ]
            ]
            
            def passedTests = 0
            parameterTests.each { test ->
                try {
                    def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                        screenPath: test.screenPath,
                        transition: "actions",
                        parameters: test.parameters
                    ])
                    
                    if (result && !result.error) {
                        println "   ✅ ${test.name}: Parameters handled correctly"
                        passedTests++
                    } else {
                        println "   ❌ ${test.name}: ${result?.error ?: 'Parameter handling failed'}"
                    }
                } catch (Exception e) {
                    println "   ❌ ${test.name}: ${e.message}"
                }
            }
            
            testResults.parameterHandling = passedTests > 0
            println "✅ Parameter tests passed: ${passedTests}/${parameterTests.size()}"
            
        } catch (Exception e) {
            println "❌ Parameter handling test failed: ${e.message}"
            testResults.parameterHandling = false
        }
    }
    
    void testSubscreenNavigation(String sessionId) {
        println "\n🗂️  Test 7: Subscreen Navigation"
        println "================================="
        
        try {
            // Test subscreen navigation
            def subscreenTests = [
                [
                    name: "Order Subscreens",
                    basePath: "SimpleScreens/Order",
                    subscreens: ["FindOrder", "OrderDetail", "QuickItems"]
                ],
                [
                    name: "Catalog Subscreens",
                    basePath: "SimpleScreens/Catalog", 
                    subscreens: ["Product", "Category", "Search"]
                ],
                [
                    name: "Customer Subscreens",
                    basePath: "SimpleScreens/Customer",
                    subscreens: ["FindCustomer", "EditCustomer", "CustomerData"]
                ]
            ]
            
            def passedTests = 0
            subscreenTests.each { test ->
                def accessibleSubscreens = 0
                test.subscreens.each { subscreen ->
                    try {
                        def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                            screenPath: "${test.basePath}/${subscreen}",
                            parameters: [:]
                        ])
                        
                        if (result && !result.error) {
                            accessibleSubscreens++
                        }
                    } catch (Exception e) {
                        // Expected for some subscreens
                    }
                }
                
                if (accessibleSubscreens > 0) {
                    println "   ✅ ${test.name}: ${accessibleSubscreens}/${test.subscreens.size()} subscreens accessible"
                    passedTests++
                } else {
                    println "   ❌ ${test.name}: No accessible subscreens"
                }
            }
            
            testResults.subscreenNavigation = passedTests > 0
            println "✅ Subscreen tests passed: ${passedTests}/${subscreenTests.size()}"
            
        } catch (Exception e) {
            println "❌ Subscreen navigation test failed: ${e.message}"
            testResults.subscreenNavigation = false
        }
    }
    
    void testScreenSecurity(String sessionId) {
        println "\n🔒 Test 8: Screen Security"
        println "============================"
        
        try {
            // Test security and permissions
            def securityTests = [
                [
                    name: "Admin Screen Access",
                    screenPath: "SimpleScreens/Accounting/Invoice",
                    expectAccess: false  // Should require admin permissions
                ],
                [
                    name: "Public Screen Access",
                    screenPath: "SimpleScreens/Order/FindOrder", 
                    expectAccess: true   // Should be accessible
                ],
                [
                    name: "User Screen Access",
                    screenPath: "MyAccount/User/Account",
                    expectAccess: true   // Should be accessible to authenticated user
                ]
            ]
            
            def passedTests = 0
            securityTests.each { test ->
                try {
                    def result = callMcpService("org.moqui.mcp.McpServices.execute#Screen", [
                        screenPath: test.screenPath,
                        parameters: [:]
                    ])
                    
                    def hasAccess = result && !result.error
                    def testPassed = (hasAccess == test.expectAccess)
                    
                    if (testPassed) {
                        println "   ✅ ${test.name}: Access ${hasAccess ? 'granted' : 'denied'} as expected"
                        passedTests++
                    } else {
                        println "   ⚠️  ${test.name}: Access ${hasAccess ? 'granted' : 'denied'} (expected ${test.expectAccess ? 'granted' : 'denied'})"
                    }
                } catch (Exception e) {
                    def accessDenied = e.message?.contains('access') || e.message?.contains('permission') || e.message?.contains('authorized')
                    def testPassed = (!accessDenied == test.expectAccess)
                    
                    if (testPassed) {
                        println "   ✅ ${test.name}: Security working as expected"
                        passedTests++
                    } else {
                        println "   ❌ ${test.name}: Unexpected security behavior: ${e.message}"
                    }
                }
            }
            
            testResults.screenSecurity = passedTests > 0
            println "✅ Security tests passed: ${passedTests}/${securityTests.size()}"
            
        } catch (Exception e) {
            println "❌ Screen security test failed: ${e.message}"
            testResults.screenSecurity = false
        }
    }
    
    def callMcpService(String serviceName, Map parameters) {
        try {
            def url = "http://localhost:8080/rest/s1/org/moqui/mcp/McpTestServices/${serviceName.split('\\.')[2]}"
            def connection = url.toURL().openConnection()
            connection.setRequestMethod("POST")
            connection.setRequestProperty("Content-Type", "application/json")
            connection.setRequestProperty("Authorization", "Basic ${"john.sales:opencode".bytes.encodeBase64()}")
            connection.doOutput = true
            
            def json = new JsonBuilder(parameters).toString()
            connection.outputStream.write(json.bytes)
            
            def response = connection.inputStream.text
            return jsonSlurper.parseText(response)
        } catch (Exception e) {
            // println "Error calling ${serviceName}: ${e.message}"
            return null
        }
    }
    
    void generateReport() {
        def duration = System.currentTimeMillis() - startTime
        
        println "\n" + "=".repeat(60)
        println "📋 SCREEN INFRASTRUCTURE TEST REPORT"
        println "=".repeat(60)
        println "Duration: ${duration}ms"
        println ""
        
        def totalTests = testResults.size()
        def passedTests = testResults.values().count { it == true }
        
        testResults.each { test, result ->
            def status = result ? "✅" : "❌"
            println "${status} ${test}"
        }
        
        println ""
        println "Overall Result: ${passedTests}/${totalTests} tests passed"
        println "Success Rate: ${Math.round((passedTests / totalTests) * 100)}%"
        
        if (passedTests == totalTests) {
            println "🎉 ALL SCREEN INFRASTRUCTURE TESTS PASSED!"
            println "MCP screen integration is working correctly."
        } else {
            println "⚠️  Some tests failed. Review the results above."
        }
        
        println "=".repeat(60)
    }
}