mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 08:39:02 +03:00
test(cli-rs): check local config edits at the file boundary
cli_config_files_match_typescript covers ordered edits, merging, legacy tabs and preservation of neighboring settings in the saved file. decode_and_resolve_match_typescript checks default values. Keep reloading_keeps_the_initial_tabs_environment for the reload-only case. jsonc_edits_and_parse_match_typescript covers parsing and number formatting with an independent implementation. Exercise escaped surrogates, nested values, and both sides of the decimal/exponent boundary through the edit result instead of a second formatter table.
This commit is contained in:
@@ -709,47 +709,10 @@ impl Resolved {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn merge_overlays_and_normalizes_tabs() {
|
||||
let merged = merge(&[
|
||||
Some(json!({"tabs": {"enabled": true, "scope": "global"}, "theme": {"name": "a", "mode": "dark"}})),
|
||||
Some(json!({"theme": {"name": "b"}})),
|
||||
]);
|
||||
assert_eq!(
|
||||
merged.to_value(),
|
||||
json!({"theme": {"name": "b", "mode": "dark"}, "tabs": {"mode": "on", "scope": "global"}})
|
||||
);
|
||||
assert_eq!(merge(&[Some(json!({"mouse": 1}))]), Info::default());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn changes_diff_like_typescript() {
|
||||
let mut edits = Vec::new();
|
||||
changes(
|
||||
Some(&json!({"a": {"b": 1, "c": [1]}, "d": true})),
|
||||
Some(&json!({"a": {"b": 2, "c": [1]}, "e": "x"})),
|
||||
&mut Vec::new(),
|
||||
&mut edits,
|
||||
);
|
||||
let key = |k: &str| Segment::Key(k.into());
|
||||
assert_eq!(
|
||||
edits,
|
||||
vec![
|
||||
(vec![key("a"), key("b")], Some(json!(2))),
|
||||
(vec![key("d")], None),
|
||||
(vec![key("e")], Some(json!("x"))),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn resolve_applies_defaults() {
|
||||
fn reloading_keeps_the_initial_tabs_environment() {
|
||||
let resolved = Resolved::new(Info::default(), |_| None);
|
||||
assert_eq!(resolved.leader_timeout, Duration::from_millis(2000));
|
||||
assert_eq!(resolved.attention.volume, 0.4);
|
||||
assert!(resolved.tabs.enabled && resolved.mouse && resolved.tps);
|
||||
let herdr = Resolved::new(Info::default(), |name| (name == "HERDR_ENV").then(|| "1".to_string()));
|
||||
assert!(!herdr.tabs.enabled);
|
||||
// A reload keeps the environment of the first resolve.
|
||||
|
||||
@@ -1047,45 +1047,3 @@ fn format_range(document: &str, range_start: usize, range_end: usize) -> Vec<Edi
|
||||
}
|
||||
edits
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn parses_jsonc_like_the_library() {
|
||||
assert_eq!(
|
||||
parse("// c\n{\"a\": [1, 2,], /* b */ \"b\": {\"c\": null,},}"),
|
||||
Some(json!({"a": [1, 2], "b": {"c": null}}))
|
||||
);
|
||||
assert_eq!(parse("{\"a\": 1, \"a\": 2}"), Some(json!({"a": 2})));
|
||||
assert_eq!(parse("{\"a\": \"\\ud83d\\ude00\"}"), Some(json!({"a": "😀"})));
|
||||
assert_eq!(parse(""), None);
|
||||
assert_eq!(parse("{\"a\" 1}"), None);
|
||||
assert_eq!(parse("{a: 1}"), None);
|
||||
assert_eq!(parse("[01]"), None);
|
||||
assert_eq!(parse("{\"a\": 1} x"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stringify_formats_numbers_as_javascript() {
|
||||
let cases = [
|
||||
(1.0, "1"),
|
||||
(0.4, "0.4"),
|
||||
(-2.5, "-2.5"),
|
||||
(1e21, "1e+21"),
|
||||
(1.5e-7, "1.5e-7"),
|
||||
(0.000001, "0.000001"),
|
||||
(123456789012345680000.0, "123456789012345680000"),
|
||||
(1e300, "1e+300"),
|
||||
];
|
||||
for (value, text) in cases {
|
||||
assert_eq!(stringify(&number(value)), text, "{value}");
|
||||
}
|
||||
assert_eq!(
|
||||
stringify_pretty(&json!({"a": [1, {}], "b": "\u{1}"})),
|
||||
"{\n \"a\": [\n 1,\n {}\n ],\n \"b\": \"\\u0001\"\n}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +122,7 @@ fn jsonc_edits_and_parse_match_typescript() {
|
||||
return;
|
||||
}
|
||||
let texts = [
|
||||
r#"{"emoji":"\ud83d\ude00"}"#,
|
||||
"{}",
|
||||
"{\n}\n",
|
||||
"",
|
||||
@@ -169,8 +170,18 @@ fn jsonc_edits_and_parse_match_typescript() {
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
// Numeric edits must agree with JSON.stringify across the fixed/exponent boundary.
|
||||
for exponent in -12..=24 {
|
||||
for factor in [-2.5, 0.4, 1.0] {
|
||||
cases.push(json!({"text": "{}", "path": ["n"], "value": factor * 10f64.powi(exponent)}));
|
||||
}
|
||||
}
|
||||
for value in [json!(1e300), json!(123456789012345680000.0), json!({"a": [1, {}], "b": "\u{1}"})] {
|
||||
cases.push(json!({"text": "{}", "path": [], "value": value}));
|
||||
}
|
||||
cases.extend(texts.iter().chain(&invalid).map(|text| json!({ "text": text })));
|
||||
let expected = oracle(&["jsonc"], None, &Value::Array(cases.clone()));
|
||||
assert_eq!(expected.len(), cases.len());
|
||||
let mut failures = Vec::new();
|
||||
for (case, expected) in cases.iter().zip(&expected) {
|
||||
let source = case["text"].as_str().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user