package qa import ( "context" "fmt" "net/http" "net/http/httptest" "os" "strings" "testing" "time" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora" ) // TestReplayRecordedStreamDiagnostics replays a recorded WeKnora SSE stream // (path in WK_RAW_STREAM) through the real client and prints what the narration // observation sees. Temporary diagnostic, removed after the investigation. func TestReplayRecordedStreamDiagnostics(t *testing.T) { path := os.Getenv("WK_RAW_STREAM") if path == "" { t.Skip("WK_RAW_STREAM not set") } raw, err := os.ReadFile(path) if err != nil { t.Fatalf("read stream: %v", err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") _, _ = w.Write(raw) })) defer server.Close() client, err := weknora.NewClient(server.URL, "test-key", 30*time.Second) if err != nil { t.Fatalf("client: %v", err) } var observation reasoningmode.NarrationObservation runStarts := []string{} eventTypes := map[string]int{} observer := func(e weknora.StreamEvent) { eventTypes[e.ResponseType]++ switch e.ResponseType { case "answer": if e.AnswerRunID != "" { observation.Observed = true } if e.AnswerRunID != "" && e.AnswerRunID != lastRunID(runStarts) { runStarts = append(runStarts, fmt.Sprintf("%s@%d", e.AnswerRunID, e.AnswerRunStart)) observation.AnswerRuns++ observation.LastRunStart = e.AnswerRunStart observation.LastRunClean = true } case "thinking", "tool_call", "tool_result": observation.ToolEvents++ observation.LastRunClean = false } } result, err := client.AgentChatEvents(context.Background(), "session", weknora.AgentChatRequest{AgentID: "agent", Query: "q"}, observer) if err != nil { t.Fatalf("agent chat events: %v", err) } t.Logf("eventTypes=%v", eventTypes) t.Logf("runs=%v", runStarts) t.Logf("observation: observed=%v runs=%d tools=%d lastRunStart=%d lastRunClean=%v", observation.Observed, observation.AnswerRuns, observation.ToolEvents, observation.LastRunStart, observation.LastRunClean) content := result.Answer observed := reasoningmode.TrimReasoningNarrationObserved(content, observation) heuristic := reasoningmode.TrimReasoningNarration(content) t.Logf("contentLen=%d observedKept=%d heuristicKept=%d", len(content), len(observed), len(heuristic)) if n := len(content) - len(observed); n > 0 { t.Logf("observedDropPrefix=%q", firstRunes(content[:n], 160)) } if n := len(content) - len(heuristic); n > 0 { t.Logf("heuristicDropPrefix=%q", firstRunes(content[:n], 160)) } t.Logf("observedKeptHead=%q", firstRunes(observed, 160)) } func lastRunID(runs []string) string { if len(runs) == 0 { return "" } id := runs[len(runs)-1] if i := strings.LastIndex(id, "@"); i >= 0 { return id[:i] } return id } func firstRunes(s string, n int) string { r := []rune(s) if len(r) <= n { return string(r) } return string(r[:n]) + "…" }