Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

96 wiersze
2.0KB

  1. package onboarding
  2. import (
  3. "testing"
  4. "qctextbuilder/internal/qcclient"
  5. )
  6. func TestDetectFieldKind(t *testing.T) {
  7. tests := []struct {
  8. name string
  9. path string
  10. sample string
  11. want string
  12. }{
  13. {
  14. name: "exact image token",
  15. path: "hero.image",
  16. sample: "#IMAGE#",
  17. want: "image",
  18. },
  19. {
  20. name: "image placeholder bracketed",
  21. path: "gallery.galleryImage_m4178_15",
  22. sample: "[image image image image]",
  23. want: "image",
  24. },
  25. {
  26. name: "image path hint even with text sample",
  27. path: "section.thumbnailUrl",
  28. sample: "headline",
  29. want: "image",
  30. },
  31. {
  32. name: "empty sample is unknown",
  33. path: "hero.title",
  34. sample: "",
  35. want: "unknown",
  36. },
  37. {
  38. name: "plain text field",
  39. path: "hero.title",
  40. sample: "This is a headline",
  41. want: "text",
  42. },
  43. }
  44. for _, tt := range tests {
  45. t.Run(tt.name, func(t *testing.T) {
  46. got := detectFieldKind(tt.path, tt.sample)
  47. if got != tt.want {
  48. t.Fatalf("detectFieldKind() = %q, want %q", got, tt.want)
  49. }
  50. })
  51. }
  52. }
  53. func TestFlattenDiscovery_DisablesImageLikeFields(t *testing.T) {
  54. data := qcclient.GenerateContentData{
  55. "gallery": {
  56. "galleryImage_m4178_15": "[image image image image]",
  57. },
  58. "hero": {
  59. "title": "Welcome",
  60. },
  61. }
  62. fields := flattenDiscovery(99, "manifest-1", data)
  63. if len(fields) != 2 {
  64. t.Fatalf("flattenDiscovery() field count = %d, want 2", len(fields))
  65. }
  66. byPath := map[string]string{}
  67. enabled := map[string]bool{}
  68. for _, f := range fields {
  69. byPath[f.Path] = f.FieldKind
  70. enabled[f.Path] = f.IsEnabled
  71. }
  72. imagePath := "gallery.galleryImage_m4178_15"
  73. if byPath[imagePath] != "image" {
  74. t.Fatalf("field %s kind = %q, want image", imagePath, byPath[imagePath])
  75. }
  76. if enabled[imagePath] {
  77. t.Fatalf("field %s should be disabled by default", imagePath)
  78. }
  79. textPath := "hero.title"
  80. if byPath[textPath] != "text" {
  81. t.Fatalf("field %s kind = %q, want text", textPath, byPath[textPath])
  82. }
  83. if !enabled[textPath] {
  84. t.Fatalf("field %s should be enabled by default", textPath)
  85. }
  86. }