From fdfa96a537a9ae87ca6b416f6d01c1057b1f2a16 Mon Sep 17 00:00:00 2001 From: Nate Date: Wed, 16 Jul 2025 23:54:14 -0700 Subject: [PATCH] fix tests --- core/.eslintrc.json | 10 ++- core/llm/llms/stubs/ContinueProxy.vitest.ts | 99 ++++++++++++--------- 2 files changed, 65 insertions(+), 44 deletions(-) diff --git a/core/.eslintrc.json b/core/.eslintrc.json index 08948b866..7b4563436 100644 --- a/core/.eslintrc.json +++ b/core/.eslintrc.json @@ -19,5 +19,13 @@ "max-depth": ["error", { "max": 6 }], "max-nested-callbacks": ["error", { "max": 4 }], "max-params": ["error", { "max": 8 }] - } + }, + "overrides": [ + { + "files": ["**/*.vitest.ts"], + "rules": { + "max-lines-per-function": "off" + } + } + ] } diff --git a/core/llm/llms/stubs/ContinueProxy.vitest.ts b/core/llm/llms/stubs/ContinueProxy.vitest.ts index 09a78ff9b..20bed0ec1 100644 --- a/core/llm/llms/stubs/ContinueProxy.vitest.ts +++ b/core/llm/llms/stubs/ContinueProxy.vitest.ts @@ -3,7 +3,7 @@ import { ILLM } from "../../../index.js"; import ContinueProxy from "./ContinueProxy.js"; vi.mock("@continuedev/config-yaml", async (importOriginal) => { - const mod = await importOriginal(); + const mod = (await importOriginal()) as any; return { ...mod, parseProxyModelName: vi.fn(() => ({ @@ -12,7 +12,6 @@ vi.mock("@continuedev/config-yaml", async (importOriginal) => { ownerSlug: "test-owner", packageSlug: "test-package", })), - decodeSecretLocation: vi.fn(), }; }); @@ -30,35 +29,29 @@ interface LlmTestCase { mockStream?: any[]; } -async function runLlmTest(testCase: LlmTestCase) { - const { - llm, - methodToTest, - params, - expectedRequest, - mockResponse, - mockStream, - } = testCase; +function createMockStream(mockStream: any[]) { + const encoder = new TextEncoder(); + return new ReadableStream({ + start(controller) { + for (const chunk of mockStream) { + controller.enqueue( + encoder.encode( + `data: ${ + typeof chunk === "string" ? chunk : JSON.stringify(chunk) + }\n\n`, + ), + ); + } + controller.close(); + }, + }); +} +function setupMockFetch(mockResponse?: any, mockStream?: any[]) { const mockFetch = vi.fn(); if (mockStream) { - const encoder = new TextEncoder(); - const stream = new ReadableStream({ - start(controller) { - for (const chunk of mockStream) { - controller.enqueue( - encoder.encode( - `data: ${ - typeof chunk === "string" ? chunk : JSON.stringify(chunk) - }\n\n`, - ), - ); - } - controller.close(); - }, - }); - + const stream = createMockStream(mockStream); mockFetch.mockResolvedValue( new Response(stream, { headers: { @@ -74,6 +67,10 @@ async function runLlmTest(testCase: LlmTestCase) { ); } + return mockFetch; +} + +function setupReadableStreamPolyfill() { // This can be removed if https://github.com/nodejs/undici/issues/2888 is resolved // @ts-ignore const originalFrom = ReadableStream.from; @@ -84,9 +81,13 @@ async function runLlmTest(testCase: LlmTestCase) { } return originalFrom(body); }; +} - (llm as any).fetch = mockFetch; - +async function executeLlmMethod( + llm: ILLM, + methodToTest: keyof ILLM, + params: any[], +) { if (typeof (llm as any)[methodToTest] !== "function") { throw new Error( `Method ${String(methodToTest)} does not exist on the LLM instance.`, @@ -98,7 +99,9 @@ async function runLlmTest(testCase: LlmTestCase) { for await (const _ of result) { } } +} +function assertFetchCall(mockFetch: any, expectedRequest: any) { expect(mockFetch).toHaveBeenCalledTimes(1); const [url, options] = mockFetch.mock.calls[0]; @@ -117,6 +120,25 @@ async function runLlmTest(testCase: LlmTestCase) { } } +async function runLlmTest(testCase: LlmTestCase) { + const { + llm, + methodToTest, + params, + expectedRequest, + mockResponse, + mockStream, + } = testCase; + + const mockFetch = setupMockFetch(mockResponse, mockStream); + setupReadableStreamPolyfill(); + + (llm as any).fetch = mockFetch; + + await executeLlmMethod(llm, methodToTest, params); + assertFetchCall(mockFetch, expectedRequest); +} + describe("ContinueProxy", () => { afterEach(() => { vi.clearAllMocks(); @@ -382,10 +404,7 @@ describe("ContinueProxy", () => { }, }, mockResponse: { - data: [ - { embedding: [0.1, 0.2, 0.3] }, - { embedding: [0.4, 0.5, 0.6] }, - ], + data: [{ embedding: [0.1, 0.2, 0.3] }, { embedding: [0.4, 0.5, 0.6] }], }, }); }); @@ -411,10 +430,7 @@ describe("ContinueProxy", () => { }, }, mockResponse: { - data: [ - { id: "gpt-4" }, - { id: "gpt-3.5-turbo" }, - ], + data: [{ id: "gpt-4" }, { id: "gpt-3.5-turbo" }], }, }); }); @@ -484,7 +500,7 @@ describe("ContinueProxy", () => { headers: { "Content-Type": "application/json", Authorization: "Bearer test-api-key", - "api-key": "test-api-key", + "api-key": "test-api-key", "x-continue-unique-id": "NOT_UNIQUE", }, body: { @@ -587,10 +603,7 @@ describe("ContinueProxy", () => { }, }, }, - mockStream: [ - '{"choices": [{"delta": {"content": "42"}}]}', - "[DONE]", - ], + mockStream: ['{"choices": [{"delta": {"content": "42"}}]}', "[DONE]"], }); }); @@ -684,7 +697,7 @@ describe("ContinueProxy", () => { headers: { "Content-Type": "application/json", Authorization: "Bearer test-api-key", - "api-key": "test-api-key", + "api-key": "test-api-key", "x-continue-unique-id": "NOT_UNIQUE", }, body: {