Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/__tests__/commands/batch-scrape.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import {
collectBatchScrapeUrls,
executeBatchScrape,
handleBatchScrapeCommand,
} from '../../commands/batch-scrape';
import { getClient } from '../../utils/client';
import { initializeConfig } from '../../utils/config';
Expand Down Expand Up @@ -441,3 +442,93 @@ describe('executeBatchScrape', () => {
});
});
});

describe('handleBatchScrapeCommand exit status', () => {
let mockClient: any;

beforeEach(() => {
setupTest();
initializeConfig({
apiKey: 'test-api-key',
apiUrl: 'https://api.firecrawl.dev',
});
mockClient = {
startBatchScrape: vi.fn(),
getBatchScrapeStatus: vi.fn(),
batchScrape: vi.fn(),
cancelBatchScrape: vi.fn(),
};
vi.mocked(getClient).mockReturnValue(mockClient as any);
});

afterEach(() => {
process.exitCode = undefined;
teardownTest();
vi.restoreAllMocks();
vi.clearAllMocks();
});

for (const status of ['failed', 'cancelled']) {
it(`should print the job and exit 1 when a waited batch ends ${status}`, async () => {
const stdout = vi
.spyOn(process.stdout, 'write')
.mockImplementation(() => true);
const stderr = vi.spyOn(console, 'error').mockImplementation(() => {});
mockClient.batchScrape.mockResolvedValue({
id: JOB_ID,
status,
total: 2,
completed: 0,
data: [],
});

await handleBatchScrapeCommand({ urls: URLS, wait: true });

expect(
stdout.mock.calls.map(([chunk]) => String(chunk)).join('')
).toContain(`"status":"${status}"`);
expect(stderr).toHaveBeenCalledWith(`Error: Batch scrape ${status}.`);
expect(process.exitCode).toBe(1);
});
}

it('should leave the exit status alone when a waited batch completes', async () => {
vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
mockClient.batchScrape.mockResolvedValue({
id: JOB_ID,
status: 'completed',
total: 2,
completed: 2,
data: [],
});

await handleBatchScrapeCommand({ urls: URLS, wait: true });

expect(process.exitCode).toBeUndefined();
});

it('should leave the exit status alone when a job is cancelled with --cancel --wait', async () => {
vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
mockClient.cancelBatchScrape.mockResolvedValue(true);

await handleBatchScrapeCommand({ jobId: JOB_ID, cancel: true, wait: true });

expect(mockClient.cancelBatchScrape).toHaveBeenCalledWith(JOB_ID);
expect(process.exitCode).toBeUndefined();
});

it('should leave the exit status alone when reading a failed job without --wait', async () => {
vi.spyOn(process.stdout, 'write').mockImplementation(() => true);
mockClient.getBatchScrapeStatus.mockResolvedValue({
id: JOB_ID,
status: 'failed',
total: 2,
completed: 0,
data: [],
});

await handleBatchScrapeCommand({ jobId: JOB_ID });

expect(process.exitCode).toBeUndefined();
});
});
10 changes: 10 additions & 0 deletions src/commands/batch-scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,4 +246,14 @@ export async function handleBatchScrapeCommand(
`Check status: firecrawl batch-scrape ${result.data.jobId}\n`
);
}

const finalStatus = !started && result.data ? result.data.status : undefined;

@cubic-dev-ai cubic-dev-ai Bot Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This also marks failed or cancelled job-ID status reads as failures when --wait is supplied, even though executeBatchScrape does not wait for job IDs and the status-read path is supposed to remain unchanged. Exclude options.jobId from this final-status check.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/commands/batch-scrape.ts, line 250:

<comment>This also marks failed or cancelled job-ID status reads as failures when `--wait` is supplied, even though `executeBatchScrape` does not wait for job IDs and the status-read path is supposed to remain unchanged. Exclude `options.jobId` from this final-status check.</comment>

<file context>
@@ -246,4 +246,14 @@ export async function handleBatchScrapeCommand(
     );
   }
+
+  const finalStatus = !started && result.data ? result.data.status : undefined;
+  if (
+    options.wait &&
</file context>
Suggested change
const finalStatus = !started && result.data ? result.data.status : undefined;
const finalStatus =
!started && !options.jobId && result.data ? result.data.status : undefined;
Fix with cubic

if (
options.wait &&
!options.cancel &&
(finalStatus === 'failed' || finalStatus === 'cancelled')
) {
console.error(`Error: Batch scrape ${finalStatus}.`);
process.exitCode = 1;
}
}