|
9 | 9 | mothershipClientToolWaiterMockFns, |
10 | 10 | } from '@sim/testing/mocks/mothership-client-tool-waiter.mock' |
11 | 11 | import { sleep } from '@sim/utils/helpers' |
| 12 | +import { toRecord } from '@sim/utils/object' |
12 | 13 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
13 | 14 | import { AsyncToolCallOwnershipError } from '@/lib/mothership/async-runs/errors' |
14 | 15 | import { restoreStreamingContext } from '@/lib/mothership/request/context/restore' |
@@ -1363,6 +1364,79 @@ describe('sse-handlers tool lifecycle', () => { |
1363 | 1364 | expect(workflowResults[0].payload.executor).toBe(MothershipStreamV1ToolExecutor.sim) |
1364 | 1365 | }) |
1365 | 1366 |
|
| 1367 | + describe('workflow tool pickup grace', () => { |
| 1368 | + /** Dispatches a workflow call whose browser wait only ends when the server takes it over. */ |
| 1369 | + async function dispatchUnpickedWorkflowCall(toolCallId: string, hasClientViewer: boolean) { |
| 1370 | + waitForWorkflowToolCompletion.mockImplementationOnce((params: unknown) => { |
| 1371 | + const abortSignal = toRecord(params).abortSignal |
| 1372 | + return new Promise((resolve) => { |
| 1373 | + if (abortSignal instanceof AbortSignal) { |
| 1374 | + abortSignal.addEventListener('abort', () => resolve(null)) |
| 1375 | + } |
| 1376 | + }) |
| 1377 | + }) |
| 1378 | + claimWorkflowToolExecution.mockResolvedValueOnce({ toolCallId }) |
| 1379 | + executeTool.mockResolvedValueOnce({ success: true, output: {} }) |
| 1380 | + await sseHandlers.tool( |
| 1381 | + { |
| 1382 | + type: MothershipStreamV1EventType.tool, |
| 1383 | + payload: { |
| 1384 | + toolCallId, |
| 1385 | + toolName: 'run_workflow', |
| 1386 | + arguments: { workflowId: 'workflow-1' }, |
| 1387 | + executor: MothershipStreamV1ToolExecutor.client, |
| 1388 | + mode: MothershipStreamV1ToolMode.async, |
| 1389 | + phase: MothershipStreamV1ToolPhase.call, |
| 1390 | + }, |
| 1391 | + } satisfies StreamEvent, |
| 1392 | + context, |
| 1393 | + execContext, |
| 1394 | + { interactive: true, hasClientViewer: async () => hasClientViewer } |
| 1395 | + ) |
| 1396 | + } |
| 1397 | + |
| 1398 | + it('runs a call no chat view is reading on the server after the short grace', async () => { |
| 1399 | + vi.useFakeTimers() |
| 1400 | + try { |
| 1401 | + await dispatchUnpickedWorkflowCall('tool-no-viewer', false) |
| 1402 | + await vi.advanceTimersByTimeAsync(5_000) |
| 1403 | + expect(claimWorkflowToolExecution).toHaveBeenCalledWith( |
| 1404 | + 'tool-no-viewer', |
| 1405 | + expect.any(String), |
| 1406 | + 'sim' |
| 1407 | + ) |
| 1408 | + |
| 1409 | + await Promise.allSettled(context.pendingToolPromises.values()) |
| 1410 | + expect(executeTool.mock.calls.at(-1)?.[0]).toBe('run_workflow') |
| 1411 | + } finally { |
| 1412 | + vi.useRealTimers() |
| 1413 | + } |
| 1414 | + }) |
| 1415 | + |
| 1416 | + it('keeps waiting the full grace for a call a chat view is reading', async () => { |
| 1417 | + vi.useFakeTimers() |
| 1418 | + try { |
| 1419 | + await dispatchUnpickedWorkflowCall('tool-viewer', true) |
| 1420 | + await vi.advanceTimersByTimeAsync(29_000) |
| 1421 | + expect(claimWorkflowToolExecution).not.toHaveBeenCalledWith( |
| 1422 | + 'tool-viewer', |
| 1423 | + expect.any(String), |
| 1424 | + 'sim' |
| 1425 | + ) |
| 1426 | + |
| 1427 | + await vi.advanceTimersByTimeAsync(1_000) |
| 1428 | + await Promise.allSettled(context.pendingToolPromises.values()) |
| 1429 | + expect(claimWorkflowToolExecution).toHaveBeenCalledWith( |
| 1430 | + 'tool-viewer', |
| 1431 | + expect.any(String), |
| 1432 | + 'sim' |
| 1433 | + ) |
| 1434 | + } finally { |
| 1435 | + vi.useRealTimers() |
| 1436 | + } |
| 1437 | + }) |
| 1438 | + }) |
| 1439 | + |
1366 | 1440 | it('claims and runs the same workflow when the call omits an explicit workflowId', async () => { |
1367 | 1441 | // The waiter resolves the target via resolveWorkflowToolTargetId(args, ctx) |
1368 | 1442 | // while the handler resolves it as params.workflowId || context.workflowId. |
|
0 commit comments