|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { callActorGetDataset } from '../../src/tools/core/actor_execution.js'; |
| 4 | + |
| 5 | +describe('callActorGetDataset', () => { |
| 6 | + it('should return null without starting a run when the signal is already aborted', async () => { |
| 7 | + const controller = new AbortController(); |
| 8 | + controller.abort(); |
| 9 | + |
| 10 | + const start = vi.fn(); |
| 11 | + |
| 12 | + const apifyClient = { |
| 13 | + actor: vi.fn().mockReturnValue({ start }), |
| 14 | + }; |
| 15 | + |
| 16 | + const result = await callActorGetDataset({ |
| 17 | + actorName: 'apify/rag-web-browser', |
| 18 | + input: { query: 'https://apify.com' }, |
| 19 | + apifyClient: apifyClient as never, |
| 20 | + abortSignal: controller.signal, |
| 21 | + }); |
| 22 | + |
| 23 | + expect(result).toBeNull(); |
| 24 | + expect(start).not.toHaveBeenCalled(); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should abort and return null when the signal is aborted during actor start', async () => { |
| 28 | + const controller = new AbortController(); |
| 29 | + const actorRun = { |
| 30 | + id: 'run-123', |
| 31 | + defaultDatasetId: 'dataset-123', |
| 32 | + usageTotalUsd: 0, |
| 33 | + usageUsd: {}, |
| 34 | + }; |
| 35 | + |
| 36 | + const abort = vi.fn().mockResolvedValue(undefined); |
| 37 | + const waitForFinish = vi.fn().mockResolvedValue(actorRun); |
| 38 | + const start = vi.fn().mockImplementation(async () => { |
| 39 | + controller.abort(); |
| 40 | + return actorRun; |
| 41 | + }); |
| 42 | + |
| 43 | + const apifyClient = { |
| 44 | + actor: vi.fn().mockReturnValue({ |
| 45 | + start, |
| 46 | + }), |
| 47 | + run: vi.fn().mockReturnValue({ |
| 48 | + abort, |
| 49 | + waitForFinish, |
| 50 | + }), |
| 51 | + }; |
| 52 | + |
| 53 | + const result = await callActorGetDataset({ |
| 54 | + actorName: 'apify/rag-web-browser', |
| 55 | + input: { query: 'https://apify.com' }, |
| 56 | + apifyClient: apifyClient as never, |
| 57 | + abortSignal: controller.signal, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(result).toBeNull(); |
| 61 | + expect(abort).toHaveBeenCalledWith({ gracefully: false }); |
| 62 | + expect(waitForFinish).not.toHaveBeenCalled(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return null immediately when cancelled after start even if API abort is slow', async () => { |
| 66 | + const controller = new AbortController(); |
| 67 | + const actorRun = { |
| 68 | + id: 'run-456', |
| 69 | + defaultDatasetId: 'dataset-456', |
| 70 | + usageTotalUsd: 0, |
| 71 | + usageUsd: {}, |
| 72 | + }; |
| 73 | + |
| 74 | + let resolveAbort: (() => void) | undefined; |
| 75 | + let resolveWaitForFinish: ((value: typeof actorRun) => void) | undefined; |
| 76 | + const abort = vi.fn().mockImplementation(async () => await new Promise<void>((resolve) => { |
| 77 | + resolveAbort = resolve; |
| 78 | + })); |
| 79 | + const waitForFinish = vi.fn().mockImplementation(async () => await new Promise<typeof actorRun>((resolve) => { |
| 80 | + resolveWaitForFinish = resolve; |
| 81 | + })); |
| 82 | + const start = vi.fn().mockResolvedValue(actorRun); |
| 83 | + const listItems = vi.fn().mockResolvedValue({ items: [], total: 0 }); |
| 84 | + |
| 85 | + const apifyClient = { |
| 86 | + actor: vi.fn().mockReturnValue({ start }), |
| 87 | + run: vi.fn().mockReturnValue({ |
| 88 | + abort, |
| 89 | + waitForFinish, |
| 90 | + }), |
| 91 | + dataset: vi.fn().mockReturnValue({ listItems }), |
| 92 | + }; |
| 93 | + |
| 94 | + const resultPromise = callActorGetDataset({ |
| 95 | + actorName: 'apify/rag-web-browser', |
| 96 | + input: { query: 'https://apify.com' }, |
| 97 | + apifyClient: apifyClient as never, |
| 98 | + abortSignal: controller.signal, |
| 99 | + }); |
| 100 | + |
| 101 | + await vi.waitUntil(() => Boolean(resolveWaitForFinish)); |
| 102 | + controller.abort(); |
| 103 | + resolveWaitForFinish!(actorRun); |
| 104 | + |
| 105 | + const result = await resultPromise; |
| 106 | + |
| 107 | + expect(result).toBeNull(); |
| 108 | + await vi.waitUntil(() => abort.mock.calls.length > 0); |
| 109 | + expect(abort).toHaveBeenCalledWith({ gracefully: false }); |
| 110 | + expect(listItems).not.toHaveBeenCalled(); |
| 111 | + resolveAbort!(); |
| 112 | + }); |
| 113 | +}); |
0 commit comments