|
1
|
import { medusaIntegrationTestRunner } from "@medusajs/test-utils" |
|
2
|
import { MedusaContainer } from "@medusajs/types" |
|
3
|
import { Modules } from "@medusajs/utils" |
|
4
|
import { |
|
5
|
adminHeaders, |
|
6
|
createAdminUser, |
|
7
|
} from "../../../../helpers/create-admin-user" |
|
8
|
|
|
9
|
jest.setTimeout(100000) |
|
10
|
|
|
11
|
process.env.MEDUSA_FF_TRANSLATION = "true" |
|
12
|
|
|
13
|
medusaIntegrationTestRunner({ |
|
14
|
testSuite: ({ dbConnection, getContainer, api, dbUtils }) => { |
|
15
|
describe("Admin Translation API", () => { |
|
16
|
let appContainer: MedusaContainer |
|
17
|
|
|
18
|
beforeAll(async () => { |
|
19
|
appContainer = getContainer() |
|
20
|
}) |
|
21
|
|
|
22
|
beforeAll(async () => { |
|
23
|
await createAdminUser(dbConnection, adminHeaders, getContainer()) |
|
24
|
|
|
25
|
const translationModule = appContainer.resolve(Modules.TRANSLATION) |
|
26
|
await translationModule.__hooks?.onApplicationStart?.().catch(() => {}) |
|
27
|
|
|
28
|
const storeModule = appContainer.resolve(Modules.STORE) |
|
29
|
const [defaultStore] = await storeModule.listStores( |
|
30
|
{}, |
|
31
|
{ |
|
32
|
select: ["id"], |
|
33
|
take: 1, |
|
34
|
} |
|
35
|
) |
|
36
|
await storeModule.updateStores(defaultStore.id, { |
|
37
|
supported_locales: [ |
|
38
|
{ locale_code: "en-US" }, |
|
39
|
{ locale_code: "fr-FR" }, |
|
40
|
{ locale_code: "de-DE" }, |
|
41
|
], |
|
42
|
}) |
|
43
|
|
|
44
|
await dbUtils.snapshot() |
|
45
|
}) |
|
46
|
|
|
47
|
afterAll(async () => { |
|
48
|
delete process.env.MEDUSA_FF_TRANSLATION |
|
49
|
}) |
|
50
|
|
|
51
|
describe("GET /admin/translations", () => { |
|
52
|
it("should list translations (empty initially)", async () => { |
|
53
|
const response = await api.get("/admin/translations", adminHeaders) |
|
54
|
|
|
55
|
expect(response.status).toEqual(200) |
|
56
|
expect(response.data).toEqual( |
|
57
|
expect.objectContaining({ |
|
58
|
translations: [], |
|
59
|
count: 0, |
|
60
|
offset: 0, |
|
61
|
limit: 20, |
|
62
|
}) |
|
63
|
) |
|
64
|
}) |
|
65
|
|
|
66
|
it("should list translations after creating some", async () => { |
|
67
|
await api.post( |
|
68
|
"/admin/translations/batch", |
|
69
|
{ |
|
70
|
create: [ |
|
71
|
{ |
|
72
|
reference_id: "prod_123", |
|
73
|
reference: "product", |
|
74
|
locale_code: "fr-FR", |
|
75
|
translations: { |
|
76
|
title: "Titre du produit", |
|
77
|
description: "Description en français", |
|
78
|
}, |
|
79
|
}, |
|
80
|
{ |
|
81
|
reference_id: "prod_123", |
|
82
|
reference: "product", |
|
83
|
locale_code: "de-DE", |
|
84
|
translations: { |
|
85
|
title: "Produkttitel", |
|
86
|
description: "Beschreibung auf Deutsch", |
|
87
|
}, |
|
88
|
}, |
|
89
|
], |
|
90
|
}, |
|
91
|
adminHeaders |
|
92
|
) |
|
93
|
|
|
94
|
const response = await api.get("/admin/translations", adminHeaders) |
|
95
|
|
|
96
|
expect(response.status).toEqual(200) |
|
97
|
expect(response.data.translations).toHaveLength(2) |
|
98
|
expect(response.data.count).toEqual(2) |
|
99
|
expect(response.data.translations).toEqual( |
|
100
|
expect.arrayContaining([ |
|
101
|
expect.objectContaining({ |
|
102
|
reference_id: "prod_123", |
|
103
|
reference: "product", |
|
104
|
locale_code: "fr-FR", |
|
105
|
translations: { |
|
106
|
title: "Titre du produit", |
|
107
|
description: "Description en français", |
|
108
|
}, |
|
109
|
}), |
|
110
|
expect.objectContaining({ |
|
111
|
reference_id: "prod_123", |
|
112
|
reference: "product", |
|
113
|
locale_code: "de-DE", |
|
114
|
translations: { |
|
115
|
title: "Produkttitel", |
|
116
|
description: "Beschreibung auf Deutsch", |
|
117
|
}, |
|
118
|
}), |
|
119
|
]) |
|
120
|
) |
|
121
|
}) |
|
122
|
|
|
123
|
it("should filter translations by reference_id", async () => { |
|
124
|
await api.post( |
|
125
|
"/admin/translations/batch", |
|
126
|
{ |
|
127
|
create: [ |
|
128
|
{ |
|
129
|
reference_id: "prod_1", |
|
130
|
reference: "product", |
|
131
|
locale_code: "fr-FR", |
|
132
|
translations: { title: "Produit Un" }, |
|
133
|
}, |
|
134
|
{ |
|
135
|
reference_id: "prod_2", |
|
136
|
reference: "product", |
|
137
|
locale_code: "fr-FR", |
|
138
|
translations: { title: "Produit Deux" }, |
|
139
|
}, |
|
140
|
], |
|
141
|
}, |
|
142
|
adminHeaders |
|
143
|
) |
|
144
|
|
|
145
|
const response = await api.get( |
|
146
|
"/admin/translations?reference_id=prod_1", |
|
147
|
adminHeaders |
|
148
|
) |
|
149
|
|
|
150
|
expect(response.status).toEqual(200) |
|
151
|
expect(response.data.translations).toHaveLength(1) |
|
152
|
expect(response.data.translations[0].reference_id).toEqual("prod_1") |
|
153
|
}) |
|
154
|
|
|
155
|
it("should filter translations by reference", async () => { |
|
156
|
await api.post( |
|
157
|
"/admin/translations/batch", |
|
158
|
{ |
|
159
|
create: [ |
|
160
|
{ |
|
161
|
reference_id: "prod_1", |
|
162
|
reference: "product", |
|
163
|
locale_code: "fr-FR", |
|
164
|
translations: { title: "Produit" }, |
|
165
|
}, |
|
166
|
{ |
|
167
|
reference_id: "cat_1", |
|
168
|
reference: "product_category", |
|
169
|
locale_code: "fr-FR", |
|
170
|
translations: { name: "Catégorie" }, |
|
171
|
}, |
|
172
|
], |
|
173
|
}, |
|
174
|
adminHeaders |
|
175
|
) |
|
176
|
|
|
177
|
const response = await api.get( |
|
178
|
"/admin/translations?reference=product_category", |
|
179
|
adminHeaders |
|
180
|
) |
|
181
|
|
|
182
|
expect(response.status).toEqual(200) |
|
183
|
expect(response.data.translations).toHaveLength(1) |
|
184
|
expect(response.data.translations[0].reference).toEqual( |
|
185
|
"product_category" |
|
186
|
) |
|
187
|
}) |
|
188
|
|
|
189
|
it("should filter translations by locale_code", async () => { |
|
190
|
await api.post( |
|
191
|
"/admin/translations/batch", |
|
192
|
{ |
|
193
|
create: [ |
|
194
|
{ |
|
195
|
reference_id: "prod_1", |
|
196
|
reference: "product", |
|
197
|
locale_code: "fr-FR", |
|
198
|
translations: { title: "Titre français" }, |
|
199
|
}, |
|
200
|
{ |
|
201
|
reference_id: "prod_1", |
|
202
|
reference: "product", |
|
203
|
locale_code: "de-DE", |
|
204
|
translations: { title: "Deutscher Titel" }, |
|
205
|
}, |
|
206
|
], |
|
207
|
}, |
|
208
|
adminHeaders |
|
209
|
) |
|
210
|
|
|
211
|
const response = await api.get( |
|
212
|
"/admin/translations?locale_code=de-DE", |
|
213
|
adminHeaders |
|
214
|
) |
|
215
|
|
|
216
|
expect(response.status).toEqual(200) |
|
217
|
expect(response.data.translations).toHaveLength(1) |
|
218
|
expect(response.data.translations[0].locale_code).toEqual("de-DE") |
|
219
|
}) |
|
220
|
|
|
221
|
it("should filter translations by multiple criteria", async () => { |
|
222
|
await api.post( |
|
223
|
"/admin/translations/batch", |
|
224
|
{ |
|
225
|
create: [ |
|
226
|
{ |
|
227
|
reference_id: "prod_1", |
|
228
|
reference: "product", |
|
229
|
locale_code: "fr-FR", |
|
230
|
translations: { title: "Produit Un FR" }, |
|
231
|
}, |
|
232
|
{ |
|
233
|
reference_id: "prod_1", |
|
234
|
reference: "product", |
|
235
|
locale_code: "de-DE", |
|
236
|
translations: { title: "Produkt Eins DE" }, |
|
237
|
}, |
|
238
|
{ |
|
239
|
reference_id: "prod_2", |
|
240
|
reference: "product", |
|
241
|
locale_code: "fr-FR", |
|
242
|
translations: { title: "Produit Deux FR" }, |
|
243
|
}, |
|
244
|
], |
|
245
|
}, |
|
246
|
adminHeaders |
|
247
|
) |
|
248
|
|
|
249
|
const response = await api.get( |
|
250
|
"/admin/translations?reference_id=prod_1&locale_code=fr-FR", |
|
251
|
adminHeaders |
|
252
|
) |
|
253
|
|
|
254
|
expect(response.status).toEqual(200) |
|
255
|
expect(response.data.translations).toHaveLength(1) |
|
256
|
expect(response.data.translations[0]).toEqual( |
|
257
|
expect.objectContaining({ |
|
258
|
reference_id: "prod_1", |
|
259
|
locale_code: "fr-FR", |
|
260
|
translations: { title: "Produit Un FR" }, |
|
261
|
}) |
|
262
|
) |
|
263
|
}) |
|
264
|
|
|
265
|
it("should support pagination", async () => { |
|
266
|
await api.post( |
|
267
|
"/admin/translations/batch", |
|
268
|
{ |
|
269
|
create: [ |
|
270
|
{ |
|
271
|
reference_id: "prod_1", |
|
272
|
reference: "product", |
|
273
|
locale_code: "fr-FR", |
|
274
|
translations: { title: "Produit 1" }, |
|
275
|
}, |
|
276
|
{ |
|
277
|
reference_id: "prod_2", |
|
278
|
reference: "product", |
|
279
|
locale_code: "fr-FR", |
|
280
|
translations: { title: "Produit 2" }, |
|
281
|
}, |
|
282
|
{ |
|
283
|
reference_id: "prod_3", |
|
284
|
reference: "product", |
|
285
|
locale_code: "fr-FR", |
|
286
|
translations: { title: "Produit 3" }, |
|
287
|
}, |
|
288
|
], |
|
289
|
}, |
|
290
|
adminHeaders |
|
291
|
) |
|
292
|
|
|
293
|
const response = await api.get( |
|
294
|
"/admin/translations?limit=2&offset=0", |
|
295
|
adminHeaders |
|
296
|
) |
|
297
|
|
|
298
|
expect(response.status).toEqual(200) |
|
299
|
expect(response.data.translations).toHaveLength(2) |
|
300
|
expect(response.data.count).toEqual(3) |
|
301
|
expect(response.data.limit).toEqual(2) |
|
302
|
expect(response.data.offset).toEqual(0) |
|
303
|
|
|
304
|
const response2 = await api.get( |
|
305
|
"/admin/translations?limit=2&offset=2", |
|
306
|
adminHeaders |
|
307
|
) |
|
308
|
|
|
309
|
expect(response2.status).toEqual(200) |
|
310
|
expect(response2.data.translations).toHaveLength(1) |
|
311
|
expect(response2.data.offset).toEqual(2) |
|
312
|
}) |
|
313
|
|
|
314
|
it("should filter translations using q parameter (JSONB search)", async () => { |
|
315
|
await api.post( |
|
316
|
"/admin/translations/batch", |
|
317
|
{ |
|
318
|
create: [ |
|
319
|
{ |
|
320
|
reference_id: "prod_1", |
|
321
|
reference: "product", |
|
322
|
locale_code: "fr-FR", |
|
323
|
translations: { |
|
324
|
title: "Chaussures de sport", |
|
325
|
description: "Des chaussures confortables", |
|
326
|
}, |
|
327
|
}, |
|
328
|
{ |
|
329
|
reference_id: "prod_2", |
|
330
|
reference: "product", |
|
331
|
locale_code: "fr-FR", |
|
332
|
translations: { |
|
333
|
title: "T-shirt de sport", |
|
334
|
description: "Un t-shirt léger", |
|
335
|
}, |
|
336
|
}, |
|
337
|
], |
|
338
|
}, |
|
339
|
adminHeaders |
|
340
|
) |
|
341
|
|
|
342
|
const response = await api.get( |
|
343
|
"/admin/translations?q=chaussures", |
|
344
|
adminHeaders |
|
345
|
) |
|
346
|
|
|
347
|
expect(response.status).toEqual(200) |
|
348
|
expect(response.data.translations).toHaveLength(1) |
|
349
|
expect(response.data.translations[0].reference_id).toEqual("prod_1") |
|
350
|
}) |
|
351
|
}) |
|
352
|
|
|
353
|
describe("POST /admin/translations/batch", () => { |
|
354
|
describe("create", () => { |
|
355
|
it("should create a single translation", async () => { |
|
356
|
const response = await api.post( |
|
357
|
"/admin/translations/batch", |
|
358
|
{ |
|
359
|
create: [ |
|
360
|
{ |
|
361
|
reference_id: "prod_123", |
|
362
|
reference: "product", |
|
363
|
locale_code: "fr-FR", |
|
364
|
translations: { |
|
365
|
title: "Titre du produit", |
|
366
|
}, |
|
367
|
}, |
|
368
|
], |
|
369
|
}, |
|
370
|
adminHeaders |
|
371
|
) |
|
372
|
|
|
373
|
expect(response.status).toEqual(200) |
|
374
|
expect(response.data.created).toHaveLength(1) |
|
375
|
expect(response.data.created[0]).toEqual( |
|
376
|
expect.objectContaining({ |
|
377
|
id: expect.stringMatching(/^trans_/), |
|
378
|
reference_id: "prod_123", |
|
379
|
reference: "product", |
|
380
|
locale_code: "fr-FR", |
|
381
|
translations: { |
|
382
|
title: "Titre du produit", |
|
383
|
}, |
|
384
|
}) |
|
385
|
) |
|
386
|
}) |
|
387
|
|
|
388
|
it("should create multiple translations", async () => { |
|
389
|
const response = await api.post( |
|
390
|
"/admin/translations/batch", |
|
391
|
{ |
|
392
|
create: [ |
|
393
|
{ |
|
394
|
reference_id: "prod_123", |
|
395
|
reference: "product", |
|
396
|
locale_code: "fr-FR", |
|
397
|
translations: { title: "Titre FR" }, |
|
398
|
}, |
|
399
|
{ |
|
400
|
reference_id: "prod_123", |
|
401
|
reference: "product", |
|
402
|
locale_code: "de-DE", |
|
403
|
translations: { title: "Titel DE" }, |
|
404
|
}, |
|
405
|
{ |
|
406
|
reference_id: "var_456", |
|
407
|
reference: "product_variant", |
|
408
|
locale_code: "fr-FR", |
|
409
|
translations: { title: "Variante FR" }, |
|
410
|
}, |
|
411
|
], |
|
412
|
}, |
|
413
|
adminHeaders |
|
414
|
) |
|
415
|
|
|
416
|
expect(response.status).toEqual(200) |
|
417
|
expect(response.data.created).toHaveLength(3) |
|
418
|
}) |
|
419
|
|
|
420
|
it("should create translations for different entity types", async () => { |
|
421
|
const response = await api.post( |
|
422
|
"/admin/translations/batch", |
|
423
|
{ |
|
424
|
create: [ |
|
425
|
{ |
|
426
|
reference_id: "prod_123", |
|
427
|
reference: "product", |
|
428
|
locale_code: "fr-FR", |
|
429
|
translations: { title: "Produit" }, |
|
430
|
}, |
|
431
|
{ |
|
432
|
reference_id: "cat_456", |
|
433
|
reference: "product_category", |
|
434
|
locale_code: "fr-FR", |
|
435
|
translations: { name: "Catégorie" }, |
|
436
|
}, |
|
437
|
{ |
|
438
|
reference_id: "col_789", |
|
439
|
reference: "product_collection", |
|
440
|
locale_code: "fr-FR", |
|
441
|
translations: { title: "Collection" }, |
|
442
|
}, |
|
443
|
], |
|
444
|
}, |
|
445
|
adminHeaders |
|
446
|
) |
|
447
|
|
|
448
|
expect(response.status).toEqual(200) |
|
449
|
expect(response.data.created).toHaveLength(3) |
|
450
|
expect(response.data.created).toEqual( |
|
451
|
expect.arrayContaining([ |
|
452
|
expect.objectContaining({ reference: "product" }), |
|
453
|
expect.objectContaining({ reference: "product_category" }), |
|
454
|
expect.objectContaining({ reference: "product_collection" }), |
|
455
|
]) |
|
456
|
) |
|
457
|
}) |
|
458
|
}) |
|
459
|
|
|
460
|
describe("update", () => { |
|
461
|
it("should update an existing translation", async () => { |
|
462
|
const createResponse = await api.post( |
|
463
|
"/admin/translations/batch", |
|
464
|
{ |
|
465
|
create: [ |
|
466
|
{ |
|
467
|
reference_id: "prod_123", |
|
468
|
reference: "product", |
|
469
|
locale_code: "fr-FR", |
|
470
|
translations: { title: "Ancien titre" }, |
|
471
|
}, |
|
472
|
], |
|
473
|
}, |
|
474
|
adminHeaders |
|
475
|
) |
|
476
|
|
|
477
|
const translationId = createResponse.data.created[0].id |
|
478
|
|
|
479
|
const updateResponse = await api.post( |
|
480
|
"/admin/translations/batch", |
|
481
|
{ |
|
482
|
update: [ |
|
483
|
{ |
|
484
|
id: translationId, |
|
485
|
translations: { title: "Nouveau titre" }, |
|
486
|
}, |
|
487
|
], |
|
488
|
}, |
|
489
|
adminHeaders |
|
490
|
) |
|
491
|
|
|
492
|
expect(updateResponse.status).toEqual(200) |
|
493
|
expect(updateResponse.data.updated).toHaveLength(1) |
|
494
|
expect(updateResponse.data.updated[0]).toEqual( |
|
495
|
expect.objectContaining({ |
|
496
|
id: translationId, |
|
497
|
translations: { title: "Nouveau titre" }, |
|
498
|
}) |
|
499
|
) |
|
500
|
}) |
|
501
|
|
|
502
|
it("should update multiple translations", async () => { |
|
503
|
const createResponse = await api.post( |
|
504
|
"/admin/translations/batch", |
|
505
|
{ |
|
506
|
create: [ |
|
507
|
{ |
|
508
|
reference_id: "prod_1", |
|
509
|
reference: "product", |
|
510
|
locale_code: "fr-FR", |
|
511
|
translations: { title: "Titre 1" }, |
|
512
|
}, |
|
513
|
{ |
|
514
|
reference_id: "prod_2", |
|
515
|
reference: "product", |
|
516
|
locale_code: "fr-FR", |
|
517
|
translations: { title: "Titre 2" }, |
|
518
|
}, |
|
519
|
], |
|
520
|
}, |
|
521
|
adminHeaders |
|
522
|
) |
|
523
|
|
|
524
|
const [trans1, trans2] = createResponse.data.created |
|
525
|
|
|
526
|
const updateResponse = await api.post( |
|
527
|
"/admin/translations/batch", |
|
528
|
{ |
|
529
|
update: [ |
|
530
|
{ id: trans1.id, translations: { title: "Nouveau 1" } }, |
|
531
|
{ id: trans2.id, translations: { title: "Nouveau 2" } }, |
|
532
|
], |
|
533
|
}, |
|
534
|
adminHeaders |
|
535
|
) |
|
536
|
|
|
537
|
expect(updateResponse.status).toEqual(200) |
|
538
|
expect(updateResponse.data.updated).toHaveLength(2) |
|
539
|
}) |
|
540
|
}) |
|
541
|
|
|
542
|
describe("delete", () => { |
|
543
|
it("should delete a translation", async () => { |
|
544
|
const createResponse = await api.post( |
|
545
|
"/admin/translations/batch", |
|
546
|
{ |
|
547
|
create: [ |
|
548
|
{ |
|
549
|
reference_id: "prod_123", |
|
550
|
reference: "product", |
|
551
|
locale_code: "fr-FR", |
|
552
|
translations: { title: "À supprimer" }, |
|
553
|
}, |
|
554
|
], |
|
555
|
}, |
|
556
|
adminHeaders |
|
557
|
) |
|
558
|
|
|
559
|
const translationId = createResponse.data.created[0].id |
|
560
|
|
|
561
|
const deleteResponse = await api.post( |
|
562
|
"/admin/translations/batch", |
|
563
|
{ |
|
564
|
delete: [translationId], |
|
565
|
}, |
|
566
|
adminHeaders |
|
567
|
) |
|
568
|
|
|
569
|
expect(deleteResponse.status).toEqual(200) |
|
570
|
expect(deleteResponse.data.deleted).toEqual({ |
|
571
|
ids: [translationId], |
|
572
|
object: "translation", |
|
573
|
deleted: true, |
|
574
|
}) |
|
575
|
|
|
576
|
const listResponse = await api.get( |
|
577
|
`/admin/translations?reference_id=prod_123`, |
|
578
|
adminHeaders |
|
579
|
) |
|
580
|
expect(listResponse.data.translations).toHaveLength(0) |
|
581
|
}) |
|
582
|
|
|
583
|
it("should delete multiple translations", async () => { |
|
584
|
const createResponse = await api.post( |
|
585
|
"/admin/translations/batch", |
|
586
|
{ |
|
587
|
create: [ |
|
588
|
{ |
|
589
|
reference_id: "prod_1", |
|
590
|
reference: "product", |
|
591
|
locale_code: "fr-FR", |
|
592
|
translations: { title: "Supprimer 1" }, |
|
593
|
}, |
|
594
|
{ |
|
595
|
reference_id: "prod_2", |
|
596
|
reference: "product", |
|
597
|
locale_code: "fr-FR", |
|
598
|
translations: { title: "Supprimer 2" }, |
|
599
|
}, |
|
600
|
], |
|
601
|
}, |
|
602
|
adminHeaders |
|
603
|
) |
|
604
|
|
|
605
|
const ids = createResponse.data.created.map((t) => t.id) |
|
606
|
|
|
607
|
const deleteResponse = await api.post( |
|
608
|
"/admin/translations/batch", |
|
609
|
{ |
|
610
|
delete: ids, |
|
611
|
}, |
|
612
|
adminHeaders |
|
613
|
) |
|
614
|
|
|
615
|
expect(deleteResponse.status).toEqual(200) |
|
616
|
expect(deleteResponse.data.deleted.ids).toHaveLength(2) |
|
617
|
}) |
|
618
|
}) |
|
619
|
|
|
620
|
describe("combined operations", () => { |
|
621
|
it("should handle create, update, and delete in a single batch", async () => { |
|
622
|
const createResponse = await api.post( |
|
623
|
"/admin/translations/batch", |
|
624
|
{ |
|
625
|
create: [ |
|
626
|
{ |
|
627
|
reference_id: "prod_existing", |
|
628
|
reference: "product", |
|
629
|
locale_code: "fr-FR", |
|
630
|
translations: { title: "Existant" }, |
|
631
|
}, |
|
632
|
{ |
|
633
|
reference_id: "prod_to_delete", |
|
634
|
reference: "product", |
|
635
|
locale_code: "fr-FR", |
|
636
|
translations: { title: "À supprimer" }, |
|
637
|
}, |
|
638
|
], |
|
639
|
}, |
|
640
|
adminHeaders |
|
641
|
) |
|
642
|
|
|
643
|
const existingId = createResponse.data.created[0].id |
|
644
|
const toDeleteId = createResponse.data.created[1].id |
|
645
|
|
|
646
|
const batchResponse = await api.post( |
|
647
|
"/admin/translations/batch", |
|
648
|
{ |
|
649
|
create: [ |
|
650
|
{ |
|
651
|
reference_id: "prod_new", |
|
652
|
reference: "product", |
|
653
|
locale_code: "fr-FR", |
|
654
|
translations: { title: "Nouveau" }, |
|
655
|
}, |
|
656
|
], |
|
657
|
update: [ |
|
658
|
{ |
|
659
|
id: existingId, |
|
660
|
translations: { title: "Mis à jour" }, |
|
661
|
}, |
|
662
|
], |
|
663
|
delete: [toDeleteId], |
|
664
|
}, |
|
665
|
adminHeaders |
|
666
|
) |
|
667
|
|
|
668
|
expect(batchResponse.status).toEqual(200) |
|
669
|
expect(batchResponse.data.created).toHaveLength(1) |
|
670
|
expect(batchResponse.data.updated).toHaveLength(1) |
|
671
|
expect(batchResponse.data.deleted.ids).toContain(toDeleteId) |
|
672
|
|
|
673
|
expect(batchResponse.data.created[0].translations.title).toEqual( |
|
674
|
"Nouveau" |
|
675
|
) |
|
676
|
expect(batchResponse.data.updated[0].translations.title).toEqual( |
|
677
|
"Mis à jour" |
|
678
|
) |
|
679
|
}) |
|
680
|
|
|
681
|
it("should keep created and updated separate when both are returned in one batch", async () => { |
|
682
|
const seedResponse = await api.post( |
|
683
|
"/admin/translations/batch", |
|
684
|
{ |
|
685
|
create: Array.from({ length: 5 }, (_, i) => ({ |
|
686
|
reference_id: `prod_seed_${i}`, |
|
687
|
reference: "product", |
|
688
|
locale_code: "fr-FR", |
|
689
|
translations: { title: `Semence ${i}` }, |
|
690
|
})), |
|
691
|
}, |
|
692
|
adminHeaders |
|
693
|
) |
|
694
|
|
|
695
|
const seededIds = seedResponse.data.created.map((t) => t.id) |
|
696
|
expect(seededIds).toHaveLength(5) |
|
697
|
|
|
698
|
const batchResponse = await api.post( |
|
699
|
"/admin/translations/batch", |
|
700
|
{ |
|
701
|
create: Array.from({ length: 5 }, (_, i) => ({ |
|
702
|
reference_id: `prod_frais_${i}`, |
|
703
|
reference: "product", |
|
704
|
locale_code: "fr-FR", |
|
705
|
translations: { title: `Frais ${i}` }, |
|
706
|
})), |
|
707
|
update: seededIds.map((id, i) => ({ |
|
708
|
id, |
|
709
|
translations: { title: `Mis à jour ${i}` }, |
|
710
|
})), |
|
711
|
}, |
|
712
|
adminHeaders |
|
713
|
) |
|
714
|
|
|
715
|
expect(batchResponse.status).toEqual(200) |
|
716
|
|
|
717
|
const createdIds = batchResponse.data.created.map((t) => t.id) |
|
718
|
const updatedIds = batchResponse.data.updated.map((t) => t.id) |
|
719
|
|
|
720
|
expect(createdIds).toHaveLength(5) |
|
721
|
expect(updatedIds).toHaveLength(5) |
|
722
|
|
|
723
|
// Every seeded row lands in `updated`, and no id may show up in both buckets |
|
724
|
expect([...updatedIds].sort()).toEqual([...seededIds].sort()) |
|
725
|
expect(createdIds.filter((id) => updatedIds.includes(id))).toEqual( |
|
726
|
[] |
|
727
|
) |
|
728
|
|
|
729
|
// Each bucket carries its own rows, not a mix of the two |
|
730
|
expect( |
|
731
|
batchResponse.data.created.every((t) => |
|
732
|
t.reference_id.startsWith("prod_frais_") |
|
733
|
) |
|
734
|
).toBe(true) |
|
735
|
expect( |
|
736
|
batchResponse.data.updated.every((t) => |
|
737
|
t.translations.title.startsWith("Mis à jour") |
|
738
|
) |
|
739
|
).toBe(true) |
|
740
|
}) |
|
741
|
}) |
|
742
|
}) |
|
743
|
|
|
744
|
describe("GET /admin/translations/statistics", () => { |
|
745
|
it("should return statistics for entity types with no translations", async () => { |
|
746
|
const productModule = appContainer.resolve(Modules.PRODUCT) |
|
747
|
await productModule.createProducts([ |
|
748
|
{ title: "Product 1" }, |
|
749
|
{ title: "Product 2" }, |
|
750
|
]) |
|
751
|
|
|
752
|
const response = await api.get( |
|
753
|
"/admin/translations/statistics?locales=en-US&locales=fr-FR&entity_types=product", |
|
754
|
adminHeaders |
|
755
|
) |
|
756
|
|
|
757
|
expect(response.status).toEqual(200) |
|
758
|
expect(response.data.statistics).toBeDefined() |
|
759
|
expect(response.data.statistics.product).toEqual({ |
|
760
|
// 2 products × 4 translatable fields × 2 locales = 16 expected |
|
761
|
expected: 16, |
|
762
|
translated: 0, |
|
763
|
missing: 16, |
|
764
|
by_locale: { |
|
765
|
"en-US": { expected: 8, translated: 0, missing: 8 }, |
|
766
|
"fr-FR": { expected: 8, translated: 0, missing: 8 }, |
|
767
|
}, |
|
768
|
}) |
|
769
|
}) |
|
770
|
|
|
771
|
it("should return statistics with partial translations", async () => { |
|
772
|
const productModule = appContainer.resolve(Modules.PRODUCT) |
|
773
|
const [product1, product2] = await productModule.createProducts([ |
|
774
|
{ title: "Product 1" }, |
|
775
|
{ title: "Product 2" }, |
|
776
|
]) |
|
777
|
|
|
778
|
// Create translations for product1 with partial fields |
|
779
|
await api.post( |
|
780
|
"/admin/translations/batch", |
|
781
|
{ |
|
782
|
create: [ |
|
783
|
{ |
|
784
|
reference_id: product1.id, |
|
785
|
reference: "product", |
|
786
|
locale_code: "fr-FR", |
|
787
|
translations: { |
|
788
|
title: "Produit 1", |
|
789
|
description: "Description du produit 1", |
|
790
|
}, |
|
791
|
}, |
|
792
|
{ |
|
793
|
reference_id: product2.id, |
|
794
|
reference: "product", |
|
795
|
locale_code: "fr-FR", |
|
796
|
translations: { |
|
797
|
title: "Produit 2", |
|
798
|
}, |
|
799
|
}, |
|
800
|
], |
|
801
|
}, |
|
802
|
adminHeaders |
|
803
|
) |
|
804
|
|
|
805
|
const response = await api.get( |
|
806
|
"/admin/translations/statistics?locales=fr-FR&entity_types=product", |
|
807
|
adminHeaders |
|
808
|
) |
|
809
|
|
|
810
|
expect(response.status).toEqual(200) |
|
811
|
// 2 products × 4 fields × 1 locale = 8 expected |
|
812
|
// product1 has 2 fields, product2 has 1 field = 3 translated |
|
813
|
expect(response.data.statistics.product).toEqual({ |
|
814
|
expected: 8, |
|
815
|
translated: 3, |
|
816
|
missing: 5, |
|
817
|
by_locale: { |
|
818
|
"fr-FR": { expected: 8, translated: 3, missing: 5 }, |
|
819
|
}, |
|
820
|
}) |
|
821
|
}) |
|
822
|
|
|
823
|
it("should return statistics for multiple entity types", async () => { |
|
824
|
const productModule = appContainer.resolve(Modules.PRODUCT) |
|
825
|
const [product] = await productModule.createProducts([ |
|
826
|
{ |
|
827
|
title: "Product with variant", |
|
828
|
variants: [{ title: "Variant 1" }, { title: "Variant 2" }], |
|
829
|
}, |
|
830
|
]) |
|
831
|
|
|
832
|
await api.post( |
|
833
|
"/admin/translations/batch", |
|
834
|
{ |
|
835
|
create: [ |
|
836
|
{ |
|
837
|
reference_id: product.id, |
|
838
|
reference: "product", |
|
839
|
locale_code: "fr-FR", |
|
840
|
translations: { |
|
841
|
title: "Produit", |
|
842
|
description: "Description", |
|
843
|
subtitle: "Sous-titre", |
|
844
|
material: "Matériau", |
|
845
|
}, |
|
846
|
}, |
|
847
|
{ |
|
848
|
reference_id: product.variants[0].id, |
|
849
|
reference: "product_variant", |
|
850
|
locale_code: "fr-FR", |
|
851
|
translations: { |
|
852
|
title: "Variante 1", |
|
853
|
}, |
|
854
|
}, |
|
855
|
], |
|
856
|
}, |
|
857
|
adminHeaders |
|
858
|
) |
|
859
|
|
|
860
|
const response = await api.get( |
|
861
|
"/admin/translations/statistics?locales=fr-FR&entity_types=product&entity_types=product_variant", |
|
862
|
adminHeaders |
|
863
|
) |
|
864
|
|
|
865
|
expect(response.status).toEqual(200) |
|
866
|
|
|
867
|
// Product: 1 × 4 fields × 1 locale = 4, all translated |
|
868
|
expect(response.data.statistics.product).toEqual({ |
|
869
|
expected: 4, |
|
870
|
translated: 4, |
|
871
|
missing: 0, |
|
872
|
by_locale: { |
|
873
|
"fr-FR": { expected: 4, translated: 4, missing: 0 }, |
|
874
|
}, |
|
875
|
}) |
|
876
|
|
|
877
|
// Variant: 2 × 2 fields × 1 locale = 4, 1 translated |
|
878
|
expect(response.data.statistics.product_variant).toEqual({ |
|
879
|
expected: 4, |
|
880
|
translated: 1, |
|
881
|
missing: 3, |
|
882
|
by_locale: { |
|
883
|
"fr-FR": { expected: 4, translated: 1, missing: 3 }, |
|
884
|
}, |
|
885
|
}) |
|
886
|
}) |
|
887
|
|
|
888
|
it("should return statistics for multiple locales", async () => { |
|
889
|
const productModule = appContainer.resolve(Modules.PRODUCT) |
|
890
|
const [product] = await productModule.createProducts([ |
|
891
|
{ title: "Product" }, |
|
892
|
]) |
|
893
|
|
|
894
|
await api.post( |
|
895
|
"/admin/translations/batch", |
|
896
|
{ |
|
897
|
create: [ |
|
898
|
{ |
|
899
|
reference_id: product.id, |
|
900
|
reference: "product", |
|
901
|
locale_code: "fr-FR", |
|
902
|
translations: { |
|
903
|
title: "Produit", |
|
904
|
description: "Description", |
|
905
|
}, |
|
906
|
}, |
|
907
|
{ |
|
908
|
reference_id: product.id, |
|
909
|
reference: "product", |
|
910
|
locale_code: "de-DE", |
|
911
|
translations: { title: "Produkt" }, |
|
912
|
}, |
|
913
|
], |
|
914
|
}, |
|
915
|
adminHeaders |
|
916
|
) |
|
917
|
|
|
918
|
const response = await api.get( |
|
919
|
"/admin/translations/statistics?locales=fr-FR&locales=de-DE&entity_types=product", |
|
920
|
adminHeaders |
|
921
|
) |
|
922
|
|
|
923
|
expect(response.status).toEqual(200) |
|
924
|
// 1 product × 4 fields × 2 locales = 8 expected |
|
925
|
// fr-FR: 2 translated, de-DE: 1 translated = 3 total |
|
926
|
expect(response.data.statistics.product.expected).toEqual(8) |
|
927
|
expect(response.data.statistics.product.translated).toEqual(3) |
|
928
|
expect(response.data.statistics.product.missing).toEqual(5) |
|
929
|
|
|
930
|
expect(response.data.statistics.product.by_locale["fr-FR"]).toEqual({ |
|
931
|
expected: 4, |
|
932
|
translated: 2, |
|
933
|
missing: 2, |
|
934
|
}) |
|
935
|
expect(response.data.statistics.product.by_locale["de-DE"]).toEqual({ |
|
936
|
expected: 4, |
|
937
|
translated: 1, |
|
938
|
missing: 3, |
|
939
|
}) |
|
940
|
}) |
|
941
|
|
|
942
|
it("should return zeros for unknown entity types", async () => { |
|
943
|
const response = await api.get( |
|
944
|
"/admin/translations/statistics?locales=fr-FR&entity_types=unknown_entity", |
|
945
|
adminHeaders |
|
946
|
) |
|
947
|
|
|
948
|
expect(response.status).toEqual(200) |
|
949
|
expect(response.data.statistics.unknown_entity).toEqual({ |
|
950
|
expected: 0, |
|
951
|
translated: 0, |
|
952
|
missing: 0, |
|
953
|
by_locale: { |
|
954
|
"fr-FR": { expected: 0, translated: 0, missing: 0 }, |
|
955
|
}, |
|
956
|
}) |
|
957
|
}) |
|
958
|
|
|
959
|
it("should validate required fields", async () => { |
|
960
|
const response1 = await api |
|
961
|
.get( |
|
962
|
"/admin/translations/statistics?entity_types=product", |
|
963
|
adminHeaders |
|
964
|
) |
|
965
|
.catch((e) => e.response) |
|
966
|
|
|
967
|
expect(response1.status).toEqual(400) |
|
968
|
|
|
969
|
const response2 = await api |
|
970
|
.get("/admin/translations/statistics?locales=fr-FR", adminHeaders) |
|
971
|
.catch((e) => e.response) |
|
972
|
|
|
973
|
expect(response2.status).toEqual(400) |
|
974
|
|
|
975
|
const response3 = await api |
|
976
|
.get("/admin/translations/statistics", adminHeaders) |
|
977
|
.catch((e) => e.response) |
|
978
|
|
|
979
|
expect(response3.status).toEqual(400) |
|
980
|
}) |
|
981
|
}) |
|
982
|
|
|
983
|
describe("GET /admin/translations/entities", () => { |
|
984
|
it("should return entities with only translatable fields", async () => { |
|
985
|
const productModule = appContainer.resolve(Modules.PRODUCT) |
|
986
|
await productModule.createProducts([ |
|
987
|
{ |
|
988
|
title: "Product 1", |
|
989
|
description: "Description 1", |
|
990
|
handle: "product-1", |
|
991
|
status: "published", |
|
992
|
}, |
|
993
|
{ |
|
994
|
title: "Product 2", |
|
995
|
description: "Description 2", |
|
996
|
handle: "product-2", |
|
997
|
status: "draft", |
|
998
|
}, |
|
999
|
]) |
|
1000
|
|
|
1001
|
const response = await api.get( |
|
1002
|
"/admin/translations/entities?type=product", |
|
1003
|
adminHeaders |
|
1004
|
) |
|
1005
|
|
|
1006
|
expect(response.status).toEqual(200) |
|
1007
|
expect(response.data.data).toHaveLength(2) |
|
1008
|
expect(response.data.count).toEqual(2) |
|
1009
|
expect(response.data.offset).toEqual(0) |
|
1010
|
expect(response.data.limit).toEqual(20) |
|
1011
|
|
|
1012
|
response.data.data.forEach((entity: Record<string, unknown>) => { |
|
1013
|
expect(entity).toHaveProperty("id") |
|
1014
|
expect(entity.title).toBeDefined() |
|
1015
|
expect(entity.description).toBeDefined() |
|
1016
|
expect(entity.material).toBeDefined() |
|
1017
|
expect(entity.status).not.toBeDefined() |
|
1018
|
}) |
|
1019
|
}) |
|
1020
|
|
|
1021
|
it("should return empty array for unknown entity type", async () => { |
|
1022
|
const response = await api.get( |
|
1023
|
"/admin/translations/entities?type=unknown_entity", |
|
1024
|
adminHeaders |
|
1025
|
) |
|
1026
|
|
|
1027
|
expect(response.status).toEqual(200) |
|
1028
|
expect(response.data.data).toEqual([]) |
|
1029
|
expect(response.data.count).toEqual(0) |
|
1030
|
}) |
|
1031
|
|
|
1032
|
it("should support pagination", async () => { |
|
1033
|
const productModule = appContainer.resolve(Modules.PRODUCT) |
|
1034
|
await productModule.createProducts([ |
|
1035
|
{ title: "Product 1" }, |
|
1036
|
{ title: "Product 2" }, |
|
1037
|
{ title: "Product 3" }, |
|
1038
|
{ title: "Product 4" }, |
|
1039
|
{ title: "Product 5" }, |
|
1040
|
]) |
|
1041
|
|
|
1042
|
const response = await api.get( |
|
1043
|
"/admin/translations/entities?type=product&limit=2&offset=0", |
|
1044
|
adminHeaders |
|
1045
|
) |
|
1046
|
|
|
1047
|
expect(response.status).toEqual(200) |
|
1048
|
expect(response.data.data).toHaveLength(2) |
|
1049
|
expect(response.data.count).toEqual(5) |
|
1050
|
expect(response.data.limit).toEqual(2) |
|
1051
|
expect(response.data.offset).toEqual(0) |
|
1052
|
|
|
1053
|
const response2 = await api.get( |
|
1054
|
"/admin/translations/entities?type=product&limit=2&offset=2", |
|
1055
|
adminHeaders |
|
1056
|
) |
|
1057
|
|
|
1058
|
expect(response2.status).toEqual(200) |
|
1059
|
expect(response2.data.data).toHaveLength(2) |
|
1060
|
expect(response2.data.offset).toEqual(2) |
|
1061
|
}) |
|
1062
|
|
|
1063
|
it("should return product variants with their translatable fields", async () => { |
|
1064
|
const productModule = appContainer.resolve(Modules.PRODUCT) |
|
1065
|
await productModule.createProducts([ |
|
1066
|
{ |
|
1067
|
title: "Product with variants", |
|
1068
|
variants: [ |
|
1069
|
{ title: "Variant 1", manage_inventory: false }, |
|
1070
|
{ title: "Variant 2", manage_inventory: false }, |
|
1071
|
], |
|
1072
|
}, |
|
1073
|
]) |
|
1074
|
|
|
1075
|
const response = await api.get( |
|
1076
|
"/admin/translations/entities?type=product_variant", |
|
1077
|
adminHeaders |
|
1078
|
) |
|
1079
|
|
|
1080
|
expect(response.status).toEqual(200) |
|
1081
|
expect(response.data.data).toHaveLength(2) |
|
1082
|
expect(response.data.count).toEqual(2) |
|
1083
|
|
|
1084
|
response.data.data.forEach((variant: Record<string, unknown>) => { |
|
1085
|
expect(variant).toHaveProperty("id") |
|
1086
|
expect(variant.title).toBeDefined() |
|
1087
|
expect(variant.manage_inventory).not.toBeDefined() |
|
1088
|
}) |
|
1089
|
}) |
|
1090
|
|
|
1091
|
it("should validate required type parameter", async () => { |
|
1092
|
const response = await api |
|
1093
|
.get("/admin/translations/entities", adminHeaders) |
|
1094
|
.catch((e) => e.response) |
|
1095
|
|
|
1096
|
expect(response.status).toEqual(400) |
|
1097
|
}) |
|
1098
|
}) |
|
1099
|
}) |
|
1100
|
}, |
|
1101
|
}) |