|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { databaseMock } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | +import type { MutationProof } from '@/lib/table/mutation-locks' |
| 7 | +import type { DbTransaction } from '@/lib/table/planner' |
| 8 | + |
| 9 | +vi.mock('@/lib/table/constants', () => ({ |
| 10 | + getDeleteSnapshotBatchSize: () => 1, |
| 11 | + TABLE_LIMITS: { UPDATE_BATCH_SIZE: 100 }, |
| 12 | +})) |
| 13 | +vi.mock('@/lib/table/tx', () => ({ setTableTxTimeouts: vi.fn() })) |
| 14 | + |
| 15 | +import { |
| 16 | + type DeletedRowsHandler, |
| 17 | + deleteOrderedRowsByIds, |
| 18 | + deletePageByIds, |
| 19 | +} from '@/lib/table/rows/ordering' |
| 20 | + |
| 21 | +const mockTransaction = databaseMock.db.transaction as ReturnType<typeof vi.fn> |
| 22 | +const proof = {} as MutationProof<'delete'> |
| 23 | + |
| 24 | +type DeleteRunner = (onDeleted: DeletedRowsHandler) => Promise<unknown> |
| 25 | + |
| 26 | +describe('ordered row delete trigger handoff', () => { |
| 27 | + beforeEach(() => { |
| 28 | + vi.clearAllMocks() |
| 29 | + }) |
| 30 | + |
| 31 | + it.each([ |
| 32 | + [ |
| 33 | + 'direct deletes', |
| 34 | + (onDeleted: DeletedRowsHandler) => |
| 35 | + deleteOrderedRowsByIds({ |
| 36 | + tableId: 'table-1', |
| 37 | + workspaceId: 'workspace-1', |
| 38 | + rowIds: ['row-1', 'row-2'], |
| 39 | + proof, |
| 40 | + onDeleted, |
| 41 | + }), |
| 42 | + ], |
| 43 | + [ |
| 44 | + 'background delete pages', |
| 45 | + (onDeleted: DeletedRowsHandler) => |
| 46 | + deletePageByIds('table-1', 'workspace-1', ['row-1', 'row-2'], proof, undefined, onDeleted), |
| 47 | + ], |
| 48 | + ])( |
| 49 | + 'runs %s handlers after commit and before the next batch', |
| 50 | + async (_label, run: DeleteRunner) => { |
| 51 | + const events: string[] = [] |
| 52 | + let batchIndex = 0 |
| 53 | + let releaseFirstHandler: (() => void) | undefined |
| 54 | + const firstHandlerGate = new Promise<void>((resolve) => { |
| 55 | + releaseFirstHandler = resolve |
| 56 | + }) |
| 57 | + const trx = { |
| 58 | + delete: () => ({ |
| 59 | + where: () => ({ |
| 60 | + returning: async () => { |
| 61 | + const id = `row-${batchIndex + 1}` |
| 62 | + batchIndex++ |
| 63 | + return [{ id, data: { title: id } }] |
| 64 | + }, |
| 65 | + }), |
| 66 | + }), |
| 67 | + } as unknown as DbTransaction |
| 68 | + |
| 69 | + mockTransaction.mockImplementation( |
| 70 | + async (callback: (transaction: DbTransaction) => Promise<unknown>) => { |
| 71 | + const result = await callback(trx) |
| 72 | + events.push(`commit-${mockTransaction.mock.calls.length}`) |
| 73 | + return result |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + const onDeleted = vi.fn(async (rows: Array<{ id: string }>) => { |
| 78 | + events.push(`trigger-${rows[0]?.id}`) |
| 79 | + if (rows[0]?.id === 'row-1') await firstHandlerGate |
| 80 | + }) |
| 81 | + const pending = run(onDeleted) |
| 82 | + |
| 83 | + await vi.waitFor(() => { |
| 84 | + expect(events).toEqual(['commit-1', 'trigger-row-1']) |
| 85 | + }) |
| 86 | + expect(mockTransaction).toHaveBeenCalledTimes(1) |
| 87 | + |
| 88 | + releaseFirstHandler?.() |
| 89 | + await pending |
| 90 | + |
| 91 | + expect(events).toEqual(['commit-1', 'trigger-row-1', 'commit-2', 'trigger-row-2']) |
| 92 | + expect(onDeleted.mock.calls.map(([rows]) => rows)).toEqual([ |
| 93 | + [{ id: 'row-1', data: { title: 'row-1' } }], |
| 94 | + [{ id: 'row-2', data: { title: 'row-2' } }], |
| 95 | + ]) |
| 96 | + } |
| 97 | + ) |
| 98 | +}) |
0 commit comments