image_generation_intent_grok_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package service
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/require"
  5. )
  6. func TestIsImageGenerationIntentForPlatform_GrokCodexDeclarations(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. body string
  10. want bool
  11. }{
  12. {
  13. name: "top-level image_gen namespace is passive",
  14. body: `{"model":"grok-4.5","tools":[{"type":"namespace","name":"image_gen","tools":[{"type":"function","name":"imagegen"}]}],"tool_choice":"auto","input":"write code"}`,
  15. },
  16. {
  17. name: "responses lite additional_tools image_gen is passive",
  18. body: `{"model":"grok-4.5","tool_choice":"auto","input":[{"type":"additional_tools","tools":[{"type":"namespace","name":"image_gen","tools":[{"type":"function","name":"imagegen"}]}]},{"type":"message","role":"user","content":"write code"}]}`,
  19. },
  20. {
  21. name: "legacy adapter flattened image_gen function declaration is passive",
  22. body: `{"model":"grok-4.5","tools":[{"type":"function","name":"image_gen.imagegen"}],"input":"write code"}`,
  23. },
  24. {
  25. name: "native image generation declaration remains explicit",
  26. body: `{"model":"grok-4.5","tools":[{"type":"image_generation"}],"input":"draw a cat"}`,
  27. want: true,
  28. },
  29. {
  30. name: "native image generation tool choice is explicit",
  31. body: `{"model":"grok-4.5","tools":[{"type":"image_generation"}],"tool_choice":{"type":"image_generation"},"input":"draw a cat"}`,
  32. want: true,
  33. },
  34. {
  35. name: "image_gen namespace tool choice is explicit",
  36. body: `{"model":"grok-4.5","tools":[{"type":"namespace","name":"image_gen"}],"tool_choice":{"type":"namespace","name":"image_gen"},"input":"draw a cat"}`,
  37. want: true,
  38. },
  39. {
  40. name: "flattened image_gen function tool choice is explicit",
  41. body: `{"model":"grok-4.5","tools":[{"type":"function","name":"image_gen.imagegen"}],"tool_choice":{"type":"function","name":"image_gen.imagegen"},"input":"draw a cat"}`,
  42. want: true,
  43. },
  44. {
  45. name: "wrapped image_gen function tool choice is explicit",
  46. body: `{"model":"grok-4.5","tools":[{"type":"function","name":"image_gen.imagegen"}],"tool_choice":{"tool":{"type":"function","name":"image_gen.imagegen"}},"input":"draw a cat"}`,
  47. want: true,
  48. },
  49. {
  50. name: "vision input is not image generation",
  51. body: `{"model":"grok-4.5","input":[{"type":"message","role":"user","content":[{"type":"input_image","image_url":"data:image/png;base64,AA=="}]}]}`,
  52. },
  53. {
  54. name: "ordinary function declaration is not image generation",
  55. body: `{"model":"grok-4.5","tools":[{"type":"function","name":"lookup"}],"tool_choice":"auto","input":"write code"}`,
  56. },
  57. {
  58. name: "image_gen function call history is not current intent",
  59. body: `{"model":"grok-4.5","input":[{"type":"function_call","namespace":"image_gen","name":"imagegen","arguments":"{}"}]}`,
  60. },
  61. {
  62. name: "image generation call history is not current intent",
  63. body: `{"model":"grok-4.5","input":[{"type":"image_generation_call","id":"ig_1"}]}`,
  64. },
  65. }
  66. for _, tt := range tests {
  67. t.Run(tt.name, func(t *testing.T) {
  68. require.Equal(t, tt.want, IsImageGenerationIntentForPlatform(
  69. openAIResponsesEndpoint,
  70. "grok-4.5",
  71. []byte(tt.body),
  72. PlatformGrok,
  73. ))
  74. })
  75. }
  76. }
  77. func TestIsImageGenerationIntentForPlatform_GrokPreservesHardSignals(t *testing.T) {
  78. require.True(t, IsImageGenerationIntentForPlatform(
  79. "/v1/images/generations",
  80. "grok-4.5",
  81. []byte(`{"input":"draw"}`),
  82. PlatformGrok,
  83. ))
  84. require.True(t, IsImageGenerationIntentForPlatform(
  85. openAIResponsesEndpoint,
  86. "gpt-image-2",
  87. []byte(`{"input":"draw"}`),
  88. PlatformGrok,
  89. ))
  90. require.True(t, IsImageGenerationIntentForPlatform(
  91. openAIResponsesEndpoint,
  92. "grok-4.5",
  93. []byte(`{"model":"gpt-image-2","input":"draw"}`),
  94. PlatformGrok,
  95. ))
  96. }
  97. func TestIsImageGenerationIntentForPlatform_OtherPlatformsKeepDeclarationSemantics(t *testing.T) {
  98. body := []byte(`{"model":"gpt-5.5","tools":[{"type":"namespace","name":"image_gen","tools":[{"type":"function","name":"imagegen"}]}],"input":"write code"}`)
  99. require.True(t, IsImageGenerationIntentForPlatform(openAIResponsesEndpoint, "gpt-5.5", body, PlatformOpenAI))
  100. require.True(t, IsImageGenerationIntentForPlatform(openAIResponsesEndpoint, "gpt-5.5", body, PlatformAnthropic))
  101. }