Skip to content

Commit e61feb1

Browse files
committed
feat(api): add throwIfAborted helper and completePrompt options regression tests
Add a fast-fail throwIfAborted guard to the shared abort-signal utilities and regression tests for the CompletePromptOptions interface (added by #901).
1 parent 252c69b commit e61feb1

3 files changed

Lines changed: 74 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, it, expect } from "vitest"
2+
3+
import type { CompletePromptOptions } from "../../index"
4+
5+
describe("CompletePromptOptions", () => {
6+
it("should allow abortSignal property", () => {
7+
const controller = new AbortController()
8+
const options: CompletePromptOptions = { abortSignal: controller.signal }
9+
expect(options.abortSignal).toBe(controller.signal)
10+
})
11+
12+
it("should allow timeoutMs property", () => {
13+
const options: CompletePromptOptions = { timeoutMs: 5000 }
14+
expect(options.timeoutMs).toBe(5000)
15+
})
16+
17+
it("should allow both abortSignal and timeoutMs together", () => {
18+
const controller = new AbortController()
19+
const options: CompletePromptOptions = { abortSignal: controller.signal, timeoutMs: 10000 }
20+
expect(options.abortSignal).toBe(controller.signal)
21+
expect(options.timeoutMs).toBe(10000)
22+
})
23+
24+
it("should allow empty options object", () => {
25+
const options: CompletePromptOptions = {}
26+
expect(options.abortSignal).toBeUndefined()
27+
expect(options.timeoutMs).toBeUndefined()
28+
})
29+
})

‎src/api/providers/utils/__tests__/abort-signal.spec.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mergeAbortSignalAndTimeout, mergeAbortSignals } from "../abort-signal"
1+
import { mergeAbortSignalAndTimeout, mergeAbortSignals, throwIfAborted } from "../abort-signal"
22

33
describe("abort-signal utilities", () => {
44
describe("mergeAbortSignalAndTimeout", () => {
@@ -99,4 +99,31 @@ describe("abort-signal utilities", () => {
9999
expect(result.aborted).toBe(true)
100100
})
101101
})
102+
103+
describe("throwIfAborted", () => {
104+
it("does not throw when signal is undefined", () => {
105+
expect(() => throwIfAborted()).not.toThrow()
106+
})
107+
108+
it("does not throw when signal is not aborted", () => {
109+
const controller = new AbortController()
110+
111+
expect(() => throwIfAborted(controller.signal)).not.toThrow()
112+
})
113+
114+
it("throws an AbortError when signal is already aborted", () => {
115+
const controller = new AbortController()
116+
controller.abort()
117+
118+
let caught: unknown
119+
try {
120+
throwIfAborted(controller.signal)
121+
} catch (error) {
122+
caught = error
123+
}
124+
125+
expect(caught).toBeInstanceOf(Error)
126+
expect((caught as Error).name).toBe("AbortError")
127+
})
128+
})
102129
})

‎src/api/providers/utils/abort-signal.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ export function mergeAbortSignals(primarySignal: AbortSignal, secondarySignal?:
3535

3636
return AbortSignal.any([primarySignal, secondarySignal])
3737
}
38+
39+
/**
40+
* Throw an AbortError if the given signal is already aborted.
41+
*
42+
* Use as a fast-fail guard at the top of request-building code paths so
43+
* callers receive a consistent `name === "AbortError"` when the operation
44+
* was cancelled before it started, without building or issuing the request.
45+
*/
46+
export function throwIfAborted(signal?: AbortSignal): void {
47+
if (!signal?.aborted) {
48+
return
49+
}
50+
51+
const abortError = new Error("This operation was aborted")
52+
abortError.name = "AbortError"
53+
throw abortError
54+
}

0 commit comments

Comments
 (0)