From 7fdce04449ca815b835ca3a480a40c20995c2dcc Mon Sep 17 00:00:00 2001 From: chezsmithy Date: Tue, 12 Aug 2025 10:56:34 -0700 Subject: [PATCH] fix: :white_check_mark: Prettier and test fixes --- core/llm/llms/Bedrock.test.ts | 95 ++++++++++++++++++++++------------- core/llm/llms/Bedrock.ts | 2 +- 2 files changed, 60 insertions(+), 37 deletions(-) diff --git a/core/llm/llms/Bedrock.test.ts b/core/llm/llms/Bedrock.test.ts index 32a8c5f21..262f2266b 100644 --- a/core/llm/llms/Bedrock.test.ts +++ b/core/llm/llms/Bedrock.test.ts @@ -315,45 +315,35 @@ describe("Bedrock", () => { }); describe("streaming vs non-streaming behavior", () => { - class TestableStreamingBedrock extends Bedrock { - public streamingMethodCalled = false; - public nonStreamingMethodCalled = false; - - protected async *_streamChatStreaming( - messages: ChatMessage[], - signal: AbortSignal, - options: CompletionOptions, - ): AsyncGenerator { - this.streamingMethodCalled = true; - yield { role: "assistant", content: "Streaming response" }; - } - - protected async *_streamChatNonStreaming( - messages: ChatMessage[], - signal: AbortSignal, - options: CompletionOptions, - ): AsyncGenerator { - this.nonStreamingMethodCalled = true; - yield { role: "assistant", content: "Non-streaming response" }; - } - } - - let bedrock: TestableStreamingBedrock; + let bedrock: Bedrock; const mockAbortSignal = new AbortController().signal; beforeEach(() => { - bedrock = new TestableStreamingBedrock({ + bedrock = new Bedrock({ apiKey: "test-key", model: "anthropic.claude-3-sonnet-20240229-v1:0", region: "us-east-1", }); }); - it("should use streaming method when stream is not false", async () => { + it("should route to streaming path when stream is not false", async () => { const messages: ChatMessage[] = [ { role: "user", content: "Hello" } as UserChatMessage, ]; + // Mock the private methods by spying on them + const streamingMethodSpy = jest + .spyOn(bedrock as any, "_streamChatStreaming") + .mockImplementation(async function* () { + yield { role: "assistant", content: "Streaming response" }; + }); + + const nonStreamingMethodSpy = jest + .spyOn(bedrock as any, "_streamChatNonStreaming") + .mockImplementation(async function* () { + yield { role: "assistant", content: "Non-streaming response" }; + }); + const options: CompletionOptions = { model: "anthropic.claude-3-sonnet-20240229-v1:0", stream: true, // Explicitly set streaming @@ -368,18 +358,34 @@ describe("Bedrock", () => { results.push(message); } - expect(bedrock.streamingMethodCalled).toBe(true); - expect(bedrock.nonStreamingMethodCalled).toBe(false); + expect(streamingMethodSpy).toHaveBeenCalled(); + expect(nonStreamingMethodSpy).not.toHaveBeenCalled(); expect(results).toEqual([ { role: "assistant", content: "Streaming response" }, ]); + + streamingMethodSpy.mockRestore(); + nonStreamingMethodSpy.mockRestore(); }); - it("should use non-streaming method when stream is false", async () => { + it("should route to non-streaming path when stream is false", async () => { const messages: ChatMessage[] = [ { role: "user", content: "Hello" } as UserChatMessage, ]; + // Mock the private methods by spying on them + const streamingMethodSpy = jest + .spyOn(bedrock as any, "_streamChatStreaming") + .mockImplementation(async function* () { + yield { role: "assistant", content: "Streaming response" }; + }); + + const nonStreamingMethodSpy = jest + .spyOn(bedrock as any, "_streamChatNonStreaming") + .mockImplementation(async function* () { + yield { role: "assistant", content: "Non-streaming response" }; + }); + const options: CompletionOptions = { model: "anthropic.claude-3-sonnet-20240229-v1:0", stream: false, // Explicitly set non-streaming @@ -394,18 +400,34 @@ describe("Bedrock", () => { results.push(message); } - expect(bedrock.streamingMethodCalled).toBe(false); - expect(bedrock.nonStreamingMethodCalled).toBe(true); + expect(streamingMethodSpy).not.toHaveBeenCalled(); + expect(nonStreamingMethodSpy).toHaveBeenCalled(); expect(results).toEqual([ { role: "assistant", content: "Non-streaming response" }, ]); + + streamingMethodSpy.mockRestore(); + nonStreamingMethodSpy.mockRestore(); }); - it("should default to streaming method when stream option is undefined", async () => { + it("should default to streaming path when stream option is undefined", async () => { const messages: ChatMessage[] = [ { role: "user", content: "Hello" } as UserChatMessage, ]; + // Mock the private methods by spying on them + const streamingMethodSpy = jest + .spyOn(bedrock as any, "_streamChatStreaming") + .mockImplementation(async function* () { + yield { role: "assistant", content: "Streaming response" }; + }); + + const nonStreamingMethodSpy = jest + .spyOn(bedrock as any, "_streamChatNonStreaming") + .mockImplementation(async function* () { + yield { role: "assistant", content: "Non-streaming response" }; + }); + const options: CompletionOptions = { model: "anthropic.claude-3-sonnet-20240229-v1:0", // stream option is undefined, should default to streaming @@ -420,13 +442,14 @@ describe("Bedrock", () => { results.push(message); } - expect(bedrock.streamingMethodCalled).toBe(true); - expect(bedrock.nonStreamingMethodCalled).toBe(false); + expect(streamingMethodSpy).toHaveBeenCalled(); + expect(nonStreamingMethodSpy).not.toHaveBeenCalled(); + + streamingMethodSpy.mockRestore(); + nonStreamingMethodSpy.mockRestore(); }); }); - - describe("message conversion", () => { let bedrock: TestBedrock; diff --git a/core/llm/llms/Bedrock.ts b/core/llm/llms/Bedrock.ts index 6c87dcc31..0054ab2ca 100644 --- a/core/llm/llms/Bedrock.ts +++ b/core/llm/llms/Bedrock.ts @@ -7,7 +7,7 @@ import { ImageFormat, InvokeModelCommand, Message, - ToolConfiguration + ToolConfiguration, } from "@aws-sdk/client-bedrock-runtime"; import { fromNodeProviderChain } from "@aws-sdk/credential-providers";