test(shared): update model-suggestion-retry tests for promptAsync passthrough
This commit is contained in:
parent
46e02b9457
commit
e984a5c639
@ -377,162 +377,3 @@ describe("promptWithModelSuggestionRetry", () => {
|
|||||||
expect(promptMock).toHaveBeenCalledTimes(1)
|
expect(promptMock).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.mockResolvedValueOnce(undefined)
|
|
||||||
const client = { session: { prompt: promptMock } }
|
|
||||||
|
|
||||||
// when calling promptWithModelSuggestionRetry
|
|
||||||
await promptWithModelSuggestionRetry(client as any, {
|
|
||||||
path: { id: "session-1" },
|
|
||||||
body: {
|
|
||||||
agent: "explore",
|
|
||||||
parts: [{ type: "text", text: "hello" }],
|
|
||||||
model: { providerID: "anthropic", modelID: "claude-sonet-4" },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// then should call prompt twice - first with original, then with suggestion
|
|
||||||
expect(promptMock).toHaveBeenCalledTimes(2)
|
|
||||||
const retryCall = promptMock.mock.calls[1][0]
|
|
||||||
expect(retryCall.body.model).toEqual({
|
|
||||||
providerID: "anthropic",
|
|
||||||
modelID: "claude-sonnet-4",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should throw original error when no suggestion available", async () => {
|
|
||||||
// given a client that fails with a non-model-not-found error
|
|
||||||
const originalError = new Error("Connection refused")
|
|
||||||
const promptMock = mock().mockRejectedValueOnce(originalError)
|
|
||||||
const client = { session: { prompt: promptMock } }
|
|
||||||
|
|
||||||
// when calling promptWithModelSuggestionRetry
|
|
||||||
// then should throw the original error
|
|
||||||
await expect(
|
|
||||||
promptWithModelSuggestionRetry(client as any, {
|
|
||||||
path: { id: "session-1" },
|
|
||||||
body: {
|
|
||||||
parts: [{ type: "text", text: "hello" }],
|
|
||||||
model: { providerID: "anthropic", modelID: "claude-sonnet-4" },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
).rejects.toThrow("Connection refused")
|
|
||||||
|
|
||||||
expect(promptMock).toHaveBeenCalledTimes(1)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should throw original error when retry also fails", async () => {
|
|
||||||
// given a client that fails with model-not-found, retry also fails
|
|
||||||
const modelNotFoundError = {
|
|
||||||
name: "ProviderModelNotFoundError",
|
|
||||||
data: {
|
|
||||||
providerID: "anthropic",
|
|
||||||
modelID: "claude-sonet-4",
|
|
||||||
suggestions: ["claude-sonnet-4"],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
const retryError = new Error("Still not found")
|
|
||||||
const promptMock = mock()
|
|
||||||
.mockRejectedValueOnce(modelNotFoundError)
|
|
||||||
.mockRejectedValueOnce(retryError)
|
|
||||||
const client = { session: { prompt: promptMock } }
|
|
||||||
|
|
||||||
// when calling promptWithModelSuggestionRetry
|
|
||||||
// then should throw the retry error (not the original)
|
|
||||||
await expect(
|
|
||||||
promptWithModelSuggestionRetry(client as any, {
|
|
||||||
path: { id: "session-1" },
|
|
||||||
body: {
|
|
||||||
parts: [{ type: "text", text: "hello" }],
|
|
||||||
model: { providerID: "anthropic", modelID: "claude-sonet-4" },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
).rejects.toThrow("Still not found")
|
|
||||||
|
|
||||||
expect(promptMock).toHaveBeenCalledTimes(2)
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should preserve other body fields during retry", async () => {
|
|
||||||
// given a client that fails first with model-not-found
|
|
||||||
const promptMock = mock()
|
|
||||||
.mockRejectedValueOnce({
|
|
||||||
name: "ProviderModelNotFoundError",
|
|
||||||
data: {
|
|
||||||
providerID: "anthropic",
|
|
||||||
modelID: "claude-sonet-4",
|
|
||||||
suggestions: ["claude-sonnet-4"],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.mockResolvedValueOnce(undefined)
|
|
||||||
const client = { session: { prompt: promptMock } }
|
|
||||||
|
|
||||||
// when calling with additional body fields
|
|
||||||
await promptWithModelSuggestionRetry(client as any, {
|
|
||||||
path: { id: "session-1" },
|
|
||||||
body: {
|
|
||||||
agent: "explore",
|
|
||||||
system: "You are a helpful agent",
|
|
||||||
tools: { task: false },
|
|
||||||
parts: [{ type: "text", text: "hello" }],
|
|
||||||
model: { providerID: "anthropic", modelID: "claude-sonet-4" },
|
|
||||||
variant: "max",
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// then retry call should preserve all fields except corrected model
|
|
||||||
const retryCall = promptMock.mock.calls[1][0]
|
|
||||||
expect(retryCall.body.agent).toBe("explore")
|
|
||||||
expect(retryCall.body.system).toBe("You are a helpful agent")
|
|
||||||
expect(retryCall.body.tools).toEqual({ task: false })
|
|
||||||
expect(retryCall.body.variant).toBe("max")
|
|
||||||
expect(retryCall.body.model).toEqual({
|
|
||||||
providerID: "anthropic",
|
|
||||||
modelID: "claude-sonnet-4",
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should handle string error message with suggestion", async () => {
|
|
||||||
// given a client that fails with a string error containing suggestion
|
|
||||||
const promptMock = mock()
|
|
||||||
.mockRejectedValueOnce(
|
|
||||||
new Error("Model not found: anthropic/claude-sonet-4. Did you mean: claude-sonnet-4?")
|
|
||||||
)
|
|
||||||
.mockResolvedValueOnce(undefined)
|
|
||||||
const client = { session: { prompt: promptMock } }
|
|
||||||
|
|
||||||
// when calling promptWithModelSuggestionRetry
|
|
||||||
await promptWithModelSuggestionRetry(client as any, {
|
|
||||||
path: { id: "session-1" },
|
|
||||||
body: {
|
|
||||||
parts: [{ type: "text", text: "hello" }],
|
|
||||||
model: { providerID: "anthropic", modelID: "claude-sonet-4" },
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// then should retry with suggested model
|
|
||||||
expect(promptMock).toHaveBeenCalledTimes(2)
|
|
||||||
const retryCall = promptMock.mock.calls[1][0]
|
|
||||||
expect(retryCall.body.model.modelID).toBe("claude-sonnet-4")
|
|
||||||
})
|
|
||||||
|
|
||||||
it("should not retry when no model in original request", async () => {
|
|
||||||
// given a client that fails with model-not-found but original has no model param
|
|
||||||
const modelNotFoundError = new Error(
|
|
||||||
"Model not found: anthropic/claude-sonet-4. Did you mean: claude-sonnet-4?"
|
|
||||||
)
|
|
||||||
const promptMock = mock().mockRejectedValueOnce(modelNotFoundError)
|
|
||||||
const client = { session: { prompt: promptMock } }
|
|
||||||
|
|
||||||
// when calling without model in body
|
|
||||||
// then should throw without retrying
|
|
||||||
await expect(
|
|
||||||
promptWithModelSuggestionRetry(client as any, {
|
|
||||||
path: { id: "session-1" },
|
|
||||||
body: {
|
|
||||||
parts: [{ type: "text", text: "hello" }],
|
|
||||||
},
|
|
||||||
})
|
|
||||||
).rejects.toThrow()
|
|
||||||
|
|
||||||
expect(promptMock).toHaveBeenCalledTimes(1)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user