1+ import { HeliconeSqlManager } from "../HeliconeSqlManager" ;
2+ import { AuthParams } from "../../packages/common/auth/types" ;
3+
4+ describe ( "HeliconeSqlManager - Simple Tests" , ( ) => {
5+ let manager : any ;
6+ const mockAuthParams : AuthParams = {
7+ organizationId : "test-org-id" ,
8+ userId : "test-user-id" ,
9+ role : "admin" ,
10+ } ;
11+
12+ beforeEach ( ( ) => {
13+ // Create manager instance with access to private methods
14+ manager = new HeliconeSqlManager ( mockAuthParams ) ;
15+ } ) ;
16+
17+ describe ( "getRequestIdForS3" , ( ) => {
18+ test ( "should return cache reference ID when it exists and is not default UUID" , ( ) => {
19+ const requestId = "request-123" ;
20+ const cacheReferenceId = "cache-456" ;
21+
22+ const result = manager . getRequestIdForS3 ( requestId , cacheReferenceId ) ;
23+
24+ expect ( result ) . toBe ( cacheReferenceId ) ;
25+ } ) ;
26+
27+ test ( "should return request ID when cache reference ID is default UUID" , ( ) => {
28+ const requestId = "request-123" ;
29+ const cacheReferenceId = "00000000-0000-0000-0000-000000000000" ;
30+
31+ const result = manager . getRequestIdForS3 ( requestId , cacheReferenceId ) ;
32+
33+ expect ( result ) . toBe ( requestId ) ;
34+ } ) ;
35+
36+ test ( "should return request ID when cache reference ID is undefined" , ( ) => {
37+ const requestId = "request-123" ;
38+
39+ const result = manager . getRequestIdForS3 ( requestId , undefined ) ;
40+
41+ expect ( result ) . toBe ( requestId ) ;
42+ } ) ;
43+
44+ test ( "should return request ID when cache reference ID is empty string" , ( ) => {
45+ const requestId = "request-123" ;
46+ const cacheReferenceId = "" ;
47+
48+ const result = manager . getRequestIdForS3 ( requestId , cacheReferenceId ) ;
49+
50+ expect ( result ) . toBe ( requestId ) ;
51+ } ) ;
52+ } ) ;
53+
54+ describe ( "createRowWithNullBodies" , ( ) => {
55+ test ( "should add null bodies to a row" , ( ) => {
56+ const row = {
57+ request_id : "123" ,
58+ status : 200 ,
59+ model : "gpt-4" ,
60+ } ;
61+
62+ const result = manager . createRowWithNullBodies ( row ) ;
63+
64+ expect ( result ) . toEqual ( {
65+ request_id : "123" ,
66+ status : 200 ,
67+ model : "gpt-4" ,
68+ request_body : null ,
69+ response_body : null ,
70+ } ) ;
71+ } ) ;
72+
73+ test ( "should preserve existing properties when adding null bodies" , ( ) => {
74+ const row = {
75+ request_id : "123" ,
76+ existing_body : "should remain" ,
77+ nested : { data : "test" } ,
78+ array_field : [ 1 , 2 , 3 ] ,
79+ } ;
80+
81+ const result = manager . createRowWithNullBodies ( row ) ;
82+
83+ expect ( result ) . toEqual ( {
84+ request_id : "123" ,
85+ existing_body : "should remain" ,
86+ nested : { data : "test" } ,
87+ array_field : [ 1 , 2 , 3 ] ,
88+ request_body : null ,
89+ response_body : null ,
90+ } ) ;
91+ } ) ;
92+
93+ test ( "should handle empty row object" , ( ) => {
94+ const row = { } ;
95+
96+ const result = manager . createRowWithNullBodies ( row ) ;
97+
98+ expect ( result ) . toEqual ( {
99+ request_body : null ,
100+ response_body : null ,
101+ } ) ;
102+ } ) ;
103+ } ) ;
104+
105+ describe ( "enrichResultsWithS3Bodies" , ( ) => {
106+ test ( "should return empty array when no rows provided" , async ( ) => {
107+ const result = await manager . enrichResultsWithS3Bodies ( [ ] ) ;
108+ expect ( result ) . toEqual ( [ ] ) ;
109+ } ) ;
110+
111+ test ( "should return null when rows is null" , async ( ) => {
112+ const result = await manager . enrichResultsWithS3Bodies ( null ) ;
113+ expect ( result ) . toBeNull ( ) ;
114+ } ) ;
115+
116+ test ( "should return undefined when rows is undefined" , async ( ) => {
117+ const result = await manager . enrichResultsWithS3Bodies ( undefined ) ;
118+ expect ( result ) . toBeUndefined ( ) ;
119+ } ) ;
120+
121+ test ( "should return rows unchanged when request_id field is missing" , async ( ) => {
122+ const rows = [
123+ { id : "1" , status : 200 } ,
124+ { id : "2" , status : 201 } ,
125+ ] ;
126+
127+ const result = await manager . enrichResultsWithS3Bodies ( rows ) ;
128+
129+ expect ( result ) . toEqual ( rows ) ;
130+ } ) ;
131+
132+ test ( "should return rows unchanged when first row has no request_id" , async ( ) => {
133+ const rows = [
134+ { other_id : "1" , status : 200 } ,
135+ { request_id : "2" , status : 201 } , // Even though second row has request_id
136+ ] ;
137+
138+ const result = await manager . enrichResultsWithS3Bodies ( rows ) ;
139+
140+ expect ( result ) . toEqual ( rows ) ;
141+ } ) ;
142+ } ) ;
143+
144+ describe ( "fetchBodyFromS3Url" , ( ) => {
145+ test ( "should handle network errors gracefully" , async ( ) => {
146+ // Mock global fetch to simulate network error
147+ const originalFetch = global . fetch ;
148+ global . fetch = jest . fn ( ) . mockRejectedValue ( new Error ( "Network error" ) ) ;
149+
150+ const result = await manager . fetchBodyFromS3Url ( "https://example.com" ) ;
151+
152+ expect ( result ) . toBeNull ( ) ;
153+
154+ // Restore original fetch
155+ global . fetch = originalFetch ;
156+ } ) ;
157+
158+ test ( "should return null for non-ok responses" , async ( ) => {
159+ const originalFetch = global . fetch ;
160+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
161+ ok : false ,
162+ status : 404 ,
163+ } ) ;
164+
165+ const result = await manager . fetchBodyFromS3Url ( "https://example.com" ) ;
166+
167+ expect ( result ) . toBeNull ( ) ;
168+
169+ global . fetch = originalFetch ;
170+ } ) ;
171+
172+ test ( "should parse valid JSON response" , async ( ) => {
173+ const originalFetch = global . fetch ;
174+ const mockData = { request : "test request" , response : "test response" } ;
175+
176+ global . fetch = jest . fn ( ) . mockResolvedValue ( {
177+ ok : true ,
178+ json : async ( ) => mockData ,
179+ } ) ;
180+
181+ const result = await manager . fetchBodyFromS3Url ( "https://example.com" ) ;
182+
183+ expect ( result ) . toEqual ( mockData ) ;
184+
185+ global . fetch = originalFetch ;
186+ } ) ;
187+ } ) ;
188+ } ) ;
0 commit comments