-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathvertexai-to-ai.jasmine.ts
More file actions
258 lines (204 loc) · 9.29 KB
/
Copy pathvertexai-to-ai.jasmine.ts
File metadata and controls
258 lines (204 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { logging } from '@angular-devkit/core';
import { HostTree, SchematicContext } from '@angular-devkit/schematics';
import { rewriteVertexAIToAI } from './vertexai-to-ai.js';
import 'jasmine';
const context = { logger: new logging.Logger('test') } as unknown as SchematicContext;
const treeWith = (files: Record<string, string>) => {
const tree = new HostTree();
tree.create('angular.json', JSON.stringify({
projects: { app: { root: '', sourceRoot: 'src' } },
}));
Object.entries(files).forEach(([path, content]) => tree.create(path, content));
return tree;
};
describe('rewriteVertexAIToAI', () => {
it('rewrites a named import and its usages', () => {
const source = [
`import { provideVertexAI, getVertexAI, VertexAI } from '@angular/fire/vertexai';`,
`import { inject } from '@angular/core';`,
``,
`export const providers = [provideVertexAI(() => getVertexAI())];`,
`export class Foo { private ai = inject(VertexAI); }`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
const changed = rewriteVertexAIToAI(tree, context);
expect(changed).toBe(true);
const out = tree.readText('src/app/foo.ts');
expect(out).toContain(`from '@angular/fire/ai'`);
expect(out).not.toContain('vertexai');
expect(out).toContain('provideAI(() => getAI())');
expect(out).toContain('inject(AI)');
expect(out).not.toContain('VertexAI');
});
it('renames only the imported name for an aliased import, leaving usages of the alias', () => {
const source = [
`import { VertexAI as MyAI } from '@angular/fire/vertexai';`,
`import { inject } from '@angular/core';`,
`export class Foo { private ai = inject(MyAI); }`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const out = tree.readText('src/app/foo.ts');
expect(out).toContain(`import { AI as MyAI } from '@angular/fire/ai';`);
expect(out).toContain('inject(MyAI)');
});
it('handles the older vertexai-preview entry point', () => {
const tree = treeWith({
'src/app/foo.ts': `import { getVertexAI } from '@angular/fire/vertexai-preview';`,
});
rewriteVertexAIToAI(tree, context);
expect(tree.readText('src/app/foo.ts')).toBe(`import { getAI } from '@angular/fire/ai';`);
});
it('rewrites namespace-import member accesses', () => {
const source = [
`import * as vai from '@angular/fire/vertexai';`,
`export const p = vai.provideVertexAI(() => vai.getVertexAI());`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const out = tree.readText('src/app/foo.ts');
expect(out).toContain(`import * as vai from '@angular/fire/ai';`);
expect(out).toContain('vai.provideAI(() => vai.getAI())');
});
it('leaves unchanged symbols alone', () => {
const tree = treeWith({
'src/app/foo.ts': `import { getGenerativeModel, getVertexAI } from '@angular/fire/vertexai';`,
});
rewriteVertexAIToAI(tree, context);
expect(tree.readText('src/app/foo.ts'))
.toBe(`import { getGenerativeModel, getAI } from '@angular/fire/ai';`);
});
it('does not touch strings, comments, or unrelated member names (the AST win over regex)', () => {
const source = [
`import { VertexAI } from '@angular/fire/vertexai';`,
`import { inject } from '@angular/core';`,
`// VertexAI is now AI Logic`,
`export const label = 'VertexAI docs';`,
`export class Foo { VertexAI = 1; private ai = inject(VertexAI); }`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const out = tree.readText('src/app/foo.ts');
// comment and string keep the old word
expect(out).toContain('// VertexAI is now AI Logic');
expect(out).toContain(`'VertexAI docs'`);
// a class member literally named VertexAI is not the import binding, so it is untouched
expect(out).toContain('VertexAI = 1;');
// the real usage and the import are rewritten
expect(out).toContain(`from '@angular/fire/ai'`);
expect(out).toContain('inject(AI)');
});
it('is a no-op when there is nothing to rewrite', () => {
const tree = treeWith({
'src/app/foo.ts': `import { getAI } from '@angular/fire/ai';`,
});
const changed = rewriteVertexAIToAI(tree, context);
expect(changed).toBe(false);
expect(tree.readText('src/app/foo.ts')).toBe(`import { getAI } from '@angular/fire/ai';`);
});
it('does not crash without an angular.json', () => {
const tree = new HostTree();
tree.create('src/app/foo.ts', `import { getVertexAI } from '@angular/fire/vertexai';`);
expect(() => rewriteVertexAIToAI(tree, context)).not.toThrow();
});
it('rewrites files in a non-root project (library / multi-project workspace)', () => {
const tree = new HostTree();
tree.create('angular.json', JSON.stringify({
projects: {
app: { root: '', sourceRoot: 'src' },
lib: { root: 'projects/lib', sourceRoot: 'projects/lib/src' },
},
}));
tree.create('projects/lib/src/foo.ts', `import { getVertexAI } from '@angular/fire/vertexai';`);
const changed = rewriteVertexAIToAI(tree, context);
expect(changed).toBe(true);
expect(tree.readText('projects/lib/src/foo.ts')).toBe(`import { getAI } from '@angular/fire/ai';`);
});
it('renames a renamed symbol used in type position', () => {
const source = [
`import { VertexAI } from '@angular/fire/vertexai';`,
`export let x: VertexAI;`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
expect(tree.readText('src/app/foo.ts')).toContain('let x: AI;');
});
it('rewrites namespace member access in type position', () => {
const source = [
`import * as fire from '@angular/fire/vertexai';`,
`export function f(): fire.VertexAI { return fire.getVertexAI(); }`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const out = tree.readText('src/app/foo.ts');
expect(out).toContain('fire.AI');
expect(out).toContain('fire.getAI()');
expect(out).not.toContain('VertexAI');
});
it('expands a shorthand property instead of changing its key', () => {
const source = [
`import { getVertexAI } from '@angular/fire/vertexai';`,
`export const registry = { getVertexAI };`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
expect(tree.readText('src/app/foo.ts')).toContain('{ getVertexAI: getAI }');
});
it('preserves the export name for a bare local re-export', () => {
const source = [
`import { VertexAI } from '@angular/fire/vertexai';`,
`export { VertexAI };`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const out = tree.readText('src/app/foo.ts');
expect(out).toContain(`import { AI } from '@angular/fire/ai';`);
expect(out).toContain('export { AI as VertexAI };');
});
it('renames the instances token and instance observable', () => {
const tree = treeWith({
'src/app/foo.ts': `import { VertexAIInstances, vertexAIInstance$ } from '@angular/fire/vertexai';`,
});
rewriteVertexAIToAI(tree, context);
expect(tree.readText('src/app/foo.ts'))
.toBe(`import { AIInstances, AIInstance$ } from '@angular/fire/ai';`);
});
it('does not rename a get/set accessor named like an imported symbol', () => {
const source = [
`import { getVertexAI } from '@angular/fire/vertexai';`,
`export class Foo { get getVertexAI() { return 1; } }`,
`export const used = getVertexAI();`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const out = tree.readText('src/app/foo.ts');
expect(out).toContain('get getVertexAI()');
expect(out).toContain('getAI()');
});
it('does not rename a destructuring property key, but does rename a binding initializer', () => {
const source = [
`import { getVertexAI } from '@angular/fire/vertexai';`,
`export function a(obj: any) { const { getVertexAI: local } = obj; return local; }`,
`export function b({ cb = getVertexAI }: any) { return cb; }`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const out = tree.readText('src/app/foo.ts');
// the property key read from obj is not the import, so it is left untouched
expect(out).toContain('const { getVertexAI: local } = obj;');
// the default initializer IS a genuine use of the import, so it is renamed
expect(out).toContain('cb = getAI');
});
it('is idempotent when re-run on already-migrated code', () => {
const source = [
`import { provideVertexAI, getVertexAI } from '@angular/fire/vertexai';`,
`export const p = provideVertexAI(() => getVertexAI());`,
].join('\n');
const tree = treeWith({ 'src/app/foo.ts': source });
rewriteVertexAIToAI(tree, context);
const afterFirst = tree.readText('src/app/foo.ts');
const changedAgain = rewriteVertexAIToAI(tree, context);
expect(changedAgain).toBe(false);
expect(tree.readText('src/app/foo.ts')).toBe(afterFirst);
});
});