Files
ai-gateway-go/internal/workbench/workbench_test.go
T
superidou 5759c1862e AI Gateway Go 0.10.0 源码快照 + 旗舰版需求规划报告
M0-M7 已完成:核心网关(身份/RBAC/TOTP/OIDC/SAML/Provider/配额/路由/内容策略/审计/定价)+ 资源市场(MCP/Skills/数字员工)。
含 22 个 PostgreSQL 迁移、管理端/门户端前端源码、OpenAPI 契约、部署 compose。

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-12 11:45:54 +08:00

52 lines
1.4 KiB
Go

package workbench
import (
"reflect"
"testing"
)
func TestRenderPromptValidatesRequiredVariables(t *testing.T) {
definitions := []Variable{{Name: "topic", Required: true}, {Name: "tone", Default: "简洁"}}
rendered, err := RenderPrompt("用{{ tone }}风格介绍 {{topic}},保留 {{unknown}}。", definitions, map[string]any{"topic": "Go"})
if err != nil {
t.Fatal(err)
}
if rendered != "用简洁风格介绍 Go,保留 {{unknown}}。" {
t.Fatalf("unexpected render: %q", rendered)
}
if _, err = RenderPrompt("{{topic}}", definitions, nil); err == nil {
t.Fatal("expected missing variable error")
}
}
func TestChunkTextParagraphAwareAndOverlapped(t *testing.T) {
text := "第一段。\n\n" + repeatRune('甲', 240) + "\n\n最后一段。"
chunks := ChunkText(text, 200, 20)
if len(chunks) < 3 {
t.Fatalf("expected at least 3 chunks, got %d", len(chunks))
}
for _, chunk := range chunks {
if RuneCount(chunk) > 222 {
t.Fatalf("chunk unexpectedly large: %d", RuneCount(chunk))
}
}
}
func TestNormalizeStringsStableDeduplication(t *testing.T) {
values, err := normalizeStrings([]string{" b ", "a", "b", ""}, 10)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(values, []string{"b", "a"}) {
t.Fatalf("unexpected values: %#v", values)
}
}
func repeatRune(value rune, count int) string {
runes := make([]rune, count)
for i := range runes {
runes[i] = value
}
return string(runes)
}