1515 * limitations under the License.
1616 */
1717
18+ import { VertexAIError } from '../errors' ;
1819import { expect , use } from 'chai' ;
1920import sinonChai from 'sinon-chai' ;
2021import chaiAsPromised from 'chai-as-promised' ;
@@ -26,7 +27,7 @@ import {
2627 LanguageModelMessageContent
2728} from '../types/language-model' ;
2829import { match , stub } from 'sinon' ;
29- import { GenerateContentRequest } from '../types' ;
30+ import { GenerateContentRequest , VertexAIErrorCode } from '../types' ;
3031
3132use ( sinonChai ) ;
3233use ( chaiAsPromised ) ;
@@ -363,17 +364,8 @@ describe('ChromeAdapter', () => {
363364 } ) ;
364365 } ) ;
365366 describe ( 'countTokens' , ( ) => {
366- it ( 'counts tokens from a singular input ' , async ( ) => {
367+ it ( 'counts tokens is not yet available ' , async ( ) => {
367368 const inputText = 'first' ;
368- const expectedCount = 10 ;
369- const onDeviceParams = {
370- systemPrompt : 'be yourself'
371- } as LanguageModelCreateOptions ;
372- const expectedOnDeviceParams = {
373- systemPrompt : 'be yourself' ,
374- expectedInputs : [ { type : 'image' } ]
375- } as LanguageModelCreateOptions ;
376-
377369 // setting up stubs
378370 const languageModelProvider = {
379371 create : ( ) => Promise . resolve ( { } )
@@ -385,34 +377,27 @@ describe('ChromeAdapter', () => {
385377 languageModel
386378 ) ;
387379
388- // overrides impl with stub method
389- const measureInputUsageStub = stub (
390- languageModel ,
391- 'measureInputUsage'
392- ) . resolves ( expectedCount ) ;
393-
394380 const adapter = new ChromeAdapter (
395381 languageModelProvider ,
396- 'prefer_on_device' ,
397- onDeviceParams
382+ 'prefer_on_device'
398383 ) ;
399384
400385 const countTokenRequest = {
401386 contents : [ { role : 'user' , parts : [ { text : inputText } ] } ]
402387 } as GenerateContentRequest ;
403- const response = await adapter . countTokens ( countTokenRequest ) ;
404- // Asserts initialization params are proxied.
405- expect ( createStub ) . to . have . been . calledOnceWith ( expectedOnDeviceParams ) ;
406- // Asserts Vertex input type is mapped to Chrome type.
407- expect ( measureInputUsageStub ) . to . have . been . calledOnceWith ( [
408- {
409- type : 'text' ,
410- content : inputText
411- }
412- ] ) ;
413- expect ( await response . json ( ) ) . to . deep . equal ( {
414- totalTokens : expectedCount
415- } ) ;
388+
389+ try {
390+ await adapter . countTokens ( countTokenRequest ) ;
391+ } catch ( e ) {
392+ // the call to countToken should be rejected with Error
393+ expect ( ( e as VertexAIError ) . code ) . to . equal (
394+ VertexAIErrorCode . REQUEST_ERROR
395+ ) ;
396+ expect ( ( e as VertexAIError ) . message ) . includes ( 'not yet available' ) ;
397+ }
398+
399+ // Asserts that no language model was initialized
400+ expect ( createStub ) . not . called ;
416401 } ) ;
417402 } ) ;
418403 describe ( 'generateContentStream' , ( ) => {
0 commit comments