aix-rs 1.0.0

Manage AI gateway profiles and launch AI coding tools with the right credentials
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
use assert_cmd::Command;
use assert_fs::prelude::*;

fn cmd() -> Command {
    Command::cargo_bin("aix").expect("binary exists")
}

// Config with direct (non-env) secret values so tests don't depend on env vars.
const CONFIG_DIRECT: &str = r#"
[endpoint]
base_url = "https://ai.example.com"

[profiles.swtb]
api_key = "sk-swtb-key"

[profiles.work]
api_key = "sk-work-key"
"#;

const CONFIG_WITH_DEFAULT: &str = r#"
default_profile = "swtb"

[endpoint]
base_url = "https://ai.example.com"

[profiles.swtb]
api_key = "sk-swtb-key"
"#;

const CONFIG_OPENAI_COMPAT: &str = r#"
[endpoint]
base_url = "https://ai.example.com"

[profiles.swtb]
api_key = "sk-swtb-key"
"#;

const CONFIG_SPECIAL_CHARS: &str = r#"
[endpoint]
base_url = "https://ai.example.com?token=abc&id=1"

[profiles.test]
api_key = "sk-it's a test"
"#;

// --- sh format ---

#[test]
fn env_sh_contains_aix_vars() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "sh"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    assert!(s.contains("export AIX_PROFILE='swtb'"), "got: {s}");
    // anthropic_env defaults to true
    assert!(
        s.contains("export ANTHROPIC_API_KEY='sk-swtb-key'"),
        "got: {s}"
    );
    assert!(
        s.contains("export ANTHROPIC_BASE_URL='https://ai.example.com'"),
        "got: {s}"
    );
}

#[test]
fn env_sh_default_format_is_sh() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb"]) // no --format flag → defaults to sh
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();
    assert!(s.contains("export AIX_PROFILE='swtb'"), "got: {s}");
}

#[test]
fn env_sh_special_chars_in_key_and_url() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_SPECIAL_CHARS).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "test", "--format", "sh"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    // Single quote in API key must be escaped as '\''
    assert!(s.contains(r"'sk-it'\''s a test'"), "got: {s}");
    // URL with & and = preserved inside single quotes
    assert!(
        s.contains("'https://ai.example.com?token=abc&id=1'"),
        "got: {s}"
    );
}

#[test]
fn env_uses_profile_base_url_override() {
    let config = r#"
[endpoint]
base_url = "https://shared.example.com"

[profiles.local]
api_key = "sk-local-key"
base_url = "https://local.example.com"
"#;
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(config).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "local", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let Ok(parsed) = serde_json::from_slice::<serde_json::Value>(&out) else {
        panic!("env output must be valid JSON");
    };
    assert_eq!(parsed["ANTHROPIC_BASE_URL"], "https://local.example.com");
    assert_eq!(parsed["OPENAI_BASE_URL"], "https://local.example.com/v1");
}

#[test]
fn env_does_not_duplicate_v1_before_a_query_string() {
    let config = r#"
[endpoint]
base_url = "https://gateway.example/v1/?tenant=work"

[profiles.work]
api_key = "test-key"
"#;
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(config).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "work", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let parsed: serde_json::Value = serde_json::from_slice(&out).unwrap();

    assert_eq!(
        parsed["OPENAI_BASE_URL"],
        "https://gateway.example/v1?tenant=work"
    );
    assert_eq!(
        parsed["LITELLM_BASE_URL"],
        "https://gateway.example/v1?tenant=work"
    );
}

#[test]
fn env_includes_profile_custom_variables() {
    let config = r#"
[endpoint]
base_url = "https://ai.example.com"

[profiles.qwen]
api_key = "sk-qwen-key"

[profiles.qwen.env]
ANTHROPIC_AUTH_TOKEN = { env = "QWEN_TEST_TOKEN" }
ANTHROPIC_MODEL = "qwen3.8-max-preview"
CLAUDE_CODE_MAX_CONTEXT_TOKENS = "983616"
"#;
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(config).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .env("QWEN_TEST_TOKEN", "token-from-env")
        .args(["env", "qwen", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let parsed: serde_json::Value = serde_json::from_slice(&out).unwrap();
    assert_eq!(parsed["ANTHROPIC_AUTH_TOKEN"], "token-from-env");
    assert_eq!(parsed["ANTHROPIC_MODEL"], "qwen3.8-max-preview");
    assert_eq!(parsed["CLAUDE_CODE_MAX_CONTEXT_TOKENS"], "983616");
}

// --- json format ---

#[test]
fn env_json_produces_valid_json() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let parsed: serde_json::Value =
        serde_json::from_slice(&out).expect("output must be valid JSON");
    assert_eq!(parsed["AIX_PROFILE"], "swtb");
    assert_eq!(parsed["ANTHROPIC_API_KEY"], "sk-swtb-key");
}

#[test]
fn env_json_special_chars_round_trip() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_SPECIAL_CHARS).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "test", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let parsed: serde_json::Value =
        serde_json::from_slice(&out).expect("output must be valid JSON");
    assert_eq!(parsed["AIX_PROFILE"], "test");
}

// --- nu format ---

#[test]
fn env_nu_contains_env_assignments() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "nu"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    assert!(s.contains("$env.AIX_PROFILE = \"swtb\""), "got: {s}");
}

// --- fish format ---

#[test]
fn env_fish_contains_set_x_lines() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "fish"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    assert!(s.contains("set -x AIX_PROFILE 'swtb'"), "got: {s}");
}

#[test]
fn env_fish_special_chars_escaped() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_SPECIAL_CHARS).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "test", "--format", "fish"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    // fish: single quote escaped as \'
    assert!(s.contains(r"'sk-it\'s a test'"), "got: {s}");
}

// --- powershell format ---

#[test]
fn env_powershell_contains_env_colon_assignments() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "powershell"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    assert!(s.contains("$env:AIX_PROFILE = 'swtb'"), "got: {s}");
}

#[test]
fn env_powershell_single_quote_doubled() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_SPECIAL_CHARS).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "test", "--format", "powershell"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    // PowerShell: single quote doubled → ''
    assert!(s.contains("'sk-it''s a test'"), "got: {s}");
}

// --- compat flags ---

#[test]
fn env_openai_compat_enabled_includes_openai_vars() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_OPENAI_COMPAT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "json"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let parsed: serde_json::Value = serde_json::from_slice(&out).unwrap();
    assert_eq!(parsed["OPENAI_API_KEY"], "sk-swtb-key");
    assert_eq!(parsed["OPENAI_BASE_URL"], "https://ai.example.com/v1");
}

// --- profile selection ---

#[test]
fn env_uses_default_profile_when_none_specified() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_WITH_DEFAULT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .env_remove("AIX_PROFILE") // prevent real env leaking in
        .args(["env", "--format", "json"]) // no positional profile
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let parsed: serde_json::Value = serde_json::from_slice(&out).unwrap();
    assert_eq!(parsed["AIX_PROFILE"], "swtb");
}

#[test]
fn env_errors_when_no_profile_and_no_default() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap(); // no default_profile

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .env_remove("AIX_PROFILE") // prevent real env leaking in
        .args(["env", "--format", "json"]) // no positional profile
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let s = std::str::from_utf8(&out).unwrap();
    assert!(s.contains("profile") || s.contains("default"), "got: {s}");
}

#[test]
fn env_errors_when_named_profile_not_in_config() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "nonexistent", "--format", "json"])
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let s = std::str::from_utf8(&out).unwrap();
    assert!(s.contains("nonexistent"), "got: {s}");
}

#[test]
fn env_no_config_file_exits_nonzero() {
    let home = assert_fs::TempDir::new().unwrap();
    cmd()
        .env("HOME", home.path())
        .env_remove("AIX_CONFIG")
        .env_remove("XDG_CONFIG_HOME")
        .args(["env", "swtb", "--format", "json"])
        .assert()
        .failure();
}

// --- missing / empty profiles ---

const CONFIG_NO_PROFILES: &str = r#"
[endpoint]
base_url = "https://example.com"
"#;

const CONFIG_EMPTY_PROFILES: &str = r#"
[endpoint]
base_url = "https://example.com"

[profiles]
"#;

#[test]
fn env_errors_on_config_with_no_profiles_section() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_NO_PROFILES).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "json"])
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let s = std::str::from_utf8(&out).unwrap();
    assert!(
        s.contains("[profiles]"),
        "expected mention of [profiles], got: {s}"
    );
    assert!(
        s.contains("profile"),
        "expected mention of profile, got: {s}"
    );
}

#[test]
fn env_errors_on_config_with_empty_profiles() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_EMPTY_PROFILES).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "json"])
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let s = std::str::from_utf8(&out).unwrap();
    assert!(
        s.contains("[profiles]"),
        "expected mention of [profiles], got: {s}"
    );
}

// --- unknown profile list formatting ---

const CONFIG_WITH_LABELS: &str = r#"
default_profile = "swtb"

[endpoint]
base_url = "https://example.com"

[profiles.swtb]
label = "SWTB"
api_key = "sk-swtb"

[profiles.work]
label = "Work account"
api_key = "sk-work"
"#;

#[test]
fn env_unknown_profile_list_uses_single_space_before_label() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_WITH_LABELS).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "typo", "--format", "json"])
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let s = std::str::from_utf8(&out).unwrap();
    assert!(s.contains("swtb (SWTB)"), "expected single space, got: {s}");
    assert!(
        s.contains("work (Work account)"),
        "expected single space, got: {s}"
    );
    assert!(!s.contains("swtb  ("), "found double space: {s}");
}

#[test]
fn env_unknown_profile_list_omits_parens_when_no_label() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap(); // CONFIG_DIRECT has no labels

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "typo", "--format", "json"])
        .assert()
        .failure()
        .get_output()
        .stderr
        .clone();
    let s = std::str::from_utf8(&out).unwrap();
    assert!(s.contains("swtb"), "should list profile name: {s}");
    assert!(
        !s.contains("swtb (swtb)"),
        "should not show name in parens: {s}"
    );
}

// --- cmd format ---

#[test]
fn env_cmd_contains_set_quoted_lines() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "cmd"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    assert!(s.contains(r#"set "AIX_PROFILE=swtb""#), "got: {s}");
    assert!(
        s.contains(r#"set "ANTHROPIC_API_KEY=sk-swtb-key""#),
        "got: {s}"
    );
}

#[test]
fn env_cmd_no_debug_on_stdout() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "cmd"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    for line in s.lines() {
        if !line.is_empty() {
            assert!(line.starts_with("set \""), "unexpected line: {line}");
        }
    }
}

#[test]
fn env_cmd_percent_in_url_is_doubled() {
    const CONFIG_PERCENT: &str = r#"
[endpoint]
base_url = "https://ai.example.com?token=abc%20def"

[profiles.test]
api_key = "sk-key"
"#;
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_PERCENT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "test", "--format", "cmd"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    assert!(
        s.contains(r#"set "ANTHROPIC_BASE_URL=https://ai.example.com?token=abc%%20def""#),
        "got: {s}"
    );
}

// --- no debug/config data leaks ---

#[test]
fn env_output_does_not_contain_unrelated_config_fields() {
    let file = assert_fs::NamedTempFile::new("aix.toml").unwrap();
    file.write_str(CONFIG_DIRECT).unwrap();

    let out = cmd()
        .env("AIX_CONFIG", file.path())
        .args(["env", "swtb", "--format", "sh"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();
    let s = std::str::from_utf8(&out).unwrap();

    assert!(!s.contains("api_format"), "got: {s}");
    assert!(!s.contains("aix.toml"), "got: {s}");
}