openai_x_search_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package handler
  2. import (
  3. "testing"
  4. "github.com/Wei-Shaw/sub2api/internal/pkg/xai"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tidwall/gjson"
  7. )
  8. func TestBuildGrokXSearchResponsesBody(t *testing.T) {
  9. t.Parallel()
  10. understandImages := true
  11. understandVideos := false
  12. body, err := buildGrokXSearchResponsesBody(grokStandaloneSearchRequest{
  13. Query: "latest posts from xAI",
  14. AllowedXHandles: []string{"xai"},
  15. ExcludedXHandles: []string{"spam"},
  16. FromDate: "2026-08-01",
  17. ToDate: "2026-08-10",
  18. EnableImageUnderstanding: &understandImages,
  19. EnableVideoUnderstanding: &understandVideos,
  20. }, xai.DefaultTextModel)
  21. require.NoError(t, err)
  22. require.Equal(t, xai.DefaultTextModel, gjson.GetBytes(body, "model").String())
  23. require.Contains(t, gjson.GetBytes(body, "input").String(), "latest posts from xAI")
  24. require.Contains(t, gjson.GetBytes(body, "input").String(), "Return ONLY valid JSON")
  25. require.Equal(t, "x_search_call.action.sources", gjson.GetBytes(body, "include.0").String())
  26. require.Equal(t, "required", gjson.GetBytes(body, "tool_choice").String())
  27. require.Equal(t, "x_search", gjson.GetBytes(body, "tools.0.type").String())
  28. require.Equal(t, "xai", gjson.GetBytes(body, "tools.0.allowed_x_handles.0").String())
  29. require.Equal(t, "spam", gjson.GetBytes(body, "tools.0.excluded_x_handles.0").String())
  30. require.Equal(t, "2026-08-01", gjson.GetBytes(body, "tools.0.from_date").String())
  31. require.Equal(t, "2026-08-10", gjson.GetBytes(body, "tools.0.to_date").String())
  32. require.True(t, gjson.GetBytes(body, "tools.0.enable_image_understanding").Bool())
  33. require.False(t, gjson.GetBytes(body, "tools.0.enable_video_understanding").Bool())
  34. require.False(t, gjson.GetBytes(body, "store").Bool())
  35. require.False(t, gjson.GetBytes(body, "stream").Bool())
  36. }
  37. func TestBuildGrokXSearchResponsesBodyAcceptsInputAlias(t *testing.T) {
  38. t.Parallel()
  39. body, err := buildGrokXSearchResponsesBody(grokStandaloneSearchRequest{Input: "latest posts from xAI"}, xai.DefaultTextModel)
  40. require.NoError(t, err)
  41. require.Contains(t, gjson.GetBytes(body, "input").String(), "latest posts from xAI")
  42. }
  43. func TestResolveGrokStandaloneSearchModelUsesRuntimeDefault(t *testing.T) {
  44. original := xai.RuntimeModelMappingOptions()
  45. t.Cleanup(func() { xai.SetRuntimeModelMappingOptions(original) })
  46. xai.SetRuntimeModelMappingOptions(xai.ModelMappingOptions{DefaultText: "grok-4.6"})
  47. model := resolveGrokStandaloneSearchModel()
  48. body, err := buildGrokXSearchResponsesBody(grokStandaloneSearchRequest{Query: "latest posts from xAI"}, model)
  49. require.NoError(t, err)
  50. require.Equal(t, "grok-4.6", model)
  51. require.Equal(t, model, gjson.GetBytes(body, "model").String())
  52. }