Raw
1 import { ITranslationModuleService } from "@medusajs/framework/types"
2 import { DmlEntity, Module, Modules } from "@medusajs/framework/utils"
3 import { moduleIntegrationTestRunner } from "@medusajs/test-utils"
4 import TranslationModuleService from "@services/translation-module"
5 import { createLocaleFixture, createTranslationFixture } from "../__fixtures__"
6
7 jest.setTimeout(100000)
8
9 // Set up the mock before module initialization
10 let mockGetTranslatableEntities: jest.SpyInstance
11
12 moduleIntegrationTestRunner<ITranslationModuleService>({
13 moduleName: Modules.TRANSLATION,
14 hooks: {
15 beforeModuleInit: async () => {
16 mockGetTranslatableEntities = jest.spyOn(
17 DmlEntity,
18 "getTranslatableEntities"
19 )
20 mockGetTranslatableEntities.mockReturnValue([
21 {
22 entity: "Product",
23 fields: ["title", "description", "subtitle", "material"],
24 },
25 { entity: "ProductVariant", fields: ["title", "material"] },
26 { entity: "ProductCategory", fields: ["name"] },
27 ])
28 },
29 },
30 testSuite: ({ service }) => {
31 describe("Translation Module Service", () => {
32 beforeEach(async () => {
33 await service.__hooks?.onApplicationStart?.().catch(() => {})
34 })
35 afterAll(() => {
36 // Restore the mock after all tests complete
37 mockGetTranslatableEntities.mockRestore()
38 })
39
40 it(`should export the appropriate linkable configuration`, () => {
41 const linkable = Module(Modules.TRANSLATION, {
42 service: TranslationModuleService,
43 }).linkable
44
45 expect(Object.keys(linkable)).toEqual([
46 "locale",
47 "translation",
48 "translationSettings",
49 ])
50
51 Object.keys(linkable).forEach((key) => {
52 delete linkable[key].toJSON
53 })
54
55 expect(linkable).toEqual({
56 locale: {
57 id: {
58 linkable: "locale_id",
59 entity: "Locale",
60 primaryKey: "id",
61 serviceName: "translation",
62 field: "locale",
63 },
64 },
65 translation: {
66 id: {
67 linkable: "translation_id",
68 entity: "Translation",
69 primaryKey: "id",
70 serviceName: "translation",
71 field: "translation",
72 },
73 },
74 translationSettings: {
75 id: {
76 linkable: "translation_settings_id",
77 entity: "TranslationSettings",
78 primaryKey: "id",
79 serviceName: "translation",
80 field: "translationSettings",
81 },
82 },
83 })
84 })
85
86 describe("Locale", () => {
87 describe("creating a locale", () => {
88 it("should create a locale successfully", async () => {
89 const locale = await service.createLocales(createLocaleFixture)
90
91 expect(locale).toEqual(
92 expect.objectContaining({
93 code: "test-LC",
94 name: "Test Locale",
95 created_at: expect.any(Date),
96 updated_at: expect.any(Date),
97 })
98 )
99 })
100
101 it("should create multiple locales successfully", async () => {
102 const locales = await service.createLocales([
103 createLocaleFixture,
104 { code: "test-LC2", name: "Test Locale 2" },
105 ])
106
107 expect(locales).toHaveLength(2)
108 expect(locales[0].code).toEqual("test-LC")
109 expect(locales[1].code).toEqual("test-LC2")
110 })
111 })
112
113 describe("retrieving a locale", () => {
114 it("should retrieve a locale by id", async () => {
115 const created = await service.createLocales(createLocaleFixture)
116 const retrieved = await service.retrieveLocale(created.id)
117
118 expect(retrieved).toEqual(
119 expect.objectContaining({
120 id: created.id,
121 code: created.code,
122 name: "Test Locale",
123 })
124 )
125 })
126
127 it("should throw when retrieving non-existent locale", async () => {
128 const error = await service
129 .retrieveLocale("non-existent-id")
130 .catch((e) => e)
131
132 expect(error.message).toContain("Locale with id: non-existent-id")
133 })
134 })
135
136 describe("listing locales", () => {
137 it("should list all locales including defaults", async () => {
138 const locales = await service.listLocales()
139
140 expect(locales.length).toBeGreaterThanOrEqual(45)
141 })
142
143 it("should filter locales by code", async () => {
144 await service.createLocales(createLocaleFixture)
145 const locales = await service.listLocales({ code: "test-LC" })
146
147 expect(locales).toHaveLength(1)
148 expect(locales[0].code).toEqual("test-LC")
149 })
150
151 it("should filter locales by name", async () => {
152 const locales = await service.listLocales({
153 name: "English (United States)",
154 })
155
156 expect(locales).toHaveLength(1)
157 expect(locales[0].code).toEqual("en-US")
158 })
159
160 it("should support pagination", async () => {
161 const paginatedLocales = await service.listLocales(
162 {},
163 { take: 5, skip: 0 }
164 )
165
166 expect(paginatedLocales).toHaveLength(5)
167 })
168 })
169
170 describe("listing and counting locales", () => {
171 it("should list and count locales", async () => {
172 const [locales, count] = await service.listAndCountLocales()
173
174 expect(count).toBeGreaterThanOrEqual(45)
175 expect(locales.length).toEqual(count)
176 })
177
178 it("should filter and count correctly", async () => {
179 await service.createLocales([
180 { code: "custom-A", name: "Custom A" },
181 { code: "custom-B", name: "Custom B" },
182 ])
183
184 const [locales, count] = await service.listAndCountLocales({
185 code: ["custom-A", "custom-B"],
186 })
187
188 expect(count).toEqual(2)
189 expect(locales).toHaveLength(2)
190 })
191 })
192
193 describe("updating a locale", () => {
194 it("should update a locale successfully", async () => {
195 const created = await service.createLocales(createLocaleFixture)
196 const updated = await service.updateLocales({
197 id: created.id,
198 code: created.code,
199 name: "Updated Locale Name",
200 })
201
202 expect(updated.name).toEqual("Updated Locale Name")
203 expect(updated.code).toEqual("test-LC")
204 })
205
206 it("should update multiple locales", async () => {
207 const created = await service.createLocales([
208 { code: "upd-1", name: "Update 1" },
209 { code: "upd-2", name: "Update 2" },
210 ])
211
212 const updated = await service.updateLocales([
213 { id: created[0].id, code: created[0].code, name: "Updated 1" },
214 { id: created[1].id, code: created[1].code, name: "Updated 2" },
215 ])
216
217 expect(updated).toHaveLength(2)
218 const updatedById = updated.reduce(
219 (acc, l) => ({ ...acc, [l.code]: l }),
220 {} as Record<string, any>
221 )
222 expect(updatedById[created[0].code].name).toEqual("Updated 1")
223 expect(updatedById[created[1].code].name).toEqual("Updated 2")
224 })
225 })
226
227 describe("deleting a locale", () => {
228 it("should delete a locale successfully", async () => {
229 const created = await service.createLocales(createLocaleFixture)
230 await service.deleteLocales(created.id)
231
232 const error = await service
233 .retrieveLocale(created.id)
234 .catch((e) => e)
235
236 expect(error.message).toContain("Locale with id")
237 })
238
239 it("should delete multiple locales", async () => {
240 const created = await service.createLocales([
241 { code: "del-1", name: "Delete 1" },
242 { code: "del-2", name: "Delete 2" },
243 ])
244
245 await service.deleteLocales([created[0].id, created[1].id])
246
247 const locales = await service.listLocales({
248 code: ["del-1", "del-2"],
249 })
250
251 expect(locales).toHaveLength(0)
252 })
253 })
254
255 describe("soft deleting a locale", () => {
256 it("should soft delete a locale", async () => {
257 const created = await service.createLocales(createLocaleFixture)
258 await service.softDeleteLocales(created.id)
259
260 const locales = await service.listLocales({ code: created.code })
261 expect(locales).toHaveLength(0)
262 })
263 })
264
265 describe("restoring a locale", () => {
266 it("should restore a soft deleted locale", async () => {
267 const created = await service.createLocales(createLocaleFixture)
268 await service.softDeleteLocales(created.id)
269 await service.restoreLocales(created.id)
270
271 const restored = await service.retrieveLocale(created.id)
272 expect(restored.code).toEqual(created.code)
273 })
274 })
275 })
276
277 describe("Translation", () => {
278 describe("creating a translation", () => {
279 it("should create a translation successfully", async () => {
280 const translation = await service.createTranslations(
281 createTranslationFixture
282 )
283
284 expect(translation).toEqual(
285 expect.objectContaining({
286 id: expect.stringMatching(/^trans_/),
287 reference_id: "prod_123",
288 reference: "product",
289 locale_code: "fr-FR",
290 translations: {
291 title: "Titre du produit",
292 description: "Description du produit en français",
293 },
294 created_at: expect.any(Date),
295 updated_at: expect.any(Date),
296 })
297 )
298 })
299
300 it("should create multiple translations successfully", async () => {
301 const translations = await service.createTranslations([
302 createTranslationFixture,
303 {
304 reference_id: "prod_123",
305 reference: "product",
306 locale_code: "de-DE",
307 translations: {
308 title: "Produkttitel",
309 description: "Produktbeschreibung auf Deutsch",
310 },
311 },
312 ])
313
314 expect(translations).toHaveLength(2)
315 expect(translations[0].locale_code).toEqual("fr-FR")
316 expect(translations[1].locale_code).toEqual("de-DE")
317 })
318
319 it("should fail when creating duplicate translation for same entity/type/locale", async () => {
320 await service.createTranslations(createTranslationFixture)
321
322 const error = await service
323 .createTranslations(createTranslationFixture)
324 .catch((e) => e)
325
326 expect(error.message).toMatch(
327 /unique|duplicate|constraint|already exists/i
328 )
329 })
330 })
331
332 describe("retrieving a translation", () => {
333 it("should retrieve a translation by id", async () => {
334 const created = await service.createTranslations(
335 createTranslationFixture
336 )
337 const retrieved = await service.retrieveTranslation(created.id)
338
339 expect(retrieved).toEqual(
340 expect.objectContaining({
341 id: created.id,
342 reference_id: "prod_123",
343 reference: "product",
344 locale_code: "fr-FR",
345 })
346 )
347 })
348
349 it("should throw when retrieving non-existent translation", async () => {
350 const error = await service
351 .retrieveTranslation("non-existent-id")
352 .catch((e) => e)
353
354 expect(error.message).toContain(
355 "Translation with id: non-existent-id"
356 )
357 })
358 })
359
360 describe("listing translations", () => {
361 beforeEach(async () => {
362 await service.createTranslations([
363 {
364 reference_id: "prod_1",
365 reference: "product",
366 locale_code: "fr-FR",
367 translations: { title: "Produit Un" },
368 },
369 {
370 reference_id: "prod_1",
371 reference: "product",
372 locale_code: "de-DE",
373 translations: { title: "Produkt Eins" },
374 },
375 {
376 reference_id: "prod_2",
377 reference: "product",
378 locale_code: "fr-FR",
379 translations: { title: "Produit Deux" },
380 },
381 {
382 reference_id: "cat_1",
383 reference: "product_category",
384 locale_code: "fr-FR",
385 translations: { name: "Catégorie" },
386 },
387 ])
388 })
389
390 it("should list all translations", async () => {
391 const translations = await service.listTranslations()
392
393 expect(translations.length).toBeGreaterThanOrEqual(4)
394 })
395
396 it("should filter by reference_id", async () => {
397 const translations = await service.listTranslations({
398 reference_id: "prod_1",
399 })
400
401 expect(translations).toHaveLength(2)
402 })
403
404 it("should filter by reference", async () => {
405 const translations = await service.listTranslations({
406 reference: "product_category",
407 })
408
409 expect(translations).toHaveLength(1)
410 expect(translations[0].reference_id).toEqual("cat_1")
411 })
412
413 it("should filter by locale_code", async () => {
414 const translations = await service.listTranslations({
415 locale_code: "de-DE",
416 })
417
418 expect(translations).toHaveLength(1)
419 expect(translations[0].reference_id).toEqual("prod_1")
420 })
421
422 it("should filter by multiple criteria", async () => {
423 const translations = await service.listTranslations({
424 reference_id: "prod_1",
425 locale_code: "fr-FR",
426 })
427
428 expect(translations).toHaveLength(1)
429 expect(translations[0].translations).toEqual({
430 title: "Produit Un",
431 })
432 })
433
434 it("should support pagination", async () => {
435 const translations = await service.listTranslations(
436 {},
437 { take: 2, skip: 0 }
438 )
439
440 expect(translations).toHaveLength(2)
441 })
442 })
443
444 describe("listing translations with q filter (JSONB search)", () => {
445 beforeEach(async () => {
446 await service.createTranslations([
447 {
448 reference_id: "prod_search_1",
449 reference: "product",
450 locale_code: "fr-FR",
451 translations: {
452 title: "Chaussures de sport",
453 description: "Des chaussures confortables pour le running",
454 },
455 },
456 {
457 reference_id: "prod_search_2",
458 reference: "product",
459 locale_code: "fr-FR",
460 translations: {
461 title: "T-shirt de sport",
462 description: "Un t-shirt léger et respirant",
463 },
464 },
465 {
466 reference_id: "prod_search_3",
467 reference: "product",
468 locale_code: "de-DE",
469 translations: {
470 title: "Sportschuhe",
471 description: "Bequeme Schuhe zum Laufen",
472 },
473 },
474 ])
475 })
476
477 it("should search within JSONB translations field", async () => {
478 const translations = await service.listTranslations({
479 q: "chaussures",
480 })
481
482 expect(translations).toHaveLength(1)
483 expect(translations[0].reference_id).toEqual("prod_search_1")
484 })
485
486 it("should search case-insensitively", async () => {
487 const translations = await service.listTranslations({
488 q: "CHAUSSURES",
489 })
490
491 expect(translations).toHaveLength(1)
492 })
493
494 it("should search across all JSONB values", async () => {
495 const translations = await service.listTranslations({
496 q: "running",
497 })
498
499 expect(translations).toHaveLength(1)
500 expect(translations[0].reference_id).toEqual("prod_search_1")
501 })
502
503 it("should combine q filter with other filters", async () => {
504 const translations = await service.listTranslations({
505 q: "sport",
506 locale_code: "fr-FR",
507 })
508
509 expect(translations).toHaveLength(2)
510 })
511
512 it("should return empty array when q matches nothing", async () => {
513 const translations = await service.listTranslations({
514 q: "nonexistent-term-xyz",
515 })
516
517 expect(translations).toHaveLength(0)
518 })
519 })
520
521 describe("listing and counting translations", () => {
522 beforeEach(async () => {
523 await service.createTranslations([
524 {
525 reference_id: "cnt_1",
526 reference: "product",
527 locale_code: "fr-FR",
528 translations: { title: "Un" },
529 },
530 {
531 reference_id: "cnt_2",
532 reference: "product",
533 locale_code: "fr-FR",
534 translations: { title: "Deux" },
535 },
536 {
537 reference_id: "cnt_3",
538 reference: "product",
539 locale_code: "fr-FR",
540 translations: { title: "Trois" },
541 },
542 ])
543 })
544
545 it("should list and count translations", async () => {
546 const [translations, count] =
547 await service.listAndCountTranslations({
548 reference: "product",
549 locale_code: "fr-FR",
550 })
551
552 expect(count).toEqual(3)
553 expect(translations).toHaveLength(3)
554 })
555
556 it("should list and count with q filter", async () => {
557 const [translations, count] =
558 await service.listAndCountTranslations({
559 q: "Deux",
560 })
561
562 expect(count).toEqual(1)
563 expect(translations).toHaveLength(1)
564 expect(translations[0].reference_id).toEqual("cnt_2")
565 })
566 })
567
568 describe("updating a translation", () => {
569 it("should update a translation successfully", async () => {
570 const created = await service.createTranslations(
571 createTranslationFixture
572 )
573 const updated = await service.updateTranslations({
574 id: created.id,
575 translations: {
576 title: "Nouveau titre",
577 description: "Nouvelle description",
578 },
579 })
580
581 expect(updated.translations).toEqual({
582 title: "Nouveau titre",
583 description: "Nouvelle description",
584 })
585 })
586
587 it("should update multiple translations", async () => {
588 const created = await service.createTranslations([
589 {
590 reference_id: "upd_1",
591 reference: "product",
592 locale_code: "fr-FR",
593 translations: { title: "Original 1" },
594 },
595 {
596 reference_id: "upd_2",
597 reference: "product",
598 locale_code: "fr-FR",
599 translations: { title: "Original 2" },
600 },
601 ])
602
603 const updated = await service.updateTranslations([
604 { id: created[0].id, translations: { title: "Updated 1" } },
605 { id: created[1].id, translations: { title: "Updated 2" } },
606 ])
607
608 expect(updated).toHaveLength(2)
609 const updatedById = updated.reduce(
610 (acc, t) => ({ ...acc, [t.id]: t }),
611 {} as Record<string, any>
612 )
613 expect(updatedById[created[0].id].translations).toEqual({
614 title: "Updated 1",
615 })
616 expect(updatedById[created[1].id].translations).toEqual({
617 title: "Updated 2",
618 })
619 })
620 })
621
622 describe("deleting a translation", () => {
623 it("should delete a translation successfully", async () => {
624 const created = await service.createTranslations(
625 createTranslationFixture
626 )
627 await service.deleteTranslations(created.id)
628
629 const error = await service
630 .retrieveTranslation(created.id)
631 .catch((e) => e)
632
633 expect(error.message).toContain("Translation with id")
634 })
635
636 it("should delete multiple translations", async () => {
637 const created = await service.createTranslations([
638 {
639 reference_id: "del_1",
640 reference: "product",
641 locale_code: "fr-FR",
642 translations: { title: "Delete 1" },
643 },
644 {
645 reference_id: "del_2",
646 reference: "product",
647 locale_code: "fr-FR",
648 translations: { title: "Delete 2" },
649 },
650 ])
651
652 await service.deleteTranslations([created[0].id, created[1].id])
653
654 const translations = await service.listTranslations({
655 reference_id: ["del_1", "del_2"],
656 })
657
658 expect(translations).toHaveLength(0)
659 })
660 })
661
662 describe("soft deleting a translation", () => {
663 it("should soft delete a translation", async () => {
664 const created = await service.createTranslations(
665 createTranslationFixture
666 )
667 await service.softDeleteTranslations(created.id)
668
669 const translations = await service.listTranslations({
670 id: created.id,
671 })
672 expect(translations).toHaveLength(0)
673 })
674 })
675
676 describe("restoring a translation", () => {
677 it("should restore a soft deleted translation", async () => {
678 const created = await service.createTranslations(
679 createTranslationFixture
680 )
681 await service.softDeleteTranslations(created.id)
682 await service.restoreTranslations(created.id)
683
684 const restored = await service.retrieveTranslation(created.id)
685 expect(restored.id).toEqual(created.id)
686 })
687 })
688 })
689
690 describe("Settings", () => {
691 describe("getTranslatableFields", () => {
692 it("should return all translatable fields from database", async () => {
693 const fields = await service.getTranslatableFields()
694
695 expect(fields).toHaveProperty("product")
696 expect(fields).toHaveProperty("product_variant")
697 expect(fields.product).toEqual(
698 expect.arrayContaining(["title", "description"])
699 )
700 })
701
702 it("should return translatable fields for a specific entity type", async () => {
703 const fields = await service.getTranslatableFields("product")
704
705 expect(Object.keys(fields)).toEqual(["product"])
706 expect(fields.product).toEqual(
707 expect.arrayContaining(["title", "description"])
708 )
709 })
710
711 it("should return empty object for unknown entity type", async () => {
712 const fields = await service.getTranslatableFields("unknown_entity")
713
714 expect(fields).toEqual({})
715 })
716
717 it("should return empty array when entity has fields configured but is_active is false", async () => {
718 const [productTranslationSettings] =
719 await service.listTranslationSettings({
720 entity_type: "product",
721 })
722 await service.updateTranslationSettings({
723 id: productTranslationSettings.id,
724 is_active: false,
725 })
726
727 const fields = await service.getTranslatableFields("product")
728
729 expect(fields).toEqual({
730 product: [],
731 })
732 })
733
734 it("should return empty array for inactive entity when getting all fields", async () => {
735 const [productTranslationSettings] =
736 await service.listTranslationSettings({
737 entity_type: "product",
738 })
739 await service.updateTranslationSettings({
740 id: productTranslationSettings.id,
741 is_active: false,
742 })
743
744 const fields = await service.getTranslatableFields()
745
746 expect(fields.product).toEqual([])
747 })
748 })
749
750 describe("listing translations filters by configured fields", () => {
751 it("should only return configured fields in translations", async () => {
752 await service.createTranslations({
753 reference_id: "prod_filter_1",
754 reference: "product",
755 locale_code: "en-US",
756 translations: {
757 title: "Product Title",
758 description: "Product Description",
759 unconfigured_field: "Should be filtered out",
760 },
761 })
762
763 const translations = await service.listTranslations({
764 reference_id: "prod_filter_1",
765 })
766
767 expect(translations).toHaveLength(1)
768 expect(translations[0].translations).toHaveProperty("title")
769 expect(translations[0].translations).toHaveProperty("description")
770 expect(translations[0].translations).not.toHaveProperty(
771 "unconfigured_field"
772 )
773 })
774
775 it("should return empty translations for unconfigured entity types", async () => {
776 await service.createTranslations({
777 reference_id: "unconfigured_1",
778 reference: "unconfigured_entity",
779 locale_code: "en-US",
780 translations: {
781 field1: "Value 1",
782 field2: "Value 2",
783 },
784 })
785
786 const translations = await service.listTranslations({
787 reference_id: "unconfigured_1",
788 })
789
790 expect(translations).toHaveLength(1)
791 expect(translations[0].translations).toEqual({})
792 })
793 })
794 })
795
796 describe("Statistics", () => {
797 describe("getStatistics", () => {
798 it("should return statistics for a single entity type and locale", async () => {
799 await service.createTranslations([
800 {
801 reference_id: "prod_stat_1",
802 reference: "product",
803 locale_code: "en-US",
804 translations: {
805 title: "Product 1",
806 description: "Description 1",
807 // material and subtitle are missing
808 },
809 },
810 {
811 reference_id: "prod_stat_2",
812 reference: "product",
813 locale_code: "en-US",
814 translations: {
815 title: "Product 2",
816 description: "Description 2",
817 subtitle: "Subtitle 2",
818 },
819 },
820 ])
821
822 const stats = await service.getStatistics({
823 locales: ["en-US"],
824 entities: {
825 product: { count: 2 },
826 },
827 })
828
829 // Expected: 2 products × 4 fields × 1 locale = 8
830 // Translated: prod_1 has 2, prod_2 has 3 = 5
831 expect(stats.product).toEqual({
832 expected: 8,
833 translated: 5,
834 missing: 3,
835 by_locale: {
836 "en-US": {
837 expected: 8,
838 translated: 5,
839 missing: 3,
840 },
841 },
842 })
843 })
844
845 it("should return statistics for multiple locales", async () => {
846 await service.createTranslations([
847 {
848 reference_id: "prod_multi_1",
849 reference: "product",
850 locale_code: "en-US",
851 translations: {
852 title: "Product 1 EN",
853 description: "Description EN",
854 },
855 },
856 {
857 reference_id: "prod_multi_1",
858 reference: "product",
859 locale_code: "fr-FR",
860 translations: {
861 title: "Produit 1 FR",
862 // only title translated for French
863 },
864 },
865 ])
866
867 const stats = await service.getStatistics({
868 locales: ["en-US", "fr-FR"],
869 entities: {
870 product: { count: 1 },
871 },
872 })
873
874 // Expected per locale: 1 product × 4 fields = 4
875 // Total expected: 4 × 2 locales = 8
876 expect(stats.product.expected).toEqual(8)
877 expect(stats.product.translated).toEqual(3) // 2 EN + 1 FR
878 expect(stats.product.missing).toEqual(5)
879
880 expect(stats.product.by_locale["en-US"]).toEqual({
881 expected: 4,
882 translated: 2,
883 missing: 2,
884 })
885
886 expect(stats.product.by_locale["fr-FR"]).toEqual({
887 expected: 4,
888 translated: 1,
889 missing: 3,
890 })
891 })
892
893 it("should return statistics for multiple entity types", async () => {
894 await service.createTranslations([
895 {
896 reference_id: "prod_type_1",
897 reference: "product",
898 locale_code: "en-US",
899 translations: {
900 title: "Product Title",
901 description: "Product Description",
902 subtitle: "Product Subtitle",
903 material: "Product Material",
904 },
905 },
906 {
907 reference_id: "var_type_1",
908 reference: "product_variant",
909 locale_code: "en-US",
910 translations: {
911 title: "Variant Title",
912 // material missing
913 },
914 },
915 ])
916
917 const stats = await service.getStatistics({
918 locales: ["en-US"],
919 entities: {
920 product: { count: 1 },
921 product_variant: { count: 1 },
922 },
923 })
924
925 // Product: 1 × 4 fields = 4 expected, 4 translated
926 expect(stats.product).toEqual({
927 expected: 4,
928 translated: 4,
929 missing: 0,
930 by_locale: {
931 "en-US": { expected: 4, translated: 4, missing: 0 },
932 },
933 })
934
935 // Variant: 1 × 2 fields = 2 expected, 1 translated
936 expect(stats.product_variant).toEqual({
937 expected: 2,
938 translated: 1,
939 missing: 1,
940 by_locale: {
941 "en-US": { expected: 2, translated: 1, missing: 1 },
942 },
943 })
944 })
945
946 it("should return zeros for entity types not in config", async () => {
947 const stats = await service.getStatistics({
948 locales: ["en-US"],
949 entities: {
950 unknown_entity: { count: 10 },
951 },
952 })
953
954 expect(stats.unknown_entity).toEqual({
955 expected: 0,
956 translated: 0,
957 missing: 0,
958 by_locale: {
959 "en-US": { expected: 0, translated: 0, missing: 0 },
960 },
961 })
962 })
963
964 it("should return all missing when no translations exist", async () => {
965 const stats = await service.getStatistics({
966 locales: ["en-US", "fr-FR"],
967 entities: {
968 product: { count: 5 },
969 },
970 })
971
972 // 5 products × 4 fields × 2 locales = 40 expected, 0 translated
973 expect(stats.product).toEqual({
974 expected: 40,
975 translated: 0,
976 missing: 40,
977 by_locale: {
978 "en-US": { expected: 20, translated: 0, missing: 20 },
979 "fr-FR": { expected: 20, translated: 0, missing: 20 },
980 },
981 })
982 })
983
984 it("should ignore empty string and null values in translations", async () => {
985 await service.createTranslations([
986 {
987 reference_id: "prod_empty_1",
988 reference: "product",
989 locale_code: "en-US",
990 translations: {
991 title: "Valid Title",
992 description: "", // empty string should not count
993 subtitle: "Valid Subtitle",
994 },
995 },
996 ])
997
998 const stats = await service.getStatistics({
999 locales: ["en-US"],
1000 entities: {
1001 product: { count: 1 },
1002 },
1003 })
1004
1005 // Only title and subtitle count (2), not empty description
1006 expect(stats.product.translated).toEqual(2)
1007 expect(stats.product.missing).toEqual(2)
1008 })
1009
1010 it("should normalize locale codes", async () => {
1011 await service.createTranslations([
1012 {
1013 reference_id: "prod_norm_1",
1014 reference: "product",
1015 locale_code: "en-us",
1016 translations: {
1017 title: "Product Title",
1018 },
1019 },
1020 ])
1021
1022 const stats = await service.getStatistics({
1023 locales: ["EN-US"],
1024 entities: {
1025 product: { count: 1 },
1026 },
1027 })
1028
1029 expect(stats.product.translated).toEqual(1)
1030 })
1031
1032 it("should throw error when no locales provided", async () => {
1033 const error = await service
1034 .getStatistics({
1035 locales: [],
1036 entities: { product: { count: 1 } },
1037 })
1038 .catch((e) => e)
1039
1040 expect(error.message).toContain(
1041 "At least one locale must be provided"
1042 )
1043 })
1044
1045 it("should throw error when no entities provided", async () => {
1046 const error = await service
1047 .getStatistics({
1048 locales: ["en-US"],
1049 entities: {},
1050 })
1051 .catch((e) => e)
1052
1053 expect(error.message).toContain(
1054 "At least one entity type must be provided"
1055 )
1056 })
1057
1058 it("should handle large entity counts correctly", async () => {
1059 // This tests that the expected calculation works with large numbers
1060 // without actually creating that many translations
1061 const stats = await service.getStatistics({
1062 locales: ["en-US", "fr-FR", "de-DE"],
1063 entities: {
1064 product: { count: 10000 },
1065 product_variant: { count: 50000 },
1066 },
1067 })
1068
1069 // Product: 10000 × 4 fields × 3 locales = 120000
1070 expect(stats.product.expected).toEqual(120000)
1071 expect(stats.product.translated).toEqual(0)
1072 expect(stats.product.missing).toEqual(120000)
1073
1074 // Variant: 50000 × 2 fields × 3 locales = 300000
1075 expect(stats.product_variant.expected).toEqual(300000)
1076 expect(stats.product_variant.translated).toEqual(0)
1077 expect(stats.product_variant.missing).toEqual(300000)
1078 })
1079
1080 it("should update statistics after translation is updated", async () => {
1081 const created = await service.createTranslations({
1082 reference_id: "prod_update_stat_1",
1083 reference: "product",
1084 locale_code: "en-US",
1085 translations: {
1086 title: "Product Title",
1087 // only 1 of 4 fields
1088 },
1089 })
1090
1091 let stats = await service.getStatistics({
1092 locales: ["en-US"],
1093 entities: { product: { count: 1 } },
1094 })
1095 expect(stats.product.translated).toEqual(1)
1096
1097 await service.updateTranslations({
1098 id: created.id,
1099 translations: {
1100 title: "Product Title",
1101 description: "Product Description",
1102 subtitle: "Product Subtitle",
1103 },
1104 })
1105
1106 stats = await service.getStatistics({
1107 locales: ["en-US"],
1108 entities: { product: { count: 1 } },
1109 })
1110 expect(stats.product.translated).toEqual(3)
1111 expect(stats.product.missing).toEqual(1)
1112 })
1113 })
1114 })
1115 })
1116 },
1117 })