Files
CLIProxyAPI/internal/runtime/executor/xai_configuration_update_plugin_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

59 lines
2.5 KiB
Go

package executor
import (
"context"
"testing"
"github.com/MoeForge/moego/ojson"
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v8/sdk/cliproxy/executor"
sdktranslator "github.com/router-for-me/CLIProxyAPI/v8/sdk/translator"
)
type updateNormalizingHooks struct{ effort string }
func (h *updateNormalizingHooks) NormalizeRequest(_ context.Context, _, _ sdktranslator.Format, _ string, body []byte, _ bool) []byte {
if h.effort == "" {
out, _ := ojson.DeleteBytes(body, "input.0")
return out
}
out, _ := ojson.SetBytes(body, "input.0.reasoning.effort", h.effort)
return out
}
func (*updateNormalizingHooks) TranslateRequest(context.Context, sdktranslator.Format, sdktranslator.Format, string, []byte, bool) ([]byte, bool) {
return nil, false
}
func (*updateNormalizingHooks) NormalizeResponseBefore(context.Context, sdktranslator.Format, sdktranslator.Format, string, []byte, []byte, []byte, bool) []byte {
return nil
}
func (*updateNormalizingHooks) TranslateResponse(context.Context, sdktranslator.Format, sdktranslator.Format, string, []byte, []byte, []byte, bool) ([]byte, bool) {
return nil, false
}
func (*updateNormalizingHooks) NormalizeResponseAfter(context.Context, sdktranslator.Format, sdktranslator.Format, string, []byte, []byte, []byte, bool) []byte {
return nil
}
func TestXAIResponsesPluginNormalizationOverridesOldSourceUpdate(t *testing.T) {
const source = `{"model":"grok-4.5","reasoning":{"effort":"medium"},"input":[{"type":"configuration_update","reasoning":{"effort":"low"}},{"role":"user","content":"hi"}]}`
for _, tc := range []struct{ name, effort, want string }{
{name: "delete update", want: "medium"},
{name: "rewrite update", effort: "high", want: "high"},
} {
t.Run(tc.name, func(t *testing.T) {
sdktranslator.SetPluginHooks(&updateNormalizingHooks{effort: tc.effort})
t.Cleanup(func() { sdktranslator.SetPluginHooks(nil) })
exec := NewXAIExecutor(&config.Config{})
prepared, errPrepare := exec.prepareResponsesRequest(t.Context(), cliproxyexecutor.Request{Model: "grok-4.5", Payload: []byte(source)}, cliproxyexecutor.Options{SourceFormat: sdktranslator.FormatOpenAIResponse}, false)
if errPrepare != nil {
t.Fatal(errPrepare)
}
if got := ojson.GetBytes(prepared.body, "reasoning.effort").String(); got != tc.want {
t.Fatalf("effort = %q, want %q; body=%s", got, tc.want, prepared.body)
}
if len(ojson.GetBytes(prepared.body, "input").Array()) != 1 {
t.Fatalf("plugin-normalized update was restored: %s", prepared.body)
}
})
}
}