Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

104 lines
2.4KB

  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. websiteSections := map[string]string{}
  69. for _, f := range fields {
  70. byPath[f.Path] = f.FieldKind
  71. enabled[f.Path] = f.IsEnabled
  72. websiteSections[f.Path] = f.WebsiteSection
  73. }
  74. imagePath := "gallery.galleryImage_m4178_15"
  75. if byPath[imagePath] != "image" {
  76. t.Fatalf("field %s kind = %q, want image", imagePath, byPath[imagePath])
  77. }
  78. if enabled[imagePath] {
  79. t.Fatalf("field %s should be disabled by default", imagePath)
  80. }
  81. if websiteSections[imagePath] != "gallery" {
  82. t.Fatalf("field %s websiteSection = %q, want gallery", imagePath, websiteSections[imagePath])
  83. }
  84. textPath := "hero.title"
  85. if byPath[textPath] != "text" {
  86. t.Fatalf("field %s kind = %q, want text", textPath, byPath[textPath])
  87. }
  88. if !enabled[textPath] {
  89. t.Fatalf("field %s should be enabled by default", textPath)
  90. }
  91. if websiteSections[textPath] != "hero" {
  92. t.Fatalf("field %s websiteSection = %q, want hero", textPath, websiteSections[textPath])
  93. }
  94. }