|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +/* eslint-disable no-new */ |
| 3 | +import { test, expect, describe, jest, beforeEach } from "@jest/globals"; |
| 4 | +import { SageMakerEndpointEmbeddings } from "../sagemaker_endpoint.js"; |
| 5 | + |
| 6 | +const mockSend = jest.fn<() => Promise<any>>(); |
| 7 | + |
| 8 | +// Mock the AWS SDK |
| 9 | +jest.mock("@aws-sdk/client-sagemaker-runtime", () => ({ |
| 10 | + SageMakerRuntimeClient: jest.fn().mockImplementation(() => ({ |
| 11 | + send: mockSend, |
| 12 | + })), |
| 13 | + InvokeEndpointCommand: jest.fn().mockImplementation((params) => params), |
| 14 | +})); |
| 15 | + |
| 16 | +describe("SageMakerEndpointEmbeddings", () => { |
| 17 | + beforeEach(() => { |
| 18 | + jest.clearAllMocks(); |
| 19 | + mockSend.mockClear(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe("Constructor validation", () => { |
| 23 | + test("should throw error when region is missing", () => { |
| 24 | + expect(() => { |
| 25 | + new SageMakerEndpointEmbeddings({ |
| 26 | + endpointName: "test-endpoint", |
| 27 | + clientOptions: {}, |
| 28 | + }); |
| 29 | + }).toThrow( |
| 30 | + 'Please pass a "clientOptions" object with a "region" field to the constructor' |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + test("should throw error when endpointName is missing", () => { |
| 35 | + expect(() => { |
| 36 | + // @ts-expect-error Testing missing required field |
| 37 | + new SageMakerEndpointEmbeddings({ |
| 38 | + clientOptions: { |
| 39 | + region: "us-east-1", |
| 40 | + }, |
| 41 | + }); |
| 42 | + }).toThrow('Please pass an "endpointName" field to the constructor'); |
| 43 | + }); |
| 44 | + |
| 45 | + test("should create instance with valid parameters", () => { |
| 46 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 47 | + endpointName: "test-endpoint", |
| 48 | + clientOptions: { |
| 49 | + region: "us-east-1", |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + expect(embeddings).toBeDefined(); |
| 54 | + expect(embeddings.endpointName).toBe("test-endpoint"); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + describe("embedQuery", () => { |
| 59 | + test("should embed a single query", async () => { |
| 60 | + const mockEmbedding = [0.1, 0.2, 0.3, 0.4, 0.5]; |
| 61 | + mockSend.mockResolvedValueOnce({ |
| 62 | + Body: new TextEncoder().encode(JSON.stringify(mockEmbedding)), |
| 63 | + }); |
| 64 | + |
| 65 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 66 | + endpointName: "test-endpoint", |
| 67 | + clientOptions: { |
| 68 | + region: "us-east-1", |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + const result = await embeddings.embedQuery("Hello world"); |
| 73 | + |
| 74 | + expect(mockSend).toHaveBeenCalledTimes(1); |
| 75 | + expect(mockSend).toHaveBeenCalledWith( |
| 76 | + expect.objectContaining({ |
| 77 | + EndpointName: "test-endpoint", |
| 78 | + ContentType: "application/json", |
| 79 | + Body: expect.any(Buffer), |
| 80 | + }) |
| 81 | + ); |
| 82 | + |
| 83 | + // Verify the request body |
| 84 | + const calls = mockSend.mock.calls as any[]; |
| 85 | + const calledWith = calls[0][0]; |
| 86 | + const requestBody = JSON.parse(calledWith.Body.toString()); |
| 87 | + expect(requestBody).toEqual({ inputs: ["Hello world"] }); |
| 88 | + |
| 89 | + // Note: Due to the bug in the implementation (line 62), this will fail |
| 90 | + // The implementation should JSON.parse the response |
| 91 | + expect(result).toEqual(mockEmbedding); |
| 92 | + }); |
| 93 | + |
| 94 | + test("should handle empty string", async () => { |
| 95 | + const mockEmbedding = [0.0, 0.0, 0.0]; |
| 96 | + mockSend.mockResolvedValueOnce({ |
| 97 | + Body: new TextEncoder().encode(JSON.stringify(mockEmbedding)), |
| 98 | + }); |
| 99 | + |
| 100 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 101 | + endpointName: "test-endpoint", |
| 102 | + clientOptions: { |
| 103 | + region: "us-east-1", |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + await embeddings.embedQuery(""); |
| 108 | + |
| 109 | + expect(mockSend).toHaveBeenCalledTimes(1); |
| 110 | + const calls = mockSend.mock.calls as any[]; |
| 111 | + const calledWith = calls[0][0]; |
| 112 | + const requestBody = JSON.parse(calledWith.Body.toString()); |
| 113 | + expect(requestBody).toEqual({ inputs: [""] }); |
| 114 | + }); |
| 115 | + |
| 116 | + test("should handle API errors", async () => { |
| 117 | + const error = new Error("SageMaker endpoint error"); |
| 118 | + mockSend.mockRejectedValueOnce(error); |
| 119 | + |
| 120 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 121 | + endpointName: "test-endpoint", |
| 122 | + clientOptions: { |
| 123 | + region: "us-east-1", |
| 124 | + }, |
| 125 | + }); |
| 126 | + |
| 127 | + await expect(embeddings.embedQuery("Hello world")).rejects.toThrow( |
| 128 | + "SageMaker endpoint error" |
| 129 | + ); |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + describe("embedDocuments", () => { |
| 134 | + test("should embed multiple documents", async () => { |
| 135 | + const mockEmbeddings = [ |
| 136 | + [0.1, 0.2, 0.3], |
| 137 | + [0.4, 0.5, 0.6], |
| 138 | + [0.7, 0.8, 0.9], |
| 139 | + ]; |
| 140 | + |
| 141 | + // Mock responses for each document |
| 142 | + mockEmbeddings.forEach((embedding) => { |
| 143 | + mockSend.mockResolvedValueOnce({ |
| 144 | + Body: new TextEncoder().encode(JSON.stringify(embedding)), |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 149 | + endpointName: "test-endpoint", |
| 150 | + clientOptions: { |
| 151 | + region: "us-east-1", |
| 152 | + }, |
| 153 | + }); |
| 154 | + |
| 155 | + const documents = ["Document 1", "Document 2", "Document 3"]; |
| 156 | + const result = await embeddings.embedDocuments(documents); |
| 157 | + |
| 158 | + expect(mockSend).toHaveBeenCalledTimes(3); |
| 159 | + expect(result).toEqual(mockEmbeddings); |
| 160 | + |
| 161 | + // Verify each request |
| 162 | + documents.forEach((doc, index) => { |
| 163 | + const calls = mockSend.mock.calls as any[]; |
| 164 | + const calledWith = calls[index][0]; |
| 165 | + const requestBody = JSON.parse(calledWith.Body.toString()); |
| 166 | + expect(requestBody).toEqual({ inputs: [doc] }); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + test("should handle empty array", async () => { |
| 171 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 172 | + endpointName: "test-endpoint", |
| 173 | + clientOptions: { |
| 174 | + region: "us-east-1", |
| 175 | + }, |
| 176 | + }); |
| 177 | + |
| 178 | + const result = await embeddings.embedDocuments([]); |
| 179 | + |
| 180 | + expect(mockSend).not.toHaveBeenCalled(); |
| 181 | + expect(result).toEqual([]); |
| 182 | + }); |
| 183 | + |
| 184 | + test("should handle single document", async () => { |
| 185 | + const mockEmbedding = [0.1, 0.2, 0.3]; |
| 186 | + mockSend.mockResolvedValueOnce({ |
| 187 | + Body: new TextEncoder().encode(JSON.stringify(mockEmbedding)), |
| 188 | + }); |
| 189 | + |
| 190 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 191 | + endpointName: "test-endpoint", |
| 192 | + clientOptions: { |
| 193 | + region: "us-east-1", |
| 194 | + }, |
| 195 | + }); |
| 196 | + |
| 197 | + const result = await embeddings.embedDocuments(["Single document"]); |
| 198 | + |
| 199 | + expect(mockSend).toHaveBeenCalledTimes(1); |
| 200 | + expect(result).toEqual([mockEmbedding]); |
| 201 | + }); |
| 202 | + |
| 203 | + test("should handle partial failures", async () => { |
| 204 | + const error = new Error("SageMaker endpoint error"); |
| 205 | + |
| 206 | + // First call succeeds, second fails |
| 207 | + mockSend |
| 208 | + .mockResolvedValueOnce({ |
| 209 | + Body: new TextEncoder().encode(JSON.stringify([0.1, 0.2, 0.3])), |
| 210 | + }) |
| 211 | + .mockRejectedValueOnce(error); |
| 212 | + |
| 213 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 214 | + endpointName: "test-endpoint", |
| 215 | + clientOptions: { |
| 216 | + region: "us-east-1", |
| 217 | + }, |
| 218 | + }); |
| 219 | + |
| 220 | + await expect( |
| 221 | + embeddings.embedDocuments(["Document 1", "Document 2"]) |
| 222 | + ).rejects.toThrow("SageMaker endpoint error"); |
| 223 | + |
| 224 | + expect(mockSend).toHaveBeenCalledTimes(2); |
| 225 | + }); |
| 226 | + }); |
| 227 | + |
| 228 | + describe("Configuration options", () => { |
| 229 | + test("should pass additional client options", () => { |
| 230 | + const { SageMakerRuntimeClient } = jest.requireMock( |
| 231 | + "@aws-sdk/client-sagemaker-runtime" |
| 232 | + ) as any; |
| 233 | + |
| 234 | + new SageMakerEndpointEmbeddings({ |
| 235 | + endpointName: "test-endpoint", |
| 236 | + clientOptions: { |
| 237 | + region: "us-west-2", |
| 238 | + credentials: { |
| 239 | + accessKeyId: "test-key", |
| 240 | + secretAccessKey: "test-secret", |
| 241 | + }, |
| 242 | + maxAttempts: 3, |
| 243 | + }, |
| 244 | + }); |
| 245 | + |
| 246 | + expect(SageMakerRuntimeClient).toHaveBeenCalledWith({ |
| 247 | + region: "us-west-2", |
| 248 | + credentials: { |
| 249 | + accessKeyId: "test-key", |
| 250 | + secretAccessKey: "test-secret", |
| 251 | + }, |
| 252 | + maxAttempts: 3, |
| 253 | + }); |
| 254 | + }); |
| 255 | + }); |
| 256 | + |
| 257 | + describe("Response handling", () => { |
| 258 | + test("should handle non-JSON response gracefully", async () => { |
| 259 | + mockSend.mockResolvedValueOnce({ |
| 260 | + Body: new TextEncoder().encode("Invalid JSON"), |
| 261 | + }); |
| 262 | + |
| 263 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 264 | + endpointName: "test-endpoint", |
| 265 | + clientOptions: { |
| 266 | + region: "us-east-1", |
| 267 | + }, |
| 268 | + }); |
| 269 | + |
| 270 | + // This will currently fail due to the implementation bug |
| 271 | + // The actual behavior would throw an error when trying to parse |
| 272 | + const result = await embeddings.embedQuery("Hello world"); |
| 273 | + |
| 274 | + // Current implementation returns the string directly |
| 275 | + expect(result).toBe("Invalid JSON"); |
| 276 | + }); |
| 277 | + |
| 278 | + test("should handle undefined response body", async () => { |
| 279 | + mockSend.mockResolvedValueOnce({ |
| 280 | + Body: undefined, |
| 281 | + }); |
| 282 | + |
| 283 | + const embeddings = new SageMakerEndpointEmbeddings({ |
| 284 | + endpointName: "test-endpoint", |
| 285 | + clientOptions: { |
| 286 | + region: "us-east-1", |
| 287 | + }, |
| 288 | + }); |
| 289 | + |
| 290 | + // This will throw an error in the current implementation |
| 291 | + await expect(embeddings.embedQuery("Hello world")).rejects.toThrow(); |
| 292 | + }); |
| 293 | + }); |
| 294 | +}); |
0 commit comments