Skip to content

Commit 9581187

Browse files
committed
fix(search-experts): say when the filter, not the topic, left no expert; test a withheld member directory
When contributors were found but none matched the department or location filter, the note now says so instead of claiming no one worked on the topic. The directory-closed tests now also cover a plain member kept out by a withheld member directory, with People enabled.
1 parent 056570a commit 9581187

2 files changed

Lines changed: 69 additions & 23 deletions

File tree

‎apps/sim/lib/knowledge/application/experts.integration.ts‎

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import { eq, inArray } from 'drizzle-orm'
1919
import { afterAll, beforeAll, beforeEach, expect, it, vi } from 'vitest'
2020
import type { LiveContributionItem } from '@/lib/sim-search/live/application'
2121

22-
const { liveItems, peopleOpen } = vi.hoisted(() => ({
22+
const { liveItems, peopleOpen, directoryWithheld } = vi.hoisted(() => ({
23+
directoryWithheld: { value: false },
2324
liveItems: { value: [] as unknown[] },
2425
peopleOpen: { value: true },
2526
}))
@@ -55,6 +56,18 @@ vi.mock('@/lib/sim-search/live/application', () => ({
5556
},
5657
},
5758
}))
59+
vi.mock('@/lib/permission-groups/capability-assertions', async (importOriginal) => {
60+
const actual =
61+
await importOriginal<typeof import('@/lib/permission-groups/capability-assertions')>()
62+
return {
63+
...actual,
64+
isOrganizationCapabilityWithheld: async (
65+
...args: Parameters<typeof actual.isOrganizationCapabilityWithheld>
66+
) =>
67+
(directoryWithheld.value && args[1] === 'organization.member_directory') ||
68+
actual.isOrganizationCapabilityWithheld(...args),
69+
}
70+
})
5871
vi.mock('@/lib/people/feature-flag', async (importOriginal) => ({
5972
...(await importOriginal<typeof import('@/lib/people/feature-flag')>()),
6073
requirePeopleEnabled: async () => {
@@ -66,7 +79,10 @@ import { searchExperts } from '@/lib/knowledge/application/experts'
6679

6780
const organizationId = generateId()
6881
const adminId = generateId()
82+
const readerId = generateId()
6983
const principal = createSessionPrincipal({ userId: adminId })
84+
/** A plain member, whom a withheld member directory keeps out of People. */
85+
const reader = createSessionPrincipal({ userId: readerId })
7086
const email = (name: string) => `${name}-${organizationId}@example.test`
7187
const [billingLead, billingDev, namesake] = [generateId(), generateId(), generateId()]
7288
const now = new Date().toISOString()
@@ -98,28 +114,34 @@ function item(
98114
}
99115
}
100116

101-
function search(extra: { department?: string } = {}) {
117+
function search(extra: { department?: string } = {}, as = principal) {
102118
return searchExperts.execute({
103-
principal,
119+
principal: as,
104120
input: { query: 'billing', topK: 10, organizationId, surface: 'mcp', ...extra },
105121
})
106122
}
107123

108124
beforeAll(async () => {
109-
await db.insert(user).values({
110-
id: adminId,
111-
name: 'Administrator',
112-
email: email('admin'),
113-
emailVerified: true,
114-
createdAt: new Date(),
115-
updatedAt: new Date(),
116-
})
125+
await db.insert(user).values(
126+
[
127+
[adminId, 'Administrator'],
128+
[readerId, 'Reader'],
129+
].map(([id, name]) => ({
130+
id,
131+
name,
132+
email: email(id),
133+
emailVerified: true,
134+
createdAt: new Date(),
135+
updatedAt: new Date(),
136+
}))
137+
)
117138
await db
118139
.insert(organization)
119140
.values({ id: organizationId, slug: organizationId, name: 'Experts', createdAt: new Date() })
120-
await db
121-
.insert(member)
122-
.values({ id: generateId(), organizationId, userId: adminId, role: 'owner' })
141+
await db.insert(member).values([
142+
{ id: generateId(), organizationId, userId: adminId, role: 'owner' },
143+
{ id: generateId(), organizationId, userId: readerId, role: 'member' },
144+
])
123145
await db
124146
.insert(peopleDirectorySync)
125147
.values({ organizationId, source: 'okta', sourceTarget: 'example.okta.com' })
@@ -151,6 +173,7 @@ beforeAll(async () => {
151173

152174
beforeEach(() => {
153175
peopleOpen.value = true
176+
directoryWithheld.value = false
154177
liveItems.value = [
155178
item('jira', 'ENG-1', [
156179
{ role: 'assignee', email: email(billingLead), name: 'Billing Lead' },
@@ -164,7 +187,7 @@ beforeEach(() => {
164187

165188
afterAll(async () => {
166189
await db.delete(organization).where(eq(organization.id, organizationId))
167-
await db.delete(user).where(eq(user.id, adminId))
190+
await db.delete(user).where(inArray(user.id, [adminId, readerId]))
168191
vi.unstubAllEnvs()
169192
})
170193

@@ -215,9 +238,24 @@ it('narrows to a department', async () => {
215238
expect(result.otherContributors).toBeUndefined()
216239
})
217240

218-
it('names experts without profiles when the directory is closed to the caller', async () => {
219-
peopleOpen.value = false
220-
const result = await search()
241+
it.each([
242+
[
243+
'People is off for the organization',
244+
(): void => {
245+
peopleOpen.value = false
246+
},
247+
principal,
248+
],
249+
[
250+
'the member directory is withheld from members',
251+
(): void => {
252+
directoryWithheld.value = true
253+
},
254+
reader,
255+
],
256+
] as const)('names experts without profiles when %s', async (_closed, close, as) => {
257+
close()
258+
const result = await search({}, as)
221259
expect(result.experts).toEqual([])
222260
expect(result.otherContributors?.[0]).toMatchObject({ email: email(billingLead) })
223261
expect(result.otherContributors?.[0]).not.toHaveProperty('personId')
@@ -269,8 +307,8 @@ it('counts an item two connected accounts both return once', async () => {
269307
})
270308

271309
it('still names contributors when the directory is closed and a department is asked for', async () => {
272-
peopleOpen.value = false
273-
const result = await search({ department: 'platform' })
310+
directoryWithheld.value = true
311+
const result = await search({ department: 'platform' }, reader)
274312
expect(result.experts).toEqual([])
275313
expect(result.otherContributors?.length).toBeGreaterThan(0)
276314
expect(result.note).toMatch(/department and location filters were not applied/)
@@ -311,3 +349,9 @@ it('matches no one by an email two directory people share, or a Slack ID two wor
311349
await db.delete(peopleSlackIdentity).where(eq(peopleSlackIdentity.slackUserId, 'U-DUP'))
312350
}
313351
})
352+
353+
it('says the filter, not the topic, left no one when a department matches no contributor', async () => {
354+
const result = await search({ department: 'no such department' })
355+
expect(result.experts).toEqual([])
356+
expect(result.note).toMatch(/no one in the directory matches the department or location filter/)
357+
})

‎apps/sim/lib/knowledge/application/experts.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,11 @@ export const searchExperts = defineAuthorizedKnowledgeUseCase({
204204
)
205205
if (!experts.length && !others.length)
206206
notes.push(
207-
search.live?.accounts.length
208-
? 'No one in the items you can read worked on this topic. Try a broader topic, another source, or a wider window.'
209-
: (search.live?.guidance ?? 'No connected source could be searched.')
207+
ranked.length
208+
? 'People worked on this topic, but no one in the directory matches the department or location filter.'
209+
: search.live?.accounts.length
210+
? 'No one in the items you can read worked on this topic. Try a broader topic, another source, or a wider window.'
211+
: (search.live?.guidance ?? 'No connected source could be searched.')
210212
)
211213
return {
212214
experts,

0 commit comments

Comments
 (0)