mirror of
https://github.com/sipeed/picoclaw.git
synced 2026-06-12 18:08:54 +00:00
cb0c8703fb
Add comprehensive unit tests for the ToolRegistry covering registration, lookup, execution, context injection, async callbacks, schema generation, provider definition conversion, and concurrent access. Fix a defensive edge case in Truncate where a negative maxLen would cause a slice bounds panic, and add table-driven tests covering boundary conditions, zero/negative lengths, and Unicode handling. Co-authored-by: Cursor <cursoragent@cursor.com>
29 lines
686 B
Go
29 lines
686 B
Go
package utils
|
|
|
|
// Truncate returns a truncated version of s with at most maxLen runes.
|
|
// Handles multi-byte Unicode characters properly.
|
|
// If the string is truncated, "..." is appended to indicate truncation.
|
|
func Truncate(s string, maxLen int) string {
|
|
if maxLen <= 0 {
|
|
return ""
|
|
}
|
|
runes := []rune(s)
|
|
if len(runes) <= maxLen {
|
|
return s
|
|
}
|
|
// Reserve 3 chars for "..."
|
|
if maxLen <= 3 {
|
|
return string(runes[:maxLen])
|
|
}
|
|
return string(runes[:maxLen-3]) + "..."
|
|
}
|
|
|
|
// DerefStr dereferences a pointer to a string and
|
|
// returns the value or a fallback if the pointer is nil.
|
|
func DerefStr(s *string, fallback string) string {
|
|
if s == nil {
|
|
return fallback
|
|
}
|
|
return *s
|
|
}
|