25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

189 satır
6.2KB

  1. package mapping
  2. import (
  3. "testing"
  4. "time"
  5. "qctextbuilder/internal/domain"
  6. )
  7. func TestSuggestFieldValues_FillsEmptyMappedFields(t *testing.T) {
  8. t.Parallel()
  9. fields := []domain.TemplateField{
  10. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  11. {Path: "services.servicesTitle_r4830_8", KeyName: "servicesTitle_r4830_8", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionServices},
  12. {Path: "services.servicesDescription_r4830_9", KeyName: "servicesDescription_r4830_9", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionServices},
  13. {Path: "text.buttonText_c1165_1", KeyName: "buttonText_c1165_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionCTA},
  14. }
  15. result := SuggestFieldValues(SuggestionRequest{
  16. Fields: fields,
  17. GlobalData: map[string]any{
  18. "companyName": "Muster AG",
  19. "businessType": "Solar",
  20. },
  21. DraftContext: &domain.DraftContext{
  22. LLM: domain.DraftLLMContext{
  23. WebsiteSummary: "Wir planen und installieren Solaranlagen fuer KMU und Privatkunden.",
  24. StyleProfile: domain.DraftStyleProfile{
  25. ContentTone: "professionell",
  26. },
  27. },
  28. },
  29. Existing: map[string]string{},
  30. })
  31. if _, ok := result.ByFieldPath["text.textTitle_m1710_1"]; !ok {
  32. t.Fatalf("expected hero title suggestion")
  33. }
  34. if got := result.ByFieldPath["text.buttonText_c1165_1"].Value; got == "" {
  35. t.Fatalf("expected cta suggestion")
  36. }
  37. if got := result.ByFieldPath["services.servicesDescription_r4830_9"].Slot; got != "service_items[0].description" {
  38. t.Fatalf("unexpected slot: %q", got)
  39. }
  40. }
  41. func TestSuggestFieldValues_RespectsExistingValues(t *testing.T) {
  42. t.Parallel()
  43. fields := []domain.TemplateField{
  44. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  45. }
  46. result := SuggestFieldValues(SuggestionRequest{
  47. Fields: fields,
  48. GlobalData: map[string]any{
  49. "companyName": "Muster AG",
  50. },
  51. Existing: map[string]string{
  52. "text.textTitle_m1710_1": "Schon gesetzt",
  53. },
  54. })
  55. if len(result.Suggestions) != 0 {
  56. t.Fatalf("expected no suggestions, got %d", len(result.Suggestions))
  57. }
  58. }
  59. func TestGenerateAllSuggestions_IncludesFilledFields(t *testing.T) {
  60. t.Parallel()
  61. fields := []domain.TemplateField{
  62. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  63. }
  64. state := GenerateAllSuggestions(SuggestionRequest{
  65. Fields: fields,
  66. GlobalData: map[string]any{
  67. "companyName": "Muster AG",
  68. },
  69. Existing: map[string]string{
  70. "text.textTitle_m1710_1": "Bereits gesetzt",
  71. },
  72. }, domain.DraftSuggestionState{}, time.Now().UTC())
  73. if _, ok := state.ByFieldPath["text.textTitle_m1710_1"]; !ok {
  74. t.Fatalf("expected suggestion for filled field")
  75. }
  76. }
  77. func TestApplySuggestionsToEmptyFields_DoesNotOverwriteExisting(t *testing.T) {
  78. t.Parallel()
  79. now := time.Now().UTC()
  80. values, state := ApplySuggestionsToEmptyFields(map[string]string{
  81. "field.hero": "Custom",
  82. }, domain.DraftSuggestionState{
  83. ByFieldPath: map[string]domain.DraftSuggestion{
  84. "field.hero": {
  85. FieldPath: "field.hero",
  86. Value: "Suggestion",
  87. Status: domain.DraftSuggestionStatusSuggested,
  88. },
  89. "field.cta": {
  90. FieldPath: "field.cta",
  91. Value: "Jetzt anfragen",
  92. Status: domain.DraftSuggestionStatusSuggested,
  93. },
  94. },
  95. }, now)
  96. if got := values["field.hero"]; got != "Custom" {
  97. t.Fatalf("expected existing value unchanged, got %q", got)
  98. }
  99. if got := values["field.cta"]; got != "Jetzt anfragen" {
  100. t.Fatalf("expected empty value filled, got %q", got)
  101. }
  102. if got := state.ByFieldPath["field.hero"].Status; got != domain.DraftSuggestionStatusSuggested {
  103. t.Fatalf("expected hero status unchanged, got %q", got)
  104. }
  105. if got := state.ByFieldPath["field.cta"].Status; got != domain.DraftSuggestionStatusApplied {
  106. t.Fatalf("expected cta status applied, got %q", got)
  107. }
  108. }
  109. func TestApplyAllSuggestions_OverwritesExisting(t *testing.T) {
  110. t.Parallel()
  111. now := time.Now().UTC()
  112. values, state := ApplyAllSuggestions(map[string]string{
  113. "field.hero": "Custom",
  114. }, domain.DraftSuggestionState{
  115. ByFieldPath: map[string]domain.DraftSuggestion{
  116. "field.hero": {
  117. FieldPath: "field.hero",
  118. Value: "Suggestion",
  119. Status: domain.DraftSuggestionStatusSuggested,
  120. },
  121. "field.cta": {
  122. FieldPath: "field.cta",
  123. Value: "Jetzt anfragen",
  124. Status: domain.DraftSuggestionStatusSuggested,
  125. },
  126. },
  127. }, now)
  128. if got := values["field.hero"]; got != "Suggestion" {
  129. t.Fatalf("expected existing value overwritten, got %q", got)
  130. }
  131. if got := values["field.cta"]; got != "Jetzt anfragen" {
  132. t.Fatalf("expected cta applied, got %q", got)
  133. }
  134. if got := state.ByFieldPath["field.hero"].Status; got != domain.DraftSuggestionStatusApplied {
  135. t.Fatalf("expected hero status applied, got %q", got)
  136. }
  137. if got := state.ByFieldPath["field.cta"].Status; got != domain.DraftSuggestionStatusApplied {
  138. t.Fatalf("expected cta status applied, got %q", got)
  139. }
  140. }
  141. func TestRegenerateFieldSuggestion_OnlyChangesTargetField(t *testing.T) {
  142. t.Parallel()
  143. fields := []domain.TemplateField{
  144. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  145. {Path: "text.buttonText_c1165_1", KeyName: "buttonText_c1165_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionCTA},
  146. }
  147. current := domain.DraftSuggestionState{
  148. ByFieldPath: map[string]domain.DraftSuggestion{
  149. "text.textTitle_m1710_1": {FieldPath: "text.textTitle_m1710_1", Value: "Old Hero"},
  150. "text.buttonText_c1165_1": {FieldPath: "text.buttonText_c1165_1", Value: "Old CTA"},
  151. },
  152. }
  153. updated := RegenerateFieldSuggestion(SuggestionRequest{
  154. Fields: fields,
  155. GlobalData: map[string]any{
  156. "companyName": "Muster AG",
  157. },
  158. }, current, "text.buttonText_c1165_1", time.Now().UTC())
  159. if got := updated.ByFieldPath["text.textTitle_m1710_1"].Value; got != "Old Hero" {
  160. t.Fatalf("expected untargeted field unchanged, got %q", got)
  161. }
  162. if got := updated.ByFieldPath["text.buttonText_c1165_1"].Value; got == "" || got == "Old CTA" {
  163. t.Fatalf("expected target field regenerated, got %q", got)
  164. }
  165. }