|
1 | 1 | /** |
2 | 2 | * @vitest-environment node |
3 | 3 | */ |
4 | | -import { describe, expect, it } from 'vitest' |
| 4 | +import { |
| 5 | + workspaceFileSearchBackfill, |
| 6 | + workspaceFileSearchDispatchQueue, |
| 7 | + workspaceFileSearchIndex, |
| 8 | +} from '@sim/db/schema' |
| 9 | +import { dbChainMock, dbChainMockFns, queueTableRows, resetDbChainMock } from '@sim/testing' |
| 10 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 11 | + |
| 12 | +const mocks = vi.hoisted(() => ({ |
| 13 | + batchTrigger: vi.fn(), |
| 14 | + info: vi.fn(), |
| 15 | + error: vi.fn(), |
| 16 | +})) |
| 17 | + |
| 18 | +vi.mock('@sim/db/schema', async () => ({ |
| 19 | + ...(await import('@sim/testing/mocks/schema.mock')).schemaMock, |
| 20 | + workspaceFileSearchBackfill: { id: 'backfill.id' }, |
| 21 | + workspaceFileSearchDispatchQueue: { |
| 22 | + workspaceId: 'queue.workspaceId', |
| 23 | + lastDispatchedAt: 'queue.lastDispatchedAt', |
| 24 | + enqueuedAt: 'queue.enqueuedAt', |
| 25 | + }, |
| 26 | +})) |
| 27 | + |
| 28 | +vi.mock('@sim/logger', () => ({ createLogger: () => ({ info: mocks.info, error: mocks.error }) })) |
| 29 | +vi.mock('@trigger.dev/sdk', () => ({ tasks: { batchTrigger: mocks.batchTrigger } })) |
| 30 | +vi.mock('@/lib/core/config/env-flags', () => ({ isTriggerDevEnabled: true })) |
| 31 | +vi.mock('@/lib/core/async-jobs/region', () => ({ resolveTriggerRegion: async () => 'us-east-1' })) |
| 32 | +vi.mock('@/lib/workspace-files/search/indexing', () => ({ |
| 33 | + indexWorkspaceFileForSearch: vi.fn(), |
| 34 | + markWorkspaceFileSearchIndexFailed: vi.fn(), |
| 35 | +})) |
| 36 | + |
5 | 37 | import { |
6 | 38 | buildWorkspaceFileSearchTriggerItems, |
| 39 | + dispatchWorkspaceFileSearchIndexJobs, |
| 40 | + prepareWorkspaceFileSearchDispatch, |
7 | 41 | shouldUseWorkspaceFileSearchTrigger, |
8 | 42 | } from '@/lib/workspace-files/search/dispatcher' |
9 | 43 |
|
@@ -34,3 +68,102 @@ describe('workspace file search dispatch policy', () => { |
34 | 68 | ]) |
35 | 69 | }) |
36 | 70 | }) |
| 71 | + |
| 72 | +describe('workspace file search dispatch deadlines', () => { |
| 73 | + beforeEach(() => { |
| 74 | + vi.clearAllMocks() |
| 75 | + resetDbChainMock() |
| 76 | + }) |
| 77 | + |
| 78 | + it('sets local database deadlines before taking the advisory lock', async () => { |
| 79 | + dbChainMockFns.execute.mockResolvedValueOnce([]).mockResolvedValueOnce([{ acquired: false }]) |
| 80 | + |
| 81 | + await expect(prepareWorkspaceFileSearchDispatch()).resolves.toEqual({ |
| 82 | + payloads: [], |
| 83 | + backfilledFiles: 0, |
| 84 | + reapedClaims: 0, |
| 85 | + lockAcquired: false, |
| 86 | + }) |
| 87 | + |
| 88 | + const guards = JSON.stringify(dbChainMockFns.execute.mock.calls[0][0]) |
| 89 | + expect(guards).toContain("set_config('statement_timeout', ") |
| 90 | + expect(guards).toContain('10000ms') |
| 91 | + expect(guards).toContain("set_config('lock_timeout', ") |
| 92 | + expect(guards).toContain('2000ms') |
| 93 | + expect(guards).toContain("set_config('transaction_timeout', ") |
| 94 | + expect(guards).toContain('20000ms') |
| 95 | + expect(guards.match(/, true\)/g)).toHaveLength(3) |
| 96 | + expect(JSON.stringify(dbChainMockFns.execute.mock.calls[1][0])).toContain( |
| 97 | + 'pg_try_advisory_xact_lock' |
| 98 | + ) |
| 99 | + expect(dbChainMockFns.insert).not.toHaveBeenCalled() |
| 100 | + }) |
| 101 | + |
| 102 | + it.each(['57014', '55P03', '25P04'])( |
| 103 | + 'propagates SQLSTATE %s without enqueuing an uncommitted claim', |
| 104 | + async (code) => { |
| 105 | + const error = new Error('Failed query\nparams: sensitive-value', { |
| 106 | + cause: Object.assign(new Error('database timeout'), { code }), |
| 107 | + }) |
| 108 | + dbChainMockFns.execute.mockResolvedValueOnce([]).mockResolvedValueOnce([{ acquired: true }]) |
| 109 | + dbChainMockFns.onConflictDoNothing.mockRejectedValueOnce(error) |
| 110 | + |
| 111 | + await expect(dispatchWorkspaceFileSearchIndexJobs()).rejects.toBe(error) |
| 112 | + |
| 113 | + expect(mocks.batchTrigger).not.toHaveBeenCalled() |
| 114 | + expect(mocks.error).toHaveBeenCalledWith('Workspace file search dispatch phase failed', { |
| 115 | + phase: 'backfill', |
| 116 | + durationMs: expect.any(Number), |
| 117 | + code, |
| 118 | + error: 'Failed query', |
| 119 | + }) |
| 120 | + expect(JSON.stringify(mocks.error.mock.calls)).not.toContain('sensitive-value') |
| 121 | + } |
| 122 | + ) |
| 123 | + |
| 124 | + it('reports a transaction failure even after the transaction callback finishes', async () => { |
| 125 | + const error = Object.assign(new Error('commit failed'), { code: '08006' }) |
| 126 | + dbChainMockFns.execute.mockResolvedValueOnce([]).mockResolvedValueOnce([{ acquired: false }]) |
| 127 | + dbChainMockFns.transaction.mockImplementationOnce(async (callback) => { |
| 128 | + await callback(dbChainMock.db) |
| 129 | + throw error |
| 130 | + }) |
| 131 | + |
| 132 | + await expect(prepareWorkspaceFileSearchDispatch()).rejects.toBe(error) |
| 133 | + |
| 134 | + expect(mocks.error).toHaveBeenCalledWith('Workspace file search dispatch phase failed', { |
| 135 | + phase: 'prepare-transaction', |
| 136 | + durationMs: expect.any(Number), |
| 137 | + code: '08006', |
| 138 | + error: 'commit failed', |
| 139 | + }) |
| 140 | + }) |
| 141 | + |
| 142 | + it('also bounds releasing committed claims when batch submission fails', async () => { |
| 143 | + queueTableRows(workspaceFileSearchBackfill, [{ completedAt: new Date() }]) |
| 144 | + queueTableRows(workspaceFileSearchIndex, []) |
| 145 | + queueTableRows(workspaceFileSearchIndex, [{ active: 0 }]) |
| 146 | + queueTableRows(workspaceFileSearchDispatchQueue, [{ workspaceId: 'workspace-1' }]) |
| 147 | + dbChainMockFns.execute |
| 148 | + .mockResolvedValueOnce([]) |
| 149 | + .mockResolvedValueOnce([{ acquired: true }]) |
| 150 | + .mockResolvedValueOnce([ |
| 151 | + { |
| 152 | + workspaceId: 'workspace-1', |
| 153 | + fileId: 'file-1', |
| 154 | + sourceContentUpdatedAt: new Date('2026-09-16T00:00:00Z'), |
| 155 | + }, |
| 156 | + ]) |
| 157 | + const error = new Error('Trigger unavailable') |
| 158 | + mocks.batchTrigger.mockRejectedValueOnce(error) |
| 159 | + |
| 160 | + await expect(dispatchWorkspaceFileSearchIndexJobs()).rejects.toBe(error) |
| 161 | + |
| 162 | + expect(dbChainMockFns.transaction).toHaveBeenCalledTimes(2) |
| 163 | + const guards = dbChainMockFns.execute.mock.calls.filter(([query]) => |
| 164 | + JSON.stringify(query).includes('statement_timeout') |
| 165 | + ) |
| 166 | + expect(guards).toHaveLength(2) |
| 167 | + expect(dbChainMockFns.set).toHaveBeenCalledWith(expect.objectContaining({ dispatchedAt: null })) |
| 168 | + }) |
| 169 | +}) |
0 commit comments