Skip to content

Commit 7ad2959

Browse files
committed
test(poe): observe cancellation while the generation is still in flight
1 parent 7b7f795 commit 7ad2959

1 file changed

Lines changed: 38 additions & 15 deletions

File tree

‎src/api/providers/__tests__/poe.spec.ts‎

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -401,32 +401,55 @@ describe("PoeHandler", () => {
401401
})
402402
})
403403

404+
// Holds generateText pending so a test can observe the request signal while it is in
405+
// flight; `release` settles the call.
406+
function holdGeneration(generateTextMock: {
407+
mockImplementation: (fn: (options: { abortSignal?: AbortSignal }) => Promise<{ text: string }>) => unknown
408+
}) {
409+
let captured: { abortSignal?: AbortSignal } | undefined
410+
let release: () => void = () => {}
411+
generateTextMock.mockImplementation(
412+
(options) =>
413+
new Promise<{ text: string }>((resolve) => {
414+
captured = options
415+
release = () => resolve({ text: "generated response" })
416+
}),
417+
)
418+
return { release: () => release(), abortSignal: () => captured?.abortSignal }
419+
}
420+
404421
describe("completePrompt", () => {
405-
it("links the caller's stop into the merged generation signal", async () => {
422+
it("links the caller's stop into the in-flight generation signal", async () => {
406423
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
407-
mockGenerateText.mockResolvedValue({ text: "generated response" })
424+
const { release, abortSignal } = holdGeneration(mockGenerateText)
408425
const abortController = new AbortController()
409426

410-
await handler.completePrompt("complete this", { abortSignal: abortController.signal, timeoutMs: 5_000 })
411-
412-
const mergedSignal = mockGenerateText.mock.calls[0][0].abortSignal
413-
// A per-request signal carries both the caller's stop and the deadline.
414-
expect(mergedSignal).not.toBe(abortController.signal)
415-
expect(mergedSignal?.aborted).toBe(false)
427+
const generation = handler.completePrompt("complete this", {
428+
abortSignal: abortController.signal,
429+
timeoutMs: 5_000,
430+
})
431+
await vi.waitFor(() => expect(abortSignal()).toBeDefined())
432+
// A per-request signal carries the caller's stop while the request is still running.
433+
expect(abortSignal()?.aborted).toBe(false)
416434

417435
abortController.abort()
418-
expect(mergedSignal?.aborted).toBe(true)
436+
expect(abortSignal()?.aborted).toBe(true)
437+
// The generation only settles when the provider call is released.
438+
release()
439+
await expect(generation).resolves.toBe("generated response")
419440
})
420441

421-
it("aborts the merged generation signal when the deadline passes", async () => {
442+
it("aborts the in-flight generation signal when the deadline passes", async () => {
422443
const handler = new PoeHandler({ poeApiKey: "key", apiModelId: "openai/gpt-4o" })
423-
mockGenerateText.mockResolvedValue({ text: "generated response" })
444+
const { release, abortSignal } = holdGeneration(mockGenerateText)
424445

425-
await handler.completePrompt("complete this", { timeoutMs: 200 })
446+
const generation = handler.completePrompt("complete this", { timeoutMs: 200 })
447+
await vi.waitFor(() => expect(abortSignal()).toBeDefined())
426448

427-
const mergedSignal = mockGenerateText.mock.calls[0][0].abortSignal
428-
expect(mergedSignal?.aborted).toBe(false)
429-
await vi.waitFor(() => expect(mergedSignal?.aborted).toBe(true))
449+
// The deadline aborts the request while the provider call is still pending.
450+
await vi.waitFor(() => expect(abortSignal()?.aborted).toBe(true))
451+
release()
452+
await expect(generation).resolves.toBe("generated response")
430453
})
431454

432455
it("does not start a generation when the caller already aborted", async () => {

0 commit comments

Comments
 (0)