Files
xinmi/internal/agent/tools/sanitize_messages_test.go
T
yawan f7fc992f57
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
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

87 lines
2.6 KiB
Go

package tools
import (
"testing"
"github.com/Tencent/WeKnora/internal/models/chat"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSanitizeMessages(t *testing.T) {
t.Run("normal messages unchanged", func(t *testing.T) {
messages := []chat.Message{
{Role: "system", Content: "You are helpful"},
{Role: "user", Content: "Hello"},
{Role: "assistant", Content: "Hi there"},
}
result := SanitizeMessages(messages)
assert.Len(t, result, 3)
})
t.Run("consecutive user messages merged", func(t *testing.T) {
messages := []chat.Message{
{Role: "system", Content: "You are helpful"},
{Role: "user", Content: "Hello"},
{Role: "user", Content: "How are you?"},
}
result := SanitizeMessages(messages)
require.Len(t, result, 2) // system + merged user
assert.Contains(t, result[1].Content, "Hello")
assert.Contains(t, result[1].Content, "How are you?")
})
t.Run("consecutive tool messages not merged", func(t *testing.T) {
messages := []chat.Message{
{Role: "system", Content: "system"},
{Role: "assistant", Content: "thinking", ToolCalls: []chat.ToolCall{
{ID: "call_1"}, {ID: "call_2"},
}},
{Role: "tool", Content: "result1", ToolCallID: "call_1"},
{Role: "tool", Content: "result2", ToolCallID: "call_2"},
}
result := SanitizeMessages(messages)
assert.Len(t, result, 4) // all preserved
})
t.Run("empty content messages removed and consecutive merged", func(t *testing.T) {
messages := []chat.Message{
{Role: "system", Content: "system"},
{Role: "user", Content: "hello"},
{Role: "assistant", Content: ""},
{Role: "user", Content: "bye"},
}
result := SanitizeMessages(messages)
// empty assistant removed → two user messages merge
assert.Len(t, result, 2)
assert.Contains(t, result[1].Content, "hello")
assert.Contains(t, result[1].Content, "bye")
})
t.Run("empty system message preserved", func(t *testing.T) {
messages := []chat.Message{
{Role: "system", Content: ""},
{Role: "user", Content: "hello"},
}
result := SanitizeMessages(messages)
assert.Len(t, result, 2) // system preserved even if empty
})
t.Run("orphaned tool result converted", func(t *testing.T) {
messages := []chat.Message{
{Role: "system", Content: "system"},
{Role: "tool", Content: "some result",
ToolCallID: "nonexistent_id", Name: "search"},
}
result := SanitizeMessages(messages)
require.Len(t, result, 2)
assert.Equal(t, "system", result[1].Role) // converted
assert.Contains(t, result[1].Content, "search")
})
t.Run("empty slice", func(t *testing.T) {
result := SanitizeMessages(nil)
assert.Empty(t, result)
})
}