@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.
- package/.turbo/turbo-build.log +1 -1
- package/dist/indexer/search-indexer.js +21 -4
- package/dist/indexer/search-indexer.js.map +2 -2
- package/dist/modules/search/__integration__/TC-SEARCH-016.spec.js +187 -0
- package/dist/modules/search/__integration__/TC-SEARCH-016.spec.js.map +7 -0
- package/dist/modules/search/__integration__/TC-SEARCH-017.spec.js +94 -0
- package/dist/modules/search/__integration__/TC-SEARCH-017.spec.js.map +7 -0
- package/dist/modules/search/__integration__/TC-SEARCH-018.spec.js +74 -0
- package/dist/modules/search/__integration__/TC-SEARCH-018.spec.js.map +7 -0
- package/dist/modules/search/ai-tools.js +3 -1
- package/dist/modules/search/ai-tools.js.map +2 -2
- package/dist/modules/search/api/openapi.js +1 -0
- package/dist/modules/search/api/openapi.js.map +2 -2
- package/dist/modules/search/api/reindex/route.js +35 -16
- package/dist/modules/search/api/reindex/route.js.map +2 -2
- package/dist/modules/search/cli.js +2 -16
- package/dist/modules/search/cli.js.map +2 -2
- package/dist/modules/search/frontend/components/sections/FulltextSearchSection.js +1 -1
- package/dist/modules/search/frontend/components/sections/FulltextSearchSection.js.map +2 -2
- package/dist/modules/search/i18n/de.json +2 -0
- package/dist/modules/search/i18n/en.json +2 -0
- package/dist/modules/search/i18n/es.json +2 -0
- package/dist/modules/search/i18n/ko.json +2 -0
- package/dist/modules/search/i18n/pl.json +2 -0
- package/dist/modules/search/lib/reindex-lock.js +29 -6
- package/dist/modules/search/lib/reindex-lock.js.map +2 -2
- package/dist/modules/search/lib/reindex-progress.js +28 -3
- package/dist/modules/search/lib/reindex-progress.js.map +2 -2
- package/dist/modules/search/workers/fulltext-index.worker.js +19 -6
- package/dist/modules/search/workers/fulltext-index.worker.js.map +2 -2
- package/dist/strategies/fulltext.strategy.js +33 -2
- package/dist/strategies/fulltext.strategy.js.map +2 -2
- package/package.json +4 -4
- package/src/__tests__/reindex-strategy-policy.test.ts +2 -0
- package/src/__tests__/search-indexer-fulltext-reindex-scope.test.ts +435 -0
- package/src/__tests__/workers.test.ts +46 -1
- package/src/indexer/search-indexer.ts +31 -7
- package/src/modules/search/__integration__/TC-SEARCH-016.spec.ts +247 -0
- package/src/modules/search/__integration__/TC-SEARCH-017.spec.ts +141 -0
- package/src/modules/search/__integration__/TC-SEARCH-018.spec.ts +114 -0
- package/src/modules/search/ai-tools.ts +3 -1
- package/src/modules/search/api/__tests__/reindex.routes.test.ts +222 -6
- package/src/modules/search/api/openapi.ts +1 -0
- package/src/modules/search/api/reindex/route.ts +39 -16
- package/src/modules/search/cli.ts +2 -16
- package/src/modules/search/frontend/components/sections/FulltextSearchSection.tsx +1 -1
- package/src/modules/search/frontend/components/sections/__tests__/FulltextSearchSection.test.tsx +112 -3
- package/src/modules/search/i18n/de.json +2 -0
- package/src/modules/search/i18n/en.json +2 -0
- package/src/modules/search/i18n/es.json +2 -0
- package/src/modules/search/i18n/ko.json +2 -0
- package/src/modules/search/i18n/pl.json +2 -0
- package/src/modules/search/lib/__tests__/reindex-progress.test.ts +157 -0
- package/src/modules/search/lib/reindex-lock.ts +66 -8
- package/src/modules/search/lib/reindex-progress.ts +55 -2
- package/src/modules/search/workers/fulltext-index.worker.ts +21 -6
- package/src/strategies/fulltext.strategy.ts +39 -2
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { expect, test, type APIRequestContext, type APIResponse } from '@playwright/test'
|
|
2
|
+
import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
|
|
3
|
+
import { createOrganizationFixture } from '@open-mercato/core/helpers/integration/authFixtures'
|
|
4
|
+
import { withClient } from '@open-mercato/core/helpers/integration/dbFixtures'
|
|
5
|
+
import {
|
|
6
|
+
deleteGeneralEntityIfExists,
|
|
7
|
+
expectId,
|
|
8
|
+
getTokenContext,
|
|
9
|
+
readJsonSafe,
|
|
10
|
+
} from '@open-mercato/core/helpers/integration/generalFixtures'
|
|
11
|
+
import { drainIntegrationQueue } from '@open-mercato/core/helpers/integration/queue'
|
|
12
|
+
|
|
13
|
+
type Scope = { tenantId: string; organizationId: string }
|
|
14
|
+
|
|
15
|
+
type SearchSettingsResponse = {
|
|
16
|
+
settings?: {
|
|
17
|
+
strategies?: Array<{ id?: unknown; available?: unknown }>
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type SearchResponse = {
|
|
22
|
+
results?: Array<{ entityId?: unknown; presenter?: { title?: unknown } | null }>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type ReindexResponse = { ok?: unknown; error?: unknown }
|
|
26
|
+
|
|
27
|
+
const COMPANY_ENTITY = 'customers:customer_company_profile'
|
|
28
|
+
const FULLTEXT_QUEUE = 'fulltext-indexing'
|
|
29
|
+
|
|
30
|
+
function appBaseUrl(): string {
|
|
31
|
+
const baseUrl = new URL(process.env.BASE_URL || 'http://localhost:3000')
|
|
32
|
+
if (baseUrl.hostname === '127.0.0.1') baseUrl.hostname = 'localhost'
|
|
33
|
+
return baseUrl.origin
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function scopedRequest(
|
|
37
|
+
request: APIRequestContext,
|
|
38
|
+
method: string,
|
|
39
|
+
path: string,
|
|
40
|
+
options: { token: string; scope: Scope; data?: unknown },
|
|
41
|
+
): Promise<APIResponse> {
|
|
42
|
+
return request.fetch(`${appBaseUrl()}${path}`, {
|
|
43
|
+
method,
|
|
44
|
+
headers: {
|
|
45
|
+
Authorization: `Bearer ${options.token}`,
|
|
46
|
+
Cookie: [
|
|
47
|
+
`om_selected_tenant=${encodeURIComponent(options.scope.tenantId)}`,
|
|
48
|
+
`om_selected_org=${encodeURIComponent(options.scope.organizationId)}`,
|
|
49
|
+
].join('; '),
|
|
50
|
+
'Content-Type': 'application/json',
|
|
51
|
+
Origin: appBaseUrl(),
|
|
52
|
+
},
|
|
53
|
+
data: options.data,
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function countFulltextHits(
|
|
58
|
+
request: APIRequestContext,
|
|
59
|
+
token: string,
|
|
60
|
+
scope: Scope,
|
|
61
|
+
name: string,
|
|
62
|
+
): Promise<number> {
|
|
63
|
+
const params = new URLSearchParams({ q: name, limit: '10', strategies: 'fulltext', entityTypes: COMPANY_ENTITY })
|
|
64
|
+
const response = await scopedRequest(request, 'GET', `/api/search/search?${params.toString()}`, { token, scope })
|
|
65
|
+
if (!response.ok()) return -1
|
|
66
|
+
const body = ((await readJsonSafe<SearchResponse>(response)) ?? {}) as SearchResponse
|
|
67
|
+
const results = Array.isArray(body.results) ? body.results : []
|
|
68
|
+
return results.filter((result) => result.entityId === COMPANY_ENTITY && result.presenter?.title === name).length
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function createCompany(request: APIRequestContext, token: string, scope: Scope, name: string): Promise<string> {
|
|
72
|
+
const response = await scopedRequest(request, 'POST', '/api/customers/companies', {
|
|
73
|
+
token,
|
|
74
|
+
scope,
|
|
75
|
+
data: { displayName: name },
|
|
76
|
+
})
|
|
77
|
+
expect(response.status(), `company fixture ${name} should be created`).toBe(201)
|
|
78
|
+
const body = await readJsonSafe<{ id?: string; entityId?: string }>(response)
|
|
79
|
+
return expectId(body?.id ?? body?.entityId, 'company create response should contain an id')
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function reindex(
|
|
83
|
+
request: APIRequestContext,
|
|
84
|
+
token: string,
|
|
85
|
+
scope: Scope,
|
|
86
|
+
data: { entityId?: string; useQueue: boolean },
|
|
87
|
+
): Promise<void> {
|
|
88
|
+
await scopedRequest(request, 'POST', '/api/search/reindex/cancel', { token, scope }).catch(() => undefined)
|
|
89
|
+
const response = await scopedRequest(request, 'POST', '/api/search/reindex', { token, scope, data })
|
|
90
|
+
const body = await readJsonSafe<ReindexResponse>(response)
|
|
91
|
+
expect(response.status(), `reindex ${JSON.stringify(data)} should succeed: ${JSON.stringify(body)}`).toBe(200)
|
|
92
|
+
expect(body?.ok, `reindex ${JSON.stringify(data)} should report ok`).toBe(true)
|
|
93
|
+
if (data.useQueue) await drainIntegrationQueue(FULLTEXT_QUEUE)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Removes a company from the database without emitting any event, so its fulltext document
|
|
98
|
+
* stays in the index until a reindex clears it — the "stale document" a reindex must remove.
|
|
99
|
+
*/
|
|
100
|
+
async function removeCompanyBehindTheIndex(companyEntityId: string): Promise<void> {
|
|
101
|
+
await withClient(async (client) => {
|
|
102
|
+
const removed = await client.query<{ id: string }>(
|
|
103
|
+
'delete from customer_companies where entity_id = $1 returning id',
|
|
104
|
+
[companyEntityId],
|
|
105
|
+
)
|
|
106
|
+
expect(removed.rowCount, 'stale company profile should be removed').toBe(1)
|
|
107
|
+
const recordIds = [companyEntityId, ...removed.rows.map((row) => row.id)]
|
|
108
|
+
await client.query('delete from entity_indexes where entity_id = any($1::text[])', [recordIds])
|
|
109
|
+
await client.query('update customer_entities set deleted_at = now() where id = $1', [companyEntityId])
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* TC-SEARCH-016: organization-scoped fulltext reindex clears and rebuilds only that organization.
|
|
115
|
+
*
|
|
116
|
+
* Meilisearch keeps one index per tenant, shared by every organization. An organization-scoped
|
|
117
|
+
* reindex — of one entity type or of every entity type, applied directly or through the
|
|
118
|
+
* fulltext queue — must drop that organization's stale documents and rebuild its live ones,
|
|
119
|
+
* while another organization of the same tenant and another tenant keep their documents.
|
|
120
|
+
* Skips when the environment has no fulltext (Meilisearch) backend.
|
|
121
|
+
*/
|
|
122
|
+
test.describe('TC-SEARCH-016: organization-scoped fulltext reindex keeps other organizations and tenants searchable', () => {
|
|
123
|
+
test('entity and full reindex, direct and queued, only touch the reindexed organization', async ({ request }) => {
|
|
124
|
+
test.slow()
|
|
125
|
+
const token = await getAuthToken(request, 'superadmin')
|
|
126
|
+
const homeTenantId = getTokenContext(token).tenantId
|
|
127
|
+
const stamp = Date.now()
|
|
128
|
+
const companyIds: Array<{ id: string; scope: Scope }> = []
|
|
129
|
+
let organizationAId: string | null = null
|
|
130
|
+
let organizationBId: string | null = null
|
|
131
|
+
let otherTenantId: string | null = null
|
|
132
|
+
let otherOrganizationId: string | null = null
|
|
133
|
+
|
|
134
|
+
try {
|
|
135
|
+
organizationAId = await createOrganizationFixture(request, token, {
|
|
136
|
+
name: `QA TC-SEARCH-016 A ${stamp}`,
|
|
137
|
+
tenantId: homeTenantId,
|
|
138
|
+
})
|
|
139
|
+
organizationBId = await createOrganizationFixture(request, token, {
|
|
140
|
+
name: `QA TC-SEARCH-016 B ${stamp}`,
|
|
141
|
+
tenantId: homeTenantId,
|
|
142
|
+
})
|
|
143
|
+
const scopeA: Scope = { tenantId: homeTenantId, organizationId: organizationAId }
|
|
144
|
+
const scopeB: Scope = { tenantId: homeTenantId, organizationId: organizationBId }
|
|
145
|
+
|
|
146
|
+
const settingsResponse = await scopedRequest(request, 'GET', '/api/search/settings', { token, scope: scopeA })
|
|
147
|
+
expect(settingsResponse.ok()).toBeTruthy()
|
|
148
|
+
const settings = ((await readJsonSafe<SearchSettingsResponse>(settingsResponse)) ?? {}) as SearchSettingsResponse
|
|
149
|
+
const strategies = Array.isArray(settings.settings?.strategies) ? settings.settings.strategies : []
|
|
150
|
+
if (!strategies.some((strategy) => strategy.id === 'fulltext' && strategy.available === true)) {
|
|
151
|
+
test.skip(true, 'Fulltext strategy is not available in this environment')
|
|
152
|
+
return
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const tenantResponse = await apiRequest(request, 'POST', '/api/directory/tenants', {
|
|
156
|
+
token,
|
|
157
|
+
data: { name: `QA TC-SEARCH-016 Tenant ${stamp}` },
|
|
158
|
+
})
|
|
159
|
+
expect(tenantResponse.status(), 'tenant fixture should be created').toBe(201)
|
|
160
|
+
otherTenantId = expectId((await readJsonSafe<{ id?: string }>(tenantResponse))?.id, 'tenant id')
|
|
161
|
+
otherOrganizationId = await createOrganizationFixture(request, token, {
|
|
162
|
+
name: `QA TC-SEARCH-016 C ${stamp}`,
|
|
163
|
+
tenantId: otherTenantId,
|
|
164
|
+
})
|
|
165
|
+
const scopeOtherTenant: Scope = { tenantId: otherTenantId, organizationId: otherOrganizationId }
|
|
166
|
+
|
|
167
|
+
const names = {
|
|
168
|
+
aLive: `QATCSEARCH016ALIVE${stamp}`,
|
|
169
|
+
b: `QATCSEARCH016B${stamp}`,
|
|
170
|
+
otherTenant: `QATCSEARCH016T${stamp}`,
|
|
171
|
+
}
|
|
172
|
+
const phases = [
|
|
173
|
+
{ label: 'entity reindex, direct', data: { entityId: COMPANY_ENTITY, useQueue: false } },
|
|
174
|
+
{ label: 'entity reindex, queued', data: { entityId: COMPANY_ENTITY, useQueue: true } },
|
|
175
|
+
{ label: 'full reindex, direct', data: { useQueue: false } },
|
|
176
|
+
{ label: 'full reindex, queued', data: { useQueue: true } },
|
|
177
|
+
].map((phase, index) => ({ ...phase, staleName: `QATCSEARCH016S${index}${stamp}`, staleId: '' }))
|
|
178
|
+
|
|
179
|
+
const track = async (scope: Scope, name: string) => {
|
|
180
|
+
const id = await createCompany(request, token, scope, name)
|
|
181
|
+
companyIds.push({ id, scope })
|
|
182
|
+
return id
|
|
183
|
+
}
|
|
184
|
+
await track(scopeA, names.aLive)
|
|
185
|
+
for (const phase of phases) phase.staleId = await track(scopeA, phase.staleName)
|
|
186
|
+
await track(scopeB, names.b)
|
|
187
|
+
await track(scopeOtherTenant, names.otherTenant)
|
|
188
|
+
|
|
189
|
+
for (const scope of [scopeB, scopeOtherTenant, scopeA]) {
|
|
190
|
+
await reindex(request, token, scope, { entityId: COMPANY_ENTITY, useQueue: false })
|
|
191
|
+
}
|
|
192
|
+
const expectSearchable = async (scope: Scope, name: string, message: string) => {
|
|
193
|
+
await expect.poll(() => countFulltextHits(request, token, scope, name), { message, timeout: 30_000 }).toBe(1)
|
|
194
|
+
}
|
|
195
|
+
await expectSearchable(scopeA, names.aLive, 'organization A company should be indexed')
|
|
196
|
+
for (const phase of phases) await expectSearchable(scopeA, phase.staleName, 'organization A fixture should be indexed')
|
|
197
|
+
expect(
|
|
198
|
+
await countFulltextHits(request, token, scopeB, names.b),
|
|
199
|
+
'seeding organization A must keep the organization B documents indexed just before',
|
|
200
|
+
).toBe(1)
|
|
201
|
+
await expectSearchable(scopeOtherTenant, names.otherTenant, 'other tenant company should be indexed')
|
|
202
|
+
|
|
203
|
+
for (const phase of phases) {
|
|
204
|
+
await removeCompanyBehindTheIndex(phase.staleId)
|
|
205
|
+
expect(
|
|
206
|
+
await countFulltextHits(request, token, scopeA, phase.staleName),
|
|
207
|
+
`${phase.label}: the removed company must still be indexed before the reindex (stale document)`,
|
|
208
|
+
).toBe(1)
|
|
209
|
+
|
|
210
|
+
await reindex(request, token, scopeA, phase.data)
|
|
211
|
+
|
|
212
|
+
await expect
|
|
213
|
+
.poll(() => countFulltextHits(request, token, scopeA, phase.staleName), {
|
|
214
|
+
message: `${phase.label}: the stale organization A document must be cleared`,
|
|
215
|
+
timeout: 30_000,
|
|
216
|
+
})
|
|
217
|
+
.toBe(0)
|
|
218
|
+
await expectSearchable(scopeA, names.aLive, `${phase.label}: the live organization A company must be rebuilt`)
|
|
219
|
+
expect(
|
|
220
|
+
await countFulltextHits(request, token, scopeB, names.b),
|
|
221
|
+
`${phase.label}: organization B must keep its documents`,
|
|
222
|
+
).toBe(1)
|
|
223
|
+
expect(
|
|
224
|
+
await countFulltextHits(request, token, scopeOtherTenant, names.otherTenant),
|
|
225
|
+
`${phase.label}: the other tenant must keep its documents`,
|
|
226
|
+
).toBe(1)
|
|
227
|
+
}
|
|
228
|
+
} finally {
|
|
229
|
+
for (const { id, scope } of companyIds) {
|
|
230
|
+
await scopedRequest(request, 'DELETE', `/api/customers/companies?id=${encodeURIComponent(id)}`, {
|
|
231
|
+
token,
|
|
232
|
+
scope,
|
|
233
|
+
}).catch(() => undefined)
|
|
234
|
+
}
|
|
235
|
+
for (const scope of [
|
|
236
|
+
organizationAId ? { tenantId: homeTenantId, organizationId: organizationAId } : null,
|
|
237
|
+
otherTenantId && otherOrganizationId ? { tenantId: otherTenantId, organizationId: otherOrganizationId } : null,
|
|
238
|
+
]) {
|
|
239
|
+
if (scope) await scopedRequest(request, 'POST', '/api/search/reindex/cancel', { token, scope }).catch(() => undefined)
|
|
240
|
+
}
|
|
241
|
+
await deleteGeneralEntityIfExists(request, token, '/api/directory/organizations', organizationAId)
|
|
242
|
+
await deleteGeneralEntityIfExists(request, token, '/api/directory/organizations', organizationBId)
|
|
243
|
+
await deleteGeneralEntityIfExists(request, token, '/api/directory/organizations', otherOrganizationId)
|
|
244
|
+
await deleteGeneralEntityIfExists(request, token, '/api/directory/tenants', otherTenantId)
|
|
245
|
+
}
|
|
246
|
+
})
|
|
247
|
+
})
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { expect, test, type APIRequestContext, type APIResponse } from '@playwright/test'
|
|
2
|
+
import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
|
|
3
|
+
import {
|
|
4
|
+
createCompanyFixture,
|
|
5
|
+
deleteEntityIfExists,
|
|
6
|
+
readJsonSafe,
|
|
7
|
+
} from '@open-mercato/core/helpers/integration/crmFixtures'
|
|
8
|
+
|
|
9
|
+
type SearchSettingsResponse = {
|
|
10
|
+
settings?: {
|
|
11
|
+
strategies?: Array<{ id?: unknown; available?: unknown }>
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
type ReindexResponse = {
|
|
16
|
+
ok?: unknown
|
|
17
|
+
result?: { recordsIndexed?: unknown; jobsEnqueued?: unknown }
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type ProgressJobRow = {
|
|
21
|
+
id: string
|
|
22
|
+
status: string
|
|
23
|
+
processedCount: number
|
|
24
|
+
totalCount: number | null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
type ProgressJobListResponse = {
|
|
28
|
+
items?: ProgressJobRow[]
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const REINDEX_JOB_TYPE = 'search.reindex.fulltext'
|
|
32
|
+
const TERMINAL_STATUSES = ['completed', 'failed', 'cancelled']
|
|
33
|
+
const DRAIN_TIMEOUT_MS = 180_000
|
|
34
|
+
|
|
35
|
+
async function readJson<T>(response: APIResponse): Promise<T> {
|
|
36
|
+
return ((await readJsonSafe<T>(response)) ?? {}) as T
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function listReindexJobs(request: APIRequestContext, token: string): Promise<ProgressJobRow[]> {
|
|
40
|
+
const params = new URLSearchParams({
|
|
41
|
+
jobType: REINDEX_JOB_TYPE,
|
|
42
|
+
status: 'pending,running,completed,failed,cancelled',
|
|
43
|
+
sortField: 'createdAt',
|
|
44
|
+
sortDir: 'desc',
|
|
45
|
+
pageSize: '20',
|
|
46
|
+
})
|
|
47
|
+
const response = await apiRequest(request, 'GET', `/api/progress/jobs?${params.toString()}`, { token })
|
|
48
|
+
if (!response.ok()) return []
|
|
49
|
+
const body = await readJson<ProgressJobListResponse>(response)
|
|
50
|
+
return Array.isArray(body.items) ? body.items : []
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function readReindexJob(request: APIRequestContext, token: string, jobId: string): Promise<ProgressJobRow | null> {
|
|
54
|
+
return (await listReindexJobs(request, token)).find((job) => job.id === jobId) ?? null
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function waitForTerminal(request: APIRequestContext, token: string, jobId: string): Promise<ProgressJobRow | null> {
|
|
58
|
+
await expect
|
|
59
|
+
.poll(async () => (await readReindexJob(request, token, jobId))?.status ?? 'missing', { timeout: DRAIN_TIMEOUT_MS })
|
|
60
|
+
.toMatch(new RegExp(`^(${TERMINAL_STATUSES.join('|')})$`))
|
|
61
|
+
return readReindexJob(request, token, jobId)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* TC-SEARCH-017: a queued full fulltext reindex keeps its lock and progress until every batch drains.
|
|
66
|
+
*
|
|
67
|
+
* Queue workers start processing batches while the request is still enqueueing the rest. The run's
|
|
68
|
+
* progress job must already exist by then, so early batches are counted and do not release the lock.
|
|
69
|
+
* The run must finish as `completed` with every enqueued record counted, and while it is still
|
|
70
|
+
* running a second reindex must be refused with 409 instead of taking the run over.
|
|
71
|
+
* Skips when the environment has no fulltext (Meilisearch) backend.
|
|
72
|
+
*/
|
|
73
|
+
test.describe('TC-SEARCH-017: queued full fulltext reindex keeps ownership until batches drain', () => {
|
|
74
|
+
test('counts every batch, completes the progress job and refuses a concurrent reindex', async ({ request }) => {
|
|
75
|
+
test.slow()
|
|
76
|
+
let token: string | null = null
|
|
77
|
+
let companyId: string | null = null
|
|
78
|
+
const companyName = `QATCSEARCH017${Date.now()}`
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
token = await getAuthToken(request, 'admin')
|
|
82
|
+
const authToken = token
|
|
83
|
+
|
|
84
|
+
const settingsResponse = await apiRequest(request, 'GET', '/api/search/settings', { token: authToken })
|
|
85
|
+
expect(settingsResponse.ok()).toBeTruthy()
|
|
86
|
+
const settings = await readJson<SearchSettingsResponse>(settingsResponse)
|
|
87
|
+
const strategies = Array.isArray(settings.settings?.strategies) ? settings.settings.strategies : []
|
|
88
|
+
if (!strategies.some((strategy) => strategy.id === 'fulltext' && strategy.available === true)) {
|
|
89
|
+
test.skip(true, 'Fulltext strategy is not available in this environment')
|
|
90
|
+
return
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
await apiRequest(request, 'POST', '/api/search/reindex/cancel', { token: authToken }).catch(() => undefined)
|
|
94
|
+
companyId = await createCompanyFixture(request, authToken, companyName)
|
|
95
|
+
const knownJobIds = new Set((await listReindexJobs(request, authToken)).map((job) => job.id))
|
|
96
|
+
|
|
97
|
+
const reindex = await apiRequest(request, 'POST', '/api/search/reindex', {
|
|
98
|
+
token: authToken,
|
|
99
|
+
data: { action: 'reindex', useQueue: true },
|
|
100
|
+
})
|
|
101
|
+
expect(reindex.status(), 'queued full reindex must be accepted').toBe(200)
|
|
102
|
+
const reindexBody = await readJson<ReindexResponse>(reindex)
|
|
103
|
+
expect(reindexBody.ok).toBe(true)
|
|
104
|
+
const recordsIndexed = Number(reindexBody.result?.recordsIndexed ?? 0)
|
|
105
|
+
expect(recordsIndexed, 'the reindex must enqueue at least the fixture company').toBeGreaterThan(0)
|
|
106
|
+
expect(Number(reindexBody.result?.jobsEnqueued ?? 0)).toBeGreaterThan(0)
|
|
107
|
+
|
|
108
|
+
const runJobs = (await listReindexJobs(request, authToken)).filter((job) => !knownJobIds.has(job.id))
|
|
109
|
+
expect(runJobs, 'the run must have exactly one new progress job').toHaveLength(1)
|
|
110
|
+
const runJobId = runJobs[0].id
|
|
111
|
+
|
|
112
|
+
const retry = await apiRequest(request, 'POST', '/api/search/reindex', {
|
|
113
|
+
token: authToken,
|
|
114
|
+
data: { action: 'reindex', useQueue: true },
|
|
115
|
+
})
|
|
116
|
+
let retryJobId: string | null = null
|
|
117
|
+
if (retry.status() !== 409) {
|
|
118
|
+
expect(retry.status()).toBe(200)
|
|
119
|
+
const runJobAtRetry = await readReindexJob(request, authToken, runJobId)
|
|
120
|
+
expect(
|
|
121
|
+
runJobAtRetry?.status,
|
|
122
|
+
'a second reindex may only be accepted after the first run completed',
|
|
123
|
+
).toBe('completed')
|
|
124
|
+
retryJobId = (await listReindexJobs(request, authToken))
|
|
125
|
+
.find((job) => job.id !== runJobId && !knownJobIds.has(job.id))?.id ?? null
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const finished = await waitForTerminal(request, authToken, runJobId)
|
|
129
|
+
expect(finished?.status, 'the queued run must complete instead of staying running').toBe('completed')
|
|
130
|
+
expect(finished?.totalCount).toBe(recordsIndexed)
|
|
131
|
+
expect(finished?.processedCount, 'every enqueued record must be counted').toBe(recordsIndexed)
|
|
132
|
+
|
|
133
|
+
if (retryJobId) await waitForTerminal(request, authToken, retryJobId)
|
|
134
|
+
} finally {
|
|
135
|
+
if (token) {
|
|
136
|
+
await apiRequest(request, 'POST', '/api/search/reindex/cancel', { token }).catch(() => undefined)
|
|
137
|
+
}
|
|
138
|
+
await deleteEntityIfExists(request, token, '/api/customers/companies', companyId)
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
})
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { expect, test, type APIRequestContext, type APIResponse } from '@playwright/test'
|
|
2
|
+
import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
|
|
3
|
+
import {
|
|
4
|
+
createCompanyFixture,
|
|
5
|
+
createPersonFixture,
|
|
6
|
+
deleteEntityIfExists,
|
|
7
|
+
readJsonSafe,
|
|
8
|
+
} from '@open-mercato/core/helpers/integration/crmFixtures'
|
|
9
|
+
|
|
10
|
+
type SearchSettingsResponse = {
|
|
11
|
+
settings?: {
|
|
12
|
+
strategies?: Array<{ id?: unknown; available?: unknown }>
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type SearchResponse = {
|
|
17
|
+
results?: Array<{ entityId?: unknown; recordId?: unknown; presenter?: { title?: unknown } | null }>
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const COMPANY_ENTITY = 'customers:customer_company_profile'
|
|
21
|
+
const PERSON_ENTITY = 'customers:customer_person_profile'
|
|
22
|
+
|
|
23
|
+
async function readJson<T>(response: APIResponse): Promise<T> {
|
|
24
|
+
return ((await readJsonSafe<T>(response)) ?? {}) as T
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
async function countFulltextHits(
|
|
28
|
+
request: APIRequestContext,
|
|
29
|
+
token: string,
|
|
30
|
+
entityId: string,
|
|
31
|
+
name: string,
|
|
32
|
+
): Promise<number> {
|
|
33
|
+
const params = new URLSearchParams({ q: name, limit: '10', strategies: 'fulltext', entityTypes: entityId })
|
|
34
|
+
const response = await apiRequest(request, 'GET', `/api/search/search?${params.toString()}`, { token })
|
|
35
|
+
if (!response.ok()) return -1
|
|
36
|
+
const body = await readJson<SearchResponse>(response)
|
|
37
|
+
const results = Array.isArray(body.results) ? body.results : []
|
|
38
|
+
return results.filter((result) => result.entityId === entityId && result.presenter?.title === name).length
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* TC-SEARCH-018: reindexing one entity type keeps the other entity types searchable.
|
|
43
|
+
*
|
|
44
|
+
* Meilisearch keeps one index per tenant shared by every entity type and organization.
|
|
45
|
+
* POST /api/search/reindex with an `entityId` must clear and rebuild only that entity's
|
|
46
|
+
* documents; it must not drop the tenant index and leave other entity types unsearchable.
|
|
47
|
+
* Meilisearch applies an index's writes in order, so once the person rebuilt by the people
|
|
48
|
+
* reindex is searchable, any deletion that reindex issued has been applied as well.
|
|
49
|
+
* Skips when the environment has no fulltext (Meilisearch) backend.
|
|
50
|
+
*/
|
|
51
|
+
test.describe('TC-SEARCH-018: entity-scoped fulltext reindex keeps other entity types searchable', () => {
|
|
52
|
+
test('reindexing people keeps an indexed company in fulltext results', async ({ request }) => {
|
|
53
|
+
test.slow()
|
|
54
|
+
let token: string | null = null
|
|
55
|
+
let companyId: string | null = null
|
|
56
|
+
let personId: string | null = null
|
|
57
|
+
const stamp = Date.now()
|
|
58
|
+
const companyName = `QATCSEARCH015${stamp}`
|
|
59
|
+
const personName = `QATCSEARCH015P${stamp}`
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
token = await getAuthToken(request, 'admin')
|
|
63
|
+
const authToken = token
|
|
64
|
+
|
|
65
|
+
const settingsResponse = await apiRequest(request, 'GET', '/api/search/settings', { token: authToken })
|
|
66
|
+
expect(settingsResponse.ok()).toBeTruthy()
|
|
67
|
+
const settings = await readJson<SearchSettingsResponse>(settingsResponse)
|
|
68
|
+
const strategies = Array.isArray(settings.settings?.strategies) ? settings.settings.strategies : []
|
|
69
|
+
if (!strategies.some((strategy) => strategy.id === 'fulltext' && strategy.available === true)) {
|
|
70
|
+
test.skip(true, 'Fulltext strategy is not available in this environment')
|
|
71
|
+
return
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
await apiRequest(request, 'POST', '/api/search/reindex/cancel', { token: authToken }).catch(() => undefined)
|
|
75
|
+
|
|
76
|
+
companyId = await createCompanyFixture(request, authToken, companyName)
|
|
77
|
+
personId = await createPersonFixture(request, authToken, {
|
|
78
|
+
firstName: 'QA',
|
|
79
|
+
lastName: personName,
|
|
80
|
+
displayName: personName,
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
const companyReindex = await apiRequest(request, 'POST', '/api/search/reindex', {
|
|
84
|
+
token: authToken,
|
|
85
|
+
data: { entityId: COMPANY_ENTITY, useQueue: false },
|
|
86
|
+
})
|
|
87
|
+
expect(companyReindex.status(), 'company reindex must succeed').toBe(200)
|
|
88
|
+
|
|
89
|
+
await expect
|
|
90
|
+
.poll(() => countFulltextHits(request, authToken, COMPANY_ENTITY, companyName), { timeout: 30_000 })
|
|
91
|
+
.toBe(1)
|
|
92
|
+
|
|
93
|
+
const personReindex = await apiRequest(request, 'POST', '/api/search/reindex', {
|
|
94
|
+
token: authToken,
|
|
95
|
+
data: { entityId: PERSON_ENTITY, useQueue: false },
|
|
96
|
+
})
|
|
97
|
+
expect(personReindex.status(), 'entity-scoped reindex must succeed').toBe(200)
|
|
98
|
+
|
|
99
|
+
await expect
|
|
100
|
+
.poll(() => countFulltextHits(request, authToken, PERSON_ENTITY, personName), { timeout: 30_000 })
|
|
101
|
+
.toBe(1)
|
|
102
|
+
expect(
|
|
103
|
+
await countFulltextHits(request, authToken, COMPANY_ENTITY, companyName),
|
|
104
|
+
`company must stay searchable after reindexing ${PERSON_ENTITY}`,
|
|
105
|
+
).toBe(1)
|
|
106
|
+
} finally {
|
|
107
|
+
if (token) {
|
|
108
|
+
await apiRequest(request, 'POST', '/api/search/reindex/cancel', { token }).catch(() => undefined)
|
|
109
|
+
}
|
|
110
|
+
await deleteEntityIfExists(request, token, '/api/customers/people', personId)
|
|
111
|
+
await deleteEntityIfExists(request, token, '/api/customers/companies', companyId)
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
})
|
|
@@ -585,7 +585,9 @@ const searchReindexTool: AiToolDefinition = {
|
|
|
585
585
|
.boolean()
|
|
586
586
|
.optional()
|
|
587
587
|
.default(false)
|
|
588
|
-
.describe(
|
|
588
|
+
.describe(
|
|
589
|
+
'Fulltext only: whether to clear the reindexed documents first (the given entity type, within the current organization when one is set); reindexing all entities with no organization recreates the whole index (default: false)'
|
|
590
|
+
),
|
|
589
591
|
}),
|
|
590
592
|
requiredFeatures: ['search.reindex'],
|
|
591
593
|
// Reindex changes server-side index state — must surface as a write so
|