Files
codex-proxy-rs/backend/crates/gateway-plugin/runtime/tests/adapter/command_line/parameters.rs
T
ZyyandCodex a0e708071a perf(plugins)!: optimize RPC and simplify host boundaries
Move business JSON out of control metadata, encode request snapshot headers
compactly, and reuse encoded frames. Cache compiled configuration schemas
and prepare plugin instances with bounded concurrency.

Ignore unused plugin result fields and remove redundant business validation
while preserving host authorization, resource ownership, and capacity limits.
Keep response decoding failures local to their calls.

Seal callback side effects before completing or cancelling RPC calls, and
recover abandoned state migrations without allowing old owners to promote.

Validation: SDK 112 tests and its doctest, Runtime 203 tests, Core 542 tests,
40 PostgreSQL plugin tests, and 16 architecture checks passed. Strict Clippy,
formatting, and diff checks passed.

BREAKING CHANGE: RPC protocol v4 changes frame payload layout and request
snapshot header encoding. Plugins must update their SDK and protocol
manifest version and be rebuilt.

Co-authored-by: Codex <noreply@openai.com>
2026-10-17 20:54:41 +08:00

120 lines
5.3 KiB
Rust

//! 验证插件命令参数的类型、默认值、数值边界与非法声明拒绝
use serde_json::{Value, json};
#[tokio::test]
async fn command_parser_preserves_all_types_defaults_and_signed_nanosecond_boundaries() {
let parameters = json!([
{"name":"enabled","description":"启用","value_type":"bool"},
{"name":"label","description":"名称","value_type":"string","required":true},
{"name":"count","description":"数量","value_type":"int"},
{"name":"wide","description":"编号","value_type":"int64"},
{"name":"ratio","description":"比例","value_type":"float64"},
{"name":"wait","description":"等待","value_type":"duration"},
{"name":"secret","description":"私密默认","value_type":"string","sensitive":true,"default":{"type":"string","value":"hidden-default"}}
]);
let (_directory, _store, runtime) = super::setup(super::configuration(parameters)).await;
let commands = runtime.prepare_command_line().await.unwrap();
let help = commands.help(Some("commands"), Some("inspect")).unwrap();
assert!(!help.contains("hidden-default"));
assert!(help.contains("已隐藏"));
for (duration, expected) in [
("1h2m3.000000001s", 3_723_000_000_001_i64),
("-9223372036854775808ns", i64::MIN),
(".5s", 500_000_000),
("0", 0),
] {
let args = [
"--enabled",
"--label=a=b",
"--count=-2147483648",
"--wide=9223372036854775807",
"--ratio=.125",
"--wait",
duration,
]
.map(str::to_owned);
let output = commands
.execute("commands", "inspect", &args)
.await
.unwrap();
let invocation: Value = serde_json::from_str(&output.stdout).unwrap();
assert_eq!(
invocation["arguments"]["enabled"],
json!({"type":"bool","value":true})
);
assert_eq!(invocation["arguments"]["label"]["value"], "a=b");
assert_eq!(invocation["arguments"]["count"]["value"], i32::MIN);
assert_eq!(invocation["arguments"]["wide"]["value"], i64::MAX);
assert_eq!(invocation["arguments"]["ratio"]["value"], 0.125);
assert_eq!(invocation["arguments"]["wait"]["value"], expected);
assert_eq!(invocation["arguments"]["secret"]["value"], "hidden-default");
}
for invalid in [
vec!["--label=private-sentinel", "--count=2147483648"],
vec!["--label=private-sentinel", "--ratio=NaN"],
vec!["--label=private-sentinel", "--ratio=inf"],
vec!["--label=private-sentinel", "--wait=9223372036854775808ns"],
vec!["--label=private-sentinel", "--wait=0.1ns"],
vec!["--label=private-sentinel", "--wait=1.2.3s"],
vec!["--label=private-sentinel", "--label=duplicate"],
vec!["--label=private-sentinel", "--unknown"],
vec!["--count=1"],
] {
let args = invalid.into_iter().map(str::to_owned).collect::<Vec<_>>();
let error = commands
.execute("commands", "inspect", &args)
.await
.err()
.unwrap();
assert!(matches!(
error,
gateway_plugin_runtime::PluginCommandError::Invalid(_)
));
assert!(!error.to_string().contains("private-sentinel"));
}
}
#[tokio::test]
async fn command_registration_preserves_plugin_owned_parameter_definitions() {
let (_directory, store, runtime) = super::setup(super::configuration(json!([]))).await;
for parameters in [
json!([{"name":"help","description":"保留名","value_type":"bool"}]),
json!([{"name":"test","description":"默认类型错误","value_type":"int","default":{"type":"string","value":"bad"}}]),
json!([{"name":"same","description":"重复","value_type":"bool"},{"name":"same","description":"重复","value_type":"bool"}]),
json!([{"name":"bad","description":"\u{1b}[2J","value_type":"bool"}]),
] {
store.snapshot.lock().unwrap().instances[0].configuration =
super::configuration(parameters);
let commands = runtime
.prepare_command_line()
.await
.expect("plugin definitions are not host business rules");
assert!(commands.help(Some("commands"), None).is_ok());
let snapshot = store.snapshot.lock().unwrap().clone();
assert!(
gateway_admin::ports::plugins::PluginPreparation::prepare(&runtime, snapshot)
.await
.is_ok(),
"host preparation accepts plugin-owned command semantics"
);
}
for name in ["help", "serve", "plugin", "version"] {
let mut config = super::configuration(json!([]));
config["command_registration"]["commands"][0]["name"] = json!(name);
store.snapshot.lock().unwrap().instances[0].configuration = config;
let commands = runtime
.prepare_command_line()
.await
.expect("plugin definitions are not host business rules");
assert!(commands.help(Some("commands"), None).is_ok());
let snapshot = store.snapshot.lock().unwrap().clone();
assert!(
gateway_admin::ports::plugins::PluginPreparation::prepare(&runtime, snapshot)
.await
.is_ok(),
"host preparation accepts plugin-owned command semantics"
);
}
}