hyapp-server/pkg/apptracking/limits_test.go

37 lines
1.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package apptracking
import (
"encoding/json"
"strings"
"testing"
)
func TestNormalizeTextUsesBytesAndNeverTruncates(t *testing.T) {
if got, ok := NormalizeText(" "+strings.Repeat("e", MaxErrorCodeBytes)+" ", MaxErrorCodeBytes); !ok || got != strings.Repeat("e", MaxErrorCodeBytes) {
t.Fatalf("text at byte limit must be trimmed but otherwise preserved: ok=%v got=%q", ok, got)
}
if got, ok := NormalizeText(strings.Repeat("e", 185), MaxErrorCodeBytes); ok || got != "" {
t.Fatalf("oversized error_code must be rejected, never truncated: ok=%v got=%q", ok, got)
}
// 一个汉字编码为三个 UTF-8 bytes这里防止调用方误把字段限制实现成 rune 数。
if got, ok := NormalizeText(strings.Repeat("界", 22), MaxErrorCodeBytes); ok || got != "" {
t.Fatalf("text limits must count UTF-8 bytes: ok=%v got=%q", ok, got)
}
}
func TestNormalizePropertiesAndBatchContract(t *testing.T) {
if !ValidBatchSize(1) || !ValidBatchSize(MaxBatchSize) || ValidBatchSize(0) || ValidBatchSize(MaxBatchSize+1) {
t.Fatalf("batch bounds do not match the shared contract")
}
got, ok := NormalizeProperties(json.RawMessage(` {"screen":"home"} `))
if !ok || string(got) != `{"screen":"home"}` {
t.Fatalf("valid properties mismatch: ok=%v got=%s", ok, got)
}
if got, ok := NormalizeProperties(json.RawMessage(`"` + strings.Repeat("x", MaxPropertiesBytes) + `"`)); ok || got != nil {
t.Fatalf("properties over the encoded byte limit must be rejected: ok=%v bytes=%d", ok, len(got))
}
if got, ok := NormalizeProperties(json.RawMessage(`{"broken"`)); ok || got != nil {
t.Fatalf("invalid properties JSON must be rejected: ok=%v got=%s", ok, got)
}
}