@@ -20,28 +20,130 @@ import sinonChai from 'sinon-chai';
20
20
import chaiAsPromised from 'chai-as-promised' ;
21
21
import { ChromeAdapter } from './chrome-adapter' ;
22
22
import { stub } from 'sinon' ;
23
+ import * as util from '@firebase/util' ;
23
24
24
25
use ( sinonChai ) ;
25
26
use ( chaiAsPromised ) ;
26
27
27
28
describe ( 'ChromeAdapter' , ( ) => {
28
- describe ( 'isOnDeviceRequest' , ( ) => {
29
- it ( 'returns true for simple text part' , async ( ) => {
29
+ describe ( 'isAvailable' , ( ) => {
30
+ it ( 'returns false if mode is only cloud' , async ( ) => {
31
+ const adapter = new ChromeAdapter ( undefined , 'only_in_cloud' ) ;
30
32
expect (
31
- ChromeAdapter . _isOnDeviceRequest ( {
32
- contents : [ { role : 'user' , parts : [ { text : 'hi' } ] } ]
33
+ await adapter . isAvailable ( {
34
+ contents : [ ]
33
35
} )
34
- ) . to . be . true ;
36
+ ) . to . be . false ;
35
37
} ) ;
36
- it ( 'returns false if contents empty' , async ( ) => {
38
+ it ( 'returns false if browser is not Chrome' , async ( ) => {
39
+ const chromeStub = stub ( util , 'isChrome' ) . returns ( false ) ;
40
+ const adapter = new ChromeAdapter ( undefined , 'prefer_on_device' ) ;
37
41
expect (
38
- ChromeAdapter . _isOnDeviceRequest ( {
42
+ await adapter . isAvailable ( {
39
43
contents : [ ]
40
44
} )
41
45
) . to . be . false ;
46
+ chromeStub . restore ( ) ;
47
+ } ) ;
48
+ it ( 'returns false if AI API is undefined' , async ( ) => {
49
+ const adapter = new ChromeAdapter ( undefined , 'prefer_on_device' ) ;
50
+ expect (
51
+ await adapter . isAvailable ( {
52
+ contents : [ ]
53
+ } )
54
+ ) . to . be . false ;
55
+ } ) ;
56
+ it ( 'returns false if LanguageModel API is undefined' , async ( ) => {
57
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
58
+ expect (
59
+ await adapter . isAvailable ( {
60
+ contents : [ ]
61
+ } )
62
+ ) . to . be . false ;
63
+ } ) ;
64
+ it ( 'returns false if request contents empty' , async ( ) => {
65
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
66
+ expect (
67
+ await adapter . isAvailable ( {
68
+ contents : [ ]
69
+ } )
70
+ ) . to . be . false ;
71
+ } ) ;
72
+ it ( 'returns false if request content has function role' , async ( ) => {
73
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
74
+ expect (
75
+ await adapter . isAvailable ( {
76
+ contents : [
77
+ {
78
+ role : 'function' ,
79
+ parts : [ ]
80
+ }
81
+ ]
82
+ } )
83
+ ) . to . be . false ;
84
+ } ) ;
85
+ it ( 'returns false if request content has multiple parts' , async ( ) => {
86
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
87
+ expect (
88
+ await adapter . isAvailable ( {
89
+ contents : [
90
+ {
91
+ role : 'user' ,
92
+ parts : [ { text : 'a' } , { text : 'b' } ]
93
+ }
94
+ ]
95
+ } )
96
+ ) . to . be . false ;
97
+ } ) ;
98
+ it ( 'returns false if request content has non-text part' , async ( ) => {
99
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
100
+ expect (
101
+ await adapter . isAvailable ( {
102
+ contents : [
103
+ {
104
+ role : 'user' ,
105
+ parts : [ { inlineData : { mimeType : 'a' , data : 'b' } } ]
106
+ }
107
+ ]
108
+ } )
109
+ ) . to . be . false ;
110
+ } ) ;
111
+ it ( 'returns false if request system instruction has function role' , async ( ) => {
112
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
113
+ expect (
114
+ await adapter . isAvailable ( {
115
+ contents : [ ] ,
116
+ systemInstruction : {
117
+ role : 'function' ,
118
+ parts : [ ]
119
+ }
120
+ } )
121
+ ) . to . be . false ;
122
+ } ) ;
123
+ it ( 'returns false if request system instruction has multiple parts' , async ( ) => {
124
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
125
+ expect (
126
+ await adapter . isAvailable ( {
127
+ contents : [ ] ,
128
+ systemInstruction : {
129
+ role : 'function' ,
130
+ parts : [ { text : 'a' } , { text : 'b' } ]
131
+ }
132
+ } )
133
+ ) . to . be . false ;
134
+ } ) ;
135
+ it ( 'returns false if request system instruction has non-text part' , async ( ) => {
136
+ const adapter = new ChromeAdapter ( { } as AI , 'prefer_on_device' ) ;
137
+ expect (
138
+ await adapter . isAvailable ( {
139
+ contents : [ ] ,
140
+ systemInstruction : {
141
+ role : 'function' ,
142
+ parts : [ { inlineData : { mimeType : 'a' , data : 'b' } } ]
143
+ }
144
+ } )
145
+ ) . to . be . false ;
42
146
} ) ;
43
- } ) ;
44
- describe ( 'isAvailable' , ( ) => {
45
147
it ( 'returns true if model is readily available' , async ( ) => {
46
148
const aiProvider = {
47
149
languageModel : {
0 commit comments