15
15
* limitations under the License.
16
16
*/
17
17
import { describe , expect , it } from '@jest/globals' ;
18
- import { getApp , type ReactNativeFirebase } from '../../app/lib' ;
18
+ import { type ReactNativeFirebase } from '../../app/lib' ;
19
19
20
- import { ModelParams , VertexAIErrorCode } from '../lib/types' ;
21
- import { VertexAIError } from '../lib/errors' ;
22
- import { getGenerativeModel , getVertexAI } from '../lib/index' ;
20
+ import { ModelParams , AIErrorCode } from '../lib/types' ;
21
+ import { AIError } from '../lib/errors' ;
22
+ import { getGenerativeModel } from '../lib/index' ;
23
23
24
- import { VertexAI } from '../lib/public-types' ;
24
+ import { AI } from '../lib/public-types' ;
25
25
import { GenerativeModel } from '../lib/models/generative-model' ;
26
26
27
- import '../../auth/lib' ;
28
- import '../../app-check/lib' ;
29
- import { getAuth } from '../../auth/lib' ;
27
+ import { AI_TYPE } from '../lib/constants' ;
28
+ import { VertexAIBackend } from '../lib/backend' ;
30
29
31
- const fakeVertexAI : VertexAI = {
30
+ const fakeAI : AI = {
32
31
app : {
33
32
name : 'DEFAULT' ,
34
33
options : {
@@ -37,66 +36,76 @@ const fakeVertexAI: VertexAI = {
37
36
projectId : 'my-project' ,
38
37
} ,
39
38
} as ReactNativeFirebase . FirebaseApp ,
39
+ backend : new VertexAIBackend ( 'us-central1' ) ,
40
40
location : 'us-central1' ,
41
41
} ;
42
42
43
43
describe ( 'Top level API' , ( ) => {
44
- it ( 'should allow auth and app check instances to be passed in' , ( ) => {
45
- const app = getApp ( ) ;
46
- const auth = getAuth ( ) ;
47
- const appCheck = app . appCheck ( ) ;
48
-
49
- getVertexAI ( app , { appCheck, auth } ) ;
50
- } ) ;
51
-
52
44
it ( 'getGenerativeModel throws if no model is provided' , ( ) => {
53
45
try {
54
- getGenerativeModel ( fakeVertexAI , { } as ModelParams ) ;
46
+ getGenerativeModel ( fakeAI , { } as ModelParams ) ;
55
47
} catch ( e ) {
56
- expect ( ( e as VertexAIError ) . code ) . toContain ( VertexAIErrorCode . NO_MODEL ) ;
57
- expect ( ( e as VertexAIError ) . message ) . toContain (
48
+ expect ( ( e as AIError ) . code ) . toContain ( AIErrorCode . NO_MODEL ) ;
49
+ expect ( ( e as AIError ) . message ) . toContain (
58
50
`VertexAI: Must provide a model name. Example: ` +
59
- `getGenerativeModel({ model: 'my-model-name' }) (vertexAI/${ VertexAIErrorCode . NO_MODEL } )` ,
51
+ `getGenerativeModel({ model: 'my-model-name' }) (vertexAI/${ AIErrorCode . NO_MODEL } )` ,
60
52
) ;
61
53
}
62
54
} ) ;
63
55
64
56
it ( 'getGenerativeModel throws if no apiKey is provided' , ( ) => {
65
57
const fakeVertexNoApiKey = {
66
- ...fakeVertexAI ,
67
- app : { options : { projectId : 'my-project' } } ,
68
- } as VertexAI ;
58
+ ...fakeAI ,
59
+ app : { options : { projectId : 'my-project' , appId : 'my-appid' } } ,
60
+ } as AI ;
69
61
try {
70
62
getGenerativeModel ( fakeVertexNoApiKey , { model : 'my-model' } ) ;
71
63
} catch ( e ) {
72
- expect ( ( e as VertexAIError ) . code ) . toContain ( VertexAIErrorCode . NO_API_KEY ) ;
73
- expect ( ( e as VertexAIError ) . message ) . toBe (
64
+ expect ( ( e as AIError ) . code ) . toContain ( AIErrorCode . NO_API_KEY ) ;
65
+ expect ( ( e as AIError ) . message ) . toBe (
74
66
`VertexAI: The "apiKey" field is empty in the local ` +
75
67
`Firebase config. Firebase VertexAI requires this field to` +
76
- ` contain a valid API key. (vertexAI/${ VertexAIErrorCode . NO_API_KEY } )` ,
68
+ ` contain a valid API key. (vertexAI/${ AIErrorCode . NO_API_KEY } )` ,
77
69
) ;
78
70
}
79
71
} ) ;
80
72
81
73
it ( 'getGenerativeModel throws if no projectId is provided' , ( ) => {
82
74
const fakeVertexNoProject = {
83
- ...fakeVertexAI ,
75
+ ...fakeAI ,
84
76
app : { options : { apiKey : 'my-key' } } ,
85
- } as VertexAI ;
77
+ } as AI ;
86
78
try {
87
79
getGenerativeModel ( fakeVertexNoProject , { model : 'my-model' } ) ;
88
80
} catch ( e ) {
89
- expect ( ( e as VertexAIError ) . code ) . toContain ( VertexAIErrorCode . NO_PROJECT_ID ) ;
90
- expect ( ( e as VertexAIError ) . message ) . toBe (
81
+ expect ( ( e as AIError ) . code ) . toContain ( AIErrorCode . NO_PROJECT_ID ) ;
82
+ expect ( ( e as AIError ) . message ) . toBe (
91
83
`VertexAI: The "projectId" field is empty in the local` +
92
84
` Firebase config. Firebase VertexAI requires this field ` +
93
- `to contain a valid project ID. (vertexAI/${ VertexAIErrorCode . NO_PROJECT_ID } )` ,
85
+ `to contain a valid project ID. (vertexAI/${ AIErrorCode . NO_PROJECT_ID } )` ,
86
+ ) ;
87
+ }
88
+ } ) ;
89
+
90
+ it ( 'getGenerativeModel throws if no appId is provided' , ( ) => {
91
+ const fakeVertexNoProject = {
92
+ ...fakeAI ,
93
+ app : { options : { apiKey : 'my-key' , projectId : 'my-projectid' } } ,
94
+ } as AI ;
95
+ try {
96
+ getGenerativeModel ( fakeVertexNoProject , { model : 'my-model' } ) ;
97
+ } catch ( e ) {
98
+ expect ( ( e as AIError ) . code ) . toContain ( AIErrorCode . NO_APP_ID ) ;
99
+ expect ( ( e as AIError ) . message ) . toBe (
100
+ `AI: The "appId" field is empty in the local` +
101
+ ` Firebase config. Firebase AI requires this field ` +
102
+ `to contain a valid app ID. (${ AI_TYPE } /${ AIErrorCode . NO_APP_ID } )` ,
94
103
) ;
95
104
}
96
105
} ) ;
97
106
98
107
it ( 'getGenerativeModel gets a GenerativeModel' , ( ) => {
99
- const genModel = getGenerativeModel ( fakeVertexAI , { model : 'my-model' } ) ;
108
+ const genModel = getGenerativeModel ( fakeAI , { model : 'my-model' } ) ;
100
109
expect ( genModel ) . toBeInstanceOf ( GenerativeModel ) ;
101
110
expect ( genModel . model ) . toBe ( 'publishers/google/models/my-model' ) ;
102
111
} ) ;
0 commit comments