Files
ai_academy/internal/models/chat/langfuse_wrapper_test.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
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
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (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 & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (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
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

114 lines
3.7 KiB
Go

package chat
import (
"reflect"
"strings"
"testing"
"academy/internal/types"
)
func TestBuildLangfuseGenerationOutput(t *testing.T) {
toolCalls := []types.LLMToolCall{{ID: "call_1", Type: "function"}}
got := buildLangfuseGenerationOutput("", "", "tool_calls", toolCalls)
want := map[string]interface{}{
"content": "",
"tool_calls": toolCalls,
"finish_reason": "tool_calls",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("output without reasoning = %#v; want %#v", got, want)
}
got = buildLangfuseGenerationOutput("answer", "thinking", "stop", nil)
want = map[string]interface{}{
"content": "answer",
"tool_calls": []types.LLMToolCall(nil),
"finish_reason": "stop",
"reasoning_content": "thinking",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("output with reasoning = %#v; want %#v", got, want)
}
}
func TestSnapshotLangfuseToolCallsKeepsModelArguments(t *testing.T) {
providerCalls := []types.LLMToolCall{{
ID: "call_1",
Function: types.FunctionCall{Name: "wiki_read_page", Arguments: `{"slugs":["res://0001"]}`},
}}
snapshot := snapshotLangfuseToolCalls(providerCalls)
providerCalls[0].Function.Arguments = `{"slugs":["summary/uuid"]}`
if got := snapshot[0].Function.Arguments; got != `{"slugs":["res://0001"]}` {
t.Fatalf("Langfuse snapshot was mutated to %s", got)
}
}
func TestBuildLangfuseMessagesReasoningContent(t *testing.T) {
msgs := buildLangfuseMessages([]Message{
{Role: "assistant", ReasoningContent: "chain of thought", ToolCalls: []ToolCall{{ID: "tc1"}}},
})
if len(msgs) != 1 {
t.Fatalf("len(messages) = %d; want 1", len(msgs))
}
if msgs[0]["reasoning_content"] != "chain of thought" {
t.Fatalf("reasoning_content = %v; want chain of thought", msgs[0]["reasoning_content"])
}
}
func TestBuildLangfuseMessagesRecordsImagesWithoutPayload(t *testing.T) {
dataURI := "data:image/png;base64," + strings.Repeat("A", 2048)
msgs := buildLangfuseMessages([]Message{
{Role: "user", Content: "look", Images: []string{dataURI, "https://example.com/a.png"}},
})
want := []string{"data:image/png (3 KB)", "https://example.com/a.png"}
if !reflect.DeepEqual(msgs[0]["images"], want) {
t.Fatalf("images = %#v; want %#v", msgs[0]["images"], want)
}
}
func TestConvertUsageIncludesPromptCacheCounters(t *testing.T) {
got := convertUsage(&types.TokenUsage{
PromptTokens: 1000, CompletionTokens: 50, TotalTokens: 1050,
CacheReadTokens: 800, CacheWriteTokens: 100, CacheMissTokens: 200,
})
if got == nil {
t.Fatal("convertUsage returned nil")
}
if got.CacheRead != 800 || got.CacheWrite != 100 || got.CacheMiss != 200 {
t.Fatalf("cache usage = read:%d write:%d miss:%d", got.CacheRead, got.CacheWrite, got.CacheMiss)
}
}
func TestBuildLangfuseChatMetadataIncludesMCPCatalog(t *testing.T) {
meta := buildLangfuseChatMetadata("model-1", "agent", "fp", true, &ChatOptions{
Tools: []Tool{
{Function: FunctionDef{Name: "knowledge_search", Description: "search kb"}},
{Function: FunctionDef{
Name: langfuseDiscoverMCPTool,
Description: "MCP tools are available without an @mention.\n{\"server_id\":\"svc\"}",
}},
{Function: FunctionDef{Name: "call_mcp_tool", Description: "call"}},
},
})
if meta["has_tools"] != true || meta["streaming"] != true {
t.Fatalf("flags = %#v", meta)
}
names, _ := meta["tool_names"].([]string)
if !reflect.DeepEqual(names, []string{"knowledge_search", langfuseDiscoverMCPTool, "call_mcp_tool"}) {
t.Fatalf("tool_names = %#v", names)
}
catalog, _ := meta["mcp_catalog"].(string)
if !strings.Contains(catalog, `"server_id":"svc"`) {
t.Fatalf("mcp_catalog = %q", catalog)
}
}
func TestTruncateLangfuseText(t *testing.T) {
if got := truncateLangfuseText("一二三四五", 3); got != "一二三…" {
t.Fatalf("got %q", got)
}
}