@@ -25,6 +25,7 @@ import { countTokens } from './count-tokens';
2525import { CountTokensRequest } from '../types' ;
2626import { ApiSettings } from '../types/internal' ;
2727import { Task } from '../requests/request' ;
28+ import { ChromeAdapter } from './chrome-adapter' ;
2829
2930use ( sinonChai ) ;
3031use ( chaiAsPromised ) ;
@@ -55,7 +56,8 @@ describe('countTokens()', () => {
5556 const result = await countTokens (
5657 fakeApiSettings ,
5758 'model' ,
58- fakeRequestParams
59+ fakeRequestParams ,
60+ new ChromeAdapter ( )
5961 ) ;
6062 expect ( result . totalTokens ) . to . equal ( 6 ) ;
6163 expect ( result . totalBillableCharacters ) . to . equal ( 16 ) ;
@@ -81,7 +83,8 @@ describe('countTokens()', () => {
8183 const result = await countTokens (
8284 fakeApiSettings ,
8385 'model' ,
84- fakeRequestParams
86+ fakeRequestParams ,
87+ new ChromeAdapter ( )
8588 ) ;
8689 expect ( result . totalTokens ) . to . equal ( 1837 ) ;
8790 expect ( result . totalBillableCharacters ) . to . equal ( 117 ) ;
@@ -109,7 +112,8 @@ describe('countTokens()', () => {
109112 const result = await countTokens (
110113 fakeApiSettings ,
111114 'model' ,
112- fakeRequestParams
115+ fakeRequestParams ,
116+ new ChromeAdapter ( )
113117 ) ;
114118 expect ( result . totalTokens ) . to . equal ( 258 ) ;
115119 expect ( result ) . to . not . have . property ( 'totalBillableCharacters' ) ;
@@ -135,8 +139,33 @@ describe('countTokens()', () => {
135139 json : mockResponse . json
136140 } as Response ) ;
137141 await expect (
138- countTokens ( fakeApiSettings , 'model' , fakeRequestParams )
142+ countTokens (
143+ fakeApiSettings ,
144+ 'model' ,
145+ fakeRequestParams ,
146+ new ChromeAdapter ( )
147+ )
139148 ) . to . be . rejectedWith ( / 4 0 4 .* n o t f o u n d / ) ;
140149 expect ( mockFetch ) . to . be . called ;
141150 } ) ;
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+ } ) ;
142171} ) ;
0 commit comments