Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
78 lines
2.7 KiB
Go
78 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
)
|
|
|
|
type fakeProbeChat struct {
|
|
chatDelegate
|
|
response *types.ChatResponse
|
|
err error
|
|
}
|
|
|
|
func (f *fakeProbeChat) Chat(context.Context, []chat.Message, *chat.ChatOptions) (*types.ChatResponse, error) {
|
|
return f.response, f.err
|
|
}
|
|
|
|
func TestRecordingChatPreservesPerCallDiagnosticsWithoutContent(t *testing.T) {
|
|
fake := &fakeProbeChat{response: &types.ChatResponse{
|
|
Content: "answer", ReasoningContent: "private reasoning", FinishReason: "length",
|
|
Usage: types.TokenUsage{PromptTokens: 10, CompletionTokens: 4, TotalTokens: 14},
|
|
}}
|
|
recorder := &recordingChat{chatDelegate: fake}
|
|
thinking := false
|
|
options := &chat.ChatOptions{MaxTokens: 4096, Thinking: &thinking}
|
|
first, err := recorder.Chat(context.Background(), nil, options)
|
|
if err != nil || first != fake.response {
|
|
t.Fatal("recorder changed the response")
|
|
}
|
|
thinking = true
|
|
fake.response = &types.ChatResponse{Content: "\u4e2d\u6587", FinishReason: "stop"}
|
|
if _, err := recorder.Chat(context.Background(), nil, options); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if recorder.calls != 2 || len(recorder.records) != 2 {
|
|
t.Fatal("missing call diagnostics")
|
|
}
|
|
a, b := recorder.records[0], recorder.records[1]
|
|
if a.Number != 1 || a.MaxTokens != 4096 || a.Thinking == nil || *a.Thinking ||
|
|
!a.ResponseReceived || a.Failed || a.FinishReason != "length" ||
|
|
a.ContentRunes != 6 || a.ReasoningRunes != 17 || a.Usage.TotalTokens != 14 {
|
|
t.Fatalf("unexpected first call metadata: %+v", a)
|
|
}
|
|
if b.Number != 2 || b.Thinking == nil || !*b.Thinking || b.ContentRunes != 2 || b.ReasoningRunes != 0 {
|
|
t.Fatalf("unexpected second call metadata: %+v", b)
|
|
}
|
|
encoded, err := json.Marshal(recorder.records)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(string(encoded), "private reasoning") || strings.Contains(string(encoded), "answer") {
|
|
t.Fatal("diagnostics exposed response content")
|
|
}
|
|
}
|
|
|
|
func TestRecordingChatRecordsNoResponseWithoutLeakingError(t *testing.T) {
|
|
want := errors.New("private upstream error")
|
|
recorder := &recordingChat{chatDelegate: &fakeProbeChat{err: want}}
|
|
response, err := recorder.Chat(context.Background(), nil, nil)
|
|
if response != nil || err != want || len(recorder.records) != 1 {
|
|
t.Fatal("recorder changed the failure")
|
|
}
|
|
record := recorder.records[0]
|
|
if record.ResponseReceived || !record.Failed || record.Thinking != nil || record.ContentRunes != 0 || record.ReasoningRunes != 0 {
|
|
t.Fatalf("unexpected failure metadata: %+v", record)
|
|
}
|
|
encoded, _ := json.Marshal(record)
|
|
if strings.Contains(string(encoded), want.Error()) {
|
|
t.Fatal("diagnostics exposed upstream error")
|
|
}
|
|
}
|