bfef465977
internal/summarise called protocol.NormalizeText, which was never defined, so the tree did not compile. Add the shared normalisation helper: drop zero-width formatting runes, turn control characters into spaces and collapse whitespace runs (including the non-breaking space variants) into single spaces. Reuse it in OutputPreview instead of duplicating the mapping logic. Cover the awkward inputs (newlines, tabs, non-breaking spaces, zero-width runes, surrounding quotes, trailing punctuation) in the protocol and summarise tests. Unrelated working-tree changes are left uncommitted.
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
package protocol
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestOutputPreviewUsesRetainedValuesWithoutAssumingSchema(t *testing.T) {
|
|
for _, test := range []struct {
|
|
output map[string]any
|
|
want string
|
|
}{
|
|
{map[string]any{"summary": "Fixed layout\nand added regression checks", "evidence": "details"}, "Fixed layout and added regression checks"},
|
|
{map[string]any{"artifacts": []string{"report.md"}}, `artifacts: ["report.md"]`},
|
|
{map[string]any{"z": true, "a": json.Number("9007199254740993")}, "a: 9007199254740993"},
|
|
{map[string]any{"input": map[string]any{"prompt": "private task"}}, ""},
|
|
{nil, ""},
|
|
} {
|
|
if got := OutputPreview(test.output); got != test.want {
|
|
t.Fatal(got, test.want)
|
|
}
|
|
}
|
|
preview := OutputPreview(map[string]any{"summary": strings.Repeat("宽", 500)})
|
|
if len([]rune(preview)) != 280 || !strings.HasSuffix(preview, "…") {
|
|
t.Fatal("preview is not bounded by characters")
|
|
}
|
|
}
|
|
|
|
func TestNormalizeTextCollapsesToSingleCleanLine(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
}{
|
|
{"whitespace and control characters", " a\t\t b\n\nc\r\nd ", "a b c d"},
|
|
{"non-breaking spaces", "a\u00a0\u202fb\u2007c", "a b c"},
|
|
{"zero-width joiners", "a\u200b\u200c\u200db", "ab"},
|
|
{"word joiner and byte-order mark", "\ufeffa\u2060b", "ab"},
|
|
{"soft hyphen and mongolian vowel separator", "so\u00adft\u180e-hyphen", "soft-hyphen"},
|
|
{"only invisible runes", "\u00a0\t\u200b\r\n", ""},
|
|
{"already clean", "one clean line", "one clean line"},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
got := NormalizeText(test.in)
|
|
if got != test.want {
|
|
t.Fatalf("NormalizeText(%q) = %q, want %q", test.in, got, test.want)
|
|
}
|
|
if strings.ContainsAny(got, "\r\n\t") || strings.Contains(got, " ") {
|
|
t.Fatalf("NormalizeText(%q) is not single-spaced: %q", test.in, got)
|
|
}
|
|
})
|
|
}
|
|
}
|