Files
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

179 lines
5.7 KiB
Go

package token
import (
"fmt"
"math"
"strings"
"testing"
"academy/internal/models/chat"
"github.com/stretchr/testify/assert"
)
func TestEstimator(t *testing.T) {
e, err := NewEstimator()
if err != nil {
t.Fatalf("Failed to create estimator: %v", err)
}
t.Run("empty string", func(t *testing.T) {
assert.Equal(t, 0, e.EstimateString(""))
})
t.Run("english text", func(t *testing.T) {
tokens := e.EstimateString("hello world")
assert.Equal(t, 2, tokens)
})
t.Run("chinese text", func(t *testing.T) {
tokens := e.EstimateString("你好世界测试数据中文")
fmt.Println(tokens)
assert.Greater(t, tokens, 0)
})
t.Run("CJK produces more tokens per char than latin", func(t *testing.T) {
latin := strings.Repeat("a", 100)
cjk := strings.Repeat("中", 100)
latinTokens := e.EstimateString(latin)
cjkTokens := e.EstimateString(cjk)
fmt.Println(latinTokens, cjkTokens)
assert.Greater(t, cjkTokens, latinTokens,
"CJK text should produce more tokens per character")
})
t.Run("message estimation includes overhead", func(t *testing.T) {
msg := chat.Message{
Role: "assistant",
Content: "hello",
}
tokens := e.EstimateMessage(&msg)
contentTokens := e.EstimateString("hello")
fmt.Println(tokens, contentTokens)
assert.Greater(t, tokens, contentTokens,
"message tokens should include overhead beyond just content")
})
t.Run("message with tool calls", func(t *testing.T) {
msg := chat.Message{
Role: "assistant",
Content: "thinking...",
ToolCalls: []chat.ToolCall{
{
Function: chat.FunctionCall{
Name: "knowledge_search",
Arguments: `{"query": "test"}`,
},
},
},
}
tokens := e.EstimateMessage(&msg)
assert.Greater(t, tokens, 10)
})
t.Run("estimate messages", func(t *testing.T) {
messages := []chat.Message{
{Role: "system", Content: "You are a helpful assistant."},
{Role: "user", Content: "Hello"},
{Role: "assistant", Content: "Hi there!"},
}
tokens := e.EstimateMessages(messages)
assert.Greater(t, tokens, 10)
})
}
// Thinking models (DeepSeek V3.2/V4, MiMo) require prior reasoning_content to
// be sent back verbatim, and it typically dwarfs the visible reply. Leaving it
// out of the estimate made a 130k context measure as 26k, so compaction cut in
// the wrong place, freed almost nothing, and ran again every round.
func TestEstimateMessageCountsReasoningContent(t *testing.T) {
e, err := NewEstimator()
assert.NoError(t, err)
reasoning := strings.Repeat("let me think about this step by step. ", 200)
plain := chat.Message{Role: "assistant", Content: "short answer"}
thinking := chat.Message{Role: "assistant", Content: "short answer", ReasoningContent: reasoning}
assert.Greater(t, e.EstimateMessage(&thinking), e.EstimateMessage(&plain)+1000,
"reasoning content must be counted, it is sent on the wire")
assert.InDelta(t, e.EstimateString(reasoning),
e.EstimateMessage(&thinking)-e.EstimateMessage(&plain), 2)
}
// Providers bill images by tile count, so neither a short https:// URL nor a
// megabyte data URI says anything useful about the cost.
func TestEstimateMessageCountsImages(t *testing.T) {
e, err := NewEstimator()
assert.NoError(t, err)
withImages := chat.Message{Role: "user", Content: "what is this", Images: []string{"https://x/a.png"}}
assert.Greater(t, e.EstimateMessage(&withImages), estimatedImageTokens)
multi := chat.Message{Role: "user", MultiContent: []chat.MessageContentPart{
{Type: "text", Text: "describe"},
{Type: "image_url", ImageURL: &chat.ImageURL{URL: "data:image/png;base64,AAAA"}},
}}
assert.Greater(t, e.EstimateMessage(&multi), estimatedImageTokens)
// MultiContent is the representation actually sent; counting Images too
// would bill the same picture twice.
both := chat.Message{
Role: "user",
Images: []string{"https://x/a.png"},
MultiContent: multi.MultiContent,
}
assert.Equal(t, e.EstimateMessage(&multi), e.EstimateMessage(&both))
}
// Tool schemas are part of the billed prompt but never appear in the message
// list, so omitting them biases every estimate low by a constant.
func TestEstimateTools(t *testing.T) {
e, err := NewEstimator()
assert.NoError(t, err)
assert.Zero(t, e.EstimateTools(nil))
tools := []chat.Tool{{
Type: "function",
Function: chat.FunctionDef{
Name: "shell_exec",
Description: strings.Repeat("run a shell command in the sandbox. ", 20),
Parameters: []byte(`{"type":"object","properties":{"command":{"type":"string"}}}`),
},
}}
assert.Greater(t, e.EstimateTools(tools), 100)
}
// Text counts carry the provider's scale; per-message overhead and the fixed
// image cost do not, since they are not text the provider tokenizes.
func TestEstimatorScalesTextOnly(t *testing.T) {
e, err := NewEstimator()
assert.NoError(t, err)
text := strings.Repeat("知识库文档检索结果,", 100)
raw := e.EstimateString(text)
e.SetScale(0.6)
assert.InDelta(t, float64(raw)*0.6, float64(e.EstimateString(text)), 1)
assert.Equal(t, raw, e.Unscaled().EstimateString(text), "Unscaled measures the raw count")
msg := chat.Message{Role: "user", Content: text}
withoutText := chat.Message{Role: "user"}
assert.InDelta(t, float64(raw)*0.6,
float64(e.EstimateMessage(&msg)-e.EstimateMessage(&withoutText)), 1)
}
// The scale is clamped: outside the bounds it says more about a provider's
// usage report than about its tokenizer. Unset or invalid means uncalibrated.
func TestEstimatorScaleIsClamped(t *testing.T) {
e, err := NewEstimator()
assert.NoError(t, err)
assert.Equal(t, 1.0, e.Scale())
e.SetScale(0.1)
assert.Equal(t, MinScale, e.Scale())
e.SetScale(9)
assert.Equal(t, MaxScale, e.Scale())
e.SetScale(0)
assert.Equal(t, 1.0, e.Scale())
e.SetScale(math.NaN())
assert.Equal(t, 1.0, e.Scale())
}