|
| 1 | +import type { Anthropic } from "@anthropic-ai/sdk" |
| 2 | + |
| 3 | +import type { ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../../index" |
| 4 | +import { FakeAIHandler } from "../fake-ai" |
| 5 | + |
| 6 | +const modelInfo = { |
| 7 | + contextWindow: 8192, |
| 8 | + maxTokens: 4096, |
| 9 | + supportsImages: false, |
| 10 | + supportsPromptCache: false, |
| 11 | +} |
| 12 | + |
| 13 | +describe("FakeAIHandler", () => { |
| 14 | + it("should delegate completePrompt with options to the cached FakeAI instance", async () => { |
| 15 | + const completePrompt = vitest.fn().mockResolvedValue("delegated response") |
| 16 | + const fakeAi: { |
| 17 | + id: string |
| 18 | + createMessage: () => AsyncGenerator<never, void, unknown> |
| 19 | + getModel: () => { id: string; info: typeof modelInfo } |
| 20 | + countTokens: ReturnType<typeof vitest.fn> |
| 21 | + completePrompt: typeof completePrompt |
| 22 | + removeFromCache?: () => void |
| 23 | + } = { |
| 24 | + id: "fake-ai-completePrompt-delegation", |
| 25 | + createMessage: async function* () {}, |
| 26 | + getModel: () => ({ id: "fake-model", info: modelInfo }), |
| 27 | + countTokens: vitest.fn().mockResolvedValue(0), |
| 28 | + completePrompt, |
| 29 | + } |
| 30 | + const controller = new AbortController() |
| 31 | + const options: CompletePromptOptions = { abortSignal: controller.signal, timeoutMs: 1234 } |
| 32 | + |
| 33 | + const handler = new FakeAIHandler({ fakeAi }) |
| 34 | + const result = await handler.completePrompt("Test prompt", options) |
| 35 | + |
| 36 | + expect(result).toBe("delegated response") |
| 37 | + expect(completePrompt).toHaveBeenCalledWith("Test prompt", options) |
| 38 | + fakeAi.removeFromCache?.() |
| 39 | + }) |
| 40 | + |
| 41 | + it("should delegate createMessage, getModel, and countTokens to FakeAI", async () => { |
| 42 | + const metadata = { taskId: "task-1" } as ApiHandlerCreateMessageMetadata |
| 43 | + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hello" }] |
| 44 | + const content: Anthropic.Messages.ContentBlockParam[] = [{ type: "text", text: "Hello" }] |
| 45 | + const createMessage = vitest.fn(async function* () { |
| 46 | + yield { type: "text" as const, text: "Hello" } |
| 47 | + }) |
| 48 | + const getModel = vitest.fn(() => ({ id: "fake-model", info: modelInfo })) |
| 49 | + const countTokens = vitest.fn().mockResolvedValue(7) |
| 50 | + const fakeAi: { |
| 51 | + id: string |
| 52 | + createMessage: typeof createMessage |
| 53 | + getModel: typeof getModel |
| 54 | + countTokens: typeof countTokens |
| 55 | + completePrompt: ReturnType<typeof vitest.fn> |
| 56 | + removeFromCache?: () => void |
| 57 | + } = { |
| 58 | + id: "fake-ai-handler-delegation", |
| 59 | + createMessage, |
| 60 | + getModel, |
| 61 | + countTokens, |
| 62 | + completePrompt: vitest.fn().mockResolvedValue("complete"), |
| 63 | + } |
| 64 | + |
| 65 | + const handler = new FakeAIHandler({ fakeAi }) |
| 66 | + const chunks = [] |
| 67 | + for await (const chunk of handler.createMessage("System", messages, metadata)) { |
| 68 | + chunks.push(chunk) |
| 69 | + } |
| 70 | + |
| 71 | + expect(chunks).toEqual([{ type: "text", text: "Hello" }]) |
| 72 | + expect(createMessage).toHaveBeenCalledWith("System", messages, metadata) |
| 73 | + expect(handler.getModel()).toEqual({ id: "fake-model", info: modelInfo }) |
| 74 | + expect(getModel).toHaveBeenCalledTimes(1) |
| 75 | + await expect(handler.countTokens(content)).resolves.toBe(7) |
| 76 | + expect(countTokens).toHaveBeenCalledWith(content) |
| 77 | + fakeAi.removeFromCache?.() |
| 78 | + }) |
| 79 | +}) |
0 commit comments