14
14
* See the License for the specific language governing permissions and
15
15
* limitations under the License.
16
16
*/
17
- import { describe , expect , it , afterEach , jest } from '@jest/globals' ;
18
- import { getMockResponse } from './test-utils/mock-response' ;
17
+ import { describe , expect , it , afterEach , jest , beforeEach } from '@jest/globals' ;
18
+ import { BackendName , getMockResponse } from './test-utils/mock-response' ;
19
19
import * as request from '../lib/requests/request' ;
20
20
import { countTokens } from '../lib/methods/count-tokens' ;
21
- import { CountTokensRequest } from '../lib/types' ;
21
+ import { CountTokensRequest , RequestOptions } from '../lib/types' ;
22
22
import { ApiSettings } from '../lib/types/internal' ;
23
23
import { Task } from '../lib/requests/request' ;
24
24
import { GoogleAIBackend } from '../lib/backend' ;
25
+ import { SpiedFunction } from 'jest-mock' ;
26
+ import { mapCountTokensRequest } from '../lib/googleai-mappers' ;
25
27
26
28
const fakeApiSettings : ApiSettings = {
27
29
apiKey : 'key' ,
@@ -31,6 +33,14 @@ const fakeApiSettings: ApiSettings = {
31
33
backend : new GoogleAIBackend ( ) ,
32
34
} ;
33
35
36
+ const fakeGoogleAIApiSettings : ApiSettings = {
37
+ apiKey : 'key' ,
38
+ project : 'my-project' ,
39
+ appId : 'my-appid' ,
40
+ location : '' ,
41
+ backend : new GoogleAIBackend ( ) ,
42
+ } ;
43
+
34
44
const fakeRequestParams : CountTokensRequest = {
35
45
contents : [ { parts : [ { text : 'hello' } ] , role : 'user' } ] ,
36
46
} ;
@@ -41,7 +51,7 @@ describe('countTokens()', () => {
41
51
} ) ;
42
52
43
53
it ( 'total tokens' , async ( ) => {
44
- const mockResponse = getMockResponse ( 'unary-success-total-tokens.json' ) ;
54
+ const mockResponse = getMockResponse ( BackendName . VertexAI , 'unary-success-total-tokens.json' ) ;
45
55
const makeRequestStub = jest
46
56
. spyOn ( request , 'makeRequest' )
47
57
. mockResolvedValue ( mockResponse as Response ) ;
@@ -58,8 +68,35 @@ describe('countTokens()', () => {
58
68
) ;
59
69
} ) ;
60
70
71
+ it ( 'total tokens with modality details' , async ( ) => {
72
+ const mockResponse = getMockResponse (
73
+ BackendName . VertexAI ,
74
+ 'unary-success-detailed-token-response.json' ,
75
+ ) ;
76
+ const makeRequestStub = jest
77
+ . spyOn ( request , 'makeRequest' )
78
+ . mockResolvedValue ( mockResponse as Response ) ;
79
+ const result = await countTokens ( fakeApiSettings , 'model' , fakeRequestParams ) ;
80
+
81
+ expect ( result . totalTokens ) . toBe ( 1837 ) ;
82
+ expect ( result . totalBillableCharacters ) . toBe ( 117 ) ;
83
+ expect ( result . promptTokensDetails ?. [ 0 ] ?. modality ) . toBe ( 'IMAGE' ) ;
84
+ expect ( result . promptTokensDetails ?. [ 0 ] ?. tokenCount ) . toBe ( 1806 ) ;
85
+ expect ( makeRequestStub ) . toHaveBeenCalledWith (
86
+ 'model' ,
87
+ Task . COUNT_TOKENS ,
88
+ fakeApiSettings ,
89
+ false ,
90
+ expect . stringContaining ( 'contents' ) ,
91
+ undefined ,
92
+ ) ;
93
+ } ) ;
94
+
61
95
it ( 'total tokens no billable characters' , async ( ) => {
62
- const mockResponse = getMockResponse ( 'unary-success-no-billable-characters.json' ) ;
96
+ const mockResponse = getMockResponse (
97
+ BackendName . VertexAI ,
98
+ 'unary-success-no-billable-characters.json' ,
99
+ ) ;
63
100
const makeRequestStub = jest
64
101
. spyOn ( request , 'makeRequest' )
65
102
. mockResolvedValue ( mockResponse as Response ) ;
@@ -77,7 +114,10 @@ describe('countTokens()', () => {
77
114
} ) ;
78
115
79
116
it ( 'model not found' , async ( ) => {
80
- const mockResponse = getMockResponse ( 'unary-failure-model-not-found.json' ) ;
117
+ const mockResponse = getMockResponse (
118
+ BackendName . VertexAI ,
119
+ 'unary-failure-model-not-found.json' ,
120
+ ) ;
81
121
const mockFetch = jest . spyOn ( globalThis , 'fetch' ) . mockResolvedValue ( {
82
122
ok : false ,
83
123
status : 404 ,
@@ -88,4 +128,40 @@ describe('countTokens()', () => {
88
128
) ;
89
129
expect ( mockFetch ) . toHaveBeenCalled ( ) ;
90
130
} ) ;
131
+
132
+ describe ( 'googleAI' , ( ) => {
133
+ let makeRequestStub : SpiedFunction <
134
+ (
135
+ model : string ,
136
+ task : Task ,
137
+ apiSettings : ApiSettings ,
138
+ stream : boolean ,
139
+ body : string ,
140
+ requestOptions ?: RequestOptions ,
141
+ ) => Promise < Response >
142
+ > ;
143
+
144
+ beforeEach ( ( ) => {
145
+ makeRequestStub = jest . spyOn ( request , 'makeRequest' ) ;
146
+ } ) ;
147
+
148
+ afterEach ( ( ) => {
149
+ jest . restoreAllMocks ( ) ;
150
+ } ) ;
151
+
152
+ it ( 'maps request to GoogleAI format' , async ( ) => {
153
+ makeRequestStub . mockResolvedValue ( { ok : true , json : ( ) => { } } as Response ) ; // Unused
154
+
155
+ await countTokens ( fakeGoogleAIApiSettings , 'model' , fakeRequestParams ) ;
156
+
157
+ expect ( makeRequestStub ) . toHaveBeenCalledWith (
158
+ 'model' ,
159
+ Task . COUNT_TOKENS ,
160
+ fakeGoogleAIApiSettings ,
161
+ false ,
162
+ JSON . stringify ( mapCountTokensRequest ( fakeRequestParams , 'model' ) ) ,
163
+ undefined ,
164
+ ) ;
165
+ } ) ;
166
+ } ) ;
91
167
} ) ;
0 commit comments