feat(xai): also allow grok imagine aspect_ratio 20:9

Grok Imagine already supports the 20:9 wide phone ratio. Accept it
from aspect_ratio and from size, matching the 9:20 whitelist change.
This commit is contained in:
Fesaluo
2026-09-18 04:04:24 +08:00
parent 75bd6a60eb
commit f049e00b76
2 changed files with 36 additions and 0 deletions

View File

@@ -301,6 +301,8 @@ func xaiImagesAspectRatio(raw string, fallback string) string {
return "9:16"
case "9:20":
return "9:20"
case "20:9":
return "20:9"
case "4:3":
return "4:3"
case "3:4":
@@ -325,6 +327,8 @@ func xaiImagesAspectRatioFromSize(size string, fallback string) string {
return "9:16"
case "9:20":
return "9:20"
case "20:9":
return "20:9"
case "1536x1024", "3:2":
return "3:2"
case "1024x1536", "2:3":

View File

@@ -164,16 +164,48 @@ func TestBuildXAIImagesGenerationsRequestNineByTwentyFromSize(t *testing.T) {
}
}
func TestBuildXAIImagesGenerationsRequestTwentyByNine(t *testing.T) {
rawJSON := []byte(`{"model":"grok-imagine-image-quality","prompt":"kitten","aspect_ratio":"20:9","resolution":"2k","n":1,"response_format":"b64_json"}`)
req := buildXAIImagesGenerationsRequest(rawJSON, "grok-imagine-image-quality", "b64_json")
if got := gjson.GetBytes(req, "aspect_ratio").String(); got != "20:9" {
t.Fatalf("aspect_ratio = %q, want 20:9", got)
}
if got := gjson.GetBytes(req, "resolution").String(); got != "2k" {
t.Fatalf("resolution = %q, want 2k", got)
}
}
func TestBuildXAIImagesGenerationsRequestTwentyByNineFromSize(t *testing.T) {
rawJSON := []byte(`{"model":"grok-imagine-image-quality","prompt":"kitten","size":"20:9","resolution":"2k"}`)
req := buildXAIImagesGenerationsRequest(rawJSON, "grok-imagine-image-quality", "b64_json")
if got := gjson.GetBytes(req, "aspect_ratio").String(); got != "20:9" {
t.Fatalf("aspect_ratio from size = %q, want 20:9", got)
}
if got := gjson.GetBytes(req, "resolution").String(); got != "2k" {
t.Fatalf("resolution = %q, want 2k", got)
}
}
func TestXAIImagesAspectRatioNineByTwenty(t *testing.T) {
if got := xaiImagesAspectRatio("9:20", "1:1"); got != "9:20" {
t.Fatalf("xaiImagesAspectRatio(9:20) = %q, want 9:20", got)
}
if got := xaiImagesAspectRatio("20:9", "1:1"); got != "20:9" {
t.Fatalf("xaiImagesAspectRatio(20:9) = %q, want 20:9", got)
}
if got := xaiImagesAspectRatio("9:21", "1:1"); got != "1:1" {
t.Fatalf("unknown ratio should keep fallback, got %q", got)
}
if got := xaiImagesAspectRatioFromSize("9:20", ""); got != "9:20" {
t.Fatalf("xaiImagesAspectRatioFromSize(9:20) = %q, want 9:20", got)
}
if got := xaiImagesAspectRatioFromSize("20:9", ""); got != "20:9" {
t.Fatalf("xaiImagesAspectRatioFromSize(20:9) = %q, want 20:9", got)
}
}
func TestBuildXAIImagesEditRequest(t *testing.T) {