@@ -25,6 +25,7 @@ import { countTokens } from './count-tokens';
25
25
import { CountTokensRequest } from '../types' ;
26
26
import { ApiSettings } from '../types/internal' ;
27
27
import { Task } from '../requests/request' ;
28
+ import { ChromeAdapter } from './chrome-adapter' ;
28
29
29
30
use ( sinonChai ) ;
30
31
use ( chaiAsPromised ) ;
@@ -55,7 +56,8 @@ describe('countTokens()', () => {
55
56
const result = await countTokens (
56
57
fakeApiSettings ,
57
58
'model' ,
58
- fakeRequestParams
59
+ fakeRequestParams ,
60
+ new ChromeAdapter ( )
59
61
) ;
60
62
expect ( result . totalTokens ) . to . equal ( 6 ) ;
61
63
expect ( result . totalBillableCharacters ) . to . equal ( 16 ) ;
@@ -81,7 +83,8 @@ describe('countTokens()', () => {
81
83
const result = await countTokens (
82
84
fakeApiSettings ,
83
85
'model' ,
84
- fakeRequestParams
86
+ fakeRequestParams ,
87
+ new ChromeAdapter ( )
85
88
) ;
86
89
expect ( result . totalTokens ) . to . equal ( 1837 ) ;
87
90
expect ( result . totalBillableCharacters ) . to . equal ( 117 ) ;
@@ -109,7 +112,8 @@ describe('countTokens()', () => {
109
112
const result = await countTokens (
110
113
fakeApiSettings ,
111
114
'model' ,
112
- fakeRequestParams
115
+ fakeRequestParams ,
116
+ new ChromeAdapter ( )
113
117
) ;
114
118
expect ( result . totalTokens ) . to . equal ( 258 ) ;
115
119
expect ( result ) . to . not . have . property ( 'totalBillableCharacters' ) ;
@@ -135,8 +139,33 @@ describe('countTokens()', () => {
135
139
json : mockResponse . json
136
140
} as Response ) ;
137
141
await expect (
138
- countTokens ( fakeApiSettings , 'model' , fakeRequestParams )
142
+ countTokens (
143
+ fakeApiSettings ,
144
+ 'model' ,
145
+ fakeRequestParams ,
146
+ new ChromeAdapter ( )
147
+ )
139
148
) . to . be . rejectedWith ( / 4 0 4 .* n o t f o u n d / ) ;
140
149
expect ( mockFetch ) . to . be . called ;
141
150
} ) ;
151
+ it ( 'on-device' , async ( ) => {
152
+ const chromeAdapter = new ChromeAdapter ( ) ;
153
+ const isAvailableStub = stub ( chromeAdapter , 'isAvailable' ) . resolves ( true ) ;
154
+ const mockResponse = getMockResponse (
155
+ 'vertexAI' ,
156
+ 'unary-success-total-tokens.json'
157
+ ) ;
158
+ const countTokensStub = stub ( chromeAdapter , 'countTokens' ) . resolves (
159
+ mockResponse as Response
160
+ ) ;
161
+ const result = await countTokens (
162
+ fakeApiSettings ,
163
+ 'model' ,
164
+ fakeRequestParams ,
165
+ chromeAdapter
166
+ ) ;
167
+ expect ( result . totalTokens ) . eq ( 6 ) ;
168
+ expect ( isAvailableStub ) . to . be . called ;
169
+ expect ( countTokensStub ) . to . be . calledWith ( fakeRequestParams ) ;
170
+ } ) ;
142
171
} ) ;
0 commit comments