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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616#!/usr/bin/env node
/**
* Global Endpoint Support Tests for Google Vertex AI
* Tests the new "global" location support for Gemini 3 Pro Preview
*
* Tests:
* 1. CLI generate with global endpoint
* 2. CLI stream with global endpoint
* 3. SDK generate with global endpoint
* 4. SDK stream with global endpoint
* 5. Regional endpoint backward compatibility
* 6. Location validation (accept "global", reject "GLOBAL", "us_central1")
* 7. Default location fallback to us-central1
*/
import { spawn } from "child_process";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
// ==================== Configuration ====================
const TEST_CONFIG = {
provider: "vertex",
globalModel: "gemini-3-pro-preview", // Model that requires global endpoint
regionalModel: "gemini-2.0-flash-exp", // Model for regional testing
maxTokens: 1000,
timeout: 60000,
};
// Environment backup
const originalEnv = { ...process.env };
// ==================== Utilities ====================
interface CommandResult {
success: boolean;
stdout: string;
stderr: string;
code: number | null;
}
function logSection(title: string): void {
console.log(`\n${"=".repeat(70)}`);
console.log(` ${title}`);
console.log(`${"=".repeat(70)}\n`);
}
function logTest(
name: string,
status: "PASS" | "FAIL" | "SKIP",
details?: string,
): void {
const symbol = status === "PASS" ? "โ" : status === "FAIL" ? "โ" : "โ";
const color =
status === "PASS"
? "\x1b[32m"
: status === "FAIL"
? "\x1b[31m"
: "\x1b[33m";
console.log(`${color}${symbol}\x1b[0m ${name}`);
if (details) {
console.log(` ${details}`);
}
}
function runCommand(
command: string,
args: string[],
env?: NodeJS.ProcessEnv,
): Promise<CommandResult> {
return new Promise((resolve) => {
const child = spawn(command, args, {
env: { ...process.env, ...env },
shell: false,
});
let stdout = "";
let stderr = "";
child.stdout?.on("data", (data) => {
stdout += data.toString();
});
child.stderr?.on("data", (data) => {
stderr += data.toString();
});
const timer = setTimeout(() => {
child.kill();
resolve({
success: false,
stdout,
stderr: stderr + "\nProcess timeout",
code: null,
});
}, TEST_CONFIG.timeout);
child.on("close", (code) => {
clearTimeout(timer);
resolve({
success: code === 0,
stdout,
stderr,
code,
});
});
child.on("error", (error) => {
clearTimeout(timer);
resolve({
success: false,
stdout,
stderr: stderr + `\nProcess error: ${error.message}`,
code: null,
});
});
});
}
function buildCLIArgs(model: string, location?: string): string[] {
const args = [
`--provider=${TEST_CONFIG.provider}`,
`--model=${model}`,
`--max-tokens=${TEST_CONFIG.maxTokens}`,
];
return args;
}
function restoreEnv(): void {
// Clear all env vars
Object.keys(process.env).forEach((key) => {
if (!originalEnv[key]) {
delete process.env[key];
}
});
// Restore original values
Object.assign(process.env, originalEnv);
}
// ==================== Test Functions ====================
async function testCLIGenerateGlobalEndpoint(): Promise<boolean> {
logSection("Test 1: CLI Generate with Global Endpoint");
try {
const result = await runCommand(
"node",
[
"dist/cli/index.js",
"generate",
...buildCLIArgs(TEST_CONFIG.globalModel),
"Say 'Global endpoint test successful' and nothing else",
],
{
GOOGLE_CLOUD_LOCATION: "global",
VERTEX_LOCATION: "global",
GOOGLE_VERTEX_LOCATION: "global",
},
);
restoreEnv();
if (!result.success) {
logTest(
"CLI Generate - Global Endpoint",
"FAIL",
`Exit code: ${result.code}, Error: ${result.stderr}`,
);
return false;
}
if (
result.stdout.toLowerCase().includes("global endpoint test successful")
) {
logTest(
"CLI Generate - Global Endpoint",
"PASS",
"Response received successfully",
);
return true;
} else {
logTest("CLI Generate - Global Endpoint", "FAIL", "Unexpected response");
return false;
}
} catch (error) {
restoreEnv();
logTest("CLI Generate - Global Endpoint", "FAIL", (error as Error).message);
return false;
}
}
async function testCLIStreamGlobalEndpoint(): Promise<boolean> {
logSection("Test 2: CLI Stream with Global Endpoint");
try {
const result = await runCommand(
"node",
[
"dist/cli/index.js",
"stream",
...buildCLIArgs(TEST_CONFIG.globalModel),
"Say 'Streaming test' and nothing else",
],
{
GOOGLE_CLOUD_LOCATION: "global",
VERTEX_LOCATION: "global",
GOOGLE_VERTEX_LOCATION: "global",
},
);
restoreEnv();
if (!result.success) {
logTest(
"CLI Stream - Global Endpoint",
"FAIL",
`Exit code: ${result.code}, Error: ${result.stderr}`,
);
return false;
}
if (result.stdout.toLowerCase().includes("streaming test")) {
logTest(
"CLI Stream - Global Endpoint",
"PASS",
"Streaming response received",
);
return true;
} else {
logTest("CLI Stream - Global Endpoint", "FAIL", "Unexpected response");
return false;
}
} catch (error) {
restoreEnv();
logTest("CLI Stream - Global Endpoint", "FAIL", (error as Error).message);
return false;
}
}
async function testSDKGenerateGlobalEndpoint(): Promise<boolean> {
logSection("Test 3: SDK Generate with Global Endpoint");
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "neurolink-test-"));
const tempScriptPath = path.join(tempDir, "test-sdk-global.mjs");
try {
const testScript = `
import { NeuroLink } from '${process.cwd()}/dist/index.js';
async function testSDKGlobalEndpoint() {
process.env.GOOGLE_CLOUD_LOCATION = 'global';
process.env.VERTEX_LOCATION = 'global';
process.env.GOOGLE_VERTEX_LOCATION = 'global';
const sdk = new NeuroLink();
try {
const result = await sdk.generate({
input: {
text: "Say 'SDK global endpoint test successful' and nothing else"
},
maxTokens: ${TEST_CONFIG.maxTokens},
provider: '${TEST_CONFIG.provider}',
model: '${TEST_CONFIG.globalModel}'
});
if (result.content.toLowerCase().includes('sdk global endpoint test successful')) {
console.log('SDK Generate - Global Endpoint: PASS');
process.exit(0);
} else {
console.log('SDK Generate - Global Endpoint: FAIL - Unexpected response');
process.exit(1);
}
} catch (error) {
console.error('SDK Generate - Global Endpoint: ERROR -', error.message);
process.exit(1);
} finally {
if (sdk && typeof sdk.dispose === 'function') {
await sdk.dispose();
}
}
}
testSDKGlobalEndpoint();
`;
fs.writeFileSync(tempScriptPath, testScript);
const result = await runCommand("node", [tempScriptPath]);
if (result.success && result.stdout.includes("PASS")) {
logTest("SDK Generate - Global Endpoint", "PASS", "SDK test successful");
return true;
} else {
logTest(
"SDK Generate - Global Endpoint",
"FAIL",
result.stderr || result.stdout,
);
return false;
}
} catch (error) {
logTest("SDK Generate - Global Endpoint", "FAIL", (error as Error).message);
return false;
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
}
async function testSDKStreamGlobalEndpoint(): Promise<boolean> {
logSection("Test 4: SDK Stream with Global Endpoint");
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "neurolink-test-"));
const tempScriptPath = path.join(tempDir, "test-sdk-stream-global.mjs");
try {
const testScript = `
import { NeuroLink } from '${process.cwd()}/dist/index.js';
async function testSDKStreamGlobal() {
process.env.GOOGLE_CLOUD_LOCATION = 'global';
process.env.VERTEX_LOCATION = 'global';
process.env.GOOGLE_VERTEX_LOCATION = 'global';
const sdk = new NeuroLink();
try {
let fullContent = '';
const result = await sdk.stream({
input: {
text: "Say 'SDK streaming test' and nothing else"
},
maxTokens: ${TEST_CONFIG.maxTokens},
provider: '${TEST_CONFIG.provider}',
model: '${TEST_CONFIG.globalModel}'
});
for await (const chunk of result.stream) {
fullContent += chunk.content;
}
if (fullContent.toLowerCase().includes('sdk streaming test')) {
console.log('SDK Stream - Global Endpoint: PASS');
process.exit(0);
} else {
console.log('SDK Stream - Global Endpoint: FAIL - Unexpected response');
process.exit(1);
}
} catch (error) {
console.error('SDK Stream - Global Endpoint: ERROR -', error.message);
process.exit(1);
} finally {
if (sdk && typeof sdk.dispose === 'function') {
await sdk.dispose();
}
}
}
testSDKStreamGlobal();
`;
fs.writeFileSync(tempScriptPath, testScript);
const result = await runCommand("node", [tempScriptPath]);
if (result.success && result.stdout.includes("PASS")) {
logTest(
"SDK Stream - Global Endpoint",
"PASS",
"SDK streaming test successful",
);
return true;
} else {
logTest(
"SDK Stream - Global Endpoint",
"FAIL",
result.stderr || result.stdout,
);
return false;
}
} catch (error) {
logTest("SDK Stream - Global Endpoint", "FAIL", (error as Error).message);
return false;
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
}
async function testRegionalEndpointBackwardCompatibility(): Promise<boolean> {
logSection("Test 5: Regional Endpoint Backward Compatibility");
try {
const result = await runCommand(
"node",
[
"dist/cli/index.js",
"generate",
...buildCLIArgs(TEST_CONFIG.regionalModel),
"Say 'Regional endpoint works' and nothing else",
],
{
GOOGLE_CLOUD_LOCATION: "us-central1",
VERTEX_LOCATION: "us-central1",
GOOGLE_VERTEX_LOCATION: "us-central1",
},
);
restoreEnv();
if (!result.success) {
logTest(
"Regional Endpoint - Backward Compatibility",
"FAIL",
`Exit code: ${result.code}, Error: ${result.stderr}`,
);
return false;
}
if (result.stdout.toLowerCase().includes("regional endpoint works")) {
logTest(
"Regional Endpoint - Backward Compatibility",
"PASS",
"Regional endpoint us-central1 works correctly",
);
return true;
} else {
logTest(
"Regional Endpoint - Backward Compatibility",
"FAIL",
"Unexpected response",
);
return false;
}
} catch (error) {
restoreEnv();
logTest(
"Regional Endpoint - Backward Compatibility",
"FAIL",
(error as Error).message,
);
return false;
}
}
async function testLocationValidation(): Promise<boolean> {
logSection("Test 6: Location Validation");
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "neurolink-test-"));
const tempScriptPath = path.join(tempDir, "test-validation.mjs");
try {
const testScript = `
import { NeuroLink } from '${process.cwd()}/dist/index.js';
async function testValidation() {
const validLocations = ['global', 'us-central1', 'europe-west1', 'asia-east1'];
const invalidLocations = ['GLOBAL', 'us_central1', 'US-CENTRAL1', 'invalid'];
let passCount = 0;
let failCount = 0;
// Test valid locations - these should work
for (const location of validLocations) {
process.env.GOOGLE_CLOUD_LOCATION = location;
process.env.VERTEX_LOCATION = location;
process.env.GOOGLE_VERTEX_LOCATION = location;
const sdk = new NeuroLink();
try {
const model = location === 'global' ? '${TEST_CONFIG.globalModel}' : '${TEST_CONFIG.regionalModel}';
const result = await sdk.generate({
input: { text: "Test" },
maxTokens: 10,
provider: '${TEST_CONFIG.provider}',
model: model
});
console.log(\`โ Valid location '\${location}' accepted\`);
passCount++;
} catch (error) {
console.log(\`โ Valid location '\${location}' rejected: \${error.message}\`);
failCount++;
} finally {
await sdk.dispose();
}
}
// Test invalid locations - these should fail with validation error
for (const location of invalidLocations) {
process.env.GOOGLE_CLOUD_LOCATION = location;
process.env.VERTEX_LOCATION = location;
process.env.GOOGLE_VERTEX_LOCATION = location;
const sdk = new NeuroLink();
try {
const result = await sdk.generate({
input: { text: "Test" },
maxTokens: 10,
provider: '${TEST_CONFIG.provider}',
model: '${TEST_CONFIG.regionalModel}'
});
console.log(\`โ Invalid location '\${location}' was accepted (should have been rejected)\`);
failCount++;
} catch (error) {
if (error.message.includes('location') || error.message.includes('region')) {
console.log(\`โ Invalid location '\${location}' rejected correctly\`);
passCount++;
} else {
console.log(\`โ Invalid location '\${location}' failed with wrong error: \${error.message}\`);
failCount++;
}
} finally {
await sdk.dispose();
}
}
console.log(\`\nValidation Results: \${passCount} passed, \${failCount} failed\`);
process.exit(failCount === 0 ? 0 : 1);
}
testValidation();
`;
fs.writeFileSync(tempScriptPath, testScript);
const result = await runCommand("node", [tempScriptPath]);
if (result.success) {
logTest("Location Validation", "PASS", result.stdout);
return true;
} else {
logTest("Location Validation", "FAIL", result.stderr || result.stdout);
return false;
}
} catch (error) {
logTest("Location Validation", "FAIL", (error as Error).message);
return false;
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
}
}
async function testDefaultLocationFallback(): Promise<boolean> {
logSection("Test 7: Default Location Fallback");
try {
const result = await runCommand(
"node",
[
"dist/cli/index.js",
"generate",
...buildCLIArgs(TEST_CONFIG.regionalModel),
"Say 'Default location test' and nothing else",
],
{
GOOGLE_CLOUD_LOCATION: "",
VERTEX_LOCATION: "",
GOOGLE_VERTEX_LOCATION: "",
},
);
restoreEnv();
if (!result.success) {
logTest(
"Default Location Fallback",
"FAIL",
`Exit code: ${result.code}, Error: ${result.stderr}`,
);
return false;
}
if (result.stdout.toLowerCase().includes("default location test")) {
logTest(
"Default Location Fallback",
"PASS",
"Default fallback to us-central1 works correctly",
);
return true;
} else {
logTest("Default Location Fallback", "FAIL", "Unexpected response");
return false;
}
} catch (error) {
restoreEnv();
logTest("Default Location Fallback", "FAIL", (error as Error).message);
return false;
}
}
// ==================== Main Execution ====================
async function runAllTests(): Promise<void> {
console.log("\n" + "=".repeat(70));
console.log(" GLOBAL ENDPOINT SUPPORT - TEST SUITE");
console.log("=".repeat(70));
const results: boolean[] = [];
// Run all tests
results.push(await testCLIGenerateGlobalEndpoint());
results.push(await testCLIStreamGlobalEndpoint());
results.push(await testSDKGenerateGlobalEndpoint());
results.push(await testSDKStreamGlobalEndpoint());
results.push(await testRegionalEndpointBackwardCompatibility());
results.push(await testLocationValidation());
results.push(await testDefaultLocationFallback());
// Summary
logSection("Test Summary");
const passed = results.filter((r) => r).length;
const failed = results.length - passed;
console.log(`Total Tests: ${results.length}`);
console.log(`\x1b[32mโ Passed: ${passed}\x1b[0m`);
console.log(`\x1b[31mโ Failed: ${failed}\x1b[0m`);
process.exit(failed === 0 ? 0 : 1);
}
runAllTests();