@open-mercato/search 0.6.4-develop.4382.1.6b4f656b77 → 0.6.4

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 (37) hide show
  1. package/.turbo/turbo-build.log +1 -1
  2. package/dist/lib/merger.js +5 -2
  3. package/dist/lib/merger.js.map +2 -2
  4. package/dist/modules/search/__integration__/TC-SEARCH-003.spec.js +64 -0
  5. package/dist/modules/search/__integration__/TC-SEARCH-003.spec.js.map +7 -0
  6. package/dist/modules/search/__integration__/TC-SEARCH-004.spec.js +97 -0
  7. package/dist/modules/search/__integration__/TC-SEARCH-004.spec.js.map +7 -0
  8. package/dist/modules/search/__integration__/TC-SEARCH-005.spec.js +28 -0
  9. package/dist/modules/search/__integration__/TC-SEARCH-005.spec.js.map +7 -0
  10. package/dist/modules/search/__integration__/TC-SEARCH-006.spec.js +48 -0
  11. package/dist/modules/search/__integration__/TC-SEARCH-006.spec.js.map +7 -0
  12. package/dist/modules/search/__integration__/TC-SEARCH-007.spec.js +38 -0
  13. package/dist/modules/search/__integration__/TC-SEARCH-007.spec.js.map +7 -0
  14. package/dist/modules/search/__integration__/TC-SEARCH-008.spec.js +77 -0
  15. package/dist/modules/search/__integration__/TC-SEARCH-008.spec.js.map +7 -0
  16. package/dist/modules/search/__integration__/TC-SEARCH-009.spec.js +54 -0
  17. package/dist/modules/search/__integration__/TC-SEARCH-009.spec.js.map +7 -0
  18. package/dist/modules/search/ai-tools.js +61 -2
  19. package/dist/modules/search/ai-tools.js.map +2 -2
  20. package/dist/vector/services/vector-index.service.js +45 -25
  21. package/dist/vector/services/vector-index.service.js.map +2 -2
  22. package/package.json +9 -10
  23. package/src/__tests__/merger.test.ts +50 -0
  24. package/src/__tests__/vector-index-security.test.ts +230 -0
  25. package/src/lib/merger.ts +16 -2
  26. package/src/modules/search/README.md +1 -1
  27. package/src/modules/search/__integration__/TC-SEARCH-003.spec.ts +102 -0
  28. package/src/modules/search/__integration__/TC-SEARCH-004.spec.ts +135 -0
  29. package/src/modules/search/__integration__/TC-SEARCH-005.spec.ts +45 -0
  30. package/src/modules/search/__integration__/TC-SEARCH-006.spec.ts +74 -0
  31. package/src/modules/search/__integration__/TC-SEARCH-007.spec.ts +72 -0
  32. package/src/modules/search/__integration__/TC-SEARCH-008.spec.ts +124 -0
  33. package/src/modules/search/__integration__/TC-SEARCH-009.spec.ts +85 -0
  34. package/src/modules/search/__tests__/ai-tools.aggregate.test.ts +89 -4
  35. package/src/modules/search/__tests__/ai-tools.get.test.ts +118 -0
  36. package/src/modules/search/ai-tools.ts +111 -3
  37. package/src/vector/services/vector-index.service.ts +57 -25
@@ -0,0 +1,230 @@
1
+ import { VectorIndexService } from '../vector/services/vector-index.service'
2
+ import type { EmbeddingService } from '../vector/services/embedding'
3
+ import type { VectorDriver, VectorDriverDocument } from '../vector/types'
4
+ import type { VectorModuleConfig } from '@open-mercato/shared/modules/vector'
5
+ import type { QueryEngine } from '@open-mercato/shared/lib/query/types'
6
+ import type { TenantDataEncryptionService } from '@open-mercato/shared/lib/encryption/tenantDataEncryptionService'
7
+
8
+ type CapturedEmbeddingInput = { value: string | string[] | null }
9
+
10
+ function createEmbeddingService(captured: CapturedEmbeddingInput): EmbeddingService {
11
+ return {
12
+ available: true,
13
+ createEmbedding: async (input: string | string[]) => {
14
+ captured.value = input
15
+ return [0.1, 0.2, 0.3]
16
+ },
17
+ } as unknown as EmbeddingService
18
+ }
19
+
20
+ function createDriver(overrides: Partial<VectorDriver> = {}): VectorDriver & { upsertCalls: VectorDriverDocument[] } {
21
+ const upsertCalls: VectorDriverDocument[] = []
22
+ const driver = {
23
+ id: 'pgvector' as const,
24
+ ensureReady: async () => {},
25
+ upsert: async (doc: VectorDriverDocument) => {
26
+ upsertCalls.push(doc)
27
+ },
28
+ delete: async () => {},
29
+ query: async () => [],
30
+ getChecksum: async () => null,
31
+ list: async () => [],
32
+ upsertCalls,
33
+ ...overrides,
34
+ }
35
+ return driver as VectorDriver & { upsertCalls: VectorDriverDocument[] }
36
+ }
37
+
38
+ function createQueryEngine(record: Record<string, unknown>): QueryEngine {
39
+ return {
40
+ query: async () => ({ items: [record], total: 1 }),
41
+ } as unknown as QueryEngine
42
+ }
43
+
44
+ function createService(args: {
45
+ driver: VectorDriver
46
+ embeddingService: EmbeddingService
47
+ queryEngine: QueryEngine
48
+ moduleConfigs: VectorModuleConfig[]
49
+ encryptionService?: TenantDataEncryptionService | null
50
+ }): VectorIndexService {
51
+ return new VectorIndexService({
52
+ drivers: [args.driver],
53
+ embeddingService: args.embeddingService,
54
+ queryEngine: args.queryEngine,
55
+ moduleConfigs: args.moduleConfigs,
56
+ containerResolver: () => ({
57
+ resolve: (name: string) => {
58
+ if (name === 'tenantEncryptionService') {
59
+ if (!args.encryptionService) throw new Error('not registered')
60
+ return args.encryptionService
61
+ }
62
+ throw new Error(`unexpected resolve: ${name}`)
63
+ },
64
+ }),
65
+ })
66
+ }
67
+
68
+ describe('VectorIndexService default source field protection (issue #2716)', () => {
69
+ it('excludes snake_case scope and timestamp fields from the default embedding source', async () => {
70
+ const captured: CapturedEmbeddingInput = { value: null }
71
+ const record = {
72
+ id: 'rec-1',
73
+ tenant_id: 'tenant-secret',
74
+ organization_id: 'org-secret',
75
+ created_at: '2026-01-01T00:00:00.000Z',
76
+ updated_at: '2026-01-02T00:00:00.000Z',
77
+ deleted_at: null,
78
+ name: 'Visible Name',
79
+ description: 'Visible Description',
80
+ }
81
+ const service = createService({
82
+ driver: createDriver(),
83
+ embeddingService: createEmbeddingService(captured),
84
+ queryEngine: createQueryEngine(record),
85
+ moduleConfigs: [{ entities: [{ entityId: 'demo:item' }] }],
86
+ })
87
+
88
+ await service.indexRecord({ entityId: 'demo:item', recordId: 'rec-1', tenantId: 'tenant-secret' })
89
+
90
+ const lines = Array.isArray(captured.value) ? captured.value.join('\n') : String(captured.value)
91
+ expect(lines).toContain('name: Visible Name')
92
+ expect(lines).toContain('description: Visible Description')
93
+ expect(lines).not.toContain('tenant-secret')
94
+ expect(lines).not.toContain('org-secret')
95
+ expect(lines).not.toContain('tenant_id')
96
+ expect(lines).not.toContain('organization_id')
97
+ expect(lines).not.toContain('created_at')
98
+ expect(lines).not.toContain('updated_at')
99
+ })
100
+
101
+ it('honors fieldPolicy.excluded/hashOnly and the searchable allowlist for record and custom fields', async () => {
102
+ const captured: CapturedEmbeddingInput = { value: null }
103
+ const record = {
104
+ id: 'rec-2',
105
+ tenant_id: 'tenant-1',
106
+ name: 'Allowed Name',
107
+ ssn: '123-45-6789',
108
+ email: 'person@example.com',
109
+ 'cf:notes': 'Allowed note',
110
+ 'cf:secret_token': 'sk-should-not-leak',
111
+ }
112
+ const service = createService({
113
+ driver: createDriver(),
114
+ embeddingService: createEmbeddingService(captured),
115
+ queryEngine: createQueryEngine(record),
116
+ moduleConfigs: [
117
+ {
118
+ entities: [
119
+ {
120
+ entityId: 'demo:item',
121
+ fieldPolicy: {
122
+ searchable: ['name', 'notes'],
123
+ hashOnly: ['email'],
124
+ excluded: ['ssn', 'secret_token'],
125
+ },
126
+ },
127
+ ],
128
+ },
129
+ ],
130
+ })
131
+
132
+ await service.indexRecord({ entityId: 'demo:item', recordId: 'rec-2', tenantId: 'tenant-1' })
133
+
134
+ const lines = Array.isArray(captured.value) ? captured.value.join('\n') : String(captured.value)
135
+ expect(lines).toContain('name: Allowed Name')
136
+ expect(lines).toContain('custom.notes: Allowed note')
137
+ expect(lines).not.toContain('123-45-6789')
138
+ expect(lines).not.toContain('person@example.com')
139
+ expect(lines).not.toContain('sk-should-not-leak')
140
+ })
141
+ })
142
+
143
+ describe('VectorIndexService encryption fail-closed (issue #2716)', () => {
144
+ const enabledEncryptionThatThrows = (): TenantDataEncryptionService =>
145
+ ({
146
+ isEnabled: () => true,
147
+ encryptEntityPayload: async () => {
148
+ throw new Error('KMS outage')
149
+ },
150
+ decryptEntityPayload: async () => {
151
+ throw new Error('KMS outage')
152
+ },
153
+ }) as unknown as TenantDataEncryptionService
154
+
155
+ it('does not persist plaintext when encryption is enabled but encryption throws', async () => {
156
+ const driver = createDriver()
157
+ const captured: CapturedEmbeddingInput = { value: null }
158
+ const record = { id: 'rec-3', tenant_id: 'tenant-1', name: 'Sensitive Title' }
159
+ const service = createService({
160
+ driver,
161
+ embeddingService: createEmbeddingService(captured),
162
+ queryEngine: createQueryEngine(record),
163
+ moduleConfigs: [{ entities: [{ entityId: 'demo:item' }] }],
164
+ encryptionService: enabledEncryptionThatThrows(),
165
+ })
166
+
167
+ await expect(
168
+ service.indexRecord({ entityId: 'demo:item', recordId: 'rec-3', tenantId: 'tenant-1' }),
169
+ ).rejects.toThrow('KMS outage')
170
+ expect(driver.upsertCalls).toHaveLength(0)
171
+ })
172
+
173
+ it('fails closed on decrypt when encryption is enabled but decryption throws', async () => {
174
+ const driver = createDriver({
175
+ query: async () => [
176
+ {
177
+ entityId: 'demo:item',
178
+ recordId: 'rec-4',
179
+ organizationId: null,
180
+ score: 0.9,
181
+ checksum: 'abc',
182
+ resultTitle: 'cipher-title',
183
+ resultSubtitle: null,
184
+ resultIcon: null,
185
+ resultBadge: null,
186
+ resultSnapshot: null,
187
+ primaryLinkHref: null,
188
+ primaryLinkLabel: null,
189
+ links: null,
190
+ payload: null,
191
+ },
192
+ ],
193
+ })
194
+ const captured: CapturedEmbeddingInput = { value: null }
195
+ const service = createService({
196
+ driver,
197
+ embeddingService: createEmbeddingService(captured),
198
+ queryEngine: createQueryEngine({ id: 'rec-4' }),
199
+ moduleConfigs: [{ entities: [{ entityId: 'demo:item' }] }],
200
+ encryptionService: enabledEncryptionThatThrows(),
201
+ })
202
+
203
+ await expect(
204
+ service.search({ tenantId: 'tenant-1', query: 'anything' }),
205
+ ).rejects.toThrow('KMS outage')
206
+ })
207
+
208
+ it('falls back to plaintext when encryption is explicitly disabled', async () => {
209
+ const driver = createDriver()
210
+ const captured: CapturedEmbeddingInput = { value: null }
211
+ const record = { id: 'rec-5', tenant_id: 'tenant-1', name: 'Title' }
212
+ const disabledEncryption = {
213
+ isEnabled: () => false,
214
+ encryptEntityPayload: async () => {
215
+ throw new Error('should not be called when disabled')
216
+ },
217
+ } as unknown as TenantDataEncryptionService
218
+ const service = createService({
219
+ driver,
220
+ embeddingService: createEmbeddingService(captured),
221
+ queryEngine: createQueryEngine(record),
222
+ moduleConfigs: [{ entities: [{ entityId: 'demo:item' }] }],
223
+ encryptionService: disabledEncryption,
224
+ })
225
+
226
+ await service.indexRecord({ entityId: 'demo:item', recordId: 'rec-5', tenantId: 'tenant-1' })
227
+ expect(driver.upsertCalls).toHaveLength(1)
228
+ expect(driver.upsertCalls[0].resultTitle).toBe('Title')
229
+ })
230
+ })
package/src/lib/merger.ts CHANGED
@@ -6,6 +6,20 @@ import type { SearchResult, ResultMergeConfig, SearchStrategyId } from '../types
6
6
  */
7
7
  const RRF_K = 60
8
8
 
9
+ /**
10
+ * Build the deduplication key for a search result.
11
+ *
12
+ * The key includes `organizationId` so the same `entityId:recordId` surfaced
13
+ * for different organizations (for example from a stale index) never collides
14
+ * into a single merged entry. This is a defense-in-depth guard: the per-strategy
15
+ * `filterResultsByOrganizationScope()` already drops foreign-org results before
16
+ * merging, but keying on org here ensures the merger can never silently coalesce
17
+ * results across tenants if that upstream filter is bypassed.
18
+ */
19
+ function dedupeKey(result: SearchResult): string {
20
+ return `${result.organizationId ?? ''}:${result.entityId}:${result.recordId}`
21
+ }
22
+
9
23
  /**
10
24
  * Reciprocal Rank Fusion (RRF) algorithm for combining results from multiple search strategies.
11
25
  *
@@ -44,7 +58,7 @@ export function mergeAndRankResults(
44
58
 
45
59
  for (let rank = 0; rank < sourceResults.length; rank++) {
46
60
  const result = sourceResults[rank]
47
- const key = `${result.entityId}:${result.recordId}`
61
+ const key = dedupeKey(result)
48
62
  const rrfScore = weight / (RRF_K + rank + 1)
49
63
 
50
64
  const existing = seen.get(key)
@@ -128,7 +142,7 @@ export function deduplicateResults(results: SearchResult[]): SearchResult[] {
128
142
  const seen = new Map<string, SearchResult>()
129
143
 
130
144
  for (const result of results) {
131
- const key = `${result.entityId}:${result.recordId}`
145
+ const key = dedupeKey(result)
132
146
  const existing = seen.get(key)
133
147
 
134
148
  if (!existing || result.score > existing.score) {
@@ -463,7 +463,7 @@ yarn mercato search worker fulltext-indexing --concurrency=5
463
463
 
464
464
  ### Debug Logging
465
465
 
466
- Set `OM_SEARCH_DEBUG=true` to enable verbose debug logging for the search module. This outputs detailed information about indexing operations, strategy selection, and error handling. Errors are always logged regardless of this flag.
466
+ Set `OM_SEARCH_DEBUG=true` to enable verbose debug logging for the search module. This outputs information about indexing operations, strategy selection, and error handling. Debug output is redacted: it includes field names, token hashes, and counts, but never raw token text, document values, or decrypted PII. Errors are always logged regardless of this flag.
467
467
 
468
468
  ```env
469
469
  OM_SEARCH_DEBUG=true
@@ -0,0 +1,102 @@
1
+ import { expect, test } from '@playwright/test'
2
+ import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
3
+ import { getTokenScope, readJsonSafe } from '@open-mercato/core/helpers/integration/generalFixtures'
4
+ import {
5
+ createRoleFixture,
6
+ createUserFixture,
7
+ deleteRoleIfExists,
8
+ deleteUserIfExists,
9
+ setRoleAclFeatures,
10
+ } from '@open-mercato/core/helpers/integration/authFixtures'
11
+
12
+ /**
13
+ * TC-SEARCH-003: Search API requires authentication and feature gates (401/403)
14
+ * Source: issue #2483 (expand `search` integration coverage)
15
+ *
16
+ * Real routes (the `search` module id prefixes every route under /api/search):
17
+ * - GET /api/search/search requireAuth + requireFeatures ['search.view']
18
+ * - POST /api/search/reindex requireAuth + requireFeatures ['search.reindex']
19
+ *
20
+ * The framework route wrapper enforces requireAuth (401) and requireFeatures
21
+ * (403) BEFORE the handler body runs, so these gates hold independently of each
22
+ * route's own inline checks. Seeded role features: admin => search.* ,
23
+ * employee => vector.* only, superadmin => all. A user that holds search.view
24
+ * but NOT search.reindex must therefore be provisioned explicitly to prove the
25
+ * granular reindex gate (employee cannot stand in — it lacks search.view too).
26
+ */
27
+ const VALID_PASSWORD = 'Valid1!Pass'
28
+
29
+ test.describe('TC-SEARCH-003: search API auth & feature gates (401/403)', () => {
30
+ test('gates search.view and search.reindex by authentication and feature', async ({ request }) => {
31
+ test.slow()
32
+
33
+ const stamp = Date.now()
34
+ let adminToken: string | null = null
35
+ let superToken: string | null = null
36
+ let roleId: string | null = null
37
+ let viewerUserId: string | null = null
38
+ const roleName = `qa-search-003-viewer-${stamp}`
39
+ const viewerEmail = `qa-search-003-viewer-${stamp}@acme.com`
40
+
41
+ try {
42
+ // 1. Unauthenticated search => 401. q is non-empty, so only the auth gate
43
+ // can produce a 401 here (the handler's empty-query 400 is unreachable
44
+ // because the framework wrapper denies first).
45
+ const unauthSearch = await request.get('/api/search/search?q=test')
46
+ expect(unauthSearch.status(), 'unauthenticated GET /api/search/search must be 401').toBe(401)
47
+
48
+ // 2. Unauthenticated reindex => 401.
49
+ const unauthReindex = await request.post('/api/search/reindex', { data: {} })
50
+ expect(unauthReindex.status(), 'unauthenticated POST /api/search/reindex must be 401').toBe(401)
51
+
52
+ adminToken = await getAuthToken(request, 'admin')
53
+ const scope = getTokenScope(adminToken)
54
+
55
+ // 3. A user with search.view but NOT search.reindex: search is allowed
56
+ // (never 401/403), reindex is forbidden (403).
57
+ roleId = await createRoleFixture(request, adminToken, { name: roleName, tenantId: scope.tenantId })
58
+ await setRoleAclFeatures(request, adminToken, { roleId, features: ['search.view'] })
59
+ viewerUserId = await createUserFixture(request, adminToken, {
60
+ email: viewerEmail,
61
+ password: VALID_PASSWORD,
62
+ organizationId: scope.organizationId,
63
+ roles: [roleName],
64
+ name: 'QA Search 003 Viewer',
65
+ })
66
+ const viewerToken = await getAuthToken(request, viewerEmail, VALID_PASSWORD)
67
+
68
+ const viewerSearch = await apiRequest(request, 'GET', '/api/search/search?q=qa-search-003', { token: viewerToken })
69
+ expect(viewerSearch.status(), 'viewer with search.view must not be unauthorized on search').not.toBe(401)
70
+ expect(viewerSearch.status(), 'viewer with search.view must not be forbidden on search').not.toBe(403)
71
+ // The search route resolves searchService and returns 200 with results
72
+ // (empty when no strategy returns hits) — it does not 503 here — so a passing
73
+ // gate yields 200.
74
+ expect(viewerSearch.status(), 'search.view passes the gate, so the search succeeds with 200').toBe(200)
75
+
76
+ const viewerReindex = await apiRequest(request, 'POST', '/api/search/reindex', { token: viewerToken, data: {} })
77
+ expect(viewerReindex.status(), 'viewer lacking search.reindex must be forbidden on reindex').toBe(403)
78
+
79
+ // 4. Superadmin can initiate reindex — never blocked by auth/feature gates.
80
+ // useQueue:false makes the route clear its own lock in `finally`, so this
81
+ // leaves no lingering fulltext lock for other tests; scoping to a single
82
+ // entity keeps the synchronous reindex cheap.
83
+ superToken = await getAuthToken(request, 'superadmin')
84
+ const superReindex = await apiRequest(request, 'POST', '/api/search/reindex', {
85
+ token: superToken,
86
+ data: { entityId: 'customers:customer_company_profile', useQueue: false },
87
+ })
88
+ expect(superReindex.status(), 'superadmin reindex must not be unauthorized').not.toBe(401)
89
+ expect(superReindex.status(), 'superadmin reindex must not be forbidden').not.toBe(403)
90
+ expect([200, 503], 'superadmin reindex returns ok (200) or service-unavailable (503)').toContain(
91
+ superReindex.status(),
92
+ )
93
+ } finally {
94
+ // Best-effort: release any fulltext lock, then tear down fixtures.
95
+ if (superToken) {
96
+ await apiRequest(request, 'POST', '/api/search/reindex/cancel', { token: superToken }).catch(() => undefined)
97
+ }
98
+ await deleteUserIfExists(request, adminToken, viewerUserId)
99
+ await deleteRoleIfExists(request, adminToken, roleId)
100
+ }
101
+ })
102
+ })
@@ -0,0 +1,135 @@
1
+ import { expect, test, type APIRequestContext } from '@playwright/test'
2
+ import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
3
+ import { expectId, getTokenScope, readJsonSafe } from '@open-mercato/core/helpers/integration/generalFixtures'
4
+ import {
5
+ apiRequestWithSelectedOrg,
6
+ createRoleFixture,
7
+ createUserFixture,
8
+ deleteOrganizationIfExists,
9
+ deleteRoleIfExists,
10
+ deleteUserIfExists,
11
+ setRoleAclFeatures,
12
+ setUserAclVisibility,
13
+ } from '@open-mercato/core/helpers/integration/authFixtures'
14
+ import { createCompanyFixture, deleteEntityIfExists } from '@open-mercato/core/helpers/integration/crmFixtures'
15
+
16
+ type SearchResultItem = { entityId?: string; presenter?: { title?: string } | null }
17
+ type SearchResponse = { results?: SearchResultItem[] }
18
+
19
+ const COMPANY_ENTITY = 'customers:customer_company_profile'
20
+ const VALID_PASSWORD = 'Valid1!Pass'
21
+
22
+ function titleOf(item: SearchResultItem): string {
23
+ const presenter = item?.presenter
24
+ const title = presenter && typeof presenter === 'object' ? (presenter as { title?: unknown }).title : undefined
25
+ return typeof title === 'string' ? title : ''
26
+ }
27
+
28
+ async function searchTitles(request: APIRequestContext, token: string, query: string): Promise<string[]> {
29
+ const params = new URLSearchParams({ q: query, limit: '20', entityTypes: COMPANY_ENTITY })
30
+ const res = await apiRequest(request, 'GET', `/api/search/search?${params.toString()}`, { token })
31
+ if (!res.ok()) return []
32
+ const body = (await readJsonSafe<SearchResponse>(res)) ?? {}
33
+ return (Array.isArray(body.results) ? body.results : []).map(titleOf)
34
+ }
35
+
36
+ /**
37
+ * TC-SEARCH-004: search results are organization-scoped.
38
+ * Source: issue #2483 (P0 — cross-tenant/org isolation).
39
+ *
40
+ * A company created in the admin's home org (orgA) must be invisible to a user
41
+ * confined to a second organization (orgB) in the same tenant, while that user
42
+ * still sees a company that lives in their own org. This exercises the
43
+ * organization scope filter the search route applies. orgB, its scoped user, and
44
+ * orgB's company are provisioned via API as superadmin; the test skips if a
45
+ * second organization cannot be provisioned in this environment.
46
+ */
47
+ test.describe('TC-SEARCH-004: search results are organization-scoped', () => {
48
+ test('a restricted org-B user cannot see org-A companies via search', async ({ request }) => {
49
+ test.slow()
50
+
51
+ const stamp = Date.now()
52
+ const unique = `QASRCH004${stamp}`
53
+ let adminToken: string | null = null
54
+ let superToken: string | null = null
55
+ let orgBId: string | null = null
56
+ let roleId: string | null = null
57
+ let userBId: string | null = null
58
+ let companyAId: string | null = null
59
+ let companyBId: string | null = null
60
+ const roleName = `qa-search-004-${stamp}`
61
+ const userBEmail = `qa-search-004-${stamp}@acme.com`
62
+
63
+ try {
64
+ adminToken = await getAuthToken(request, 'admin')
65
+ superToken = await getAuthToken(request, 'superadmin')
66
+ const adminScope = getTokenScope(adminToken)
67
+
68
+ // Provision a second organization in the admin's tenant (superadmin bypasses
69
+ // the directory create command's tenant-selection enforcement).
70
+ const orgResp = await apiRequest(request, 'POST', '/api/directory/organizations', {
71
+ token: superToken,
72
+ data: { name: `QA Search 004 OrgB ${stamp}`, tenantId: adminScope.tenantId },
73
+ })
74
+ if (orgResp.status() !== 201) {
75
+ test.skip(true, `cannot provision a second organization in this environment (status ${orgResp.status()})`)
76
+ return
77
+ }
78
+ orgBId = expectId((await readJsonSafe<{ id?: string }>(orgResp))?.id, 'organization create returns an id')
79
+
80
+ // Company A in the admin's home org (orgA).
81
+ companyAId = await createCompanyFixture(request, adminToken, `${unique} A`)
82
+
83
+ // Company B placed into orgB via the selected-org cookie (superadmin).
84
+ const companyBResp = await apiRequestWithSelectedOrg(request, 'POST', '/api/customers/companies', {
85
+ token: superToken,
86
+ selectedOrgId: orgBId,
87
+ data: { displayName: `${unique} B` },
88
+ })
89
+ expect(companyBResp.status(), 'create company in orgB should return 201').toBe(201)
90
+ companyBId = expectId((await readJsonSafe<{ id?: string }>(companyBResp))?.id, 'company B returns an id')
91
+
92
+ // A user confined to orgB with search + customers read access.
93
+ roleId = await createRoleFixture(request, adminToken, { name: roleName, tenantId: adminScope.tenantId })
94
+ await setRoleAclFeatures(request, adminToken, { roleId, features: ['search.view', 'customers.*'] })
95
+ userBId = await createUserFixture(request, superToken, {
96
+ email: userBEmail,
97
+ password: VALID_PASSWORD,
98
+ organizationId: orgBId,
99
+ roles: [roleName],
100
+ name: 'QA Search 004 OrgB User',
101
+ })
102
+ await setUserAclVisibility(request, superToken, {
103
+ userId: userBId,
104
+ organizations: [orgBId],
105
+ features: ['search.view', 'customers.*'],
106
+ })
107
+ const userBToken = await getAuthToken(request, userBEmail, VALID_PASSWORD)
108
+ expect(getTokenScope(userBToken).organizationId, 'org-B user is scoped to orgB').toBe(orgBId)
109
+
110
+ // Admin (orgA) can find company A — confirms it is indexed and searchable.
111
+ await expect
112
+ .poll(async () => (await searchTitles(request, adminToken!, unique)).includes(`${unique} A`), {
113
+ timeout: 15_000,
114
+ })
115
+ .toBe(true)
116
+
117
+ // Org-B user can find company B (their own org) — confirms their search works.
118
+ await expect
119
+ .poll(async () => (await searchTitles(request, userBToken, unique)).includes(`${unique} B`), {
120
+ timeout: 15_000,
121
+ })
122
+ .toBe(true)
123
+
124
+ // Org-B user must NOT see company A (cross-org isolation — the P0 assertion).
125
+ const orgBTitles = await searchTitles(request, userBToken, unique)
126
+ expect(orgBTitles, 'org-B user must not see the org-A company').not.toContain(`${unique} A`)
127
+ } finally {
128
+ await deleteEntityIfExists(request, superToken, '/api/customers/companies', companyAId)
129
+ await deleteEntityIfExists(request, superToken, '/api/customers/companies', companyBId)
130
+ await deleteUserIfExists(request, superToken, userBId)
131
+ await deleteRoleIfExists(request, adminToken, roleId)
132
+ await deleteOrganizationIfExists(request, superToken, orgBId)
133
+ }
134
+ })
135
+ })
@@ -0,0 +1,45 @@
1
+ import { expect, test } from '@playwright/test'
2
+ import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
3
+ import { readJsonSafe } from '@open-mercato/core/helpers/integration/generalFixtures'
4
+
5
+ /**
6
+ * TC-SEARCH-005: Search validates the query parameter (400)
7
+ * Source: issue #2483.
8
+ *
9
+ * Route: GET /api/search/search (requireFeatures ['search.view']). The handler
10
+ * trims `q` and returns 400 { error: 'Missing query' } when it is empty; a
11
+ * non-empty query is accepted with 200 (empty results when no strategy matches),
12
+ * never 400. All cases authenticate as `admin`, which holds search.view, so the
13
+ * empty-query 400 is produced by the handler rather than the framework gate.
14
+ */
15
+ test.describe('TC-SEARCH-005: search validates the query parameter (400)', () => {
16
+ let token = ''
17
+
18
+ test.beforeAll(async ({ request }) => {
19
+ token = await getAuthToken(request, 'admin')
20
+ })
21
+
22
+ test('missing q returns 400', async ({ request }) => {
23
+ const res = await apiRequest(request, 'GET', '/api/search/search', { token })
24
+ expect(res.status()).toBe(400)
25
+ const body = await readJsonSafe<{ error?: string }>(res)
26
+ expect(typeof body?.error, 'a 400 response carries an error message').toBe('string')
27
+ })
28
+
29
+ test('empty q returns 400', async ({ request }) => {
30
+ const res = await apiRequest(request, 'GET', '/api/search/search?q=', { token })
31
+ expect(res.status()).toBe(400)
32
+ })
33
+
34
+ test('whitespace-only q returns 400', async ({ request }) => {
35
+ const res = await apiRequest(request, 'GET', '/api/search/search?q=%20%20%20', { token })
36
+ expect(res.status()).toBe(400)
37
+ })
38
+
39
+ test('non-empty q is accepted with 200 (not rejected as 400)', async ({ request }) => {
40
+ const res = await apiRequest(request, 'GET', `/api/search/search?q=qa-search-005-${Date.now()}`, { token })
41
+ // The route resolves searchService and returns 200 (empty results when no
42
+ // strategy matches); it does not 503 on this path. So a valid query is a 200.
43
+ expect(res.status(), 'a valid query is accepted with 200').toBe(200)
44
+ })
45
+ })
@@ -0,0 +1,74 @@
1
+ import { expect, test } from '@playwright/test'
2
+ import { apiRequest, getAuthToken } from '@open-mercato/core/helpers/integration/api'
3
+ import { readJsonSafe } from '@open-mercato/core/helpers/integration/generalFixtures'
4
+
5
+ type GlobalSearchSettings = { enabledStrategies?: string[] }
6
+ type GlobalSearchUpdate = { ok?: boolean; enabledStrategies?: string[] }
7
+ type GlobalSearchResponse = { strategiesEnabled?: string[] }
8
+
9
+ const DEFAULT_STRATEGIES = ['fulltext', 'vector', 'tokens']
10
+
11
+ /**
12
+ * TC-SEARCH-006: global (Cmd+K) search honors the saved strategy config over a
13
+ * URL override. Source: issue #2483.
14
+ *
15
+ * Routes:
16
+ * - GET/POST /api/search/settings/global-search (POST requires search.manage)
17
+ * - GET /api/search/search/global (ignores any `strategies` URL param)
18
+ *
19
+ * Saves enabledStrategies = ['tokens'], then calls global search with a
20
+ * conflicting ?strategies=fulltext,vector and asserts the response's
21
+ * strategiesEnabled reflects the SAVED config, not the URL. The original config
22
+ * is restored in `finally`. `admin` holds both search.view and search.manage.
23
+ */
24
+ test.describe('TC-SEARCH-006: global search honors saved strategy config over URL override', () => {
25
+ test('persisted enabledStrategies wins over the strategies URL parameter', async ({ request }) => {
26
+ test.slow()
27
+ test.setTimeout(120_000)
28
+
29
+ let token: string | null = null
30
+ let originalStrategies: string[] | null = DEFAULT_STRATEGIES
31
+
32
+ try {
33
+ token = await getAuthToken(request, 'admin')
34
+
35
+ const currentRes = await apiRequest(request, 'GET', '/api/search/settings/global-search', { token })
36
+ expect(currentRes.ok(), 'GET global-search settings should succeed').toBeTruthy()
37
+ const current = (await readJsonSafe<GlobalSearchSettings>(currentRes)) ?? {}
38
+ expect(Array.isArray(current.enabledStrategies), 'settings expose an enabledStrategies array').toBe(true)
39
+ originalStrategies =
40
+ Array.isArray(current.enabledStrategies) && current.enabledStrategies.length > 0
41
+ ? current.enabledStrategies
42
+ : DEFAULT_STRATEGIES
43
+
44
+ const updateRes = await apiRequest(request, 'POST', '/api/search/settings/global-search', {
45
+ token,
46
+ data: { enabledStrategies: ['tokens'] },
47
+ })
48
+ expect(updateRes.status(), 'POST global-search settings should return 200').toBe(200)
49
+ const updated = (await readJsonSafe<GlobalSearchUpdate>(updateRes)) ?? {}
50
+ expect(updated.ok, 'update reports ok').toBe(true)
51
+ expect(updated.enabledStrategies, 'update echoes the saved strategies').toEqual(['tokens'])
52
+
53
+ const globalRes = await apiRequest(
54
+ request,
55
+ 'GET',
56
+ `/api/search/search/global?q=qa-search-006-${Date.now()}&strategies=fulltext,vector`,
57
+ { token },
58
+ )
59
+ expect(globalRes.ok(), 'GET global search should succeed').toBeTruthy()
60
+ const globalBody = (await readJsonSafe<GlobalSearchResponse>(globalRes)) ?? {}
61
+ expect(
62
+ globalBody.strategiesEnabled,
63
+ 'global search must use the saved config (tokens), ignoring the strategies URL override',
64
+ ).toEqual(['tokens'])
65
+ } finally {
66
+ if (token && originalStrategies) {
67
+ await apiRequest(request, 'POST', '/api/search/settings/global-search', {
68
+ token,
69
+ data: { enabledStrategies: originalStrategies },
70
+ }).catch(() => undefined)
71
+ }
72
+ }
73
+ })
74
+ })