chore: import upstream snapshot with attribution
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
This commit is contained in:
@@ -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()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,69 @@
|
||||
import { createLogger } from '@sim/logger'
|
||||
import { type NextRequest, NextResponse } from 'next/server'
|
||||
import { type ActiveDispatch, listActiveDispatchesContract } from '@/lib/api/contracts/tables'
|
||||
import { parseRequest } from '@/lib/api/server'
|
||||
import { checkSessionOrInternalAuth } from '@/lib/auth/hybrid'
|
||||
import { generateRequestId } from '@/lib/core/utils/request'
|
||||
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
|
||||
import { countRunningCells, listActiveDispatches } from '@/lib/table/dispatcher'
|
||||
import { accessError, checkAccess } from '@/app/api/table/utils'
|
||||
|
||||
const logger = createLogger('TableDispatchesAPI')
|
||||
|
||||
interface RouteParams {
|
||||
params: Promise<{ tableId: string }>
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/table/[tableId]/dispatches
|
||||
*
|
||||
* Returns active (`pending` / `dispatching`) dispatches for the table. Drives
|
||||
* the client's "about to run" overlay so refresh during a long Run-all keeps
|
||||
* the queued indicators on rows the dispatcher hasn't reached yet.
|
||||
*/
|
||||
export const GET = withRouteHandler(async (request: NextRequest, { params }: RouteParams) => {
|
||||
const requestId = generateRequestId()
|
||||
|
||||
try {
|
||||
const authResult = await checkSessionOrInternalAuth(request, { requireWorkflowId: false })
|
||||
if (!authResult.success || !authResult.userId) {
|
||||
return NextResponse.json({ error: 'Authentication required' }, { status: 401 })
|
||||
}
|
||||
|
||||
const parsed = await parseRequest(listActiveDispatchesContract, request, { params })
|
||||
if (!parsed.success) return parsed.response
|
||||
const { tableId } = parsed.data.params
|
||||
|
||||
const result = await checkAccess(tableId, authResult.userId, 'read')
|
||||
if (!result.ok) return accessError(result, requestId, tableId)
|
||||
|
||||
const rows = await listActiveDispatches(tableId)
|
||||
// Unclaimed `pending` pre-stamps are real queued work while a dispatch is
|
||||
// active; with none active they're abandoned orphans that would pin the
|
||||
// "X running" badge above zero forever.
|
||||
const { byRowId: runningByRowId, hasRunning } = await countRunningCells(tableId, {
|
||||
includeUnclaimedPreStamps: rows.length > 0,
|
||||
})
|
||||
const dispatches: ActiveDispatch[] = rows.map((r) => ({
|
||||
id: r.id,
|
||||
status: r.status as 'pending' | 'dispatching',
|
||||
mode: r.mode,
|
||||
isManualRun: r.isManualRun,
|
||||
cursor: r.cursor,
|
||||
scope: r.scope,
|
||||
...(r.limit ? { limit: r.limit } : {}),
|
||||
}))
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
dispatches,
|
||||
runningByRowId,
|
||||
hasRunning,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
logger.error(`[${requestId}] list-dispatches failed:`, error)
|
||||
return NextResponse.json({ error: 'Failed to list active dispatches' }, { status: 500 })
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user