mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-09-06 08:02:22 +08:00
- Implemented deterministic shortening of overlong Codex input item IDs to meet 64-character limit. - Added `SanitizeCodexInputItemIDs` helper for efficient ID shortening in request bodies. - Updated Codex executor and websocket implementations to utilize the new helper. - Introduced unit tests to validate ID length restrictions, avoidance of collisions, and deterministic behavior. Closes: #4401
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package helps
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
func TestSanitizeCodexInputItemIDsBoundaries(t *testing.T) {
|
|
id64 := strings.Repeat("a", 64)
|
|
id65 := strings.Repeat("b", 65)
|
|
unicode65 := strings.Repeat("界", 65)
|
|
body := []byte(`{"input":[{"id":"` + id64 + `"},{"id":"` + id65 + `"},{"id":"` + unicode65 + `"}]}`)
|
|
|
|
got := SanitizeCodexInputItemIDs(body)
|
|
|
|
if actual := gjson.GetBytes(got, "input.0.id").String(); actual != id64 {
|
|
t.Fatalf("64-character ID changed: %q", actual)
|
|
}
|
|
for _, path := range []string{"input.1.id", "input.2.id"} {
|
|
actual := gjson.GetBytes(got, path).String()
|
|
if len([]rune(actual)) != 64 {
|
|
t.Fatalf("%s length = %d, want 64: %q", path, len([]rune(actual)), actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSanitizeCodexInputItemIDsAvoidsExistingIDCollision(t *testing.T) {
|
|
longID := strings.Repeat("grok-item-", 10)
|
|
collidingValidID := shortenCodexInputItemID(longID)
|
|
body := []byte(`{"input":[{"id":"` + longID + `"},{"id":"` + collidingValidID + `"}]}`)
|
|
|
|
first := SanitizeCodexInputItemIDs(body)
|
|
second := SanitizeCodexInputItemIDs(body)
|
|
|
|
shortened := gjson.GetBytes(first, "input.0.id").String()
|
|
if shortened == collidingValidID {
|
|
t.Fatalf("shortened ID collided with an existing valid ID: %q", shortened)
|
|
}
|
|
if len([]rune(shortened)) > 64 {
|
|
t.Fatalf("shortened ID length = %d, want at most 64", len([]rune(shortened)))
|
|
}
|
|
if actual := gjson.GetBytes(first, "input.1.id").String(); actual != collidingValidID {
|
|
t.Fatalf("existing valid ID changed: %q", actual)
|
|
}
|
|
if actual := gjson.GetBytes(second, "input.0.id").String(); actual != shortened {
|
|
t.Fatalf("collision resolution is not deterministic: first=%q second=%q", shortened, actual)
|
|
}
|
|
}
|
|
|
|
func TestSanitizeCodexInputItemIDsLeavesUnsupportedPayloadsUnchanged(t *testing.T) {
|
|
for _, body := range [][]byte{
|
|
[]byte(`not-json`),
|
|
[]byte(`{"input":{"id":"item-1"}}`),
|
|
[]byte(`{"input":[1,{"id":2},{"id":"item-1"}]}`),
|
|
} {
|
|
if got := string(SanitizeCodexInputItemIDs(body)); got != string(body) {
|
|
t.Fatalf("payload changed: got=%q want=%q", got, body)
|
|
}
|
|
}
|
|
}
|