-
Notifications
You must be signed in to change notification settings - Fork 3.7k
fix(tables): server-authoritative run badge, tail SSE from latest, harden Stop-all #5492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+765
−326
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e8b705b
fix(tables): server-authoritative run badge, tail SSE from latest, ha…
TheodoreSpeaks ed3b78a
fix(tables): return null dispatchId when Stop-all cancels a run durin…
TheodoreSpeaks ab40f49
improvement(tables): co-locate dispatcher imports, stamp throttle clo…
TheodoreSpeaks f4a4bd3
fix(tables): table-wide hasRunning signal for Queueing label, fail fa…
TheodoreSpeaks 64c85d1
fix(tables): clear hasRunning on table-wide stop, refetch rows when a…
TheodoreSpeaks 188967c
fix(tables): don't let dispatch-cancel cleanup mask the original prep…
TheodoreSpeaks 0c7e829
fix(tables): reconcile null-dispatch runs via invalidation, not stale…
TheodoreSpeaks a5a39a8
fix(tables): widen warm-cache remount check to either query cache
TheodoreSpeaks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
apps/sim/app/api/table/[tableId]/dispatches/route.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { hybridAuthMockFns } from '@sim/testing' | ||
| import { NextRequest } from 'next/server' | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import type { TableDefinition } from '@/lib/table' | ||
|
|
||
| const { mockCheckAccess, mockListActiveDispatches, mockCountRunningCells } = vi.hoisted(() => ({ | ||
| mockCheckAccess: vi.fn(), | ||
| mockListActiveDispatches: vi.fn(), | ||
| mockCountRunningCells: vi.fn(), | ||
| })) | ||
|
|
||
| vi.mock('@/lib/table/dispatcher', () => ({ | ||
| listActiveDispatches: mockListActiveDispatches, | ||
| countRunningCells: mockCountRunningCells, | ||
| })) | ||
| vi.mock('@/app/api/table/utils', async () => { | ||
| const { NextResponse } = await import('next/server') | ||
| return { | ||
| checkAccess: mockCheckAccess, | ||
| accessError: (result: { status: number }) => | ||
| NextResponse.json({ error: 'denied' }, { status: result.status }), | ||
| } | ||
| }) | ||
|
|
||
| import { GET } from '@/app/api/table/[tableId]/dispatches/route' | ||
|
|
||
| function buildTable(overrides: Partial<TableDefinition> = {}): TableDefinition { | ||
| return { | ||
| id: 'tbl_1', | ||
| name: 'People', | ||
| description: null, | ||
| schema: { columns: [] }, | ||
| metadata: null, | ||
| rowCount: 0, | ||
| maxRows: 1_000_000, | ||
| workspaceId: 'workspace-1', | ||
| createdBy: 'user-1', | ||
| archivedAt: null, | ||
| createdAt: new Date(), | ||
| updatedAt: new Date(), | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| function makeRequest(tableId = 'tbl_1') { | ||
| const req = new NextRequest(`http://localhost:3000/api/table/${tableId}/dispatches`) | ||
| return GET(req, { params: Promise.resolve({ tableId }) }) | ||
| } | ||
|
|
||
| function buildDispatchRow(overrides: Record<string, unknown> = {}) { | ||
| return { | ||
| id: 'dispatch-1', | ||
| tableId: 'tbl_1', | ||
| workspaceId: 'workspace-1', | ||
| requestId: 'req-1', | ||
| mode: 'all', | ||
| scope: { groupIds: ['group-1'] }, | ||
| status: 'dispatching', | ||
| cursor: 4, | ||
| limit: null, | ||
| isManualRun: true, | ||
| processedCount: 5, | ||
| ...overrides, | ||
| } | ||
| } | ||
|
|
||
| describe('GET /api/table/[tableId]/dispatches', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ | ||
| success: true, | ||
| userId: 'user-1', | ||
| authType: 'session', | ||
| }) | ||
| mockCheckAccess.mockResolvedValue({ ok: true, table: buildTable() }) | ||
| mockListActiveDispatches.mockResolvedValue([]) | ||
| mockCountRunningCells.mockResolvedValue({ byRowId: {}, hasRunning: false }) | ||
| }) | ||
|
|
||
| it('returns dispatches and the per-row running map, without a total field', async () => { | ||
| mockListActiveDispatches.mockResolvedValue([buildDispatchRow()]) | ||
| mockCountRunningCells.mockResolvedValue({ | ||
| byRowId: { 'row-1': 2, 'row-2': 1 }, | ||
| hasRunning: true, | ||
| }) | ||
|
|
||
| const response = await makeRequest() | ||
| const data = await response.json() | ||
|
|
||
| expect(response.status).toBe(200) | ||
| expect(data.data.dispatches).toEqual([ | ||
| { | ||
| id: 'dispatch-1', | ||
| status: 'dispatching', | ||
| mode: 'all', | ||
| isManualRun: true, | ||
| cursor: 4, | ||
| scope: { groupIds: ['group-1'] }, | ||
| }, | ||
| ]) | ||
| expect(data.data.runningByRowId).toEqual({ 'row-1': 2, 'row-2': 1 }) | ||
| expect(data.data.hasRunning).toBe(true) | ||
| expect(data.data).not.toHaveProperty('runningCellCount') | ||
| }) | ||
|
|
||
| it('includes unclaimed pre-stamps only while a dispatch is active', async () => { | ||
| mockListActiveDispatches.mockResolvedValue([buildDispatchRow()]) | ||
| await makeRequest() | ||
| expect(mockCountRunningCells).toHaveBeenCalledWith('tbl_1', { | ||
| includeUnclaimedPreStamps: true, | ||
| }) | ||
|
|
||
| mockListActiveDispatches.mockResolvedValue([]) | ||
| await makeRequest() | ||
| expect(mockCountRunningCells).toHaveBeenLastCalledWith('tbl_1', { | ||
| includeUnclaimedPreStamps: false, | ||
| }) | ||
| }) | ||
|
|
||
| it('returns 401 when unauthenticated', async () => { | ||
| hybridAuthMockFns.mockCheckSessionOrInternalAuth.mockResolvedValue({ success: false }) | ||
| const response = await makeRequest() | ||
| expect(response.status).toBe(401) | ||
| expect(mockListActiveDispatches).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.