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
345 changes: 343 additions & 2 deletions src/api/providers/__tests__/kenari.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// npx vitest run src/api/providers/__tests__/kenari.spec.ts

// Mock vscode first to avoid import errors
vitest.mock("vscode", () => ({
workspace: {
Expand All @@ -17,6 +16,7 @@ import { kenariDefaultModelId } from "@roo-code/types"
import { KenariHandler } from "../kenari"
import { getModels } from "../fetchers/modelCache"
import { ApiHandlerOptions } from "../../../shared/api"
import { makeCreateMessageMetadata } from "../../../test-utils/api"
import { asyncStreamFrom, collectStream } from "../../../test-utils/stream"
import { clearAllMocks } from "../../../test-utils/reset"

Expand All @@ -41,7 +41,10 @@ vitest.mock("../fetchers/modelCache", () => ({
getModelsFromCache: vitest.fn().mockReturnValue(undefined),
}))

const mockCreate = vitest.fn()
// Typed to the SDK call shape so the callbacks and the asserted options are checked; the
// return stays loose because the fixtures are deliberate partial doubles.
const mockCreate =
vitest.fn<(body: OpenAI.Chat.Completions.ChatCompletionCreateParams, options?: OpenAI.RequestOptions) => unknown>()

;(OpenAI as any).mockImplementation(function () {
return {
Expand Down Expand Up @@ -343,6 +346,12 @@ describe("KenariHandler", () => {
mockCreate.mockRejectedValue(new Error("boom"))
const handler = new KenariHandler(mockOptions)
await expect(handler.completePrompt("ping")).rejects.toThrow("Kenari completion error: boom")

mockCreate.mockResolvedValue({})
await expect(handler.completePrompt("ping")).rejects.toThrow("Kenari completion error:")
const live = new AbortController() // a non-Error rejection passes through even with a live signal
mockCreate.mockRejectedValue("string failure")
await expect(handler.completePrompt("ping", { abortSignal: live.signal })).rejects.toBe("string failure")
})

it("re-throws non-Error rejections unchanged", async () => {
Expand Down Expand Up @@ -372,6 +381,338 @@ describe("KenariHandler", () => {
})
})

describe("cancellation", () => {
const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }]
const aborted = (provider: string) => ({ name: "AbortError", message: `The ${provider} request was aborted` })

describe("createMessage", () => {
const streamWith = (handler: KenariHandler, abortSignal?: AbortSignal) =>
handler.createMessage(
"sys",
messages,
abortSignal ? makeCreateMessageMetadata({ abortSignal }) : undefined,
)

const okStream = () => asyncStreamFrom([{ choices: [{ delta: { content: "ok" }, index: 0 }], usage: null }])
function yieldsThenThrows(failure: Error) {
return (async function* () {
yield { choices: [{ delta: { content: "Hello" }, index: 0 }], usage: null }
throw failure
})()
}

it("forwards the caller abort signal to the SDK request options", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
let requestOptions: OpenAI.RequestOptions | undefined
mockCreate.mockImplementation((_body, options) => {
requestOptions = options
return Promise.resolve(okStream()) // the real SDK returns a thenable, which the race awaits
})

const chunks = await collectStream(streamWith(handler, caller.signal))
expect(requestOptions?.signal).toBeInstanceOf(AbortSignal)
expect(requestOptions?.signal).not.toBe(caller.signal)
expect(chunks).toEqual([{ type: "text", text: "ok" }])
})

it("detaches the SDK abort listener the stream request attached", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
const added = vitest.spyOn(caller.signal, "addEventListener")
const removed = vitest.spyOn(caller.signal, "removeEventListener")
mockCreate.mockResolvedValue(okStream())
const stream = streamWith(handler, caller.signal)

expect(await collectStream(stream)).toEqual([{ type: "text", text: "ok" }])
const attached = added.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)
expect(attached).toHaveLength(1)
expect(removed.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)).toEqual(attached)
})

it("rejects a stream that never starts once the caller cancels", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
mockCreate.mockImplementation(() => new Promise(() => {}))
const pending = collectStream(streamWith(handler, caller.signal))
await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled())
caller.abort()

await expect(pending).rejects.toMatchObject(aborted("Kenari"))
})

it("detaches the bridge when the catalog rejects with the caller still live", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
const added = vitest.spyOn(caller.signal, "addEventListener")
const removed = vitest.spyOn(caller.signal, "removeEventListener")
const { getModels } = await import("../fetchers/modelCache")
vitest.mocked(getModels).mockRejectedValueOnce(new Error("catalog unavailable"))

await expect(collectStream(streamWith(handler, caller.signal))).rejects.toThrow("catalog unavailable")

const attached = added.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)
expect(attached).toHaveLength(1)
expect(removed.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)).toEqual(attached)
})

it("keeps the one-argument SDK call when the caller gives no signal", async () => {
const handler = new KenariHandler(mockOptions)
mockCreate.mockResolvedValue(okStream())
expect(await collectStream(streamWith(handler))).toEqual([{ type: "text", text: "ok" }])
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
expect(mockCreate.mock.calls[0]).toHaveLength(1)
})

it("rejects a pre-aborted caller before the model lookup and the SDK call", async () => {
const { getModels } = await import("../fetchers/modelCache")
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
caller.abort()

await expect(collectStream(streamWith(handler, caller.signal))).rejects.toMatchObject(aborted("Kenari"))
expect(getModels).not.toHaveBeenCalled()
expect(mockCreate).not.toHaveBeenCalled()
})

it("rejects with an AbortError when the signal aborts during model lookup", async () => {
const { getModels } = await import("../fetchers/modelCache")
vitest.mocked(getModels).mockImplementationOnce(() => new Promise(() => {}))
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
const pending = collectStream(streamWith(handler, caller.signal))
await vitest.waitFor(() => expect(getModels).toHaveBeenCalled())
caller.abort()

await expect(pending).rejects.toMatchObject(aborted("Kenari"))
expect(mockCreate).not.toHaveBeenCalled()
})

it("normalizes an SDK abort at stream creation into the provider AbortError", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
mockCreate.mockImplementation(() => Promise.reject(new Error("Request was aborted.")))

await expect(collectStream(streamWith(handler, caller.signal))).rejects.toMatchObject(aborted("Kenari"))
})

it("normalizes an abort raised mid-iteration after the chunks that already arrived", async () => {
const handler = new KenariHandler(mockOptions)
const stream = streamWith(handler, new AbortController().signal)
mockCreate.mockResolvedValue(yieldsThenThrows(new Error("Request was aborted.")))

await expect(stream.next()).resolves.toMatchObject({
done: false,
value: { type: "text", text: "Hello" },
})
await expect(stream.next()).rejects.toMatchObject(aborted("Kenari"))
})

it("rejects with an AbortError when the caller aborts just as the stream completes", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
const stream = streamWith(handler, caller.signal)
mockCreate.mockResolvedValue(
asyncStreamFrom([{ choices: [{ delta: { content: "ok" }, index: 0 }], usage: null }]),
)

await expect(stream.next()).resolves.toMatchObject({ done: false, value: { type: "text", text: "ok" } })
caller.abort()
await expect(stream.next()).rejects.toMatchObject(aborted("Kenari"))
})
it("refuses to emit the remaining tool calls of a chunk once the caller cancels", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
const call = (index: number) => ({ index, id: "c" + index, function: { arguments: "{}" } })
const toolChunk = { choices: [{ delta: { tool_calls: [call(0), call(1)] }, index: 0 }], usage: null }
mockCreate.mockResolvedValue(asyncStreamFrom([toolChunk]))
const stream = streamWith(handler, caller.signal)

await expect(stream.next()).resolves.toMatchObject({ value: { type: "tool_call_partial", index: 0 } })
caller.abort()
await expect(stream.next()).rejects.toMatchObject(aborted("Kenari"))
})

it("does not pull a buffered chunk once the caller cancels", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
const buffered = { choices: [{ delta: { content: "buffered" }, index: 0 }], usage: null }
mockCreate.mockResolvedValue(
asyncStreamFrom([{ choices: [{ delta: { content: "ok" }, index: 0 }], usage: null }, buffered]),
)
const stream = streamWith(handler, caller.signal)

await expect(stream.next()).resolves.toMatchObject({ value: { type: "text", text: "ok" } })
caller.abort()
await expect(stream.next()).rejects.toMatchObject(aborted("Kenari"))
})
it("propagates an ordinary mid-iteration failure unchanged while the caller signal is live", async () => {
const handler = new KenariHandler(mockOptions)
const failure = new Error("upstream returned 500")
const stream = streamWith(handler, new AbortController().signal)
mockCreate.mockResolvedValue(yieldsThenThrows(failure))

await expect(collectStream(stream)).rejects.toBe(failure)
})
})

describe("completePrompt", () => {
beforeEach(() => {
mockCreate.mockResolvedValue({ choices: [{ message: { role: "assistant", content: "the answer" } }] })
})

it("bridges the caller signal, with or without a timeout, before handing it to the SDK", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()

const withTimeout = await handler.completePrompt("ping", {
abortSignal: caller.signal,
timeoutMs: 5_000,
})
const requestOptions = mockCreate.mock.calls[0][1]
expect(requestOptions?.signal).toBeInstanceOf(AbortSignal)
expect(requestOptions?.signal).not.toBe(caller.signal)
expect(requestOptions?.timeout).toBe(5_000)
const signalOnly = await handler.completePrompt("ping", { abortSignal: new AbortController().signal })
const signalOnlyOptions = mockCreate.mock.calls[1][1]
expect(signalOnlyOptions?.signal).toBeInstanceOf(AbortSignal)
expect(signalOnlyOptions?.timeout).toBeUndefined()
expect([withTimeout, signalOnly]).toEqual(["the answer", "the answer"])
})

it("sends no request options with no caller signal or no usable timeout", async () => {
const handler = new KenariHandler(mockOptions)
expect(await handler.completePrompt("ping")).toBe("the answer")
expect(mockCreate.mock.calls[0]).toHaveLength(1)
expect(await handler.completePrompt("ping", { timeoutMs: 0 })).toBe("the answer")
expect(mockCreate.mock.calls[1]).toHaveLength(1)
})

it("forwards a timeout alone as an SDK timeout and its own abort signal", async () => {
const handler = new KenariHandler(mockOptions)

expect(await handler.completePrompt("ping", { timeoutMs: 1_200 })).toBe("the answer")

const requestOptions = mockCreate.mock.calls[0][1] as { signal?: AbortSignal; timeout?: number }
expect(requestOptions.timeout).toBe(1_200)
expect(requestOptions.signal).toBeInstanceOf(AbortSignal)
})

it("returns an empty string when the completion carries no choices", async () => {
const handler = new KenariHandler(mockOptions)
mockCreate.mockResolvedValue({ choices: [] })

await expect(handler.completePrompt("ping")).resolves.toBe("")
})

it("rejects a pre-aborted signal before the model lookup and the SDK call", async () => {
const { getModels } = await import("../fetchers/modelCache")
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
caller.abort()

await expect(handler.completePrompt("ping", { abortSignal: caller.signal })).rejects.toMatchObject(
aborted("Kenari"),
)
expect(getModels).not.toHaveBeenCalled()
expect(mockCreate).not.toHaveBeenCalled()
})

it("rejects with an AbortError when the timeout alone expires during the SDK call", async () => {
const handler = new KenariHandler(mockOptions)
let sdkSignal: AbortSignal | null | undefined
mockCreate.mockImplementation((_body, options) => {
sdkSignal = options?.signal
return new Promise(() => {})
})

await expect(handler.completePrompt("ping", { timeoutMs: 20 })).rejects.toMatchObject(aborted("Kenari"))
expect(sdkSignal?.aborted).toBe(true)
})

it("rejects with an AbortError when the timeout alone expires during model lookup", async () => {
const { getModels } = await import("../fetchers/modelCache")
vitest.mocked(getModels).mockImplementationOnce(() => new Promise(() => {}))
const handler = new KenariHandler(mockOptions)

await expect(handler.completePrompt("ping", { timeoutMs: 20 })).rejects.toMatchObject(aborted("Kenari"))
expect(mockCreate).not.toHaveBeenCalled()
})

it("rejects with an AbortError when the signal aborts during model lookup", async () => {
const { getModels } = await import("../fetchers/modelCache")
vitest.mocked(getModels).mockImplementationOnce(() => new Promise(() => {}))
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()

const pending = handler.completePrompt("ping", { abortSignal: caller.signal })
await vitest.waitFor(() => expect(getModels).toHaveBeenCalled())
caller.abort()

await expect(pending).rejects.toMatchObject(aborted("Kenari"))
expect(mockCreate).not.toHaveBeenCalled()
})

it("does not return a completion that resolves just before the caller aborts", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
let resolveCompletion: (value: unknown) => void = () => {}
const completion = new Promise((resolve) => {
resolveCompletion = resolve
})
mockCreate.mockImplementation(() => completion)

const pending = handler.completePrompt("ping", { abortSignal: caller.signal })
await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled())
void completion.then(() => caller.abort())
resolveCompletion({ choices: [{ message: { role: "assistant", content: "late" } }] })

await expect(pending).rejects.toMatchObject(aborted("Kenari"))
})

it("reports an SDK abort error after the caller aborts as an AbortError, not a completion error", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
mockCreate.mockImplementation(async () => {
caller.abort()
throw new Error("Request was aborted.")
})

await expect(handler.completePrompt("ping", { abortSignal: caller.signal })).rejects.toMatchObject(
{ name: "AbortError" }, // the entry guard rethrows the caller's own abort reason
)
})

it("normalizes an SDK abort raised by the completion into the provider AbortError", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController() // the caller signal stays live while the SDK aborts on its own
mockCreate.mockRejectedValue(new Error("Request was aborted."))
const pending = handler.completePrompt("ping", { abortSignal: caller.signal })

await expect(pending).rejects.toMatchObject(aborted("Kenari"))
})
it("detaches the bridge after completion, failure, and cancellation alike", async () => {
const handler = new KenariHandler(mockOptions)
const caller = new AbortController()
const liveCall = () => handler.completePrompt("ping", { abortSignal: caller.signal })
const added = vitest.spyOn(caller.signal, "addEventListener")
const removed = vitest.spyOn(caller.signal, "removeEventListener")

await expect(liveCall()).resolves.toBe("the answer")
expect(caller.signal.aborted).toBe(false) // the success and failure rows never cancel
mockCreate.mockRejectedValue(new Error("boom"))
await expect(liveCall()).rejects.toThrow("Kenari completion error: boom")
mockCreate.mockImplementation(() => new Promise(() => {}))
const pending = liveCall().catch((error: unknown) => error)
await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(3))
caller.abort()
expect(await pending).toMatchObject(aborted("Kenari")) // the cancel must surface as AbortError, not any rejection
const aborts = added.mock.calls.filter(([type]) => type === "abort").map(([, listener]) => listener)
expect(aborts).toHaveLength(3)
expect(removed.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)).toEqual(aborts)
})
})
})

describe("model catalog", () => {
it("reports its cache scope", () => {
const handler = new KenariHandler(mockOptions)
Expand Down
Loading
Loading