package knowledge_base

import (
	"ai_scheduler/internal/config"
	"bufio"
	"strings"
	"testing"
)

func TestCall(t *testing.T) {
	req := &QueryRequest{
		TenantID: "admin_test_qa",
		Query:    "lightRAG 的优势?",
		Mode:     "naive",
		Stream:   true,
		Think:    false,
		OnlyRAG:  true,
	}

	client := New(config.KnowledgeConfig{BaseURL: "http://127.0.0.1:9600"})
	resp, err := client.Query(req)
	if err != nil {
		t.Errorf("Call failed: %v", err)
	}
	if resp == nil {
		t.Error("Response is nil")
	}
	defer resp.Close()

	scanner := bufio.NewScanner(resp)
	var outThinking strings.Builder
	var outContent strings.Builder
	for scanner.Scan() {
		line := scanner.Text()
		delta, done, err := ParseOpenAIStreamData(line)
		if err != nil {
			t.Fatalf("parse openai stream failed: %v", err)
		}
		if delta == nil {
			continue
		}
		if done {
			break
		}

		if delta.XRagStatus != "" {
			t.Logf("XRagStatus: %s", delta.XRagStatus)
		}
		if delta.Content != "" {
			outContent.WriteString(delta.Content)
		}
		if delta.ReasoningContent != "" {
			outThinking.WriteString(delta.ReasoningContent)
		}

	}
	if err := scanner.Err(); err != nil {
		t.Fatalf("scan failed: %v", err)
	}

	t.Logf("Thinking: %s", outThinking.String())
	t.Logf("Content: %s", outContent.String())
}
