mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-12 16:01:27 +08:00
fix: examples
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v5/internal/builtin"
|
"github.com/httprunner/httprunner/v5/internal/builtin"
|
||||||
"github.com/httprunner/httprunner/v5/internal/config"
|
"github.com/httprunner/httprunner/v5/internal/config"
|
||||||
"github.com/httprunner/httprunner/v5/uixt"
|
"github.com/httprunner/httprunner/v5/uixt"
|
||||||
"github.com/httprunner/httprunner/v5/uixt/ai"
|
|
||||||
"github.com/httprunner/httprunner/v5/uixt/option"
|
"github.com/httprunner/httprunner/v5/uixt/option"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
@@ -124,14 +123,19 @@ func (bot *LLKGameBot) AnalyzeGameInterface() (*GameElement, error) {
|
|||||||
return gameElement, nil
|
return gameElement, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertToGameElement converts AI query result to GameElement
|
// convertToGameElement converts AI execution result to GameElement
|
||||||
func convertToGameElement(result *ai.QueryResult) (*GameElement, error) {
|
func convertToGameElement(result *uixt.AIExecutionResult) (*GameElement, error) {
|
||||||
if result == nil {
|
if result == nil {
|
||||||
return nil, fmt.Errorf("query result is nil")
|
return nil, fmt.Errorf("AI execution result is nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if result.QueryResult == nil {
|
||||||
|
return nil, fmt.Errorf("query result is nil in AI execution result")
|
||||||
|
}
|
||||||
|
queryResult := result.QueryResult
|
||||||
|
|
||||||
// Try direct conversion first
|
// Try direct conversion first
|
||||||
if gameElement, ok := result.Data.(*GameElement); ok {
|
if gameElement, ok := queryResult.Data.(*GameElement); ok {
|
||||||
return gameElement, nil
|
return gameElement, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -140,11 +144,11 @@ func convertToGameElement(result *ai.QueryResult) (*GameElement, error) {
|
|||||||
var sourceData interface{}
|
var sourceData interface{}
|
||||||
|
|
||||||
// Use Data if available, otherwise try Content
|
// Use Data if available, otherwise try Content
|
||||||
if result.Data != nil {
|
if queryResult.Data != nil {
|
||||||
sourceData = result.Data
|
sourceData = queryResult.Data
|
||||||
} else if result.Content != "" {
|
} else if queryResult.Content != "" {
|
||||||
var contentData map[string]interface{}
|
var contentData map[string]interface{}
|
||||||
if err := json.Unmarshal([]byte(result.Content), &contentData); err != nil {
|
if err := json.Unmarshal([]byte(queryResult.Content), &contentData); err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse JSON from Content: %w", err)
|
return nil, fmt.Errorf("failed to parse JSON from Content: %w", err)
|
||||||
}
|
}
|
||||||
sourceData = contentData
|
sourceData = contentData
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package llk
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -13,6 +15,47 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// convertToGameElementFromQueryResult converts AI query result to GameElement for testing
|
||||||
|
func convertToGameElementFromQueryResult(result *ai.QueryResult) (*GameElement, error) {
|
||||||
|
if result == nil {
|
||||||
|
return nil, fmt.Errorf("query result is nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try direct conversion first
|
||||||
|
if gameElement, ok := result.Data.(*GameElement); ok {
|
||||||
|
return gameElement, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert to JSON and back for flexible parsing
|
||||||
|
var gameElement GameElement
|
||||||
|
var sourceData interface{}
|
||||||
|
|
||||||
|
// Use Data if available, otherwise try Content
|
||||||
|
if result.Data != nil {
|
||||||
|
sourceData = result.Data
|
||||||
|
} else if result.Content != "" {
|
||||||
|
var contentData map[string]interface{}
|
||||||
|
if err := json.Unmarshal([]byte(result.Content), &contentData); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse JSON from Content: %w", err)
|
||||||
|
}
|
||||||
|
sourceData = contentData
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("no data available in query result")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert via JSON marshaling/unmarshaling
|
||||||
|
jsonBytes, err := json.Marshal(sourceData)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to marshal result data: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(jsonBytes, &gameElement); err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to unmarshal to GameElement: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &gameElement, nil
|
||||||
|
}
|
||||||
|
|
||||||
// hasRequiredEnvVars checks if the required environment variables are set for testing
|
// hasRequiredEnvVars checks if the required environment variables are set for testing
|
||||||
func hasRequiredEnvVars() bool {
|
func hasRequiredEnvVars() bool {
|
||||||
// Check for OpenAI environment variables
|
// Check for OpenAI environment variables
|
||||||
@@ -83,7 +126,7 @@ Return JSON with: content, dimensions{rows,cols}, elements[{type,position{row,co
|
|||||||
require.NoError(t, err, "Failed to query AI model")
|
require.NoError(t, err, "Failed to query AI model")
|
||||||
|
|
||||||
// Convert result using enhanced compatibility logic
|
// Convert result using enhanced compatibility logic
|
||||||
gameElement, err := convertToGameElement(result)
|
gameElement, err := convertToGameElementFromQueryResult(result)
|
||||||
require.NoError(t, err, "Failed to convert query result to GameElement")
|
require.NoError(t, err, "Failed to convert query result to GameElement")
|
||||||
require.NotNil(t, gameElement, "GameElement should not be nil")
|
require.NotNil(t, gameElement, "GameElement should not be nil")
|
||||||
|
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
v5.0.0-beta-2506241525
|
v5.0.0-beta-2506241536
|
||||||
|
|||||||
Reference in New Issue
Block a user