feat: M1.1 完成 + M1.2 启动 — 全量更新

M1.1 收尾:
- 24项 P0/P1/P2 bug 修复 (Rust 107 tests + Flutter 15 tests)
- Flutter App v0.3: cupertino_icons 修复, 单元测试, 调试面板, APK 52.6MB
- 示例插件完善: manifest.json + 请求/响应示范 + 7个测试
- API 文档重写 (以 routes.rs 为唯一权威)
- MILESTONES.md 更新至 100%

M1.2 启动:
- P0: 插件管理 API 闭环 (handle_manager_message Custom 分支 + broadcast_plugin_states)
- ServiceManager 集成测试 8/8 (tests/m1_2_service_manager.rs)
- M1.2 测试计划 (docs/M1.2_TEST_PLAN.md, 18个E2E场景)
- 动态插件系统: auto_rollback + version_manager GC + 路径穿越防护

总计: Rust 115/115 测试, Flutter 15/15 测试, 零 warning

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
showen
2026-03-14 18:12:42 +08:00
parent 8ed9cb2d9d
commit d30c111c71
68 changed files with 8115 additions and 1201 deletions

View File

@@ -123,8 +123,8 @@ impl PluginLoader {
/// 保存全局注册表
pub fn save_registry(&self, registry: &PluginRegistry) -> Result<()> {
let registry_path = self.store_path.join("registry.json");
let content = serde_json::to_string_pretty(registry)
.context("failed to serialize registry")?;
let content =
serde_json::to_string_pretty(registry).context("failed to serialize registry")?;
std::fs::write(&registry_path, content)
.with_context(|| format!("failed to write {}", registry_path.display()))
}
@@ -201,9 +201,7 @@ impl PluginLoader {
.plugins
.get(plugin_id)
.map(|e| e.active_version.clone())
.ok_or_else(|| {
anyhow!("plugin '{plugin_id}' not found in registry")
})?
.ok_or_else(|| anyhow!("plugin '{plugin_id}' not found in registry"))?
}
};
@@ -211,18 +209,28 @@ impl PluginLoader {
let manifest_path = version_dir.join("manifest.json");
let manifest = self.read_manifest(&manifest_path)?;
let so_path = version_dir.join(&manifest.so_filename);
if !so_path.exists() {
if manifest.id != plugin_id {
return Err(anyhow!(
"plugin .so not found: {}",
so_path.display()
"plugin manifest id mismatch: requested '{plugin_id}', found '{}'",
manifest.id
));
}
if manifest.version != version {
return Err(anyhow!(
"plugin manifest version mismatch: requested '{version}', found '{}'",
manifest.version
));
}
let so_path = version_dir.join(&manifest.so_filename);
if !so_path.exists() {
return Err(anyhow!("plugin .so not found: {}", so_path.display()));
}
let so_path_str = so_path.to_string_lossy().to_string();
let mut plugin = unsafe {
DynamicPlugin::load(&so_path_str, manifest.dependencies.clone())?
};
let mut plugin =
unsafe { DynamicPlugin::load(&so_path_str, manifest.dependencies.clone())? };
plugin.set_id(manifest.id.clone());
Ok((plugin, manifest))
@@ -255,6 +263,14 @@ mod tests {
use super::*;
use std::fs;
fn unique_test_dir(name: &str) -> std::path::PathBuf {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system time should be after unix epoch")
.as_nanos();
std::env::temp_dir().join(format!("showen_plugin_loader_{name}_{nanos}"))
}
fn setup_test_store(base: &Path) {
let plugin_dir = base.join("test-plugin").join("1.0.0");
fs::create_dir_all(&plugin_dir).unwrap();
@@ -387,4 +403,76 @@ mod tests {
assert_eq!(manifest.test_timeout_ms, 5000);
assert!(manifest.auto_test);
}
#[test]
fn manifest_test_timeout_ms_is_configurable_from_manifest() {
let json = r#"{
"id": "timed-plugin",
"version": "1.0.0",
"sdk_version": "0.2.0",
"so_filename": "libtimed_plugin.so",
"test_timeout_ms": 12000
}"#;
let manifest: PluginManifest = serde_json::from_str(json).unwrap();
assert_eq!(manifest.test_timeout_ms, 12000);
}
#[test]
fn load_plugin_rejects_manifest_id_mismatch() {
let tmp = unique_test_dir("id_mismatch");
let plugin_dir = tmp.join("expected-plugin").join("1.0.0");
fs::create_dir_all(&plugin_dir).unwrap();
fs::write(
plugin_dir.join("manifest.json"),
r#"{
"id": "other-plugin",
"version": "1.0.0",
"sdk_version": "0.2.0",
"so_filename": "libexpected_plugin.so"
}"#,
)
.unwrap();
let loader = PluginLoader::new(&tmp);
let error = match loader.load_plugin("expected-plugin", Some("1.0.0")) {
Ok(_) => panic!("id mismatch should be rejected"),
Err(error) => error,
};
assert!(error.to_string().contains(
"plugin manifest id mismatch: requested 'expected-plugin', found 'other-plugin'"
));
let _ = fs::remove_dir_all(&tmp);
}
#[test]
fn load_plugin_rejects_manifest_version_mismatch() {
let tmp = unique_test_dir("version_mismatch");
let plugin_dir = tmp.join("expected-plugin").join("1.0.0");
fs::create_dir_all(&plugin_dir).unwrap();
fs::write(
plugin_dir.join("manifest.json"),
r#"{
"id": "expected-plugin",
"version": "2.0.0",
"sdk_version": "0.2.0",
"so_filename": "libexpected_plugin.so"
}"#,
)
.unwrap();
let loader = PluginLoader::new(&tmp);
let error = match loader.load_plugin("expected-plugin", Some("1.0.0")) {
Ok(_) => panic!("version mismatch should be rejected"),
Err(error) => error,
};
assert!(error
.to_string()
.contains("plugin manifest version mismatch: requested '1.0.0', found '2.0.0'"));
let _ = fs::remove_dir_all(&tmp);
}
}