|
1 | 1 | import { createDeferred } from '@sim/testing/helpers' |
2 | 2 | import { getMockLogger } from '@sim/testing/mocks/logger.mock' |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { OrchestrationError } from '@/lib/core/orchestration/types' |
4 | 5 | import { |
| 6 | + describeSearchRequest, |
| 7 | + documentKindLabel, |
5 | 8 | measureSearchStage, |
6 | 9 | recordSearchProviderResult, |
7 | 10 | withSearchDiagnostics, |
8 | 11 | withSearchProviderDiagnostics, |
9 | 12 | withSearchRequestDiagnostics, |
10 | 13 | } from '@/lib/knowledge/search/diagnostics' |
| 14 | +import { LiveReadError } from '@/lib/sim-search/live/read-error' |
11 | 15 |
|
12 | 16 | const logs = getMockLogger('KnowledgeSearchDiagnostics') |
13 | 17 |
|
@@ -171,4 +175,112 @@ describe('search pipeline diagnostics', () => { |
171 | 175 | ) |
172 | 176 | expect(JSON.stringify(logs.info.mock.calls)).not.toContain('private preview content') |
173 | 177 | }) |
| 178 | + |
| 179 | + it('describes a search request without its text, modifiers, cursor, or account', () => { |
| 180 | + const shape = describeSearchRequest({ |
| 181 | + query: ' private launch plan ', |
| 182 | + filters: { |
| 183 | + startDate: '2026-10-30T00:00:00Z', |
| 184 | + endDate: '2026-11-13T12:00:00Z', |
| 185 | + sortBy: 'newest', |
| 186 | + }, |
| 187 | + nativeQueries: [ |
| 188 | + { |
| 189 | + provider: 'slack', |
| 190 | + query: 'private decision', |
| 191 | + modifiers: 'in:#private-channel', |
| 192 | + cursor: 'private-cursor', |
| 193 | + accountId: 'private-account', |
| 194 | + termClauses: ['private term'], |
| 195 | + }, |
| 196 | + { provider: 'jira', query: '', kind: 'issues' }, |
| 197 | + ], |
| 198 | + }) |
| 199 | + expect(shape).toEqual({ |
| 200 | + queryTermCount: 3, |
| 201 | + sortBy: 'newest', |
| 202 | + dateWindowDays: 14.5, |
| 203 | + nativeQueries: [ |
| 204 | + { |
| 205 | + provider: 'slack', |
| 206 | + hasText: true, |
| 207 | + hasModifiers: true, |
| 208 | + hasCursor: true, |
| 209 | + hasProject: false, |
| 210 | + targetsAccount: true, |
| 211 | + keywordOnly: false, |
| 212 | + termClauseCount: 1, |
| 213 | + }, |
| 214 | + { |
| 215 | + provider: 'jira', |
| 216 | + kind: 'issues', |
| 217 | + hasText: false, |
| 218 | + hasModifiers: false, |
| 219 | + hasCursor: false, |
| 220 | + hasProject: false, |
| 221 | + targetsAccount: false, |
| 222 | + keywordOnly: false, |
| 223 | + termClauseCount: 0, |
| 224 | + }, |
| 225 | + ], |
| 226 | + }) |
| 227 | + expect(JSON.stringify(shape)).not.toContain('private') |
| 228 | + }) |
| 229 | + |
| 230 | + it('keeps a document kind only when it is a bounded token', () => { |
| 231 | + expect(documentKindLabel('file')).toBe('file') |
| 232 | + expect(documentKindLabel('application/vnd.google-apps.document')).toBe( |
| 233 | + 'application/vnd.google-apps.document' |
| 234 | + ) |
| 235 | + expect(documentKindLabel('Quarterly plan for a private customer')).toBe('other') |
| 236 | + expect(documentKindLabel('ProjectAtlas_Q3')).toBe('other') |
| 237 | + expect(documentKindLabel('atlas-launch-notes')).toBe('other') |
| 238 | + expect(documentKindLabel('notes/atlas')).toBe('other') |
| 239 | + expect(documentKindLabel(undefined)).toBeUndefined() |
| 240 | + }) |
| 241 | + |
| 242 | + it('names the class of a failure that ends a trace, never its message', async () => { |
| 243 | + const failures = [ |
| 244 | + new OrchestrationError('not_found', 'private document title is gone'), |
| 245 | + new LiveReadError('private provider text', true), |
| 246 | + new Error('private unexpected text'), |
| 247 | + ] |
| 248 | + for (const error of failures) |
| 249 | + await expect( |
| 250 | + withSearchDiagnostics({ operation: 'read_document' }, async () => { |
| 251 | + throw error |
| 252 | + }) |
| 253 | + ).rejects.toBe(error) |
| 254 | + const summaries = logs.info.mock.calls |
| 255 | + .filter(([message]) => message === 'Knowledge search completed') |
| 256 | + .slice(-3) |
| 257 | + .map(([, fields]) => fields?.failure) |
| 258 | + expect(summaries).toEqual([ |
| 259 | + { reason: 'not_found' }, |
| 260 | + { reason: 'provider_retryable' }, |
| 261 | + { reason: 'unexpected_error' }, |
| 262 | + ]) |
| 263 | + const logged = JSON.stringify(logs.info.mock.calls) |
| 264 | + for (const error of failures) expect(logged).not.toContain(error.message) |
| 265 | + }) |
| 266 | + |
| 267 | + it('keeps the failure class when an outer tool call turns the error into a result', async () => { |
| 268 | + await withSearchDiagnostics({ surface: 'copilot' }, async () => { |
| 269 | + try { |
| 270 | + await withSearchDiagnostics({ operation: 'read_document' }, async () => { |
| 271 | + throw new OrchestrationError('not_found', 'private document title is gone') |
| 272 | + }) |
| 273 | + } catch { |
| 274 | + return { success: false } |
| 275 | + } |
| 276 | + }) |
| 277 | + expect(logs.info).toHaveBeenLastCalledWith( |
| 278 | + 'Knowledge search completed', |
| 279 | + expect.objectContaining({ |
| 280 | + surface: 'copilot', |
| 281 | + outcome: 'error', |
| 282 | + failure: { reason: 'not_found' }, |
| 283 | + }) |
| 284 | + ) |
| 285 | + }) |
174 | 286 | }) |
0 commit comments