Skip to content

Commit 96afe2d

Browse files
committed
fix(integrations): fail cancelled polls cleanly and test at the HTTP boundary
1 parent 758e69e commit 96afe2d

13 files changed

Lines changed: 191 additions & 161 deletions

File tree

‎apps/docs/content/docs/integrations/firecrawl.mdx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Convert web pages to Markdown or HTML, including SEO metadata, Open Graph tags,
3838
| Parameter | Type | Description |
3939
| --------- | ---- | ----------- |
4040
| `markdown` | string | Page content in markdown format |
41-
| `html` | string | Raw HTML content of the page |
41+
| `html` | string | Processed HTML content of the page |
4242
| `rawHtml` | string | Unmodified HTML of the page, when the rawHtml format is requested |
4343
| `summary` | string | Summary of the page, when the summary format is requested |
4444
| `links` | array | Links found on the page, when the links format is requested |

‎apps/sim/lib/internal/browser-use/operations/run-task.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,15 @@ function readBrowserUseJson(response: Response, signal?: AbortSignal): Promise<u
115115
})
116116
}
117117

118-
function readBrowserUseErrorText(response: Response): Promise<string> {
118+
function readBrowserUseErrorText(response: Response, signal?: AbortSignal): Promise<string> {
119119
return readResponseTextWithLimit(response, {
120120
maxBytes: DEFAULT_MAX_ERROR_BODY_BYTES,
121121
label: 'Browser Use error response',
122-
}).catch(() => response.statusText)
122+
signal,
123+
}).catch(() => {
124+
signal?.throwIfAborted()
125+
return response.statusText
126+
})
123127
}
124128

125129
async function waitForNextPoll(signal?: AbortSignal): Promise<void> {
@@ -156,7 +160,7 @@ async function createSessionWithProfile(
156160
})
157161

158162
if (!response.ok) {
159-
const errorText = await readBrowserUseErrorText(response)
163+
const errorText = await readBrowserUseErrorText(response, signal)
160164
logger.error(`Failed to create session with profile: ${errorText}`)
161165
return { error: `Failed to create session with profile: ${response.statusText}` }
162166
}
@@ -533,7 +537,7 @@ export const executeRunTaskOperation: InternalToolOperationImplementation<
533537
})
534538

535539
if (!response.ok) {
536-
const errorText = await readBrowserUseErrorText(response)
540+
const errorText = await readBrowserUseErrorText(response, signal)
537541
logger.error(`Failed to create task: ${errorText}`)
538542
return {
539543
success: false,

‎apps/sim/lib/internal/extend/operations.test.ts‎

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import { jsonResponse } from '@sim/testing'
2+
import {
3+
fileUtilsServerMock,
4+
fileUtilsServerMockFns,
5+
} from '@sim/testing/mocks/file-utils-server.mock'
6+
import {
7+
inputValidationMock,
8+
inputValidationMockFns,
9+
} from '@sim/testing/mocks/input-validation.mock'
110
import { beforeEach, describe, expect, it, vi } from 'vitest'
211

3-
const { mockResolveFileInputToUrl, mockSubmitExtendParse } = vi.hoisted(() => ({
4-
mockResolveFileInputToUrl: vi.fn(),
5-
mockSubmitExtendParse: vi.fn(),
6-
}))
12+
vi.mock('@/lib/uploads/utils/file.server', () => fileUtilsServerMock)
13+
vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)
714

8-
vi.mock('@/lib/uploads/utils/file.server', () => ({
9-
resolveFileInputToUrl: mockResolveFileInputToUrl,
10-
}))
11-
vi.mock('@/lib/internal/extend/client', () => ({
12-
submitExtendParse: mockSubmitExtendParse,
13-
}))
15+
const { mockResolveFileInputToUrl } = fileUtilsServerMockFns
16+
const { mockSecureFetchWithPinnedIP, mockValidateUrlWithDNS } = inputValidationMockFns
1417

1518
import { ExtendOperationError } from '@/lib/internal/extend/errors'
1619
import { executeExtendParse } from '@/lib/internal/extend/operations'
@@ -21,16 +24,19 @@ const input = { apiKey: 'key', filePath: 'https://files.example.com/doc.pdf' }
2124
describe('executeExtendParse', () => {
2225
beforeEach(() => {
2326
mockResolveFileInputToUrl.mockResolvedValue({ fileUrl: 'https://files.example.com/doc.pdf' })
27+
mockValidateUrlWithDNS.mockResolvedValue({ isValid: true, resolvedIP: '203.0.113.1' })
2428
})
2529

2630
it('reads the page count and credits from the parser run metrics and usage', async () => {
27-
mockSubmitExtendParse.mockResolvedValue({
28-
id: 'parser_run_1',
29-
status: 'PROCESSED',
30-
chunks: [],
31-
metrics: { processingTimeMs: 900, pageCount: 4 },
32-
usage: { credits: 8 },
33-
})
31+
mockSecureFetchWithPinnedIP.mockResolvedValue(
32+
jsonResponse({
33+
id: 'parser_run_1',
34+
status: 'PROCESSED',
35+
chunks: [],
36+
metrics: { processingTimeMs: 900, pageCount: 4 },
37+
usage: { credits: 8 },
38+
})
39+
)
3440

3541
const result = await executeExtendParse(input, context)
3642

@@ -39,12 +45,14 @@ describe('executeExtendParse', () => {
3945
})
4046

4147
it('fails with the failure reason when Extend could not parse the file', async () => {
42-
mockSubmitExtendParse.mockResolvedValue({
43-
id: 'parser_run_1',
44-
status: 'FAILED',
45-
failureReason: 'CORRUPT_FILE',
46-
chunks: [],
47-
})
48+
mockSecureFetchWithPinnedIP.mockResolvedValue(
49+
jsonResponse({
50+
id: 'parser_run_1',
51+
status: 'FAILED',
52+
failureReason: 'CORRUPT_FILE',
53+
chunks: [],
54+
})
55+
)
4856

4957
await expect(executeExtendParse(input, context)).rejects.toEqual(
5058
new ExtendOperationError(422, {

‎apps/sim/lib/internal/pulse/client.test.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { jsonResponse } from '@sim/testing'
12
import {
23
inputValidationMock,
34
inputValidationMockFns,
@@ -10,10 +11,6 @@ const { mockSecureFetchWithPinnedIP, mockValidateUrlWithDNS } = inputValidationM
1011

1112
import { submitPulseParse } from '@/lib/internal/pulse/client'
1213

13-
function jsonResponse(body: unknown) {
14-
return { ok: true, status: 200, statusText: 'OK', json: async () => body }
15-
}
16-
1714
function requestTo(url: string) {
1815
const call = mockSecureFetchWithPinnedIP.mock.calls.find(([target]) => target === url)
1916
if (!call) throw new Error(`No request to ${url}`)
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1+
import { jsonResponse } from '@sim/testing'
2+
import {
3+
fileUtilsServerMock,
4+
fileUtilsServerMockFns,
5+
} from '@sim/testing/mocks/file-utils-server.mock'
6+
import {
7+
inputValidationMock,
8+
inputValidationMockFns,
9+
} from '@sim/testing/mocks/input-validation.mock'
110
import { beforeEach, describe, expect, it, vi } from 'vitest'
211

3-
const { mockResolveFileInputToUrl, mockSubmitReductoParse } = vi.hoisted(() => ({
4-
mockResolveFileInputToUrl: vi.fn(),
5-
mockSubmitReductoParse: vi.fn(),
6-
}))
12+
vi.mock('@/lib/uploads/utils/file.server', () => fileUtilsServerMock)
13+
vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)
714

8-
vi.mock('@/lib/uploads/utils/file.server', () => ({
9-
resolveFileInputToUrl: mockResolveFileInputToUrl,
10-
}))
11-
vi.mock('@/lib/internal/reducto/client', () => ({
12-
submitReductoParse: mockSubmitReductoParse,
13-
}))
15+
const { mockResolveFileInputToUrl } = fileUtilsServerMockFns
16+
const { mockSecureFetchWithPinnedIP, mockValidateUrlWithDNS } = inputValidationMockFns
1417

1518
import { executeReductoParse } from '@/lib/internal/reducto/operations'
1619

@@ -19,7 +22,8 @@ const context = { headers: new Headers(), requestId: 'req-1', userId: 'user-1' }
1922
describe('executeReductoParse', () => {
2023
beforeEach(() => {
2124
mockResolveFileInputToUrl.mockResolvedValue({ fileUrl: 'https://files.example.com/doc.pdf' })
22-
mockSubmitReductoParse.mockResolvedValue({ job_id: 'job-1' })
25+
mockValidateUrlWithDNS.mockResolvedValue({ isValid: true, resolvedIP: '203.0.113.1' })
26+
mockSecureFetchWithPinnedIP.mockResolvedValue(jsonResponse({ job_id: 'job-1' }))
2327
})
2428

2529
it('parses only the requested pages, not every page between them', async () => {
@@ -28,10 +32,7 @@ describe('executeReductoParse', () => {
2832
context
2933
)
3034

31-
expect(mockSubmitReductoParse).toHaveBeenCalledWith(
32-
'key',
33-
expect.objectContaining({ settings: { page_range: [2, 9] } }),
34-
undefined
35-
)
35+
const [, , request] = mockSecureFetchWithPinnedIP.mock.calls[0]
36+
expect(JSON.parse(request.body).settings).toEqual({ page_range: [2, 9] })
3637
})
3738
})

0 commit comments

Comments
 (0)