ruask 0.1.1

A command-line assistant for command lookup and command output explanation
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
use std::fs;
use std::path::PathBuf;
use std::process::{Command, Output, Stdio};

use mockito::{Matcher, Server};
use tempfile::TempDir;

mod support;
use support::{openai_stream, request_query};

struct Workspace {
    root: TempDir,
}

impl Workspace {
    fn new() -> Self {
        let root = tempfile::tempdir().unwrap();
        fs::create_dir(root.path().join("home")).unwrap();
        fs::create_dir(root.path().join("work")).unwrap();
        Self { root }
    }

    fn path(&self, name: &str) -> PathBuf {
        self.root.path().join(name)
    }

    fn command(&self) -> Command {
        let mut command = Command::new(env!("CARGO_BIN_EXE_ruask"));
        command
            .env_clear()
            .env("HOME", self.path("home"))
            .env("XDG_CONFIG_HOME", self.path("settings"))
            .current_dir(self.path("work"))
            .stdin(Stdio::null());
        command
    }

    fn write_config(&self, contents: &str) {
        let path = self.path("settings/ruask/config.toml");
        fs::create_dir_all(path.parent().unwrap()).unwrap();
        fs::write(path, contents).unwrap();
    }
}

fn assert_success(output: &Output) {
    assert!(output.status.success(), "{output:?}");
    assert!(output.stderr.is_empty(), "{output:?}");
}

#[test]
fn language_uses_cli_then_saved_preference_before_locale() {
    for (saved, args, locale, expected) in [
        ("zh", vec![], "en_US.UTF-8", "zh"),
        ("en", vec![], "zh_CN.UTF-8", "en"),
        ("zh", vec!["-L", "en"], "zh_CN.UTF-8", "en"),
        ("en", vec!["--language", "zh"], "en_US.UTF-8", "zh"),
        ("fr", vec!["-L", "zh"], "en_US.UTF-8", "zh"),
    ] {
        let workspace = Workspace::new();
        workspace.write_config(&format!("language = {saved:?}\n"));
        let mut server = Server::new();
        let response = server
            .mock("POST", "/v1/chat/completions")
            .match_request(move |request| {
                let query = request_query(request);
                query["language"] == expected
            })
            .with_body(openai_stream("git status"))
            .expect(1)
            .create();
        let output = workspace
            .command()
            .arg("git")
            .args(&args)
            .env("OPENAI_BASE_URL", format!("{}/v1", server.url()))
            .env("OPENAI_API_KEY", "test-key")
            .env("OPENAI_MODEL", "test-model")
            .env("LANG", locale)
            .env("LANGUAGE", "zh:en")
            .output()
            .unwrap();
        assert_success(&output);
        response.assert();
    }
}

#[test]
fn unsupported_saved_languages_fail_locally_instead_of_using_locale() {
    let mut server = Server::new();
    let requests = server.mock("POST", Matcher::Any).expect(0).create();
    for saved in ["fr", "zh_TW", "", " ", "en_US.UTF-8"] {
        let workspace = Workspace::new();
        workspace.write_config(&format!("language = {saved:?}\n"));
        let output = workspace
            .command()
            .arg("git")
            .env("OPENAI_BASE_URL", format!("{}/v1", server.url()))
            .env("OPENAI_API_KEY", "test-key")
            .env("OPENAI_MODEL", "test-model")
            .env("LANG", "zh_CN.UTF-8")
            .output()
            .unwrap();
        assert!(!output.status.success(), "{output:?}");
        assert!(output.stdout.is_empty(), "{output:?}");
        assert!(
            String::from_utf8_lossy(&output.stderr).contains("结果语言仅支持 en 或 zh"),
            "{output:?}"
        );
    }
    requests.assert();
}

#[test]
fn init_config_creates_private_template_and_preserves_existing_file() {
    let mut server = Server::new();
    let requests = server.mock("POST", Matcher::Any).expect(0).create();
    for xdg in [Some("configured"), None, Some("")] {
        let workspace = Workspace::new();
        let mut command = workspace.command();
        let config = match xdg {
            Some("configured") => workspace.path("settings/ruask/config.toml"),
            value => {
                if value.is_none() {
                    command.env_remove("XDG_CONFIG_HOME");
                } else {
                    command.env("XDG_CONFIG_HOME", "");
                }
                workspace.path("home/.config/ruask/config.toml")
            }
        };
        command
            .arg("--init-config")
            .env("OPENAI_BASE_URL", format!("{}/v1", server.url()))
            .env("RUASK_API_TYPE", "invalid");
        let output = command.output().unwrap();
        assert_success(&output);
        assert!(String::from_utf8_lossy(&output.stdout).contains(config.to_str().unwrap()));
        let template = fs::read_to_string(&config).unwrap();
        for field in [
            "api_type",
            "language",
            "[openai]",
            "[anthropic]",
            "base_url",
            "model",
            "api_key",
        ] {
            assert!(template.contains(field), "{template}");
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            assert_eq!(
                fs::metadata(&config).unwrap().permissions().mode() & 0o777,
                0o600
            );
        }
        let saved = "existing secret-key and deliberately invalid TOML";
        fs::write(&config, saved).unwrap();
        let repeated = command.output().unwrap();
        assert_success(&repeated);
        assert_eq!(fs::read_to_string(&config).unwrap(), saved);
        assert!(!String::from_utf8_lossy(&repeated.stdout).contains("secret-key"));
    }
    requests.assert();
}

#[test]
fn saved_openai_configuration_works_from_any_working_directory() {
    let workspace = Workspace::new();
    let mut server = Server::new();
    workspace.write_config(&format!(
        r#"
api_type = "openai"
language = "zh"
[openai]
base_url = "{}/gateway/v1/"
model = "file-model"
api_key = "file-key"
[anthropic]
base_url = "http://127.0.0.1:9/unused"
model = "other-model"
api_key = "other-key"
"#,
        server.url()
    ));
    fs::create_dir(workspace.path("other")).unwrap();
    // 项目及父目录中的 dotenv / 同名配置不应改变用户配置。
    fs::write(workspace.path(".env"), "OPENAI_API_KEY=wrong-key\n").unwrap();
    for directory in ["work", "other"] {
        fs::write(
            workspace.path(&format!("{directory}/.env")),
            "OPENAI_MODEL=wrong-model\n",
        )
        .unwrap();
        fs::write(
            workspace.path(&format!("{directory}/config.toml")),
            "invalid project config",
        )
        .unwrap();
    }
    let response = server
        .mock("POST", "/gateway/v1/chat/completions")
        .match_header("authorization", "Bearer file-key")
        .match_body(Matcher::PartialJson(
            serde_json::json!({"model": "file-model"}),
        ))
        .with_body(openai_stream("查看状态:git status"))
        .expect(2)
        .create();
    for directory in ["work", "other"] {
        let output = workspace
            .command()
            .current_dir(workspace.path(directory))
            .arg("git")
            .output()
            .unwrap();
        assert_success(&output);
        assert_eq!(
            String::from_utf8_lossy(&output.stdout),
            "查看状态:git status\n"
        );
    }
    response.assert();
}

#[test]
fn protocol_selection_is_explicit_and_does_not_send_anthropic_to_openai() {
    let mut server = Server::new();
    let requests = server
        .mock("POST", "/openai/chat/completions")
        .expect(0)
        .create();
    for (file_type, override_type) in [("anthropic", None), ("openai", Some("anthropic"))] {
        let workspace = Workspace::new();
        workspace.write_config(&format!(
            r#"
api_type = "{file_type}"
[openai]
base_url = "{url}/openai"
model = "claude-via-compatible-gateway"
api_key = "openai-secret"
[anthropic]
base_url = "{url}/anthropic"
model = "native-model"
api_key = "anthropic-secret"
"#,
            url = server.url()
        ));
        let response = server.mock("POST", "/anthropic/messages")
            .match_header("authorization", "Bearer anthropic-secret")
            .match_header("anthropic-version", "2026-10-21")
            .match_body(Matcher::PartialJson(serde_json::json!({"model":"native-model", "stream":true})))
            .with_header("content-type", "application/json")
            .with_body(r#"{"type":"message","role":"assistant","stop_reason":"end_turn","content":[{"type":"text","text":"git status"}]}"#)
            .expect(1).create();
        let mut command = workspace.command();
        command.arg("git");
        if let Some(value) = override_type {
            command.env("RUASK_API_TYPE", value);
        }
        let output = command.output().unwrap();
        assert!(output.status.success(), "{output:?}");
        assert_eq!(
            String::from_utf8_lossy(&output.stderr),
            "ruask: 服务返回完整 JSON 回答,实际使用非流式响应\n"
        );
        assert_eq!(output.stdout, b"git status\n");
        response.assert();
    }
    requests.assert();
}

#[test]
fn environment_overrides_each_selected_field_and_protocol() {
    for file_type in [None, Some("anthropic")] {
        for overrides in [
            [].as_slice(),
            ["BASE_URL"].as_slice(),
            &["API_KEY"],
            &["MODEL"],
            &["BASE_URL", "API_KEY", "MODEL"],
        ] {
            let workspace = Workspace::new();
            let mut file_server = Server::new();
            let mut env_server = Server::new();
            let type_setting = file_type
                .map(|value| format!("api_type = \"{value}\"\n"))
                .unwrap_or_default();
            workspace.write_config(&format!(
                r#"{type_setting}
[openai]
base_url = "{}/file/v1"
api_key = "file-key"
model = "file-model"
[anthropic]
base_url = "http://127.0.0.1:9/anthropic"
api_key = "other-key"
model = "other-model"
"#,
                file_server.url()
            ));
            let (server, path) = if overrides.contains(&"BASE_URL") {
                (&mut env_server, "/override/chat/completions")
            } else {
                (&mut file_server, "/file/v1/chat/completions")
            };
            let expected_key = if overrides.contains(&"API_KEY") {
                "env-key"
            } else {
                "file-key"
            };
            let expected_model = if overrides.contains(&"MODEL") {
                "env-model"
            } else {
                "file-model"
            };
            let response = server
                .mock("POST", path)
                .match_header("authorization", format!("Bearer {expected_key}").as_str())
                .match_body(Matcher::PartialJson(
                    serde_json::json!({"model": expected_model}),
                ))
                .with_body(openai_stream("git status"))
                .expect(1)
                .create();
            let mut command = workspace.command();
            command
                .arg("git")
                .env("ANTHROPIC_BASE_URL", "http://127.0.0.1:9/unused")
                .env("ANTHROPIC_API_KEY", "unselected-secret")
                .env("ANTHROPIC_MODEL", "unselected-model");
            if file_type.is_some() {
                command.env("RUASK_API_TYPE", "openai");
            }
            for name in overrides {
                let value = match *name {
                    "BASE_URL" => format!("{}/override///", env_server.url()),
                    "API_KEY" => "env-key".into(),
                    "MODEL" => "env-model".into(),
                    _ => unreachable!(),
                };
                command.env(format!("OPENAI_{name}"), value);
            }
            let output = command.output().unwrap();
            assert_success(&output);
            assert_eq!(String::from_utf8_lossy(&output.stdout), "git status\n");
            response.assert();
        }
    }
}

#[test]
fn selected_protocol_requires_its_own_complete_nonblank_configuration() {
    let mut server = Server::new();
    let requests = server.mock("POST", Matcher::Any).expect(0).create();
    for (api_type, prefix, other) in [
        ("openai", "OPENAI", "ANTHROPIC"),
        ("anthropic", "ANTHROPIC", "OPENAI"),
    ] {
        for missing in ["base_url", "api_key", "model"] {
            for blank_override in [None, Some(""), Some(" \t ")] {
                let workspace = Workspace::new();
                let mut fields = format!("api_type = \"{api_type}\"\n[{api_type}]\n");
                for (field, value) in [
                    ("base_url", format!("{}/v1", server.url())),
                    ("api_key", "selected-secret".into()),
                    ("model", "selected-model".into()),
                ] {
                    if field != missing || blank_override.is_some() {
                        fields.push_str(&format!("{field} = \"{value}\"\n"));
                    }
                }
                fields.push_str(&format!("[{}]\nbase_url = \"{}/unused\"\napi_key = \"other-file-secret\"\nmodel = \"other-file-model\"\n", other.to_lowercase(), server.url()));
                workspace.write_config(&fields);
                let mut command = workspace.command();
                command
                    .arg("git")
                    .env(
                        format!("{other}_BASE_URL"),
                        format!("{}/unused", server.url()),
                    )
                    .env(format!("{other}_API_KEY"), "other-secret")
                    .env(format!("{other}_MODEL"), "other-model");
                let name = format!("{prefix}_{}", missing.to_uppercase());
                if let Some(value) = blank_override {
                    command.env(&name, value);
                }
                let output = command.output().unwrap();
                assert!(!output.status.success(), "{output:?}");
                assert!(output.stdout.is_empty());
                let error = String::from_utf8_lossy(&output.stderr);
                assert!(
                    error.contains(&name) && error.contains("config.toml"),
                    "{error}"
                );
                assert!(!error.contains("secret"), "{error}");
            }
        }
    }
    requests.assert();
}

#[test]
fn malformed_configuration_and_invalid_protocol_errors_never_echo_secrets() {
    let mut server = Server::new();
    let requests = server.mock("POST", Matcher::Any).expect(0).create();
    for (contents, api_type, expected) in [
        ("[openai]\napi_key = \"sensitive-secret\" BAD", None, "TOML"),
        (
            "[openai]\napi_key = { sensitive-secret = 123 }",
            None,
            "TOML",
        ),
        ("api_type = \"sensitive-secret\"", None, "api_type"),
        ("", Some("sensitive-secret"), "RUASK_API_TYPE"),
        ("", Some(""), "RUASK_API_TYPE"),
    ] {
        let workspace = Workspace::new();
        workspace.write_config(contents);
        let mut command = workspace.command();
        command
            .arg("git")
            .env("OPENAI_BASE_URL", format!("{}/v1", server.url()))
            .env("OPENAI_API_KEY", "env-secret")
            .env("OPENAI_MODEL", "env-model");
        if let Some(value) = api_type {
            command.env("RUASK_API_TYPE", value);
        }
        let output = command.output().unwrap();
        assert!(!output.status.success(), "{output:?}");
        assert!(output.stdout.is_empty());
        let error = String::from_utf8_lossy(&output.stderr);
        assert!(error.contains(expected), "{error}");
        assert!(!error.contains("secret"), "{error}");
    }
    requests.assert();
}

#[test]
fn a_directory_at_the_config_file_path_is_reported_as_an_error() {
    let workspace = Workspace::new();
    let config = workspace.path("settings/ruask/config.toml");
    fs::create_dir_all(&config).unwrap();
    for args in ["--init-config", "git"] {
        let output = workspace.command().arg(args).output().unwrap();
        assert!(!output.status.success(), "{output:?}");
        assert!(output.stdout.is_empty());
        assert!(String::from_utf8_lossy(&output.stderr).contains(config.to_str().unwrap()));
        assert!(config.is_dir());
    }
}

#[test]
fn environment_only_configuration_does_not_create_user_files_or_infer_protocol() {
    let workspace = Workspace::new();
    let mut server = Server::new();
    let response = server
        .mock("POST", "/compatible/chat/completions")
        .match_header("authorization", "Bearer env-key")
        .match_body(Matcher::PartialJson(
            serde_json::json!({"model": "claude-compatible-model"}),
        ))
        .with_body(openai_stream("git status"))
        .expect(1)
        .create();
    let output = workspace
        .command()
        .arg("git")
        .env("OPENAI_BASE_URL", format!("{}/compatible", server.url()))
        .env("OPENAI_API_KEY", "env-key")
        .env("OPENAI_MODEL", "claude-compatible-model")
        .env("ANTHROPIC_API_KEY", "unselected-key")
        .output()
        .unwrap();
    assert_success(&output);
    assert!(!workspace.path("settings").exists());
    assert!(!workspace.path("home/.config").exists());
    response.assert();
}

#[test]
fn help_and_input_errors_do_not_read_even_a_malformed_user_config() {
    let workspace = Workspace::new();
    workspace.write_config("this is not valid TOML and contains a saved-secret");
    let mut server = Server::new();
    let requests = server.mock("POST", Matcher::Any).expect(0).create();
    for (args, success, expected) in [
        (vec![], true, "用法"),
        (vec!["--help"], true, "用法"),
        (vec!["--unknown", "git"], false, "未知选项"),
        (vec!["-q"], false, "需要一个字符串值"),
        (vec![" "], false, "不能为空白"),
        (vec!["--init-config", "git"], false, "需单独使用"),
        (vec!["-q", "--init-config"], false, "需要一个字符串值"),
    ] {
        let output = workspace
            .command()
            .args(args)
            .env("OPENAI_BASE_URL", format!("{}/v1", server.url()))
            .output()
            .unwrap();
        assert_eq!(output.status.success(), success, "{output:?}");
        let text = if success {
            &output.stdout
        } else {
            &output.stderr
        };
        assert!(
            String::from_utf8_lossy(text).contains(expected),
            "{output:?}"
        );
        assert!(!String::from_utf8_lossy(&output.stderr).contains("saved-secret"));
    }
    requests.assert();
}

#[test]
fn configuration_roots_must_be_absolute_and_initialization_needs_a_user_location() {
    let workspace = Workspace::new();
    for args in ["git", "--init-config"] {
        let output = workspace
            .command()
            .arg(args)
            .env("XDG_CONFIG_HOME", "relative-settings")
            .output()
            .unwrap();
        assert!(!output.status.success(), "{output:?}");
        assert!(String::from_utf8_lossy(&output.stderr).contains("XDG_CONFIG_HOME 必须是绝对路径"));
        assert!(!workspace.path("work/relative-settings").exists());
    }
    let output = workspace
        .command()
        .arg("--init-config")
        .env_remove("HOME")
        .env_remove("XDG_CONFIG_HOME")
        .output()
        .unwrap();
    assert!(!output.status.success(), "{output:?}");
    assert!(String::from_utf8_lossy(&output.stderr).contains("HOME 或 XDG_CONFIG_HOME"));
}

#[test]
fn help_explains_complete_setup_and_runtime_contract_without_credentials() {
    let workspace = Workspace::new();
    let mut server = Server::new();
    let requests = server.mock("POST", Matcher::Any).expect(0).create();
    let output = workspace
        .command()
        .arg("--help")
        .env("OPENAI_BASE_URL", server.url())
        .env("ANTHROPIC_BASE_URL", server.url())
        .output()
        .unwrap();
    assert_success(&output);
    let help = String::from_utf8(output.stdout).unwrap();
    for contract in [
        "--init-config",
        "保留已有文件",
        "0600",
        "RUASK_API_TYPE → 文件 api_type → openai",
        "所选协议的环境变量逐字段覆盖文件",
        "不借用另一组",
        "/chat/completions",
        "/messages",
        "不自动补 /v1",
        "语言优先级",
        "文件 language",
        "locale",
        "英文",
        "linux / osx / macos",
        "65,536",
        "回退一次",
        "不完整",
        "模型查询每次都在线请求",
        "--update-pages",
        "CC BY 4.0",
        "不执行建议命令",
        "不保留对话历史",
        "macOS / Linux",
        "stderr",
    ] {
        assert!(
            help.contains(contract),
            "missing {contract:?} in help:\n{help}"
        );
    }
    requests.assert();
}

fn configure_initialized_workspace(workspace: &Workspace, openai: &str, anthropic: &str) -> String {
    let output = workspace.command().arg("--init-config").output().unwrap();
    assert_success(&output);
    let path = workspace.path("settings/ruask/config.toml");
    assert!(String::from_utf8_lossy(&output.stdout).contains(path.to_str().unwrap()));
    let mut template: toml::Value = toml::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
    template
        .as_table_mut()
        .unwrap()
        .insert("language".into(), toml::Value::String("en".into()));
    for (protocol, base_url, model, key) in [
        ("openai", openai, "saved-openai-model", "saved-openai-key"),
        (
            "anthropic",
            anthropic,
            "saved-native-model",
            "saved-native-key",
        ),
    ] {
        for (field, value) in [("base_url", base_url), ("model", model), ("api_key", key)] {
            template[protocol][field] = toml::Value::String(value.into());
        }
    }
    let saved = toml::to_string(&template).unwrap();
    workspace.write_config(&saved);
    fs::create_dir(workspace.path("other-work")).unwrap();
    saved
}

fn protocol_query(request: &mockito::Request, protocol: &str) -> serde_json::Value {
    if protocol == "openai" {
        request_query(request)
    } else {
        let body: serde_json::Value = serde_json::from_slice(request.body().unwrap()).unwrap();
        serde_json::from_str(body["messages"][0]["content"].as_str().unwrap()).unwrap()
    }
}

#[test]
fn initialized_configuration_combines_cross_directory_queries_and_chinese_pipe_analysis() {
    use std::io::Write;

    let workspace = Workspace::new();
    let mut openai = Server::new();
    let mut native = Server::new();
    let saved = configure_initialized_workspace(
        &workspace,
        &format!("{}/team/v1/", openai.url()),
        &format!("{}/native/v1/", native.url()),
    );
    let command = openai
        .mock("POST", "/team/v1/chat/completions")
        .match_header("authorization", "Bearer saved-openai-key")
        .match_body(Matcher::PartialJson(
            serde_json::json!({"model":"saved-openai-model", "stream":true}),
        ))
        .match_request(|request| {
            let query = request_query(request);
            query["command_query"] == "git status"
                && query["question"].is_null()
                && query["output_context"].is_null()
                && query["language"] == "en"
        })
        .with_header("content-type", "text/event-stream")
        .with_body(openai_stream("Inspect changes:\n  git status"))
        .expect(1)
        .create();
    let output = workspace
        .command()
        .args(["git", "status"])
        .output()
        .unwrap();
    assert_success(&output);
    assert_eq!(output.stdout, b"Inspect changes:\n  git status\n");

    let question = native
        .mock("POST", "/native/v1/messages")
        .match_header("authorization", "Bearer saved-native-key")
        .match_body(Matcher::PartialJson(serde_json::json!({"model":"saved-native-model", "stream":true})))
        .match_request(|request| {
            let query = protocol_query(request, "anthropic");
            query["command_query"].is_null()
                && query["question"] == "How can I inspect changes?"
                && query["output_context"].is_null()
                && query["language"] == "en"
                && query["platform"] == "osx"
        })
        .with_header("content-type", "application/json")
        .with_body(serde_json::json!({"type":"message", "role":"assistant", "content":[{"type":"text", "text":"Inspect changes:\n  git diff"}], "stop_reason":"end_turn"}).to_string())
        .expect(1)
        .create();
    let output = workspace
        .command()
        .current_dir(workspace.path("other-work"))
        .env("RUASK_API_TYPE", "anthropic")
        .args([
            "--question",
            "How can I inspect changes?",
            "-L",
            "en",
            "-p",
            "macos",
        ])
        .output()
        .unwrap();
    assert!(output.status.success(), "{output:?}");
    assert_eq!(output.stdout, b"Inspect changes:\n  git diff\n");
    assert_eq!(
        String::from_utf8_lossy(&output.stderr),
        "ruask: 服务返回完整 JSON 回答,实际使用非流式响应\n"
    );

    let interpretation = openai
        .mock("POST", "/team/v1/chat/completions")
        .match_header("authorization", "Bearer saved-openai-key")
        .match_body(Matcher::PartialJson(
            serde_json::json!({"model":"saved-openai-model", "stream":true}),
        ))
        .match_request(|request| {
            let query = request_query(request);
            query["command_query"] == "cargo build"
                && query["question"] == "为什么失败,下一步如何排查?"
                && query["output_context"] == "  error: 缺少依赖 🦀\n\n  at Cargo.toml\n"
                && query["language"] == "zh"
                && query["platform"] == "linux"
        })
        .with_header("content-type", "text/event-stream")
        .with_body(openai_stream("检查依赖配置:\n  cargo check"))
        .expect(1)
        .create();
    let mut child = workspace
        .command()
        .current_dir(workspace.path("other-work"))
        .args([
            "cargo",
            "build",
            "-q",
            "为什么失败,下一步如何排查?",
            "-L",
            "zh",
            "-p",
            "linux",
        ])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .unwrap();
    child
        .stdin
        .take()
        .unwrap()
        .write_all("  error: 缺少依赖 🦀\n\n  at Cargo.toml\n".as_bytes())
        .unwrap();
    let output = child.wait_with_output().unwrap();
    assert_success(&output);
    assert_eq!(output.stdout, "检查依赖配置:\n  cargo check\n".as_bytes());
    assert_eq!(
        fs::read_to_string(workspace.path("settings/ruask/config.toml")).unwrap(),
        saved
    );
    command.assert();
    question.assert();
    interpretation.assert();
}

#[test]
fn repeated_configured_queries_request_again_and_keep_fallback_notices_out_of_redirected_answers() {
    for protocol in ["openai", "anthropic"] {
        let workspace = Workspace::new();
        let mut server = Server::new();
        let saved = configure_initialized_workspace(
            &workspace,
            &format!("{}/openai/v1", server.url()),
            &format!("{}/native/v1", server.url()),
        );
        let (path, key, model) = if protocol == "openai" {
            (
                "/openai/v1/chat/completions",
                "Bearer saved-openai-key",
                "saved-openai-model",
            )
        } else {
            (
                "/native/v1/messages",
                "Bearer saved-native-key",
                "saved-native-model",
            )
        };
        let streaming = server.mock("POST", path)
            .match_header("authorization", key)
            .match_body(Matcher::PartialJson(serde_json::json!({"model":model, "stream":true})))
            .match_request(move |request| {
                let query = protocol_query(request, protocol);
                query["command_query"] == "git status" && query["question"].is_null() && query["language"] == "en"
            })
            .with_status(400)
            .with_header("content-type", "application/json")
            .with_body(r#"{"error":{"code":"unsupported_streaming","message":"streaming is not supported"}}"#)
            .expect(2).create();
        let content = "Show changes:\n```sh\n  git status\n```";
        let answer = if protocol == "openai" {
            serde_json::json!({"choices":[{"message":{"content":content},"finish_reason":"stop"}]})
        } else {
            serde_json::json!({"type":"message","role":"assistant","content":[{"type":"text","text":content}],"stop_reason":"end_turn"})
        };
        let fallback = server
            .mock("POST", path)
            .match_header("authorization", key)
            .match_body(Matcher::PartialJson(
                serde_json::json!({"model":model,"stream":false}),
            ))
            .match_request(move |request| {
                protocol_query(request, protocol)["command_query"] == "git status"
            })
            .with_header("content-type", "application/json")
            .with_body(answer.to_string())
            .expect(2)
            .create();
        for run in 0..2 {
            let answer_path = workspace.path(&format!("answer-{run}.txt"));
            let output = workspace
                .command()
                .current_dir(workspace.path("other-work"))
                .env("RUASK_API_TYPE", protocol)
                .args(["git", "status"])
                .stdout(fs::File::create(&answer_path).unwrap())
                .output()
                .unwrap();
            assert!(output.status.success(), "{protocol}: {output:?}");
            assert!(output.stdout.is_empty());
            assert_eq!(
                fs::read(&answer_path).unwrap(),
                b"Show changes:\n  git status\n"
            );
            assert_eq!(
                String::from_utf8_lossy(&output.stderr),
                "ruask: 服务明确不支持流式,回退一次非流式请求\n"
            );
        }
        streaming.assert();
        fallback.assert();
        assert_eq!(
            fs::read_to_string(workspace.path("settings/ruask/config.toml")).unwrap(),
            saved
        );
    }
}