Files
CLIProxyAPI/internal/runtime/executor/helps/gemini_content_turns_test.go
Luis Pater 6e6afab99a refactor: migrate gjson, sjson, and base64 to MoeForge/moego
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson
  and encoding/base64 with github.com/MoeForge/moego/base64 across the main
  module and examples
- Introduce cross-method *ojson.Doc request rewriting pipelines across
  thinking, signature, client/codex, runtime/executor/helps, pluginhost, and
  all provider executors with FinalizePayloadDoc as the final semantic barrier
- Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations)
  to batch edits on ojson.Doc
- Upgrade read-only byte paths to ojson.GetBytesNoCopy,
  ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit
  strings.Clone ownership protection across streaming and persistent state
- Use base64.StdEncoding.ValidateString for zero-allocation signature checks
  and base64.EncodeDataURL for single-allocation data URL construction
2026-11-03 03:05:04 +08:00

280 lines
11 KiB
Go

package helps
import (
"strings"
"testing"
"github.com/MoeForge/moego/ojson"
)
var (
leadingGeminiUserContentOutput []byte
trailingGeminiUserContentOutput []byte
)
func TestEnsureGeminiLeadingUserContentReusesLargeValidPayload(t *testing.T) {
input := []byte(`{"contents":[{"role":"user","parts":[{"inlineData":{"mimeType":"video/mp4","data":"` + strings.Repeat("A", 4<<20) + `"}}]}]}`)
output := EnsureGeminiLeadingUserContent(input, "contents")
if &output[0] != &input[0] {
t.Fatal("valid request should reuse the input payload")
}
result := testing.Benchmark(func(b *testing.B) {
for b.Loop() {
leadingGeminiUserContentOutput = EnsureGeminiLeadingUserContent(input, "contents")
}
})
if allocated := result.AllocedBytesPerOp(); allocated >= 1<<20 {
t.Fatalf("valid 4 MiB request allocated %d bytes/op, want less than 1 MiB", allocated)
}
}
func TestEnsureGeminiLeadingUserContent(t *testing.T) {
tests := []struct {
name string
inputJSON string
path string
wantRoles string
wantLeadingEmpty bool
}{
{
name: "user first is unchanged",
inputJSON: `{"contents":[{"role":"user","parts":[{"text":"hello"}]}]}`,
path: "contents",
wantRoles: "user",
},
{
name: "leading model functionCall gets empty user",
inputJSON: `{"contents":[{"role":"model","parts":[{"functionCall":{"name":"run"}}]},{"role":"user","parts":[{"functionResponse":{"name":"run"}}]}]}`,
path: "contents",
wantRoles: "user,model,user",
wantLeadingEmpty: true,
},
{
name: "leading model text gets empty user and preserves following turns",
inputJSON: `{"contents":[{"role":"model","parts":[{"text":"answer"}]},{"role":"user","parts":[{"text":"continue"}]}]}`,
path: "contents",
wantRoles: "user,model,user",
wantLeadingEmpty: true,
},
{
name: "nested contents are normalized",
inputJSON: `{"request":{"contents":[{"role":"model","parts":[{"text":"answer"}]},{"role":"user","parts":[{"text":"continue"}]}]}}`,
path: "request.contents",
wantRoles: "request.user,model,user",
wantLeadingEmpty: true,
},
{
name: "empty contents are unchanged",
inputJSON: `{"contents":[]}`,
path: "contents",
wantRoles: "",
},
{
name: "missing contents are unchanged",
inputJSON: `{"model":"test"}`,
path: "contents",
wantRoles: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := EnsureGeminiLeadingUserContent([]byte(tt.inputJSON), tt.path)
contents := ojson.GetBytes(out, tt.path).Array()
roles := make([]string, 0, len(contents))
for _, content := range contents {
roles = append(roles, content.Get("role").String())
}
expectedRoles := strings.TrimPrefix(tt.wantRoles, "request.")
if got := strings.Join(roles, ","); got != expectedRoles {
t.Fatalf("roles = %q, want %q; output=%s", got, expectedRoles, out)
}
if tt.wantLeadingEmpty {
text := ojson.GetBytes(out, tt.path+".0.parts.0.text")
if !text.Exists() || text.String() != "" {
t.Fatalf("leading empty user part missing; output=%s", out)
}
}
})
}
}
func TestEnsureGeminiTrailingUserContentReusesLargeValidPayload(t *testing.T) {
input := []byte(`{"contents":[{"role":"user","parts":[{"inlineData":{"mimeType":"video/mp4","data":"` + strings.Repeat("A", 4<<20) + `"}}]}]}`)
output := EnsureGeminiTrailingUserContent(input, "contents")
if &output[0] != &input[0] {
t.Fatal("valid request should reuse the input payload")
}
result := testing.Benchmark(func(b *testing.B) {
for b.Loop() {
trailingGeminiUserContentOutput = EnsureGeminiTrailingUserContent(input, "contents")
}
})
if allocated := result.AllocedBytesPerOp(); allocated >= 1<<20 {
t.Fatalf("valid 4 MiB request allocated %d bytes/op, want less than 1 MiB", allocated)
}
}
func TestEnsureGeminiTrailingUserContent(t *testing.T) {
tests := []struct {
name string
inputJSON string
path string
wantRoles string
wantTrailingEmpty bool
}{
{
name: "user last is unchanged",
inputJSON: `{"contents":[{"role":"user","parts":[{"text":"hello"}]}]}`,
path: "contents",
wantRoles: "user",
},
{
name: "trailing functionResponse preserves model role without adding user",
inputJSON: `{"contents":[{"role":"model","parts":[{"functionCall":{"name":"run"}}]},{"role":"model","parts":[{"functionResponse":{"name":"run","response":{"result":"ok"}}}]}]}`,
path: "contents",
wantRoles: "model,model",
},
{
name: "trailing model functionResponse with nested inlineData gets empty user",
inputJSON: `{"contents":[{"role":"model","parts":[{"functionCall":{"name":"view_image"}}]},{"role":"model","parts":[{"functionResponse":{"name":"view_image","response":{"result":""},"parts":[{"inlineData":{"mimeType":"image/png","data":"AAAA"}}]}}]}]}`,
path: "contents",
wantRoles: "model,model,user",
wantTrailingEmpty: true,
},
{
name: "trailing model functionResponse with nested fileData gets empty user",
inputJSON: `{"contents":[{"role":"model","parts":[{"functionCall":{"name":"view_image"}}]},{"role":"model","parts":[{"functionResponse":{"name":"view_image","response":{"result":""},"parts":[{"fileData":{"mimeType":"image/png","fileUri":"gs://b/a.png"}}]}}]}]}`,
path: "contents",
wantRoles: "model,model,user",
wantTrailingEmpty: true,
},
{
name: "trailing user functionResponse with nested inlineData is unchanged",
inputJSON: `{"contents":[{"role":"model","parts":[{"functionCall":{"name":"view_image"}}]},{"role":"user","parts":[{"functionResponse":{"name":"view_image","response":{"result":""},"parts":[{"inlineData":{"mimeType":"image/png","data":"AAAA"}}]}}]}]}`,
path: "contents",
wantRoles: "model,user",
},
{
name: "trailing model functionCall gets empty user",
inputJSON: `{"contents":[{"role":"user","parts":[{"text":"hello"}]},{"role":"model","parts":[{"functionCall":{"name":"run"}}]}]}`,
path: "contents",
wantRoles: "user,model,user",
wantTrailingEmpty: true,
},
{
name: "trailing model text gets empty user and preserves preceding turns",
inputJSON: `{"contents":[{"role":"user","parts":[{"text":"hello"}]},{"role":"model","parts":[{"text":"answer"}]}]}`,
path: "contents",
wantRoles: "user,model,user",
wantTrailingEmpty: true,
},
{
name: "nested contents are normalized",
inputJSON: `{"request":{"contents":[{"role":"user","parts":[{"text":"hello"}]},{"role":"model","parts":[{"text":"answer"}]}]}}`,
path: "request.contents",
wantRoles: "request.user,model,user",
wantTrailingEmpty: true,
},
{
name: "empty contents are unchanged",
inputJSON: `{"contents":[]}`,
path: "contents",
wantRoles: "",
},
{
name: "missing contents are unchanged",
inputJSON: `{"model":"test"}`,
path: "contents",
wantRoles: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := EnsureGeminiTrailingUserContent([]byte(tt.inputJSON), tt.path)
contents := ojson.GetBytes(out, tt.path).Array()
roles := make([]string, 0, len(contents))
for _, content := range contents {
roles = append(roles, content.Get("role").String())
}
expectedRoles := strings.TrimPrefix(tt.wantRoles, "request.")
if got := strings.Join(roles, ","); got != expectedRoles {
t.Fatalf("roles = %q, want %q; output=%s", got, expectedRoles, out)
}
if tt.wantTrailingEmpty {
lastIdx := len(contents) - 1
text := contents[lastIdx].Get("parts.0.text")
if !text.Exists() || text.String() != "" {
t.Fatalf("trailing empty user part missing; output=%s", out)
}
}
})
}
}
func TestEnsureGeminiBoundaryUserContent(t *testing.T) {
// A single model turn should get both leading and trailing user turns
inputJSON := `{"contents":[{"role":"model","parts":[{"text":"single answer"}]}]}`
out := EnsureGeminiBoundaryUserContent([]byte(inputJSON), "contents")
contents := ojson.GetBytes(out, "contents").Array()
if len(contents) != 3 {
t.Fatalf("contents len = %d, want 3; out=%s", len(contents), out)
}
if contents[0].Get("role").String() != "user" || contents[0].Get("parts.0.text").String() != "" {
t.Fatalf("leading user turn missing: %s", out)
}
if contents[1].Get("role").String() != "model" || contents[1].Get("parts.0.text").String() != "single answer" {
t.Fatalf("middle model turn corrupted: %s", out)
}
if contents[2].Get("role").String() != "user" || contents[2].Get("parts.0.text").String() != "" {
t.Fatalf("trailing user turn missing: %s", out)
}
}
func TestDropGeminiTrailingThoughtOnlyContent(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "drops trailing thought turns",
input: `{"contents":[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":[{"text":"a","thought":true}]},{"role":"model","parts":[{"text":"b","thought":true,"thoughtSignature":"sig"}]}]}`,
want: `[{"role":"user","parts":[{"text":"hi"}]}]`,
},
{
name: "keeps visible model text",
input: `{"contents":[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":[{"text":"a","thought":true},{"text":"answer"}]}]}`,
want: `[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":[{"text":"a","thought":true},{"text":"answer"}]}]`,
},
{
name: "keeps function call",
input: `{"contents":[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":[{"thought":true,"functionCall":{"name":"run","args":{}}}]}]}`,
want: `[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":[{"thought":true,"functionCall":{"name":"run","args":{}}}]}]`,
},
{
name: "keeps earlier signed reasoning",
input: `{"contents":[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":[{"text":"a","thought":true,"thoughtSignature":"sig"}]},{"role":"user","parts":[{"text":"next"}]},{"role":"model","parts":[{"text":"b","thought":true}]}]}`,
want: `[{"role":"user","parts":[{"text":"hi"}]},{"role":"model","parts":[{"text":"a","thought":true,"thoughtSignature":"sig"}]},{"role":"user","parts":[{"text":"next"}]}]`,
},
{
name: "keeps the only turn",
input: `{"contents":[{"role":"model","parts":[{"text":"a","thought":true}]}]}`,
want: `[{"role":"model","parts":[{"text":"a","thought":true}]}]`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := DropGeminiTrailingThoughtOnlyContent([]byte(tt.input), "contents")
if got := ojson.GetBytes(out, "contents").Raw; got != tt.want {
t.Fatalf("contents = %s, want %s", got, tt.want)
}
})
}
}