Skip to content

Commit 5c58c02

Browse files
committed
fix: address review and compatibility regressions
1 parent 2d935a0 commit 5c58c02

21 files changed

Lines changed: 296 additions & 111 deletions

File tree

apps/sim/app/api/cron/cleanup-stale-executions/route.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface MockCondition {
2727
conditions?: unknown[]
2828
left?: unknown
2929
right?: unknown
30+
values?: unknown
3031
toSQL?: () => { sql: string; params: unknown[] }
3132
}
3233

@@ -122,6 +123,9 @@ describe('stale execution cleanup deadline grace', () => {
122123
})
123124

124125
it('reports a worker cleanup deadline while preserving the generic stale fallback', async () => {
126+
queueTableRows(asyncJobs, [{ id: 'async-job-1' }])
127+
dbChainMockFns.returning.mockResolvedValueOnce([{ id: 'async-job-1' }])
128+
125129
const response = await GET(createRequest())
126130

127131
expect(response.status).toBe(200)
@@ -165,6 +169,8 @@ describe('stale execution cleanup deadline grace', () => {
165169
it('keeps table-job heartbeat cleanup independent from workflow timeout policy', async () => {
166170
vi.useFakeTimers()
167171
vi.setSystemTime(new Date('2026-08-03T12:00:00.000Z'))
172+
queueTableRows(tableJobs, [{ id: 'table-job-1' }])
173+
dbChainMockFns.returning.mockResolvedValueOnce([{ id: 'table-job-1' }])
168174

169175
try {
170176
const response = await GET(createRequest())
@@ -205,7 +211,7 @@ describe('stale execution cleanup deadline grace', () => {
205211
const response = await GET(createRequest())
206212

207213
expect(response.status).toBe(200)
208-
expect(dbChainMockFns.transaction).toHaveBeenCalledOnce()
214+
expect(dbChainMockFns.transaction).toHaveBeenCalledTimes(7)
209215
expect(dbChainMockFns.for).toHaveBeenCalledTimes(7)
210216
for (const [strength, options] of dbChainMockFns.for.mock.calls) {
211217
expect(strength).toBe('update')
@@ -222,6 +228,9 @@ describe('stale execution cleanup deadline grace', () => {
222228
type: 'export',
223229
resultKey: `workspace/workspace-1/exports/table-1/job-${index}/export.csv`,
224230
}))
231+
const exportCandidates = Array.from({ length: 100 }, (_, index) => ({
232+
id: `export-${index}`,
233+
}))
225234

226235
for (let batch = 0; batch < 10; batch++) {
227236
const workflowBatch = Array.from({ length: 100 }, (_, index) => ({
@@ -231,18 +240,23 @@ describe('stale execution cleanup deadline grace', () => {
231240
dbChainMockFns.returning.mockResolvedValueOnce(workflowBatch)
232241
}
233242
for (let batch = 0; batch < 10; batch++) {
243+
queueTableRows(asyncJobs, stateBatch)
234244
dbChainMockFns.returning.mockResolvedValueOnce(stateBatch)
235245
}
236246
for (let batch = 0; batch < 10; batch++) {
247+
queueTableRows(tableJobs, stateBatch)
237248
dbChainMockFns.returning.mockResolvedValueOnce(stateBatch)
238249
}
239250
for (let batch = 0; batch < 10; batch++) {
251+
queueTableRows(tableJobs, exportCandidates)
240252
dbChainMockFns.returning.mockResolvedValueOnce(exportBatch)
241253
}
242254
for (let batch = 0; batch < 10; batch++) {
255+
queueTableRows(asyncJobs, stateBatch)
243256
dbChainMockFns.returning.mockResolvedValueOnce(stateBatch)
244257
}
245258
for (let batch = 0; batch < 10; batch++) {
259+
queueTableRows(asyncJobs, retentionBatch)
246260
dbChainMockFns.returning.mockResolvedValueOnce(retentionBatch)
247261
}
248262
dbChainMockFns.returning.mockResolvedValueOnce([])
@@ -282,6 +296,13 @@ describe('stale execution cleanup deadline grace', () => {
282296
.filter((shape): shape is Record<string, unknown> => Boolean(shape))
283297
expect(returningShapes.some((shape) => 'payload' in shape)).toBe(false)
284298
expect(returningShapes.some((shape) => 'type' in shape && 'resultKey' in shape)).toBe(true)
299+
300+
const claimedIds = dbChainMockFns.where.mock.calls
301+
.flatMap(([condition]) => flattenConditions(condition))
302+
.filter((condition) => condition.type === 'inArray')
303+
.map((condition) => condition.values)
304+
expect(claimedIds.length).toBeGreaterThan(0)
305+
expect(claimedIds.every((ids) => Array.isArray(ids))).toBe(true)
285306
})
286307

287308
it('drains more than the legacy 100-row workflow cap in one bounded run', async () => {
@@ -318,9 +339,11 @@ describe('stale execution cleanup deadline grace', () => {
318339
}))
319340
queueTableRows(workflowExecutionLogs, firstBatch)
320341
queueTableRows(workflowExecutionLogs, failedBatch)
342+
queueTableRows(asyncJobs, [{ id: 'async-job-1' }])
321343
dbChainMockFns.returning
322344
.mockResolvedValueOnce(firstBatch)
323345
.mockRejectedValueOnce(new Error('database unavailable'))
346+
.mockResolvedValueOnce([{ id: 'async-job-1' }])
324347

325348
const response = await GET(createRequest())
326349

0 commit comments

Comments
 (0)