@open-mercato/search 0.10.1-develop.8231.1.ae76b1d2c5 → 0.10.1-develop.8258.1.62a94b74a2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/indexer/search-indexer.js +21 -4
  3. package/dist/indexer/search-indexer.js.map +2 -2
  4. package/dist/modules/search/__integration__/TC-SEARCH-016.spec.js +187 -0
  5. package/dist/modules/search/__integration__/TC-SEARCH-016.spec.js.map +7 -0
  6. package/dist/modules/search/__integration__/TC-SEARCH-017.spec.js +94 -0
  7. package/dist/modules/search/__integration__/TC-SEARCH-017.spec.js.map +7 -0
  8. package/dist/modules/search/__integration__/TC-SEARCH-018.spec.js +74 -0
  9. package/dist/modules/search/__integration__/TC-SEARCH-018.spec.js.map +7 -0
  10. package/dist/modules/search/ai-tools.js +3 -1
  11. package/dist/modules/search/ai-tools.js.map +2 -2
  12. package/dist/modules/search/api/openapi.js +1 -0
  13. package/dist/modules/search/api/openapi.js.map +2 -2
  14. package/dist/modules/search/api/reindex/route.js +35 -16
  15. package/dist/modules/search/api/reindex/route.js.map +2 -2
  16. package/dist/modules/search/cli.js +2 -16
  17. package/dist/modules/search/cli.js.map +2 -2
  18. package/dist/modules/search/frontend/components/sections/FulltextSearchSection.js +1 -1
  19. package/dist/modules/search/frontend/components/sections/FulltextSearchSection.js.map +2 -2
  20. package/dist/modules/search/i18n/de.json +2 -0
  21. package/dist/modules/search/i18n/en.json +2 -0
  22. package/dist/modules/search/i18n/es.json +2 -0
  23. package/dist/modules/search/i18n/ko.json +2 -0
  24. package/dist/modules/search/i18n/pl.json +2 -0
  25. package/dist/modules/search/lib/reindex-lock.js +29 -6
  26. package/dist/modules/search/lib/reindex-lock.js.map +2 -2
  27. package/dist/modules/search/lib/reindex-progress.js +28 -3
  28. package/dist/modules/search/lib/reindex-progress.js.map +2 -2
  29. package/dist/modules/search/workers/fulltext-index.worker.js +19 -6
  30. package/dist/modules/search/workers/fulltext-index.worker.js.map +2 -2
  31. package/dist/strategies/fulltext.strategy.js +33 -2
  32. package/dist/strategies/fulltext.strategy.js.map +2 -2
  33. package/package.json +4 -4
  34. package/src/__tests__/reindex-strategy-policy.test.ts +2 -0
  35. package/src/__tests__/search-indexer-fulltext-reindex-scope.test.ts +435 -0
  36. package/src/__tests__/workers.test.ts +46 -1
  37. package/src/indexer/search-indexer.ts +31 -7
  38. package/src/modules/search/__integration__/TC-SEARCH-016.spec.ts +247 -0
  39. package/src/modules/search/__integration__/TC-SEARCH-017.spec.ts +141 -0
  40. package/src/modules/search/__integration__/TC-SEARCH-018.spec.ts +114 -0
  41. package/src/modules/search/ai-tools.ts +3 -1
  42. package/src/modules/search/api/__tests__/reindex.routes.test.ts +222 -6
  43. package/src/modules/search/api/openapi.ts +1 -0
  44. package/src/modules/search/api/reindex/route.ts +39 -16
  45. package/src/modules/search/cli.ts +2 -16
  46. package/src/modules/search/frontend/components/sections/FulltextSearchSection.tsx +1 -1
  47. package/src/modules/search/frontend/components/sections/__tests__/FulltextSearchSection.test.tsx +112 -3
  48. package/src/modules/search/i18n/de.json +2 -0
  49. package/src/modules/search/i18n/en.json +2 -0
  50. package/src/modules/search/i18n/es.json +2 -0
  51. package/src/modules/search/i18n/ko.json +2 -0
  52. package/src/modules/search/i18n/pl.json +2 -0
  53. package/src/modules/search/lib/__tests__/reindex-progress.test.ts +157 -0
  54. package/src/modules/search/lib/reindex-lock.ts +66 -8
  55. package/src/modules/search/lib/reindex-progress.ts +55 -2
  56. package/src/modules/search/workers/fulltext-index.worker.ts +21 -6
  57. package/src/strategies/fulltext.strategy.ts +39 -2
@@ -26,18 +26,21 @@ jest.mock('@open-mercato/shared/lib/indexers/error-log', () => ({
26
26
  const mockGetReindexLockStatus = jest.fn().mockResolvedValue(null)
27
27
  const mockAcquireReindexLock = jest.fn().mockResolvedValue({ acquired: true })
28
28
  const mockClearReindexLock = jest.fn().mockResolvedValue(undefined)
29
+ const mockSetReindexLockTotal = jest.fn()
29
30
  jest.mock('../../lib/reindex-lock', () => ({
30
31
  getReindexLockStatus: (...args: unknown[]) => mockGetReindexLockStatus(...args),
31
32
  acquireReindexLock: (...args: unknown[]) => mockAcquireReindexLock(...args),
32
33
  clearReindexLock: (...args: unknown[]) => mockClearReindexLock(...args),
34
+ setReindexLockTotal: (...args: unknown[]) => mockSetReindexLockTotal(...args),
35
+ isReindexLockDrained: jest.requireActual('../../lib/reindex-lock').isReindexLockDrained,
33
36
  }))
34
37
 
35
- const mockEnsureReindexProgressJob = jest.fn().mockResolvedValue('progress-1')
38
+ const mockStartReindexProgressJob = jest.fn().mockResolvedValue('progress-1')
36
39
  const mockCompleteReindexProgress = jest.fn().mockResolvedValue(undefined)
37
40
  const mockFailReindexProgress = jest.fn().mockResolvedValue(undefined)
38
41
  const mockCancelReindexProgress = jest.fn().mockResolvedValue(undefined)
39
42
  jest.mock('../../lib/reindex-progress', () => ({
40
- ensureReindexProgressJob: (...args: unknown[]) => mockEnsureReindexProgressJob(...args),
43
+ startReindexProgressJob: (...args: unknown[]) => mockStartReindexProgressJob(...args),
41
44
  completeReindexProgress: (...args: unknown[]) => mockCompleteReindexProgress(...args),
42
45
  failReindexProgress: (...args: unknown[]) => mockFailReindexProgress(...args),
43
46
  cancelReindexProgress: (...args: unknown[]) => mockCancelReindexProgress(...args),
@@ -45,6 +48,13 @@ jest.mock('../../lib/reindex-progress', () => ({
45
48
 
46
49
  import { POST as fulltextReindexPost } from '../reindex/route'
47
50
  import { POST as fulltextReindexCancelPost } from '../reindex/cancel/route'
51
+ import { SearchIndexer, type SearchIndexerOptions } from '../../../../indexer/search-indexer'
52
+ import { SearchService } from '../../../../service'
53
+ import { FullTextSearchStrategy } from '../../../../strategies/fulltext.strategy'
54
+ import type { FullTextSearchDriver } from '../../../../fulltext/types'
55
+ import type { QueryEngine } from '@open-mercato/shared/lib/query/types'
56
+
57
+ const mockProgressService = { updateProgress: jest.fn().mockResolvedValue(undefined) }
48
58
 
49
59
  function mockAuth() {
50
60
  mockGetAuthFromRequest.mockResolvedValue({
@@ -67,6 +77,11 @@ describe('POST /api/search/reindex', () => {
67
77
  mockAuth()
68
78
  mockGetReindexLockStatus.mockResolvedValue(null)
69
79
  mockAcquireReindexLock.mockResolvedValue({ acquired: true })
80
+ mockClearReindexLock.mockResolvedValue(undefined)
81
+ mockSetReindexLockTotal.mockImplementation(async (_db: unknown, _tenantId: string, _type: string, totalCount: number) => ({
82
+ processedCount: 0,
83
+ totalCount,
84
+ }))
70
85
  })
71
86
 
72
87
  test('clears the fulltext lock when no indexable strategy is configured', async () => {
@@ -75,7 +90,7 @@ describe('POST /api/search/reindex', () => {
75
90
  const mockContainer = {
76
91
  resolve: jest.fn((name: string) => {
77
92
  if (name === 'em') return mockEm
78
- if (name === 'progressService') return { progress: true }
93
+ if (name === 'progressService') return mockProgressService
79
94
  if (name === 'searchStrategies') return []
80
95
  throw new Error(`Unknown service: ${name}`)
81
96
  }),
@@ -112,7 +127,7 @@ describe('POST /api/search/reindex', () => {
112
127
  const mockContainer = {
113
128
  resolve: jest.fn((name: string) => {
114
129
  if (name === 'em') return mockEm
115
- if (name === 'progressService') return { progress: true }
130
+ if (name === 'progressService') return mockProgressService
116
131
  if (name === 'searchStrategies') return [mockStrategy]
117
132
  if (name === 'searchIndexer') return mockSearchIndexer
118
133
  throw new Error(`Unknown service: ${name}`)
@@ -142,14 +157,18 @@ describe('POST /api/search/reindex', () => {
142
157
  useQueue: true,
143
158
  }),
144
159
  )
145
- expect(mockEnsureReindexProgressJob).toHaveBeenCalledWith(
160
+ expect(mockStartReindexProgressJob).toHaveBeenCalledWith(
146
161
  expect.objectContaining({
147
162
  type: 'fulltext',
148
163
  tenantId: 'tenant-123',
149
164
  organizationId: 'org-456',
150
- totalCount: 0,
151
165
  }),
152
166
  )
167
+ expect(mockProgressService.updateProgress).toHaveBeenCalledWith(
168
+ 'progress-1',
169
+ { totalCount: 0 },
170
+ expect.objectContaining({ tenantId: 'tenant-123', organizationId: 'org-456' }),
171
+ )
153
172
  expect(mockCompleteReindexProgress).toHaveBeenCalledWith(
154
173
  expect.objectContaining({
155
174
  type: 'fulltext',
@@ -166,6 +185,203 @@ describe('POST /api/search/reindex', () => {
166
185
  expect(mockContainer.dispose).toHaveBeenCalled()
167
186
  })
168
187
 
188
+ test.each([
189
+ { label: 'entity', payload: { action: 'reindex', entityId: 'test:product', useQueue: false }, failedEntityId: 'test:product' },
190
+ { label: 'full', payload: { action: 'reindex', useQueue: true }, failedEntityId: 'all' },
191
+ ])('fails progress and returns a localized error when the fulltext driver cannot clear a scoped $label reindex', async ({ payload, failedEntityId }) => {
192
+ const mockDb = { db: true }
193
+ const mockEm = { getKysely: jest.fn(() => mockDb) }
194
+ const driver: FullTextSearchDriver = {
195
+ id: 'algolia',
196
+ ensureReady: jest.fn(async () => {}),
197
+ isHealthy: jest.fn(async () => true),
198
+ search: jest.fn(async () => []),
199
+ index: jest.fn(async () => {}),
200
+ delete: jest.fn(async () => {}),
201
+ bulkIndex: jest.fn(async () => {}),
202
+ recreateIndex: jest.fn(async () => {}),
203
+ }
204
+ const strategy = new FullTextSearchStrategy(driver)
205
+ const queryEngine: QueryEngine = { query: jest.fn() }
206
+ const enqueue = jest.fn(async () => 'job-1')
207
+ const searchIndexer = new SearchIndexer(
208
+ new SearchService({ strategies: [strategy] }),
209
+ [{ entities: [{ entityId: 'test:product', enabled: true }] }],
210
+ { queryEngine, fulltextQueue: { enqueue } as unknown as SearchIndexerOptions['fulltextQueue'] },
211
+ )
212
+ const mockContainer = {
213
+ resolve: jest.fn((name: string) => {
214
+ if (name === 'em') return mockEm
215
+ if (name === 'progressService') return mockProgressService
216
+ if (name === 'searchStrategies') return [strategy]
217
+ if (name === 'searchIndexer') return searchIndexer
218
+ throw new Error(`Unknown service: ${name}`)
219
+ }),
220
+ dispose: jest.fn().mockResolvedValue(undefined),
221
+ }
222
+ mockCreateRequestContainer.mockResolvedValue(mockContainer)
223
+
224
+ const res = await fulltextReindexPost(makeRequest(payload))
225
+ const body = await res.json()
226
+
227
+ expect(body.ok).toBe(false)
228
+ expect(body.error).toBe('Reindex did not complete. Check the search activity log for details.')
229
+ expect(body.result.errors).toEqual([
230
+ { entityId: failedEntityId, error: expect.stringContaining('implement purge or listDocumentIds') },
231
+ ])
232
+ expect(mockFailReindexProgress).toHaveBeenCalledWith(expect.objectContaining({
233
+ type: 'fulltext',
234
+ tenantId: 'tenant-123',
235
+ organizationId: 'org-456',
236
+ errorMessage: expect.stringContaining('implement purge or listDocumentIds'),
237
+ }))
238
+ expect(mockCompleteReindexProgress).not.toHaveBeenCalled()
239
+ expect(mockClearReindexLock).toHaveBeenCalledWith(mockDb, 'tenant-123', 'fulltext', 'org-456')
240
+ expect(driver.recreateIndex).not.toHaveBeenCalled()
241
+ expect(driver.bulkIndex).not.toHaveBeenCalled()
242
+ expect(enqueue).not.toHaveBeenCalled()
243
+ expect(queryEngine.query).not.toHaveBeenCalled()
244
+ })
245
+
246
+ test('keeps the lock and progress with queued workers when one entity fails its scoped clear, so a retry stays excluded', async () => {
247
+ const mockDb = { db: true }
248
+ const mockEm = { getKysely: jest.fn(() => mockDb) }
249
+ let lockHeld = false
250
+ mockAcquireReindexLock.mockImplementation(async () => {
251
+ lockHeld = true
252
+ return { acquired: true }
253
+ })
254
+ mockClearReindexLock.mockImplementation(async () => {
255
+ lockHeld = false
256
+ })
257
+ mockGetReindexLockStatus.mockImplementation(async () => (lockHeld
258
+ ? { type: 'fulltext', action: 'reindex', startedAt: new Date().toISOString(), processedCount: 0, totalCount: 1 }
259
+ : null))
260
+ const driver: FullTextSearchDriver = {
261
+ id: 'meilisearch',
262
+ ensureReady: jest.fn(async () => {}),
263
+ isHealthy: jest.fn(async () => true),
264
+ search: jest.fn(async () => []),
265
+ index: jest.fn(async () => {}),
266
+ delete: jest.fn(async () => {}),
267
+ bulkIndex: jest.fn(async () => {}),
268
+ purge: jest.fn(async (entityId: string) => {
269
+ if (entityId === 'test:product') throw new Error('purge unavailable')
270
+ }),
271
+ recreateIndex: jest.fn(async () => {}),
272
+ }
273
+ const strategy = new FullTextSearchStrategy(driver)
274
+ const queryEngine = {
275
+ query: jest.fn(async () => ({ items: [{ id: 'c-1', organization_id: 'org-456' }], total: 1 })),
276
+ } as unknown as QueryEngine
277
+ const enqueue = jest.fn(async () => 'job-1')
278
+ const searchIndexer = new SearchIndexer(
279
+ new SearchService({ strategies: [strategy] }),
280
+ [{ entities: [{ entityId: 'test:product', enabled: true }, { entityId: 'test:customer', enabled: true }] }],
281
+ { queryEngine, fulltextQueue: { enqueue } as unknown as SearchIndexerOptions['fulltextQueue'] },
282
+ )
283
+ const mockContainer = {
284
+ resolve: jest.fn((name: string) => {
285
+ if (name === 'em') return mockEm
286
+ if (name === 'progressService') return mockProgressService
287
+ if (name === 'searchStrategies') return [strategy]
288
+ if (name === 'searchIndexer') return searchIndexer
289
+ throw new Error(`Unknown service: ${name}`)
290
+ }),
291
+ dispose: jest.fn().mockResolvedValue(undefined),
292
+ }
293
+ mockCreateRequestContainer.mockResolvedValue(mockContainer)
294
+
295
+ const res = await fulltextReindexPost(makeRequest({ action: 'reindex' }))
296
+ const body = await res.json()
297
+
298
+ expect(body.ok).toBe(false)
299
+ expect(body.error).toBe('Reindex did not complete. Check the search activity log for details.')
300
+ expect(body.result).toEqual(expect.objectContaining({
301
+ jobsEnqueued: 1,
302
+ errors: [{ entityId: 'test:product', error: 'purge unavailable' }],
303
+ }))
304
+ expect(enqueue).toHaveBeenCalledTimes(1)
305
+ expect(enqueue).toHaveBeenCalledWith(expect.objectContaining({
306
+ jobType: 'batch-index',
307
+ records: [{ entityId: 'test:customer', recordId: 'c-1' }],
308
+ }))
309
+ expect(mockStartReindexProgressJob.mock.invocationCallOrder[0]).toBeLessThan(enqueue.mock.invocationCallOrder[0])
310
+ expect(mockProgressService.updateProgress).toHaveBeenCalledWith('progress-1', { totalCount: 1 }, expect.anything())
311
+ expect(mockSetReindexLockTotal).toHaveBeenCalledWith(mockDb, 'tenant-123', 'fulltext', 1, 'org-456')
312
+ expect(mockFailReindexProgress).not.toHaveBeenCalled()
313
+ expect(mockCompleteReindexProgress).not.toHaveBeenCalled()
314
+ expect(mockClearReindexLock).not.toHaveBeenCalled()
315
+ expect(mockRecordIndexerError).toHaveBeenCalledWith(
316
+ { em: mockEm },
317
+ expect.objectContaining({ entityType: 'test:product' }),
318
+ )
319
+
320
+ const retry = await fulltextReindexPost(makeRequest({ action: 'reindex' }))
321
+
322
+ expect(retry.status).toBe(409)
323
+ expect(mockAcquireReindexLock).toHaveBeenCalledTimes(1)
324
+ expect(enqueue).toHaveBeenCalledTimes(1)
325
+ expect(mockStartReindexProgressJob).toHaveBeenCalledTimes(1)
326
+ })
327
+
328
+ test('starts progress before enqueueing and settles a queued reindex whose batches drained before the request finished', async () => {
329
+ const mockDb = { db: true }
330
+ const mockEm = { getKysely: jest.fn(() => mockDb) }
331
+ mockSetReindexLockTotal.mockResolvedValue({ processedCount: 2, totalCount: 2 })
332
+ const driver: FullTextSearchDriver = {
333
+ id: 'meilisearch',
334
+ ensureReady: jest.fn(async () => {}),
335
+ isHealthy: jest.fn(async () => true),
336
+ search: jest.fn(async () => []),
337
+ index: jest.fn(async () => {}),
338
+ delete: jest.fn(async () => {}),
339
+ bulkIndex: jest.fn(async () => {}),
340
+ purge: jest.fn(async () => {}),
341
+ recreateIndex: jest.fn(async () => {}),
342
+ }
343
+ const strategy = new FullTextSearchStrategy(driver)
344
+ const queryEngine = {
345
+ query: jest.fn(async () => ({
346
+ items: [{ id: 'p-1', organization_id: 'org-456' }, { id: 'p-2', organization_id: 'org-456' }],
347
+ total: 2,
348
+ })),
349
+ } as unknown as QueryEngine
350
+ const enqueue = jest.fn(async () => 'job-1')
351
+ const searchIndexer = new SearchIndexer(
352
+ new SearchService({ strategies: [strategy] }),
353
+ [{ entities: [{ entityId: 'test:product', enabled: true }] }],
354
+ { queryEngine, fulltextQueue: { enqueue } as unknown as SearchIndexerOptions['fulltextQueue'] },
355
+ )
356
+ const mockContainer = {
357
+ resolve: jest.fn((name: string) => {
358
+ if (name === 'em') return mockEm
359
+ if (name === 'progressService') return mockProgressService
360
+ if (name === 'searchStrategies') return [strategy]
361
+ if (name === 'searchIndexer') return searchIndexer
362
+ throw new Error(`Unknown service: ${name}`)
363
+ }),
364
+ dispose: jest.fn().mockResolvedValue(undefined),
365
+ }
366
+ mockCreateRequestContainer.mockResolvedValue(mockContainer)
367
+
368
+ const res = await fulltextReindexPost(makeRequest({ action: 'reindex' }))
369
+ const body = await res.json()
370
+
371
+ expect(body.ok).toBe(true)
372
+ expect(enqueue).toHaveBeenCalledTimes(1)
373
+ expect(mockStartReindexProgressJob.mock.invocationCallOrder[0]).toBeLessThan(enqueue.mock.invocationCallOrder[0])
374
+ expect(mockSetReindexLockTotal).toHaveBeenCalledWith(mockDb, 'tenant-123', 'fulltext', 2, 'org-456')
375
+ expect(mockCompleteReindexProgress).toHaveBeenCalledWith(expect.objectContaining({
376
+ type: 'fulltext',
377
+ tenantId: 'tenant-123',
378
+ organizationId: 'org-456',
379
+ resultSummary: expect.objectContaining({ recordsIndexed: 2, jobsEnqueued: 1 }),
380
+ }))
381
+ expect(mockFailReindexProgress).not.toHaveBeenCalled()
382
+ expect(mockClearReindexLock).toHaveBeenCalledWith(mockDb, 'tenant-123', 'fulltext', 'org-456')
383
+ })
384
+
169
385
  test('cancel removes only current tenant and organization fulltext queue jobs', async () => {
170
386
  const mockDb = { db: true }
171
387
  const mockEm = { getKysely: jest.fn(() => mockDb) }
@@ -154,6 +154,7 @@ export const reindexResultSchema = z.object({
154
154
 
155
155
  export const reindexResponseSchema = z.object({
156
156
  ok: z.boolean(),
157
+ error: z.string().optional().describe('Localized message present when ok is false'),
157
158
  action: z.enum(['clear', 'recreate', 'reindex']),
158
159
  entityId: z.string().nullable(),
159
160
  useQueue: z.boolean().optional(),
@@ -15,11 +15,13 @@ import {
15
15
  acquireReindexLock,
16
16
  clearReindexLock,
17
17
  getReindexLockStatus,
18
+ isReindexLockDrained,
19
+ setReindexLockTotal,
18
20
  } from '../../lib/reindex-lock'
19
21
  import {
20
22
  completeReindexProgress,
21
- ensureReindexProgressJob,
22
23
  failReindexProgress,
24
+ startReindexProgressJob,
23
25
  } from '../../lib/reindex-progress'
24
26
  import { tryResolve } from '../../lib/tryResolve'
25
27
  import { reindexOpenApi } from '../openapi'
@@ -168,6 +170,21 @@ export async function POST(req: Request) {
168
170
 
169
171
  let result
170
172
  const orgId = typeof auth.orgId === 'string' ? auth.orgId : null
173
+ const progressScope = {
174
+ tenantId,
175
+ organizationId: auth.orgId ?? null,
176
+ userId: auth.sub ?? null,
177
+ }
178
+ const progressJobId = await startReindexProgressJob({
179
+ em,
180
+ progressService,
181
+ type: 'fulltext',
182
+ ...progressScope,
183
+ description: entityId
184
+ ? `Reindex ${entityId} (${useQueue ? 'queued' : 'sync'})`
185
+ : `Reindex all entities (${useQueue ? 'queued' : 'sync'})`,
186
+ supersededMessage: t('search.api.errors.reindexSuperseded', 'Superseded by a newer reindex.'),
187
+ })
171
188
 
172
189
  // Debug: List enabled entities
173
190
  const enabledEntities = searchIndexer.listEnabledEntities()
@@ -310,20 +327,25 @@ export async function POST(req: Request) {
310
327
  }
311
328
  }
312
329
 
313
- await ensureReindexProgressJob({
314
- em,
315
- progressService,
316
- type: 'fulltext',
317
- tenantId,
318
- organizationId: auth.orgId ?? null,
319
- userId: auth.sub ?? null,
320
- totalCount: result.recordsIndexed,
321
- description: entityId
322
- ? `Reindex ${entityId} (${useQueue ? 'queued' : 'sync'})`
323
- : `Reindex all entities (${useQueue ? 'queued' : 'sync'})`,
324
- })
330
+ if (progressService && progressJobId) {
331
+ await progressService.updateProgress(progressJobId, { totalCount: result.recordsIndexed }, progressScope)
332
+ }
325
333
  const jobsEnqueued = result.jobsEnqueued ?? 0
326
- if (!useQueue || jobsEnqueued === 0) {
334
+ const lockCounts = useQueue && jobsEnqueued > 0
335
+ ? await setReindexLockTotal(db, tenantId, 'fulltext', result.recordsIndexed, auth.orgId ?? null)
336
+ : null
337
+ if (useQueue && jobsEnqueued > 0 && !isReindexLockDrained(lockCounts)) {
338
+ keepLockForQueuedWorkers = true
339
+ } else if (!result.success) {
340
+ await failReindexProgress({
341
+ em,
342
+ progressService,
343
+ type: 'fulltext',
344
+ tenantId,
345
+ organizationId: auth.orgId ?? null,
346
+ errorMessage: result.errors[0]?.error ?? t('search.api.errors.reindexFailed', 'Reindex operation failed.'),
347
+ })
348
+ } else {
327
349
  await completeReindexProgress({
328
350
  em,
329
351
  progressService,
@@ -337,8 +359,6 @@ export async function POST(req: Request) {
337
359
  errors: result.errors.length,
338
360
  },
339
361
  })
340
- } else {
341
- keepLockForQueuedWorkers = true
342
362
  }
343
363
 
344
364
  // Get updated stats from all strategies
@@ -346,6 +366,9 @@ export async function POST(req: Request) {
346
366
 
347
367
  return toJson({
348
368
  ok: result.success,
369
+ ...(result.success
370
+ ? {}
371
+ : { error: t('search.api.errors.reindexIncomplete', 'Reindex did not complete. Check the search activity log for details.') }),
349
372
  action,
350
373
  entityId: entityId ?? null,
351
374
  useQueue,
@@ -1,3 +1,4 @@
1
+ import { parseModuleCliOptions } from '@open-mercato/shared/lib/cli/parseModuleCliOptions'
1
2
  import type { ModuleCli } from '@open-mercato/shared/modules/registry'
2
3
  import { createRequestContainer } from '@open-mercato/shared/lib/di/container'
3
4
  import { getRedisUrlOrThrow } from '@open-mercato/shared/lib/redis/connection'
@@ -67,22 +68,7 @@ function createGroupedProgress(label: string, partitionTargets: number[]) {
67
68
  }
68
69
 
69
70
  function parseArgs(rest: string[]): ParsedArgs {
70
- const args: ParsedArgs = {}
71
- for (let i = 0; i < rest.length; i += 1) {
72
- const part = rest[i]
73
- if (!part?.startsWith('--')) continue
74
- const [rawKey, rawValue] = part.slice(2).split('=')
75
- if (!rawKey) continue
76
- if (rawValue !== undefined) {
77
- args[rawKey] = rawValue
78
- } else if (i + 1 < rest.length && !rest[i + 1]!.startsWith('--')) {
79
- args[rawKey] = rest[i + 1]!
80
- i += 1
81
- } else {
82
- args[rawKey] = true
83
- }
84
- }
85
- return args
71
+ return parseModuleCliOptions(rest, { skipEmptyKeys: true, consumeEmptyValues: true })
86
72
  }
87
73
 
88
74
  function stringOpt(args: ParsedArgs, ...keys: string[]): string | undefined {
@@ -188,7 +188,7 @@ export function FulltextSearchSection({
188
188
 
189
189
  const body = await response.json() as ReindexResponse
190
190
 
191
- if (!response.ok || body.error) {
191
+ if (!response.ok || body.error || body.ok === false) {
192
192
  throw new Error(body.error || t('search.settings.reindexErrorLabel', 'Failed to reindex'))
193
193
  }
194
194
 
@@ -1,12 +1,121 @@
1
1
  /** @jest-environment jsdom */
2
2
 
3
3
  import * as React from 'react'
4
- import { render, screen } from '@testing-library/react'
4
+ import { fireEvent, render, screen, waitFor } from '@testing-library/react'
5
5
  import { I18nProvider } from '@open-mercato/shared/lib/i18n/context'
6
- import { FulltextSearchSection } from '../FulltextSearchSection'
6
+ import {
7
+ FulltextSearchSection,
8
+ type FulltextSearchSectionProps,
9
+ } from '../FulltextSearchSection'
7
10
  import polishTranslations from '../../../../i18n/pl.json'
8
11
 
9
- jest.mock('@open-mercato/ui/backend/injection/useAppEvent', () => ({ useAppEvent: jest.fn() }))
12
+ jest.mock('@open-mercato/ui/backend/injection/useAppEvent', () => ({
13
+ useAppEvent: jest.fn(),
14
+ }))
15
+
16
+ const flashMock = jest.fn()
17
+
18
+ jest.mock('@open-mercato/ui/backend/FlashMessages', () => ({
19
+ flash: (...args: unknown[]) => flashMock(...args),
20
+ }))
21
+
22
+ const fulltextConfig: NonNullable<FulltextSearchSectionProps['fulltextConfig']> = {
23
+ driver: 'meilisearch',
24
+ configured: true,
25
+ envVars: {
26
+ MEILISEARCH_HOST: { set: true, hint: '' },
27
+ MEILISEARCH_API_KEY: { set: true, hint: '' },
28
+ },
29
+ optionalEnvVars: {
30
+ MEILISEARCH_INDEX_PREFIX: { set: false, hint: '' },
31
+ SEARCH_EXCLUDE_ENCRYPTED_FIELDS: { set: false, hint: '' },
32
+ },
33
+ }
34
+
35
+ function jsonResponse(body: unknown, status = 200) {
36
+ return Promise.resolve({
37
+ ok: status >= 200 && status < 300,
38
+ status,
39
+ json: async () => body,
40
+ } as Response)
41
+ }
42
+
43
+ function mockFetch(reindexBody: unknown) {
44
+ const fetchMock = jest.fn((url: string) => {
45
+ if (url === '/api/search/reindex') return jsonResponse(reindexBody)
46
+ return jsonResponse({ logs: [], errors: [] })
47
+ })
48
+ Object.defineProperty(globalThis, 'fetch', { configurable: true, value: fetchMock })
49
+ return fetchMock
50
+ }
51
+
52
+ async function runFullReindex() {
53
+ const onRefresh = jest.fn().mockResolvedValue(undefined)
54
+ render(
55
+ <I18nProvider locale="en" dict={{}}>
56
+ <FulltextSearchSection
57
+ fulltextConfig={fulltextConfig}
58
+ fulltextConfigLoading={false}
59
+ fulltextStats={null}
60
+ fulltextReindexLock={null}
61
+ loading={false}
62
+ onStatsUpdate={jest.fn()}
63
+ onRefresh={onRefresh}
64
+ />
65
+ </I18nProvider>,
66
+ )
67
+ fireEvent.click(screen.getByRole('tab', { name: 'Index Management' }))
68
+ fireEvent.click(screen.getByRole('button', { name: 'Full Reindex' }))
69
+ const confirmButtons = screen.getAllByRole('button', { name: 'Full Reindex' })
70
+ fireEvent.click(confirmButtons[confirmButtons.length - 1])
71
+ await waitFor(() => expect(flashMock).toHaveBeenCalled())
72
+ return { onRefresh }
73
+ }
74
+
75
+ describe('FulltextSearchSection full reindex result', () => {
76
+ beforeEach(() => {
77
+ flashMock.mockReset()
78
+ })
79
+
80
+ it('flashes the localized route error instead of success when the reindex is not ok', async () => {
81
+ mockFetch({
82
+ ok: false,
83
+ error: 'Reindex did not complete. Check the search activity log for details.',
84
+ action: 'reindex',
85
+ entityId: null,
86
+ useQueue: true,
87
+ result: {
88
+ entitiesProcessed: 0,
89
+ recordsIndexed: 0,
90
+ jobsEnqueued: 0,
91
+ errors: [{ entityId: 'all', error: 'Fulltext driver "algolia" cannot clear documents per entity (implement purge or listDocumentIds)' }],
92
+ },
93
+ })
94
+
95
+ const { onRefresh } = await runFullReindex()
96
+
97
+ expect(flashMock).toHaveBeenCalledTimes(1)
98
+ expect(flashMock).toHaveBeenCalledWith('Reindex did not complete. Check the search activity log for details.', 'error')
99
+ expect(onRefresh).not.toHaveBeenCalled()
100
+ })
101
+
102
+ it('falls back to the localized reindex error when ok is false without a message', async () => {
103
+ mockFetch({ ok: false, action: 'reindex', entityId: null, result: { entitiesProcessed: 0, recordsIndexed: 0 } })
104
+
105
+ await runFullReindex()
106
+
107
+ expect(flashMock).toHaveBeenCalledWith('Failed to reindex', 'error')
108
+ })
109
+
110
+ it('flashes success when the reindex is ok', async () => {
111
+ mockFetch({ ok: true, action: 'reindex', entityId: null, result: { entitiesProcessed: 1, recordsIndexed: 3 } })
112
+
113
+ const { onRefresh } = await runFullReindex()
114
+
115
+ expect(flashMock).toHaveBeenCalledWith('Operation completed successfully: 3 documents indexed', 'success')
116
+ expect(onRefresh).toHaveBeenCalled()
117
+ })
118
+ })
10
119
 
11
120
  it('renders Polish environment help even when the API returns English hints', () => {
12
121
  Object.defineProperty(globalThis, 'fetch', {
@@ -18,6 +18,8 @@
18
18
  "search.api.errors.recreateFailed": "Fehler beim Neuerstellen der Vektortabelle mit neuer Dimension.",
19
19
  "search.api.errors.reindexFailed": "Neuindizierung fehlgeschlagen.",
20
20
  "search.api.errors.reindexInProgress": "Eine Neuindizierung wird bereits durchgeführt.",
21
+ "search.api.errors.reindexIncomplete": "Die Neuindizierung wurde nicht abgeschlossen. Details finden Sie im Aktivitätsprotokoll der Suche.",
22
+ "search.api.errors.reindexSuperseded": "Durch eine neuere Neuindizierung ersetzt.",
21
23
  "search.api.errors.scopedQueueCancelFailed": "Die eingereihten Reindexierungsaufträge konnten nicht abgebrochen werden.",
22
24
  "search.api.errors.scopedQueueCancelUnavailable": "Die bereichsbezogene Warteschlangenstornierung ist nicht verfügbar.",
23
25
  "search.api.errors.searchFailed": "Suchvorgang fehlgeschlagen.",
@@ -18,6 +18,8 @@
18
18
  "search.api.errors.recreateFailed": "Failed to recreate vector table with new dimension.",
19
19
  "search.api.errors.reindexFailed": "Reindex operation failed.",
20
20
  "search.api.errors.reindexInProgress": "A reindex is already in progress.",
21
+ "search.api.errors.reindexIncomplete": "Reindex did not complete. Check the search activity log for details.",
22
+ "search.api.errors.reindexSuperseded": "Superseded by a newer reindex.",
21
23
  "search.api.errors.scopedQueueCancelFailed": "Failed to cancel queued reindex jobs.",
22
24
  "search.api.errors.scopedQueueCancelUnavailable": "Scoped queue cancellation is not available.",
23
25
  "search.api.errors.searchFailed": "Search operation failed.",
@@ -18,6 +18,8 @@
18
18
  "search.api.errors.recreateFailed": "No se pudo recrear la tabla vectorial con la nueva dimensión.",
19
19
  "search.api.errors.reindexFailed": "La operación de reindexación falló.",
20
20
  "search.api.errors.reindexInProgress": "Ya hay una reindexación en progreso.",
21
+ "search.api.errors.reindexIncomplete": "La reindexación no se completó. Consulte el registro de actividad de búsqueda para más detalles.",
22
+ "search.api.errors.reindexSuperseded": "Reemplazada por una reindexación más reciente.",
21
23
  "search.api.errors.scopedQueueCancelFailed": "No se pudieron cancelar los trabajos de reindexación en cola.",
22
24
  "search.api.errors.scopedQueueCancelUnavailable": "La cancelación de cola con alcance no está disponible.",
23
25
  "search.api.errors.searchFailed": "La operación de búsqueda falló.",
@@ -18,6 +18,8 @@
18
18
  "search.api.errors.recreateFailed": "새 차원으로 벡터 테이블을 재생성하지 못했습니다.",
19
19
  "search.api.errors.reindexFailed": "재인덱싱 작업에 실패했습니다.",
20
20
  "search.api.errors.reindexInProgress": "이미 재인덱싱이 진행 중입니다.",
21
+ "search.api.errors.reindexIncomplete": "재인덱싱이 완료되지 않았습니다. 자세한 내용은 검색 활동 로그를 확인하세요.",
22
+ "search.api.errors.reindexSuperseded": "더 최근의 재인덱싱으로 대체되었습니다.",
21
23
  "search.api.errors.scopedQueueCancelFailed": "대기 중인 재인덱싱 작업 취소에 실패했습니다.",
22
24
  "search.api.errors.scopedQueueCancelUnavailable": "범위 지정 대기열 취소를 사용할 수 없습니다.",
23
25
  "search.api.errors.searchFailed": "검색 작업에 실패했습니다.",
@@ -18,6 +18,8 @@
18
18
  "search.api.errors.recreateFailed": "Nie udało się odtworzyć tabeli wektorowej z nowym wymiarem.",
19
19
  "search.api.errors.reindexFailed": "Operacja reindeksacji nie powiodła się.",
20
20
  "search.api.errors.reindexInProgress": "Reindeksacja jest już w toku.",
21
+ "search.api.errors.reindexIncomplete": "Reindeksacja nie została ukończona. Szczegóły znajdziesz w dzienniku aktywności wyszukiwania.",
22
+ "search.api.errors.reindexSuperseded": "Zastąpiona przez nowszą reindeksację.",
21
23
  "search.api.errors.scopedQueueCancelFailed": "Nie udało się anulować zakolejkowanych zadań reindeksacji.",
22
24
  "search.api.errors.scopedQueueCancelUnavailable": "Zakresowe anulowanie kolejki jest niedostępne.",
23
25
  "search.api.errors.searchFailed": "Operacja wyszukiwania nie powiodła się.",