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.

354 wiersze
12KB

  1. package mapping
  2. import (
  3. "testing"
  4. "qctextbuilder/internal/domain"
  5. )
  6. func TestMapTemplateFieldsToSemanticSlots(t *testing.T) {
  7. t.Parallel()
  8. fields := []domain.TemplateField{
  9. {
  10. Path: "text.textTitle_m1710_1",
  11. KeyName: "textTitle_m1710_1",
  12. FieldKind: "text",
  13. IsEnabled: true,
  14. WebsiteSection: domain.WebsiteSectionHero,
  15. },
  16. {
  17. Path: "text.introTitle_c7886_1",
  18. KeyName: "introTitle_c7886_1",
  19. FieldKind: "text",
  20. IsEnabled: true,
  21. WebsiteSection: domain.WebsiteSectionIntro,
  22. },
  23. {
  24. Path: "text.introDescription_c7886_2",
  25. KeyName: "introDescription_c7886_2",
  26. FieldKind: "text",
  27. IsEnabled: true,
  28. WebsiteSection: domain.WebsiteSectionIntro,
  29. },
  30. {
  31. Path: "services.serviceTitle_r4830_1",
  32. KeyName: "serviceTitle_r4830_1",
  33. FieldKind: "text",
  34. IsEnabled: true,
  35. WebsiteSection: domain.WebsiteSectionServices,
  36. },
  37. {
  38. Path: "services.serviceDescription_r4830_2",
  39. KeyName: "serviceDescription_r4830_2",
  40. FieldKind: "text",
  41. IsEnabled: true,
  42. WebsiteSection: domain.WebsiteSectionServices,
  43. },
  44. {
  45. Path: "team.teamMemberName_r4748_1",
  46. KeyName: "teamMemberName_r4748_1",
  47. FieldKind: "text",
  48. IsEnabled: true,
  49. WebsiteSection: domain.WebsiteSectionTeam,
  50. },
  51. {
  52. Path: "team.teamDescription_r4748_2",
  53. KeyName: "teamDescription_r4748_2",
  54. FieldKind: "text",
  55. IsEnabled: true,
  56. WebsiteSection: domain.WebsiteSectionTeam,
  57. },
  58. {
  59. Path: "testimonials.testimonialTitle_r1508_1",
  60. KeyName: "testimonialTitle_r1508_1",
  61. FieldKind: "text",
  62. IsEnabled: true,
  63. WebsiteSection: domain.WebsiteSectionTestimonials,
  64. },
  65. {
  66. Path: "testimonials.testimonialDescription_r1508_2",
  67. KeyName: "testimonialDescription_r1508_2",
  68. FieldKind: "text",
  69. IsEnabled: true,
  70. WebsiteSection: domain.WebsiteSectionTestimonials,
  71. },
  72. {
  73. Path: "testimonials.testimonialName_r1508_3",
  74. KeyName: "testimonialName_r1508_3",
  75. FieldKind: "text",
  76. IsEnabled: true,
  77. WebsiteSection: domain.WebsiteSectionTestimonials,
  78. },
  79. {
  80. Path: "text.buttonText_c1165_1",
  81. KeyName: "buttonText_c1165_1",
  82. FieldKind: "text",
  83. IsEnabled: true,
  84. WebsiteSection: domain.WebsiteSectionCTA,
  85. },
  86. }
  87. got := MapTemplateFieldsToSemanticSlots(fields)
  88. if len(got.Targets) != 11 {
  89. t.Fatalf("expected 11 targets, got %d", len(got.Targets))
  90. }
  91. assertHasSlotPath(t, got, "hero.title", "text.textTitle_m1710_1")
  92. assertHasSlotPath(t, got, "intro.title", "text.introTitle_c7886_1")
  93. assertHasSlotPath(t, got, "intro.description", "text.introDescription_c7886_2")
  94. assertHasSlotPath(t, got, "service_items[0].title", "services.serviceTitle_r4830_1")
  95. assertHasSlotPath(t, got, "service_items[0].description", "services.serviceDescription_r4830_2")
  96. assertHasSlotPath(t, got, "team_items[0].name", "team.teamMemberName_r4748_1")
  97. assertHasSlotPath(t, got, "team_items[0].description", "team.teamDescription_r4748_2")
  98. assertHasSlotPath(t, got, "testimonial_items[0].title", "testimonials.testimonialTitle_r1508_1")
  99. assertHasSlotPath(t, got, "testimonial_items[0].description", "testimonials.testimonialDescription_r1508_2")
  100. assertHasSlotPath(t, got, "testimonial_items[0].name", "testimonials.testimonialName_r1508_3")
  101. assertHasSlotPath(t, got, "cta.text", "text.buttonText_c1165_1")
  102. }
  103. func TestMapTemplateFieldsToSemanticSlots_UsesSuggestedSection(t *testing.T) {
  104. t.Parallel()
  105. fields := []domain.TemplateField{
  106. {
  107. Path: "welcome.headline_m1710_1",
  108. KeyName: "headline_m1710_1",
  109. Section: "welcome",
  110. FieldKind: "text",
  111. IsEnabled: true,
  112. },
  113. }
  114. got := MapTemplateFieldsToSemanticSlots(fields)
  115. assertHasSlotPath(t, got, "hero.title", "welcome.headline_m1710_1")
  116. }
  117. func TestMapTemplateFieldsToSemanticSlots_RepeatedBlocksStaySeparatedAndTypePure(t *testing.T) {
  118. t.Parallel()
  119. fields := []domain.TemplateField{
  120. {
  121. Path: "services.servicesTitle_r4830_8",
  122. KeyName: "servicesTitle_r4830_8",
  123. FieldKind: "text",
  124. IsEnabled: true,
  125. WebsiteSection: domain.WebsiteSectionServices,
  126. },
  127. {
  128. Path: "services.servicesDescription_r4830_9",
  129. KeyName: "servicesDescription_r4830_9",
  130. FieldKind: "text",
  131. IsEnabled: true,
  132. WebsiteSection: domain.WebsiteSectionServices,
  133. },
  134. {
  135. Path: "services.servicesTitle_r4830_10",
  136. KeyName: "servicesTitle_r4830_10",
  137. FieldKind: "text",
  138. IsEnabled: true,
  139. WebsiteSection: domain.WebsiteSectionServices,
  140. },
  141. {
  142. Path: "services.servicesDescription_r4830_11",
  143. KeyName: "servicesDescription_r4830_11",
  144. FieldKind: "text",
  145. IsEnabled: true,
  146. WebsiteSection: domain.WebsiteSectionServices,
  147. },
  148. {
  149. Path: "services.servicesTitle_r4830_12",
  150. KeyName: "servicesTitle_r4830_12",
  151. FieldKind: "text",
  152. IsEnabled: true,
  153. WebsiteSection: domain.WebsiteSectionServices,
  154. },
  155. {
  156. Path: "services.servicesDescription_r4830_13",
  157. KeyName: "servicesDescription_r4830_13",
  158. FieldKind: "text",
  159. IsEnabled: true,
  160. WebsiteSection: domain.WebsiteSectionServices,
  161. },
  162. {
  163. Path: "text.textName_r4748_17",
  164. KeyName: "textName_r4748_17",
  165. FieldKind: "text",
  166. IsEnabled: true,
  167. WebsiteSection: domain.WebsiteSectionTeam,
  168. },
  169. {
  170. Path: "text.textDescription_r4748_18",
  171. KeyName: "textDescription_r4748_18",
  172. FieldKind: "text",
  173. IsEnabled: true,
  174. WebsiteSection: domain.WebsiteSectionTeam,
  175. },
  176. {
  177. Path: "text.textName_r4748_19",
  178. KeyName: "textName_r4748_19",
  179. FieldKind: "text",
  180. IsEnabled: true,
  181. WebsiteSection: domain.WebsiteSectionTeam,
  182. },
  183. {
  184. Path: "text.textDescription_r4748_20",
  185. KeyName: "textDescription_r4748_20",
  186. FieldKind: "text",
  187. IsEnabled: true,
  188. WebsiteSection: domain.WebsiteSectionTeam,
  189. },
  190. {
  191. Path: "text.textName_r4748_21",
  192. KeyName: "textName_r4748_21",
  193. FieldKind: "text",
  194. IsEnabled: true,
  195. WebsiteSection: domain.WebsiteSectionTeam,
  196. },
  197. {
  198. Path: "text.textDescription_r4748_22",
  199. KeyName: "textDescription_r4748_22",
  200. FieldKind: "text",
  201. IsEnabled: true,
  202. WebsiteSection: domain.WebsiteSectionTeam,
  203. },
  204. {
  205. Path: "testimonials.testimonialsName_r1508_23",
  206. KeyName: "testimonialsName_r1508_23",
  207. FieldKind: "text",
  208. IsEnabled: true,
  209. WebsiteSection: domain.WebsiteSectionTestimonials,
  210. },
  211. {
  212. Path: "testimonials.testimonialsTitle_r1508_24",
  213. KeyName: "testimonialsTitle_r1508_24",
  214. FieldKind: "text",
  215. IsEnabled: true,
  216. WebsiteSection: domain.WebsiteSectionTestimonials,
  217. },
  218. {
  219. Path: "testimonials.testimonialsDescription_r1508_25",
  220. KeyName: "testimonialsDescription_r1508_25",
  221. FieldKind: "text",
  222. IsEnabled: true,
  223. WebsiteSection: domain.WebsiteSectionTestimonials,
  224. },
  225. {
  226. Path: "testimonials.testimonialsName_r1508_26",
  227. KeyName: "testimonialsName_r1508_26",
  228. FieldKind: "text",
  229. IsEnabled: true,
  230. WebsiteSection: domain.WebsiteSectionTestimonials,
  231. },
  232. {
  233. Path: "testimonials.testimonialsTitle_r1508_27",
  234. KeyName: "testimonialsTitle_r1508_27",
  235. FieldKind: "text",
  236. IsEnabled: true,
  237. WebsiteSection: domain.WebsiteSectionTestimonials,
  238. },
  239. {
  240. Path: "testimonials.testimonialsDescription_r1508_28",
  241. KeyName: "testimonialsDescription_r1508_28",
  242. FieldKind: "text",
  243. IsEnabled: true,
  244. WebsiteSection: domain.WebsiteSectionTestimonials,
  245. },
  246. {
  247. Path: "testimonials.testimonialsName_r1508_29",
  248. KeyName: "testimonialsName_r1508_29",
  249. FieldKind: "text",
  250. IsEnabled: true,
  251. WebsiteSection: domain.WebsiteSectionTestimonials,
  252. },
  253. {
  254. Path: "testimonials.testimonialsTitle_r1508_30",
  255. KeyName: "testimonialsTitle_r1508_30",
  256. FieldKind: "text",
  257. IsEnabled: true,
  258. WebsiteSection: domain.WebsiteSectionTestimonials,
  259. },
  260. {
  261. Path: "testimonials.testimonialsDescription_r1508_31",
  262. KeyName: "testimonialsDescription_r1508_31",
  263. FieldKind: "text",
  264. IsEnabled: true,
  265. WebsiteSection: domain.WebsiteSectionTestimonials,
  266. },
  267. {
  268. Path: "testimonials.testimonialsName_r1508_32",
  269. KeyName: "testimonialsName_r1508_32",
  270. FieldKind: "text",
  271. IsEnabled: true,
  272. WebsiteSection: domain.WebsiteSectionTestimonials,
  273. },
  274. {
  275. Path: "testimonials.testimonialsTitle_r1508_33",
  276. KeyName: "testimonialsTitle_r1508_33",
  277. FieldKind: "text",
  278. IsEnabled: true,
  279. WebsiteSection: domain.WebsiteSectionTestimonials,
  280. },
  281. {
  282. Path: "testimonials.testimonialsDescription_r1508_34",
  283. KeyName: "testimonialsDescription_r1508_34",
  284. FieldKind: "text",
  285. IsEnabled: true,
  286. WebsiteSection: domain.WebsiteSectionTestimonials,
  287. },
  288. }
  289. got := MapTemplateFieldsToSemanticSlots(fields)
  290. assertHasSlotPath(t, got, "service_items[0].title", "services.servicesTitle_r4830_8")
  291. assertHasSlotPath(t, got, "service_items[0].description", "services.servicesDescription_r4830_9")
  292. assertHasSlotPath(t, got, "service_items[1].title", "services.servicesTitle_r4830_10")
  293. assertHasSlotPath(t, got, "service_items[1].description", "services.servicesDescription_r4830_11")
  294. assertHasSlotPath(t, got, "service_items[2].title", "services.servicesTitle_r4830_12")
  295. assertHasSlotPath(t, got, "service_items[2].description", "services.servicesDescription_r4830_13")
  296. assertHasSlotPath(t, got, "team_items[0].name", "text.textName_r4748_17")
  297. assertHasSlotPath(t, got, "team_items[0].description", "text.textDescription_r4748_18")
  298. assertHasSlotPath(t, got, "team_items[1].name", "text.textName_r4748_19")
  299. assertHasSlotPath(t, got, "team_items[1].description", "text.textDescription_r4748_20")
  300. assertHasSlotPath(t, got, "team_items[2].name", "text.textName_r4748_21")
  301. assertHasSlotPath(t, got, "team_items[2].description", "text.textDescription_r4748_22")
  302. assertHasSlotPath(t, got, "testimonial_items[0].name", "testimonials.testimonialsName_r1508_23")
  303. assertHasSlotPath(t, got, "testimonial_items[0].title", "testimonials.testimonialsTitle_r1508_24")
  304. assertHasSlotPath(t, got, "testimonial_items[0].description", "testimonials.testimonialsDescription_r1508_25")
  305. assertHasSlotPath(t, got, "testimonial_items[1].name", "testimonials.testimonialsName_r1508_26")
  306. assertHasSlotPath(t, got, "testimonial_items[1].title", "testimonials.testimonialsTitle_r1508_27")
  307. assertHasSlotPath(t, got, "testimonial_items[1].description", "testimonials.testimonialsDescription_r1508_28")
  308. assertHasSlotPath(t, got, "testimonial_items[2].name", "testimonials.testimonialsName_r1508_29")
  309. assertHasSlotPath(t, got, "testimonial_items[2].title", "testimonials.testimonialsTitle_r1508_30")
  310. assertHasSlotPath(t, got, "testimonial_items[2].description", "testimonials.testimonialsDescription_r1508_31")
  311. assertHasSlotPath(t, got, "testimonial_items[3].name", "testimonials.testimonialsName_r1508_32")
  312. assertHasSlotPath(t, got, "testimonial_items[3].title", "testimonials.testimonialsTitle_r1508_33")
  313. assertHasSlotPath(t, got, "testimonial_items[3].description", "testimonials.testimonialsDescription_r1508_34")
  314. assertSlotCount(t, got, "team_items[0].name", 1)
  315. assertSlotCount(t, got, "team_items[0].description", 1)
  316. assertSlotCount(t, got, "testimonial_items[0].name", 1)
  317. assertSlotCount(t, got, "testimonial_items[0].title", 1)
  318. assertSlotCount(t, got, "testimonial_items[0].description", 1)
  319. }
  320. func assertHasSlotPath(t *testing.T, mapping SemanticSlotMapping, slot string, path string) {
  321. t.Helper()
  322. candidates := mapping.BySlot[slot]
  323. for _, item := range candidates {
  324. if item.FieldPath == path {
  325. return
  326. }
  327. }
  328. t.Fatalf("slot %q missing path %q", slot, path)
  329. }
  330. func assertSlotCount(t *testing.T, mapping SemanticSlotMapping, slot string, expected int) {
  331. t.Helper()
  332. if got := len(mapping.BySlot[slot]); got != expected {
  333. t.Fatalf("slot %q has %d candidates, want %d", slot, got, expected)
  334. }
  335. }