Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

298 рядки
9.9KB

  1. package mapping
  2. import (
  3. "context"
  4. "encoding/json"
  5. "testing"
  6. "time"
  7. "qctextbuilder/internal/domain"
  8. "qctextbuilder/internal/qcclient"
  9. )
  10. func TestSuggestFieldValues_FillsEmptyMappedFields(t *testing.T) {
  11. t.Parallel()
  12. fields := []domain.TemplateField{
  13. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  14. {Path: "services.servicesTitle_r4830_8", KeyName: "servicesTitle_r4830_8", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionServices},
  15. {Path: "services.servicesDescription_r4830_9", KeyName: "servicesDescription_r4830_9", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionServices},
  16. {Path: "text.buttonText_c1165_1", KeyName: "buttonText_c1165_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionCTA},
  17. }
  18. result := SuggestFieldValues(SuggestionRequest{
  19. Fields: fields,
  20. GlobalData: map[string]any{
  21. "companyName": "Muster AG",
  22. "businessType": "Solar",
  23. },
  24. DraftContext: &domain.DraftContext{
  25. LLM: domain.DraftLLMContext{
  26. WebsiteSummary: "Wir planen und installieren Solaranlagen fuer KMU und Privatkunden.",
  27. StyleProfile: domain.DraftStyleProfile{
  28. ContentTone: "professionell",
  29. },
  30. },
  31. },
  32. Existing: map[string]string{},
  33. })
  34. if _, ok := result.ByFieldPath["text.textTitle_m1710_1"]; !ok {
  35. t.Fatalf("expected hero title suggestion")
  36. }
  37. if got := result.ByFieldPath["text.buttonText_c1165_1"].Value; got == "" {
  38. t.Fatalf("expected cta suggestion")
  39. }
  40. if got := result.ByFieldPath["services.servicesDescription_r4830_9"].Slot; got != "service_items[0].description" {
  41. t.Fatalf("unexpected slot: %q", got)
  42. }
  43. }
  44. func TestSuggestFieldValues_RespectsExistingValues(t *testing.T) {
  45. t.Parallel()
  46. fields := []domain.TemplateField{
  47. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  48. }
  49. result := SuggestFieldValues(SuggestionRequest{
  50. Fields: fields,
  51. GlobalData: map[string]any{
  52. "companyName": "Muster AG",
  53. },
  54. Existing: map[string]string{
  55. "text.textTitle_m1710_1": "Schon gesetzt",
  56. },
  57. })
  58. if len(result.Suggestions) != 0 {
  59. t.Fatalf("expected no suggestions, got %d", len(result.Suggestions))
  60. }
  61. }
  62. func TestGenerateAllSuggestions_IncludesFilledFields(t *testing.T) {
  63. t.Parallel()
  64. fields := []domain.TemplateField{
  65. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  66. }
  67. state := GenerateAllSuggestions(context.Background(), nil, SuggestionRequest{
  68. Fields: fields,
  69. GlobalData: map[string]any{
  70. "companyName": "Muster AG",
  71. },
  72. Existing: map[string]string{
  73. "text.textTitle_m1710_1": "Bereits gesetzt",
  74. },
  75. }, domain.DraftSuggestionState{}, time.Now().UTC())
  76. if _, ok := state.ByFieldPath["text.textTitle_m1710_1"]; !ok {
  77. t.Fatalf("expected suggestion for filled field")
  78. }
  79. }
  80. func TestApplySuggestionsToEmptyFields_DoesNotOverwriteExisting(t *testing.T) {
  81. t.Parallel()
  82. now := time.Now().UTC()
  83. values, state := ApplySuggestionsToEmptyFields(map[string]string{
  84. "field.hero": "Custom",
  85. }, domain.DraftSuggestionState{
  86. ByFieldPath: map[string]domain.DraftSuggestion{
  87. "field.hero": {
  88. FieldPath: "field.hero",
  89. Value: "Suggestion",
  90. Status: domain.DraftSuggestionStatusSuggested,
  91. },
  92. "field.cta": {
  93. FieldPath: "field.cta",
  94. Value: "Jetzt anfragen",
  95. Status: domain.DraftSuggestionStatusSuggested,
  96. },
  97. },
  98. }, now)
  99. if got := values["field.hero"]; got != "Custom" {
  100. t.Fatalf("expected existing value unchanged, got %q", got)
  101. }
  102. if got := values["field.cta"]; got != "Jetzt anfragen" {
  103. t.Fatalf("expected empty value filled, got %q", got)
  104. }
  105. if got := state.ByFieldPath["field.hero"].Status; got != domain.DraftSuggestionStatusSuggested {
  106. t.Fatalf("expected hero status unchanged, got %q", got)
  107. }
  108. if got := state.ByFieldPath["field.cta"].Status; got != domain.DraftSuggestionStatusApplied {
  109. t.Fatalf("expected cta status applied, got %q", got)
  110. }
  111. }
  112. func TestApplyAllSuggestions_OverwritesExisting(t *testing.T) {
  113. t.Parallel()
  114. now := time.Now().UTC()
  115. values, state := ApplyAllSuggestions(map[string]string{
  116. "field.hero": "Custom",
  117. }, domain.DraftSuggestionState{
  118. ByFieldPath: map[string]domain.DraftSuggestion{
  119. "field.hero": {
  120. FieldPath: "field.hero",
  121. Value: "Suggestion",
  122. Status: domain.DraftSuggestionStatusSuggested,
  123. },
  124. "field.cta": {
  125. FieldPath: "field.cta",
  126. Value: "Jetzt anfragen",
  127. Status: domain.DraftSuggestionStatusSuggested,
  128. },
  129. },
  130. }, now)
  131. if got := values["field.hero"]; got != "Suggestion" {
  132. t.Fatalf("expected existing value overwritten, got %q", got)
  133. }
  134. if got := values["field.cta"]; got != "Jetzt anfragen" {
  135. t.Fatalf("expected cta applied, got %q", got)
  136. }
  137. if got := state.ByFieldPath["field.hero"].Status; got != domain.DraftSuggestionStatusApplied {
  138. t.Fatalf("expected hero status applied, got %q", got)
  139. }
  140. if got := state.ByFieldPath["field.cta"].Status; got != domain.DraftSuggestionStatusApplied {
  141. t.Fatalf("expected cta status applied, got %q", got)
  142. }
  143. }
  144. func TestRegenerateFieldSuggestion_OnlyChangesTargetField(t *testing.T) {
  145. t.Parallel()
  146. fields := []domain.TemplateField{
  147. {Path: "text.textTitle_m1710_1", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  148. {Path: "text.buttonText_c1165_1", KeyName: "buttonText_c1165_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionCTA},
  149. }
  150. current := domain.DraftSuggestionState{
  151. ByFieldPath: map[string]domain.DraftSuggestion{
  152. "text.textTitle_m1710_1": {FieldPath: "text.textTitle_m1710_1", Value: "Old Hero"},
  153. "text.buttonText_c1165_1": {FieldPath: "text.buttonText_c1165_1", Value: "Old CTA"},
  154. },
  155. }
  156. updated := RegenerateFieldSuggestion(context.Background(), nil, SuggestionRequest{
  157. Fields: fields,
  158. GlobalData: map[string]any{
  159. "companyName": "Muster AG",
  160. },
  161. }, current, "text.buttonText_c1165_1", time.Now().UTC())
  162. if got := updated.ByFieldPath["text.textTitle_m1710_1"].Value; got != "Old Hero" {
  163. t.Fatalf("expected untargeted field unchanged, got %q", got)
  164. }
  165. if got := updated.ByFieldPath["text.buttonText_c1165_1"].Value; got == "" || got == "Old CTA" {
  166. t.Fatalf("expected target field regenerated, got %q", got)
  167. }
  168. }
  169. func TestGenerateAllSuggestions_UsesGeneratorFallbackWhenPrimaryPartial(t *testing.T) {
  170. t.Parallel()
  171. fields := []domain.TemplateField{
  172. {Path: "text.textTitle_m1710_1", Section: "text", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  173. {Path: "text.buttonText_c1165_1", Section: "text", KeyName: "buttonText_c1165_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionCTA},
  174. }
  175. generator := NewCompositeSuggestionGenerator(
  176. NewLLMSuggestionGenerator(&stubQCClient{
  177. generateContent: qcclient.GenerateContentData{
  178. "text": {
  179. "textTitle_m1710_1": "LLM Hero",
  180. },
  181. },
  182. }),
  183. NewRuleBasedSuggestionGenerator(),
  184. )
  185. state := GenerateAllSuggestions(context.Background(), generator, SuggestionRequest{
  186. TemplateID: 101,
  187. Fields: fields,
  188. GlobalData: map[string]any{"companyName": "Muster AG"},
  189. Existing: map[string]string{},
  190. }, domain.DraftSuggestionState{}, time.Now().UTC())
  191. hero := state.ByFieldPath["text.textTitle_m1710_1"]
  192. if hero.Source != domain.DraftSuggestionSourceLLM {
  193. t.Fatalf("expected hero source llm, got %q", hero.Source)
  194. }
  195. cta := state.ByFieldPath["text.buttonText_c1165_1"]
  196. if cta.Source != domain.DraftSuggestionSourceFallbackRuleBased {
  197. t.Fatalf("expected cta source fallback rule-based, got %q", cta.Source)
  198. }
  199. }
  200. func TestGenerateAllSuggestions_FallsBackWhenLLMReturnsInvalidValueType(t *testing.T) {
  201. t.Parallel()
  202. fields := []domain.TemplateField{
  203. {Path: "text.textTitle_m1710_1", Section: "text", KeyName: "textTitle_m1710_1", FieldKind: "text", IsEnabled: true, WebsiteSection: domain.WebsiteSectionHero},
  204. }
  205. generator := NewCompositeSuggestionGenerator(
  206. NewLLMSuggestionGenerator(&stubQCClient{
  207. generateContent: qcclient.GenerateContentData{
  208. "text": {
  209. "textTitle_m1710_1": true,
  210. },
  211. },
  212. }),
  213. NewRuleBasedSuggestionGenerator(),
  214. )
  215. state := GenerateAllSuggestions(context.Background(), generator, SuggestionRequest{
  216. TemplateID: 202,
  217. Fields: fields,
  218. GlobalData: map[string]any{"companyName": "Muster AG"},
  219. Existing: map[string]string{},
  220. }, domain.DraftSuggestionState{}, time.Now().UTC())
  221. hero := state.ByFieldPath["text.textTitle_m1710_1"]
  222. if hero.Value == "" {
  223. t.Fatalf("expected fallback suggestion value")
  224. }
  225. if hero.Source != domain.DraftSuggestionSourceFallbackRuleBased {
  226. t.Fatalf("expected fallback source, got %q", hero.Source)
  227. }
  228. }
  229. type stubQCClient struct {
  230. generateContent qcclient.GenerateContentData
  231. generateErr error
  232. }
  233. func (s *stubQCClient) Health(context.Context) error { return nil }
  234. func (s *stubQCClient) ListAITemplates(context.Context) ([]qcclient.Template, error) {
  235. return nil, nil
  236. }
  237. func (s *stubQCClient) GetTemplate(context.Context, int64) (*qcclient.Template, error) {
  238. return nil, nil
  239. }
  240. func (s *stubQCClient) GetTemplateSchema(context.Context, int64) (json.RawMessage, error) {
  241. return nil, nil
  242. }
  243. func (s *stubQCClient) GenerateContent(context.Context, qcclient.GenerateContentRequest) (qcclient.GenerateContentData, json.RawMessage, error) {
  244. if s.generateErr != nil {
  245. return nil, nil, s.generateErr
  246. }
  247. return s.generateContent, nil, nil
  248. }
  249. func (s *stubQCClient) CreateSite(context.Context, qcclient.CreateSiteRequest) (*qcclient.CreateSiteResponseData, json.RawMessage, error) {
  250. return nil, nil, nil
  251. }
  252. func (s *stubQCClient) GetJob(context.Context, int64) (*qcclient.JobStatusData, json.RawMessage, error) {
  253. return nil, nil, nil
  254. }
  255. func (s *stubQCClient) GetEditorURL(context.Context, int64) (*qcclient.SiteEditorLoginData, json.RawMessage, error) {
  256. return nil, nil, nil
  257. }