Skip to content

Commit 0065f39

Browse files
committed
chore(tests): make asynchronous validation deterministic
1 parent 3b93f75 commit 0065f39

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

‎apps/sim/lib/file-parsers/pdf-parser-cancellation.test.ts‎

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -176,32 +176,41 @@ describe('PdfParser cancellation', () => {
176176
})
177177

178178
it('shares one event-loop slice across concurrent parses instead of giving each its own', async () => {
179+
// Isolate the shared scheduler so the simulated clock cannot affect later parses.
180+
vi.resetModules()
181+
const { PdfParser: ConcurrentPdfParser } = await import('@/lib/file-parsers/pdf-parser')
182+
let simulatedTime = 0
183+
let workSinceProbe = 0
184+
let longestSlice = 0
185+
const clock = vi.spyOn(performance, 'now').mockImplementation(() => simulatedTime)
179186
const parseCount = 8
180187
for (let i = 0; i < parseCount; i++) {
181188
const { pdf } = pdfWithPageText(20, () => {
182-
const busyUntil = performance.now() + 2
183-
while (performance.now() < busyUntil) {}
189+
simulatedTime += 2
190+
workSinceProbe += 2
191+
longestSlice = Math.max(longestSlice, workSinceProbe)
184192
return 'page text'
185193
})
186194
mockOpenPdfDocument.mockResolvedValueOnce(pdf)
187195
}
188-
let longestTimerWait = 0
189-
let probing = true
190-
let lastTick = performance.now()
196+
let probeHandle: ReturnType<typeof setImmediate>
191197
const probe = () => {
192-
const now = performance.now()
193-
longestTimerWait = Math.max(longestTimerWait, now - lastTick)
194-
lastTick = now
195-
if (probing) setTimeout(probe, 0)
198+
workSinceProbe = 0
199+
probeHandle = setImmediate(probe)
200+
}
201+
probeHandle = setImmediate(probe)
202+
try {
203+
await Promise.all(
204+
Array.from({ length: parseCount }, () =>
205+
new ConcurrentPdfParser().parseBuffer(Buffer.from('%PDF-1.4'))
206+
)
207+
)
208+
expect(simulatedTime).toBe(parseCount * 20 * 2)
209+
expect(longestSlice).toBeLessThanOrEqual(12)
210+
} finally {
211+
clearImmediate(probeHandle)
212+
clock.mockRestore()
196213
}
197-
setTimeout(probe, 0)
198-
199-
await Promise.all(
200-
Array.from({ length: parseCount }, () => new PdfParser().parseBuffer(Buffer.from('%PDF-1.4')))
201-
)
202-
probing = false
203-
204-
expect(longestTimerWait).toBeLessThan(40)
205214
})
206215

207216
it('lets timers run while one page streams a huge content stream', async () => {

‎apps/sim/scripts/test-assistant-actions-e2e.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,15 @@ try {
238238
const approvalResponse = await approval
239239
assert.equal(approvalResponse.status(), 200, await approvalResponse.text())
240240
await expect(modal.getByRole('textbox', { name: 'Summary', exact: true })).toHaveCount(1)
241+
const cancellation = page.waitForResponse(
242+
(response) =>
243+
response.request().method() === 'PATCH' &&
244+
response.url().endsWith(`/actions/${secondCallId}`),
245+
{ timeout: 180_000 }
246+
)
241247
await modal.getByRole('button', { name: 'Cancel', exact: true }).click()
248+
const cancellationResponse = await cancellation
249+
assert.equal(cancellationResponse.status(), 200, await cancellationResponse.text())
242250
await expect(modal.getByText('No actions are waiting for review.')).toBeVisible()
243251
const rows =
244252
await sql`select tool_call_id, permission_decision, args, approved_args, execution_started_at from copilot_async_tool_calls where run_id = ${runId} order by persist_seq`

0 commit comments

Comments
 (0)