Merge pull request #1285 from trheyi/main

Refactor response handling in GetAssistant and ListAssistantTags func…
This commit is contained in:
Max 2025-11-08 15:30:28 +08:00 committed by GitHub
commit 3e93546372
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 89 deletions

View file

@ -238,9 +238,7 @@ func GetAssistant(c *gin.Context) {
FilterBuiltInAssistant(assistant) FilterBuiltInAssistant(assistant)
// Return the result with standard response format // Return the result with standard response format
response.RespondWithSuccess(c, response.StatusOK, map[string]interface{}{ response.RespondWithSuccess(c, response.StatusOK, assistant)
"data": assistant,
})
} }
// ListAssistantTags lists assistant tags with permission-based filtering // ListAssistantTags lists assistant tags with permission-based filtering
@ -312,9 +310,7 @@ func ListAssistantTags(c *gin.Context) {
} }
// Return the result with standard response format // Return the result with standard response format
response.RespondWithSuccess(c, response.StatusOK, map[string]interface{}{ response.RespondWithSuccess(c, response.StatusOK, tags)
"data": tags,
})
} }
// checkAssistantPermission checks if the user has permission to access the assistant // checkAssistantPermission checks if the user has permission to access the assistant

View file

@ -747,21 +747,18 @@ func TestListAssistantTags(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode, "Should successfully retrieve tags") assert.Equal(t, http.StatusOK, resp.StatusCode, "Should successfully retrieve tags")
var response map[string]interface{} var tags []interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&tags)
assert.NoError(t, err) assert.NoError(t, err)
data, hasData := response["data"].([]interface{}) t.Logf("Successfully retrieved %d tags", len(tags))
if hasData {
t.Logf("Successfully retrieved %d tags", len(data))
// Verify tag structure // Verify tag structure
if len(data) > 0 { if len(tags) > 0 {
tag, ok := data[0].(map[string]interface{}) tag, ok := tags[0].(map[string]interface{})
if ok { if ok {
assert.Contains(t, tag, "value", "Tag should have value field") assert.Contains(t, tag, "value", "Tag should have value field")
assert.Contains(t, tag, "label", "Tag should have label field") assert.Contains(t, tag, "label", "Tag should have label field")
}
} }
} }
}) })
@ -779,14 +776,11 @@ func TestListAssistantTags(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var tags []interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&tags)
assert.NoError(t, err) assert.NoError(t, err)
data, hasData := response["data"].([]interface{}) t.Logf("Successfully retrieved %d tags with zh-cn locale", len(tags))
if hasData {
t.Logf("Successfully retrieved %d tags with zh-cn locale", len(data))
}
}) })
t.Run("ListAssistantTagsWithFilters", func(t *testing.T) { t.Run("ListAssistantTagsWithFilters", func(t *testing.T) {
@ -802,14 +796,11 @@ func TestListAssistantTags(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var tags []interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&tags)
assert.NoError(t, err) assert.NoError(t, err)
data, hasData := response["data"].([]interface{}) t.Logf("Successfully retrieved %d tags with type filter", len(tags))
if hasData {
t.Logf("Successfully retrieved %d tags with type filter", len(data))
}
}) })
t.Run("ListAssistantTagsWithConnector", func(t *testing.T) { t.Run("ListAssistantTagsWithConnector", func(t *testing.T) {
@ -825,14 +816,11 @@ func TestListAssistantTags(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var tags []interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&tags)
assert.NoError(t, err) assert.NoError(t, err)
data, hasData := response["data"].([]interface{}) t.Logf("Successfully retrieved %d tags for openai connector", len(tags))
if hasData {
t.Logf("Successfully retrieved %d tags for openai connector", len(data))
}
}) })
t.Run("ListAssistantTagsWithBuiltInFilter", func(t *testing.T) { t.Run("ListAssistantTagsWithBuiltInFilter", func(t *testing.T) {
@ -848,14 +836,11 @@ func TestListAssistantTags(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var tags []interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&tags)
assert.NoError(t, err) assert.NoError(t, err)
data, hasData := response["data"].([]interface{}) t.Logf("Successfully retrieved %d tags for non-built-in assistants", len(tags))
if hasData {
t.Logf("Successfully retrieved %d tags for non-built-in assistants", len(data))
}
}) })
t.Run("ListAssistantTagsWithMentionableFilter", func(t *testing.T) { t.Run("ListAssistantTagsWithMentionableFilter", func(t *testing.T) {
@ -871,14 +856,11 @@ func TestListAssistantTags(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var tags []interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&tags)
assert.NoError(t, err) assert.NoError(t, err)
data, hasData := response["data"].([]interface{}) t.Logf("Successfully retrieved %d tags for mentionable assistants", len(tags))
if hasData {
t.Logf("Successfully retrieved %d tags for mentionable assistants", len(data))
}
}) })
t.Run("ListAssistantTagsWithKeywords", func(t *testing.T) { t.Run("ListAssistantTagsWithKeywords", func(t *testing.T) {
@ -894,14 +876,11 @@ func TestListAssistantTags(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var tags []interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&tags)
assert.NoError(t, err) assert.NoError(t, err)
data, hasData := response["data"].([]interface{}) t.Logf("Successfully retrieved %d tags with keywords filter", len(tags))
if hasData {
t.Logf("Successfully retrieved %d tags with keywords filter", len(data))
}
}) })
t.Run("ListAssistantTagsUnauthorized", func(t *testing.T) { t.Run("ListAssistantTagsUnauthorized", func(t *testing.T) {
@ -985,17 +964,15 @@ func TestGetAssistant(t *testing.T) {
// Expect successful response // Expect successful response
assert.Equal(t, http.StatusOK, resp.StatusCode, "Should successfully retrieve assistant") assert.Equal(t, http.StatusOK, resp.StatusCode, "Should successfully retrieve assistant")
var response map[string]interface{} var assistantDetail map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&assistantDetail)
assert.NoError(t, err) assert.NoError(t, err)
// Response should have data field with assistant details // Response should be the assistant object directly
detailData, hasDetailData := response["data"].(map[string]interface{}) assert.NotNil(t, assistantDetail, "Assistant data should not be nil")
assert.True(t, hasDetailData, "Response should have 'data' field")
assert.NotNil(t, detailData, "Assistant data should not be nil")
// Verify assistant_id matches // Verify assistant_id matches
returnedID, hasID := detailData["assistant_id"].(string) returnedID, hasID := assistantDetail["assistant_id"].(string)
assert.True(t, hasID, "Assistant should have assistant_id field") assert.True(t, hasID, "Assistant should have assistant_id field")
assert.Equal(t, assistantID, returnedID, "Returned assistant_id should match requested ID") assert.Equal(t, assistantID, returnedID, "Returned assistant_id should match requested ID")
@ -1046,12 +1023,11 @@ func TestGetAssistant(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var assistantData map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&assistantData)
assert.NoError(t, err) assert.NoError(t, err)
_, hasDetailData := response["data"].(map[string]interface{}) assert.NotNil(t, assistantData, "Assistant data should not be nil")
assert.True(t, hasDetailData, "Response should have 'data' field")
t.Logf("Successfully retrieved assistant without locale (raw data for editing)") t.Logf("Successfully retrieved assistant without locale (raw data for editing)")
}) })
@ -1151,23 +1127,22 @@ func TestGetAssistant(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var builtInAssistant map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&builtInAssistant)
assert.NoError(t, err) assert.NoError(t, err)
detailData, hasDetailData := response["data"].(map[string]interface{}) assert.NotNil(t, builtInAssistant, "Assistant data should not be nil")
assert.True(t, hasDetailData, "Response should have 'data' field")
// Verify built-in flag // Verify built-in flag
builtIn, hasBuiltIn := detailData["built_in"].(bool) builtIn, hasBuiltIn := builtInAssistant["built_in"].(bool)
if hasBuiltIn && builtIn { if hasBuiltIn && builtIn {
// Check that sensitive fields are filtered // Check that sensitive fields are filtered
prompts := detailData["prompts"] prompts := builtInAssistant["prompts"]
workflow := detailData["workflow"] workflow := builtInAssistant["workflow"]
tools := detailData["tools"] tools := builtInAssistant["tools"]
kb := detailData["kb"] kb := builtInAssistant["kb"]
mcp := detailData["mcp"] mcp := builtInAssistant["mcp"]
options := detailData["options"] options := builtInAssistant["options"]
// These should be nil or absent for built-in assistants // These should be nil or absent for built-in assistants
assert.Nil(t, prompts, "Built-in assistant should not expose prompts") assert.Nil(t, prompts, "Built-in assistant should not expose prompts")
@ -1426,24 +1401,20 @@ func TestGetAssistantResponseStructure(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode) assert.Equal(t, http.StatusOK, resp.StatusCode)
var response map[string]interface{} var responseAssistant map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&response) err = json.NewDecoder(resp.Body).Decode(&responseAssistant)
assert.NoError(t, err) assert.NoError(t, err)
// Verify response structure // Verify response is an object (not wrapped in data field)
assert.Contains(t, response, "data", "Response should have 'data' field") assert.NotNil(t, responseAssistant, "Assistant should not be nil")
// Verify data is an object (not an array)
detailData, ok := response["data"].(map[string]interface{})
assert.True(t, ok, "Data field should be an object")
assert.NotNil(t, detailData, "Data should not be nil")
// Verify essential assistant fields // Verify essential assistant fields
assert.Contains(t, detailData, "assistant_id", "Assistant should have assistant_id") assert.Contains(t, responseAssistant, "assistant_id", "Assistant should have assistant_id")
assert.Contains(t, detailData, "name", "Assistant should have name") assert.Contains(t, responseAssistant, "name", "Assistant should have name")
assert.Contains(t, detailData, "type", "Assistant should have type") assert.Contains(t, responseAssistant, "type", "Assistant should have type")
t.Logf("Response structure is correct for assistant: %s", assistantID) responseAssistantID := responseAssistant["assistant_id"].(string)
t.Logf("Response structure is correct for assistant: %s", responseAssistantID)
}) })
} }