-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathapi-proxy-service-env-forwarding.test.ts
More file actions
686 lines (617 loc) · 36.5 KB
/
Copy pathapi-proxy-service-env-forwarding.test.ts
File metadata and controls
686 lines (617 loc) · 36.5 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
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
import { generateDockerCompose, WrapperConfig, baseConfig, useTempWorkDir } from './service-test-setup.test-utils';
import { mockNetworkConfigWithProxy } from './api-proxy-service.test-utils';
import * as fs from 'fs';
import * as path from 'path';
// Create mock functions (must remain per-file — jest.mock() is hoisted before imports)
// Mock execa module
// eslint-disable-next-line @typescript-eslint/no-require-imports
jest.mock('execa', () => require('../test-helpers/mock-execa.test-utils').execaMockFactory());
let mockConfig: WrapperConfig;
describe('API proxy sidecar: env var forwarding', () => {
useTempWorkDir(
baseConfig,
(config) => {
mockConfig = config;
},
() => mockConfig
);
describe('OIDC runtime env forwarding', () => {
let savedEnv: Record<string, string | undefined>;
const oidcVars = [
'AWF_AUTH_TYPE',
'ACTIONS_ID_TOKEN_REQUEST_URL',
'ACTIONS_ID_TOKEN_REQUEST_TOKEN',
];
beforeEach(() => {
savedEnv = {};
for (const key of oidcVars) {
savedEnv[key] = process.env[key];
delete process.env[key];
}
});
afterEach(() => {
for (const key of oidcVars) {
if (savedEnv[key] !== undefined) {
process.env[key] = savedEnv[key];
} else {
delete process.env[key];
}
}
});
it('should forward ACTIONS_ID_TOKEN_REQUEST_* when AWF_AUTH_TYPE normalizes to github-oidc', () => {
process.env.AWF_AUTH_TYPE = ' GitHub-OIDC ';
process.env.ACTIONS_ID_TOKEN_REQUEST_URL = 'https://actions.local/token';
process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN = 'runtime-token';
const config = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-openai-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
expect(env.ACTIONS_ID_TOKEN_REQUEST_URL).toBe('https://actions.local/token');
expect(env.ACTIONS_ID_TOKEN_REQUEST_TOKEN).toBe('runtime-token');
});
it('should not forward ACTIONS_ID_TOKEN_REQUEST_* when AWF_AUTH_TYPE is not github-oidc', () => {
process.env.AWF_AUTH_TYPE = 'api-key';
process.env.ACTIONS_ID_TOKEN_REQUEST_URL = 'https://actions.local/token';
process.env.ACTIONS_ID_TOKEN_REQUEST_TOKEN = 'runtime-token';
const config = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-openai-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
expect(env.ACTIONS_ID_TOKEN_REQUEST_URL).toBeUndefined();
expect(env.ACTIONS_ID_TOKEN_REQUEST_TOKEN).toBeUndefined();
});
});
describe('AWF_ANTHROPIC_* env var forwarding', () => {
let savedEnv: Record<string, string | undefined>;
const anthropicVars = [
'AWF_ANTHROPIC_AUTO_CACHE',
'AWF_ANTHROPIC_CACHE_TAIL_TTL',
'AWF_ANTHROPIC_DROP_TOOLS',
'AWF_ANTHROPIC_STRIP_ANSI',
];
beforeEach(() => {
savedEnv = {};
for (const key of anthropicVars) {
savedEnv[key] = process.env[key];
delete process.env[key];
}
});
afterEach(() => {
for (const key of anthropicVars) {
if (savedEnv[key] !== undefined) {
process.env[key] = savedEnv[key];
} else {
delete process.env[key];
}
}
});
it('should forward AWF_ANTHROPIC_AUTO_CACHE to api-proxy when set', () => {
process.env.AWF_ANTHROPIC_AUTO_CACHE = '1';
const config = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
expect(env.AWF_ANTHROPIC_AUTO_CACHE).toBe('1');
});
it('should not set AWF_ANTHROPIC_AUTO_CACHE when env var is not set', () => {
const config = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
expect(env.AWF_ANTHROPIC_AUTO_CACHE).toBeUndefined();
});
it('should forward AWF_ANTHROPIC_CACHE_TAIL_TTL to api-proxy when set', () => {
process.env.AWF_ANTHROPIC_CACHE_TAIL_TTL = '1h';
const config = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
expect(env.AWF_ANTHROPIC_CACHE_TAIL_TTL).toBe('1h');
});
it('should forward AWF_ANTHROPIC_DROP_TOOLS to api-proxy when set', () => {
process.env.AWF_ANTHROPIC_DROP_TOOLS = 'NotebookEdit,CronCreate';
const config = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
expect(env.AWF_ANTHROPIC_DROP_TOOLS).toBe('NotebookEdit,CronCreate');
});
it('should forward AWF_ANTHROPIC_STRIP_ANSI to api-proxy when set', () => {
process.env.AWF_ANTHROPIC_STRIP_ANSI = '1';
const config = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
expect(env.AWF_ANTHROPIC_STRIP_ANSI).toBe('1');
});
it('should not set any AWF_ANTHROPIC_* vars when none are set in host env', () => {
const config = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test' };
const result = generateDockerCompose(config, mockNetworkConfigWithProxy);
const env = result.services['api-proxy'].environment as Record<string, string>;
for (const key of anthropicVars) {
expect(env[key]).toBeUndefined();
}
});
});
it('should set OPENAI_API_TARGET in api-proxy when openaiApiTarget is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key', openaiApiTarget: 'custom.openai-router.internal' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.OPENAI_API_TARGET).toBe('custom.openai-router.internal');
});
it('should not set OPENAI_API_TARGET in api-proxy when openaiApiTarget is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.OPENAI_API_TARGET).toBeUndefined();
});
it('should set OPENAI_API_BASE_PATH in api-proxy when openaiApiBasePath is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key', openaiApiBasePath: '/serving-endpoints' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.OPENAI_API_BASE_PATH).toBe('/serving-endpoints');
});
it('should not set OPENAI_API_BASE_PATH in api-proxy when openaiApiBasePath is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.OPENAI_API_BASE_PATH).toBeUndefined();
});
it('should set ANTHROPIC_API_TARGET in api-proxy when anthropicApiTarget is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test-key', anthropicApiTarget: 'custom.anthropic-router.internal' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.ANTHROPIC_API_TARGET).toBe('custom.anthropic-router.internal');
});
it('should strip https:// scheme from API target values (gh-aw#25137)', () => {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
anthropicApiKey: 'sk-ant-test-key',
anthropicApiTarget: 'https://my-gateway.example.com',
openaiApiKey: 'sk-openai-test',
openaiApiTarget: 'https://openai-router.internal',
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.ANTHROPIC_API_TARGET).toBe('my-gateway.example.com');
expect(env.OPENAI_API_TARGET).toBe('openai-router.internal');
});
it('should not set ANTHROPIC_API_TARGET in api-proxy when anthropicApiTarget is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.ANTHROPIC_API_TARGET).toBeUndefined();
});
it('should set ANTHROPIC_API_BASE_PATH in api-proxy when anthropicApiBasePath is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test-key', anthropicApiBasePath: '/anthropic' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.ANTHROPIC_API_BASE_PATH).toBe('/anthropic');
});
it('should not set ANTHROPIC_API_BASE_PATH in api-proxy when anthropicApiBasePath is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, anthropicApiKey: 'sk-ant-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.ANTHROPIC_API_BASE_PATH).toBeUndefined();
});
it('should set COPILOT_API_TARGET in api-proxy when copilotApiTarget is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotGithubToken: 'ghu_test_token', copilotApiTarget: 'api.copilot.internal' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.COPILOT_API_TARGET).toBe('api.copilot.internal');
});
it('should not set COPILOT_API_TARGET in api-proxy when copilotApiTarget is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotGithubToken: 'ghu_test_token' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.COPILOT_API_TARGET).toBeUndefined();
});
it('should set COPILOT_API_BASE_PATH in api-proxy when copilotApiBasePath is provided', () => {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
copilotApiKey: 'cpat_test_byok_key',
copilotApiBasePath: '/api/v1',
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.COPILOT_API_BASE_PATH).toBe('/api/v1');
});
it('should not set COPILOT_API_BASE_PATH in api-proxy when copilotApiBasePath is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.COPILOT_API_BASE_PATH).toBeUndefined();
});
it('should pass COPILOT_API_KEY to api-proxy env when copilotApiKey is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.COPILOT_API_KEY).toBe('cpat_test_byok_key');
});
it('should set COPILOT_API_URL in agent when only copilotApiKey is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_API_URL).toBe('http://172.30.0.30:10002');
});
it('should set COPILOT_TOKEN placeholder when copilotApiKey is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_TOKEN).toBe('ghu_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
});
it('should set COPILOT_OFFLINE=true in agent when copilotApiKey is provided (offline+BYOK mode)', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_OFFLINE).toBe('true');
});
it('should set COPILOT_PROVIDER_BASE_URL in agent when copilotApiKey is provided (offline+BYOK mode)', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_BASE_URL).toBe('http://172.30.0.30:10002');
});
it('should set COPILOT_PROVIDER_API_KEY placeholder in agent when copilotApiKey is provided (offline+BYOK mode)', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_API_KEY).toBe('ghu_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
});
it.each(['gpt-5', 'openai/o3-mini', 'gpt-5.4-mini', 'provider:gpt-5_preview', 'GPT-5', 'O3'])('should set COPILOT_PROVIDER_WIRE_API=responses in BYOK mode when COPILOT_MODEL is %s', (copilotModel) => {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
copilotApiKey: 'cpat_test_byok_key',
additionalEnv: { COPILOT_MODEL: copilotModel },
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_WIRE_API).toBe('responses');
});
it.each(['gpt-4o', 'o30', 'o3x'])('should not set COPILOT_PROVIDER_WIRE_API in BYOK mode when COPILOT_MODEL=%s does not require responses API', (copilotModel) => {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
copilotApiKey: 'cpat_test_byok_key',
additionalEnv: { COPILOT_MODEL: copilotModel },
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_WIRE_API).toBeUndefined();
});
it('should set COPILOT_PROVIDER_WIRE_API=responses in BYOK mode when COPILOT_MODEL is provided via host env and envAll is enabled', () => {
const previousCopilotModel = process.env.COPILOT_MODEL;
process.env.COPILOT_MODEL = 'gpt-5';
try {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
copilotApiKey: 'cpat_test_byok_key',
envAll: true,
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_WIRE_API).toBe('responses');
} finally {
if (previousCopilotModel === undefined) {
delete process.env.COPILOT_MODEL;
} else {
process.env.COPILOT_MODEL = previousCopilotModel;
}
}
});
it('should set COPILOT_PROVIDER_WIRE_API=responses in BYOK mode when COPILOT_MODEL is provided via envFile', () => {
const envFilePath = path.join(mockConfig.workDir, '.env.copilot-model');
fs.writeFileSync(envFilePath, 'COPILOT_MODEL=openai/o3-mini\n');
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
copilotApiKey: 'cpat_test_byok_key',
envFile: envFilePath,
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_WIRE_API).toBe('responses');
});
it('should not set COPILOT_OFFLINE when only copilotGithubToken is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotGithubToken: 'ghu_test_token' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_OFFLINE).toBeUndefined();
});
it('should not set COPILOT_PROVIDER_BASE_URL when only copilotGithubToken is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotGithubToken: 'ghu_test_token' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_BASE_URL).toBeUndefined();
});
it('should pass COPILOT_PROVIDER_TYPE/BASE_URL/API_KEY from additionalEnv to api-proxy', () => {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
additionalEnv: {
COPILOT_PROVIDER_TYPE: 'azure',
COPILOT_PROVIDER_BASE_URL: 'https://example-resource.openai.azure.com/openai/deployments/test',
COPILOT_PROVIDER_API_KEY: 'azure-byok-key',
},
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_TYPE).toBe('azure');
expect(env.COPILOT_PROVIDER_BASE_URL).toBe('https://example-resource.openai.azure.com/openai/deployments/test');
expect(env.COPILOT_PROVIDER_API_KEY).toBe('azure-byok-key');
});
it('should pass COPILOT_PROVIDER_TYPE/BASE_URL/API_KEY from envFile to api-proxy', () => {
const envFilePath = path.join(mockConfig.workDir, '.env.azure-byok');
fs.writeFileSync(envFilePath, [
'COPILOT_PROVIDER_TYPE=azure',
'COPILOT_PROVIDER_BASE_URL=https://example-resource.openai.azure.com/openai/deployments/test',
'COPILOT_PROVIDER_API_KEY=azure-byok-key',
].join('\n'));
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
envFile: envFilePath,
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_TYPE).toBe('azure');
expect(env.COPILOT_PROVIDER_BASE_URL).toBe('https://example-resource.openai.azure.com/openai/deployments/test');
expect(env.COPILOT_PROVIDER_API_KEY).toBe('azure-byok-key');
});
it.each(['gpt-5', 'openai/o3-mini', 'gpt-5.4-mini', 'GPT-5', 'O3'])('should set COPILOT_PROVIDER_WIRE_API=responses in GitHub token mode when COPILOT_MODEL is %s', (copilotModel) => {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
copilotGithubToken: 'ghu_test_token',
additionalEnv: { COPILOT_MODEL: copilotModel },
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_WIRE_API).toBe('responses');
});
it.each(['gpt-4o', 'o30', 'o3x'])('should not set COPILOT_PROVIDER_WIRE_API in GitHub token mode when COPILOT_MODEL=%s does not require responses API', (copilotModel) => {
const configWithProxy = {
...mockConfig,
enableApiProxy: true,
copilotGithubToken: 'ghu_test_token',
additionalEnv: { COPILOT_MODEL: copilotModel },
};
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.COPILOT_PROVIDER_WIRE_API).toBeUndefined();
});
it('should include COPILOT_PROVIDER_API_KEY in AWF_ONE_SHOT_TOKENS', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, copilotApiKey: 'cpat_test_byok_key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.AWF_ONE_SHOT_TOKENS).toContain('COPILOT_PROVIDER_API_KEY');
});
it('should include api-proxy service when enableApiProxy is true with Gemini key', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
expect(result.services['api-proxy']).toBeDefined();
const proxy = result.services['api-proxy'];
expect(proxy.container_name).toBe('awf-api-proxy');
});
it('should pass GEMINI_API_KEY to api-proxy env when geminiApiKey is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.GEMINI_API_KEY).toBe('AIza-test-gemini-key');
});
it('should set GEMINI_API_BASE_URL in agent when geminiApiKey is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.GEMINI_API_BASE_URL).toBe('http://172.30.0.30:10003');
});
it('should set GOOGLE_GEMINI_BASE_URL in agent when geminiApiKey is provided', () => {
// GOOGLE_GEMINI_BASE_URL is the env var read by the Gemini CLI (google-gemini/gemini-cli)
// to override the API endpoint. Without it, the CLI bypasses the proxy sidecar.
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.GOOGLE_GEMINI_BASE_URL).toBe('http://172.30.0.30:10003');
});
it('should set GOOGLE_GEMINI_BASE_URL and GEMINI_API_BASE_URL to the same proxy URL', () => {
// Both vars must point to the same proxy so CLI and SDK clients both route through sidecar.
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const env = result.services.agent.environment as Record<string, string>;
expect(env.GOOGLE_GEMINI_BASE_URL).toBe(env.GEMINI_API_BASE_URL);
});
it('should set GEMINI_API_KEY placeholder in agent when geminiApiKey is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
expect(env.GEMINI_API_KEY).toBe('gemini-api-key-placeholder-for-credential-isolation');
});
it('should set AWF_GEMINI_ENABLED in agent when geminiApiKey is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const env = result.services.agent.environment as Record<string, string>;
expect(env.AWF_GEMINI_ENABLED).toBe('1');
});
it('should NOT set AWF_GEMINI_ENABLED in agent when geminiApiKey is absent', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const env = result.services.agent.environment as Record<string, string>;
expect(env.AWF_GEMINI_ENABLED).toBeUndefined();
});
it('should not inherit AWF_GEMINI_ENABLED from host env via envAll when geminiApiKey is absent', () => {
const origVal = process.env.AWF_GEMINI_ENABLED;
process.env.AWF_GEMINI_ENABLED = '1';
try {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key', envAll: true };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const env = result.services.agent.environment as Record<string, string>;
// AWF_GEMINI_ENABLED is in EXCLUDED_ENV_VARS so it must not be inherited from host
expect(env.AWF_GEMINI_ENABLED).toBeUndefined();
} finally {
if (origVal !== undefined) {
process.env.AWF_GEMINI_ENABLED = origVal;
} else {
delete process.env.AWF_GEMINI_ENABLED;
}
}
});
it('should NOT set GEMINI_API_BASE_URL in agent when api-proxy is enabled without geminiApiKey', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
// GEMINI_API_BASE_URL must NOT be set when geminiApiKey is absent — it was previously
// set unconditionally which caused spurious Gemini-related log entries in Copilot runs.
expect(env.GEMINI_API_BASE_URL).toBeUndefined();
});
it('should NOT set GOOGLE_GEMINI_BASE_URL in agent when api-proxy is enabled without geminiApiKey', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
// Must not be set without a Gemini key to avoid polluting non-Gemini runs.
expect(env.GOOGLE_GEMINI_BASE_URL).toBeUndefined();
});
it('should not inherit GOOGLE_GEMINI_BASE_URL from host env via envAll when geminiApiKey is absent', () => {
const origVal = process.env.GOOGLE_GEMINI_BASE_URL;
process.env.GOOGLE_GEMINI_BASE_URL = 'http://some-other-proxy';
try {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key', envAll: true };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const env = result.services.agent.environment as Record<string, string>;
// GOOGLE_GEMINI_BASE_URL is in EXCLUDED_ENV_VARS so it must not be inherited from host
expect(env.GOOGLE_GEMINI_BASE_URL).toBeUndefined();
} finally {
if (origVal !== undefined) {
process.env.GOOGLE_GEMINI_BASE_URL = origVal;
} else {
delete process.env.GOOGLE_GEMINI_BASE_URL;
}
}
});
it('should not inherit GEMINI_API_BASE_URL from host env via envAll when geminiApiKey is absent', () => {
const origVal = process.env.GEMINI_API_BASE_URL;
process.env.GEMINI_API_BASE_URL = 'http://some-other-proxy';
try {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key', envAll: true };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const env = result.services.agent.environment as Record<string, string>;
// GEMINI_API_BASE_URL is in EXCLUDED_ENV_VARS so it must not be inherited from host
expect(env.GEMINI_API_BASE_URL).toBeUndefined();
} finally {
if (origVal !== undefined) {
process.env.GEMINI_API_BASE_URL = origVal;
} else {
delete process.env.GEMINI_API_BASE_URL;
}
}
});
it('should NOT set GEMINI_API_KEY placeholder in agent when api-proxy is enabled without geminiApiKey', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, openaiApiKey: 'sk-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
// Placeholder must NOT be set when Gemini is not in use to avoid polluting non-Gemini runs.
expect(env.GEMINI_API_KEY).toBeUndefined();
});
it('should not leak GEMINI_API_KEY to agent when api-proxy is enabled', () => {
const origKey = process.env.GEMINI_API_KEY;
process.env.GEMINI_API_KEY = 'AIza-secret-gemini-key';
try {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-secret-gemini-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
// Agent should NOT have the real API key — only the sidecar gets it
expect(env.GEMINI_API_KEY).toBe('gemini-api-key-placeholder-for-credential-isolation');
// Agent should have both base URL vars to proxy through sidecar
expect(env.GEMINI_API_BASE_URL).toBe('http://172.30.0.30:10003');
expect(env.GOOGLE_GEMINI_BASE_URL).toBe('http://172.30.0.30:10003');
} finally {
if (origKey !== undefined) {
process.env.GEMINI_API_KEY = origKey;
} else {
delete process.env.GEMINI_API_KEY;
}
}
});
it('should not leak GEMINI_API_KEY to agent when api-proxy is enabled with envAll', () => {
const origKey = process.env.GEMINI_API_KEY;
process.env.GEMINI_API_KEY = 'AIza-secret-gemini-key';
try {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-secret-gemini-key', envAll: true };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const agent = result.services.agent;
const env = agent.environment as Record<string, string>;
// Even with envAll, agent should NOT have the real GEMINI_API_KEY
expect(env.GEMINI_API_KEY).toBe('gemini-api-key-placeholder-for-credential-isolation');
expect(env.GEMINI_API_BASE_URL).toBe('http://172.30.0.30:10003');
expect(env.GOOGLE_GEMINI_BASE_URL).toBe('http://172.30.0.30:10003');
} finally {
if (origKey !== undefined) {
process.env.GEMINI_API_KEY = origKey;
} else {
delete process.env.GEMINI_API_KEY;
}
}
});
it('should set GEMINI_API_TARGET in api-proxy when geminiApiTarget is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-key', geminiApiTarget: 'custom.gemini-router.internal' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.GEMINI_API_TARGET).toBe('custom.gemini-router.internal');
});
it('should not set GEMINI_API_TARGET in api-proxy when geminiApiTarget is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.GEMINI_API_TARGET).toBeUndefined();
});
it('should set GEMINI_API_BASE_PATH in api-proxy when geminiApiBasePath is provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-key', geminiApiBasePath: '/v1beta' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.GEMINI_API_BASE_PATH).toBe('/v1beta');
});
it('should not set GEMINI_API_BASE_PATH in api-proxy when geminiApiBasePath is not provided', () => {
const configWithProxy = { ...mockConfig, enableApiProxy: true, geminiApiKey: 'AIza-test-key' };
const result = generateDockerCompose(configWithProxy, mockNetworkConfigWithProxy);
const proxy = result.services['api-proxy'];
const env = proxy.environment as Record<string, string>;
expect(env.GEMINI_API_BASE_PATH).toBeUndefined();
});
});