Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

5.3 KB
Raw Blame
package handler

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"ragflow/internal/common"
	"ragflow/internal/entity"
	"ragflow/internal/permission"
	"ragflow/internal/service"

	"github.com/gin-gonic/gin"
)

func TestChatSessionHandlerUpdateMessageFeedback_RejectsEmptyBody(t *testing.T) {
	gin.SetMode(gin.TestMode)

	recorder := httptest.NewRecorder()
	ctx, _ := gin.CreateTestContext(recorder)
	ctx.Request = httptest.NewRequest(http.MethodPut, "/api/v1/chats/chat-1/sessions/session-1/messages/msg-1/feedback", nil)
	ctx.Params = gin.Params{
		{Key: "chat_id", Value: "chat-1"},
		{Key: "session_id", Value: "session-1"},
		{Key: "msg_id", Value: "msg-1"},
	}
	ctx.Set("user", &entity.User{ID: "user-1"})

	handler := NewChatSessionHandler(service.NewChatSessionService(), nil)
	handler.UpdateMessageFeedback(ctx)

	if recorder.Code != http.StatusOK {
		t.Fatalf("status=%d", recorder.Code)
	}

	var body map[string]interface{}
	if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode response body: %v", err)
	}
	if got := body["code"]; got != float64(common.CodeArgumentError) {
		t.Fatalf("code=%v", got)
	}
	if got := body["message"]; got != "Request body cannot be empty" {
		t.Fatalf("message=%v", got)
	}
}

func TestChatSessionHandlerUpdateMessageFeedback_RejectsEmptyJSONObject(t *testing.T) {
	gin.SetMode(gin.TestMode)

	recorder := httptest.NewRecorder()
	ctx, _ := gin.CreateTestContext(recorder)
	ctx.Request = httptest.NewRequest(http.MethodPut, "/api/v1/chats/chat-1/sessions/session-1/messages/msg-1/feedback", strings.NewReader(`{}`))
	ctx.Request.Header.Set("Content-Type", "application/json")
	ctx.Params = gin.Params{
		{Key: "chat_id", Value: "chat-1"},
		{Key: "session_id", Value: "session-1"},
		{Key: "msg_id", Value: "msg-1"},
	}
	ctx.Set("user", &entity.User{ID: "user-1"})

	handler := NewChatSessionHandler(service.NewChatSessionService(), nil)
	handler.UpdateMessageFeedback(ctx)

	var body map[string]interface{}
	if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode response body: %v", err)
	}
	if got := body["code"]; got != float64(common.CodeArgumentError) {
		t.Fatalf("code=%v", got)
	}
	if got := body["message"]; got != "Request body cannot be empty" {
		t.Fatalf("message=%v", got)
	}
}

func TestChatSessionHandlerChatCompletionsRejectsNonPositiveMaxTokens(t *testing.T) {
	gin.SetMode(gin.TestMode)
	for _, value := range []string{"true", `"false"`, "null", "0", "{}"} {
		t.Run("store_history_messages="+value, func(t *testing.T) {
			recorder := httptest.NewRecorder()
			ctx, _ := gin.CreateTestContext(recorder)
			ctx.Request = httptest.NewRequest(http.MethodPost, "/api/v1/chat/completions", strings.NewReader(`{"store_history_messages":`+value+`,"messages":[{"role":"user","content":"hi"}]}`))
			ctx.Request.Header.Set("Content-Type", "application/json")
			ctx.Set("user", &entity.User{ID: "user-1"})
			handler := NewChatSessionHandler(service.NewChatSessionService(), nil)
			handler.ChatCompletions(ctx)
			var body map[string]interface{}
			if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
				t.Fatalf("decode response body: %v", err)
			}
			if body["code"] != float64(common.CodeBadRequest) || body["message"] != "`store_history_messages` only supports false." {
				t.Fatalf("unexpected response: %#v", body)
			}
		})
	}

	recorder := httptest.NewRecorder()
	ctx, _ := gin.CreateTestContext(recorder)
	ctx.Request = httptest.NewRequest(http.MethodPost, "/api/v1/chat/completions", strings.NewReader(`{
		"messages":[{"role":"user","content":"hi"}],
		"max_tokens":0
	}`))
	ctx.Request.Header.Set("Content-Type", "application/json")
	ctx.Set("user", &entity.User{ID: "user-1"})

	handler := NewChatSessionHandler(service.NewChatSessionService(), nil)
	handler.ChatCompletions(ctx)

	var body map[string]interface{}
	if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode response body: %v", err)
	}
	if got := body["code"]; got != float64(common.CodeBadRequest) {
		t.Fatalf("code=%v", got)
	}
	if got := body["message"]; got != "`max_tokens` must be greater than 0." {
		t.Fatalf("message=%v", got)
	}
}

func TestChatSessionHandlerChatCompletionsRejectsNonStringUserContent(t *testing.T) {
	gin.SetMode(gin.TestMode)
	recorder := httptest.NewRecorder()
	ctx, _ := gin.CreateTestContext(recorder)
	ctx.Request = httptest.NewRequest(http.MethodPost, "/api/v1/chat/completions", strings.NewReader(`{"stream":false,"store_history_messages":false,"messages":[{"role":"user","content":{"type":"text","text":"probe"}}]}`))
	ctx.Request.Header.Set("Content-Type", "application/json")
	ctx.Set("user", &entity.User{ID: "user-1"})
	ctx.Request = ctx.Request.WithContext(permission.WithIdentity(ctx.Request.Context(), "user-1", ""))

	handler := NewChatSessionHandler(service.NewChatSessionService(), nil)
	handler.ChatCompletions(ctx)

	var body map[string]interface{}
	if err := json.Unmarshal(recorder.Body.Bytes(), &body); err != nil {
		t.Fatalf("decode response body: %v", err)
	}
	if got := body["code"]; got != float64(common.CodeArgumentError) {
		t.Fatalf("code=%v, want %d", got, common.CodeArgumentError)
	}
	if got, _ := body["message"].(string); got != "messages[].content must be a string or an array of content parts." {
		t.Fatalf("message=%q, want non-string content validation error", got)
	}
}