① update.auto_check / update.channel 登记进 core setting schema(1.6.0)
- 新增 setting/keys/update.rs(两键 + UPDATE_CHANNELS,只允许 stable);
SettingSchema 加 with_all 供域键表批量登记。
- 新增 setting/update_prefs.rs:cache/update/prefs.json → launcher.json 的一次性迁移。
四条硬约束:源指纹闸门(幂等)、不丢 false、新值胜出、旧文件改名 .migrated 后不再被读;
主配置不可写时不动旧文件。收据 config/update-prefs-migration.json。
- presenter Runtime 建会话时迁一次并把结局留在 migration();prefs 字段换成
auto_check + 每次派发从主配置读 channel(不再硬编码 Channel::Stable)。
保留旧字段 auto_check 的真实来源(原有测试 cases 会断言)。
- CLI config get/set/dump 先跑同一次迁移(幂等),输出里给 migration 段与收据路径。
② presenter/dto/events.rs 的「本次会话不再弹提示条」注释改成真实语义
(记的是版本号:同版本压住、更高版本照弹、手动检查清掉)。
③ 检查/下载进行中按钮禁用:pages/update/busy.rs 的 busy/can_check/can_apply;
设置页两颗按钮改画禁用(不是消失),app 层 on_update_action 再挡一道
(忙碌时 CheckNow/Apply 不派发);新增两条用例(含「忙完恢回可点」)。
④ 内容库低高度:content_layout::MIN_BODY_HEIGHT=460 + BodyMode(按固定件+三整行算出来,
用例守着算式);frame::body_height 从 Ui::metrics 算正文可用高,pages::Model 带 window_height。
≥460 列表自己滚;<460 主列整列一个滚动条、列表按自然高度铺开。
⑤ 启动错误文案本地化:presenter 删掉 launch_summary / 「启动失败:」前缀 / 五条中文阶段前缀,
改发 LaunchFailureView{stage,detail};LaunchFinished 改带 exit_code/uptime_ms/failure,
Toast 由 app 层按 i18n key 现求(与 install_cancelled 同构)。InstancesFailed 拆成
InstancesFailedStage(列表单行)+ LaunchStageFailed(详情/Toast 全文)。
⑥ 导入结果块:ImportReportView 加 failure_stage(NotStarted/RolledBack/RollbackIncomplete,
由 core 的 rollback 机器位推出);标题、图标、底色三处分档,预检失败不再说「已回滚」。
顺带:六处注释/文档漂移(update.rs 的分工表与偏好落点、pack 的「已整包回滚」、
build_request 的 Errors、settings/update 的按钮禁用说明等),
以及为守 400 行把 update/cli-config/core-keys/presenter-dto/presenter-launch/i18n-golden
里贴线或越线的文件按模块拆开(纯搬移,行为不变)。
253 lines
9.6 KiB
Rust
253 lines
9.6 KiB
Rust
//! D20 隔离键的 schema 契约:全局 `instances.mode`、实例 `isolation`、`lastModeChange`、
|
||
//! 恒共享层不可配、旧配置缺键即默认值(不需要迁移步骤)。
|
||
|
||
use serde_json::json;
|
||
use unlucky_core::game::install::instance::{InstanceConfig, Isolation, OverrideKey};
|
||
use unlucky_core::game::isolation::{observe_mode, IsolationMode};
|
||
use unlucky_core::game::GameLayout;
|
||
use unlucky_core::setting::document::{LoadProblem, RenderMode, SettingDocument};
|
||
use unlucky_core::setting::instance_keys::{
|
||
instance_schema, instance_version, ISOLATION_OVERRIDES, ISOLATION_RESOLVED, OVERRIDE_KEYS,
|
||
};
|
||
use unlucky_core::setting::keys::{launcher_schema, launcher_version, INSTANCE_MODES};
|
||
use unlucky_core::setting::schema::{SchemaId, SchemaVersion};
|
||
use unlucky_core::setting::spec::{Sensitivity, ValueKind};
|
||
use unlucky_core::setting::store::SettingStore;
|
||
|
||
use super::common::TempDir;
|
||
|
||
#[test]
|
||
fn global_mode_key_defaults_to_isolated() {
|
||
let schema = launcher_schema();
|
||
let spec = schema.spec("instances.mode").expect("instances.mode 键");
|
||
assert_eq!(spec.default, json!("isolated"), "D20 默认隔离");
|
||
assert_eq!(spec.kind, ValueKind::Enum(INSTANCE_MODES));
|
||
assert_eq!(
|
||
spec.ai.sensitivity,
|
||
Sensitivity::High,
|
||
"切档会改变存档可见性,AI 改必须弹窗确认"
|
||
);
|
||
assert!(schema.validate("instances.mode", &json!("shared")).is_ok());
|
||
assert!(schema
|
||
.validate("instances.mode", &json!("isolated"))
|
||
.is_ok());
|
||
assert!(schema
|
||
.validate("instances.mode", &json!("per_version"))
|
||
.is_err());
|
||
assert!(schema.validate("instances.mode", &json!("custom")).is_err());
|
||
}
|
||
|
||
#[test]
|
||
fn shared_layers_are_not_configurable() {
|
||
let schema = launcher_schema();
|
||
for key in ["shared.versions", "shared.assets", "shared.libraries"] {
|
||
assert!(
|
||
!schema.is_known(key),
|
||
"{key} 是恒共享层,不该出现在可配键表里"
|
||
);
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn instance_isolation_defaults_to_inherit() {
|
||
let schema = instance_schema();
|
||
let spec = schema.spec("isolation").expect("isolation 键");
|
||
assert_eq!(spec.default, json!("inherit"));
|
||
assert_eq!(spec.kind, ValueKind::Enum(ISOLATION_OVERRIDES));
|
||
for value in ["inherit", "isolated", "shared", "custom"] {
|
||
assert!(
|
||
schema.validate("isolation", &json!(value)).is_ok(),
|
||
"{value}"
|
||
);
|
||
}
|
||
assert!(schema.validate("isolation", &json!("per_version")).is_err());
|
||
}
|
||
|
||
#[test]
|
||
fn inherit_never_reaches_the_file() {
|
||
// 默认值不落盘:空文档渲染出来只应该有 $schema / schemaVersion。
|
||
let schema = instance_schema();
|
||
let (document, problems) = SettingDocument::load(&schema, &json!({}));
|
||
assert!(problems.is_empty(), "{problems:?}");
|
||
let rendered = document.render(RenderMode::Full);
|
||
assert!(rendered.get("isolation").is_none(), "{rendered}");
|
||
assert!(rendered.get("lastModeChange").is_none(), "{rendered}");
|
||
let dirty = document.render(RenderMode::DirtyOnly);
|
||
assert!(dirty.get("isolation").is_none(), "{dirty}");
|
||
}
|
||
|
||
#[test]
|
||
fn last_mode_change_keys_are_bookkeeping_only() {
|
||
let schema = instance_schema();
|
||
for key in [
|
||
"lastModeChange.at",
|
||
"lastModeChange.from",
|
||
"lastModeChange.to",
|
||
"lastModeChange.probe.isolatedSide",
|
||
"lastModeChange.probe.sharedSide",
|
||
"lastModeChange.warned",
|
||
] {
|
||
assert!(schema.is_known(key), "{key} 必须登记");
|
||
assert!(!schema.accepts_ai_write(key), "{key} 不许 AI 写");
|
||
}
|
||
assert!(schema
|
||
.validate("lastModeChange.probe.isolatedSide", &json!(12))
|
||
.is_ok());
|
||
assert!(schema
|
||
.validate("lastModeChange.probe.isolatedSide", &json!(-1))
|
||
.is_err());
|
||
assert!(schema
|
||
.validate("lastModeChange.warned", &json!(true))
|
||
.is_ok());
|
||
assert!(schema.validate("lastModeChange.warned", &json!(1)).is_err());
|
||
assert!(schema
|
||
.validate("lastModeChange.from", &json!("shared"))
|
||
.is_ok());
|
||
assert!(schema
|
||
.validate("lastModeChange.from", &json!("inherit"))
|
||
.is_err());
|
||
assert_eq!(ISOLATION_RESOLVED, ["isolated", "shared", "custom"]);
|
||
}
|
||
|
||
#[test]
|
||
fn overrides_key_is_an_enum_whitelist() {
|
||
let schema = instance_schema();
|
||
assert!(schema.validate("overrides", &json!([])).is_ok());
|
||
assert!(schema
|
||
.validate("overrides", &json!(["maxMemory", "runningDirectory"]))
|
||
.is_ok());
|
||
assert!(
|
||
schema
|
||
.validate("overrides", &json!(["max_memory"]))
|
||
.is_err(),
|
||
"旧 snake_case 拼写不许过(拼错会静默失效)"
|
||
);
|
||
assert!(schema.validate("overrides", &json!([1])).is_err());
|
||
assert_eq!(OVERRIDE_KEYS, ["maxMemory", "javaType", "runningDirectory"]);
|
||
}
|
||
|
||
#[test]
|
||
fn schema_versions_register_the_new_keys_without_migration() {
|
||
// 1.6.0 登记 M161 的自更新两键(`update.auto_check` / `update.channel`;
|
||
// 1.5.0 = M116 窗口圆角,1.4.0 = M91 外观下层三键,1.3.0 = D17「外观(高级)」,
|
||
// 1.2.0 = D23 的 JDK 落点/优先级键)。
|
||
assert_eq!(launcher_version(), SchemaVersion::new(1, 6, 0));
|
||
assert_eq!(instance_version(), SchemaVersion::new(1, 1, 0));
|
||
assert_eq!(launcher_schema().id(), SchemaId::LAUNCHER);
|
||
// major 仍是 1:迁移链(按 major 走)不需要新步骤,老文件缺键即默认值。
|
||
assert_eq!(launcher_version().major, 1);
|
||
assert_eq!(instance_version().major, 1);
|
||
}
|
||
|
||
#[test]
|
||
fn old_launcher_file_without_new_keys_loads_with_defaults() {
|
||
let temp = std::env::temp_dir().join(format!(
|
||
"ull-setting-d20-{}-{}",
|
||
std::process::id(),
|
||
std::time::SystemTime::now()
|
||
.duration_since(std::time::UNIX_EPOCH)
|
||
.map_or(0, |d| d.as_nanos())
|
||
));
|
||
std::fs::create_dir_all(&temp).expect("临时目录");
|
||
let path = temp.join("launcher.json");
|
||
// 旧版本写的文件:只有 $schema 1.0.0 和一个老键,没有 instances.mode。
|
||
std::fs::write(
|
||
&path,
|
||
serde_json::to_vec_pretty(&json!({
|
||
"$schema": "https://schemas.unlucky-launcher.dev/ull/launcher/1.0.0",
|
||
"schemaVersion": 1,
|
||
"ui": { "scale": 1.25 }
|
||
}))
|
||
.expect("序列化"),
|
||
)
|
||
.expect("写文件");
|
||
|
||
let store = SettingStore::load(&path, launcher_schema());
|
||
assert!(store.access().can_read(), "1.0.0 文件必须可读");
|
||
assert!(store.problems().is_empty(), "{:?}", store.problems());
|
||
assert_eq!(
|
||
store.doc().effective_str("instances.mode"),
|
||
Some("isolated"),
|
||
"缺键取 D20 默认值"
|
||
);
|
||
assert_eq!(store.doc().effective_f64("ui.scale"), Some(1.25));
|
||
let _ = std::fs::remove_dir_all(&temp);
|
||
}
|
||
|
||
#[test]
|
||
fn setting_lists_agree_with_the_game_enums() {
|
||
// schema 白名单与 game 侧枚举必须一一对应(两处字面量漂移就是静默不生效)。
|
||
let mut modes: Vec<&str> = [IsolationMode::Isolated, IsolationMode::Shared]
|
||
.iter()
|
||
.map(|mode| mode.as_str())
|
||
.collect();
|
||
modes.sort_unstable();
|
||
let mut declared = INSTANCE_MODES.to_vec();
|
||
declared.sort_unstable();
|
||
assert_eq!(modes, declared);
|
||
|
||
for value in ISOLATION_OVERRIDES
|
||
.iter()
|
||
.filter(|value| **value != "inherit")
|
||
{
|
||
assert!(
|
||
Isolation::parse(value).is_some(),
|
||
"{value} 在 game 侧解析不出来"
|
||
);
|
||
assert_eq!(Isolation::parse(value).map(Isolation::as_str), Some(*value));
|
||
}
|
||
for value in ISOLATION_RESOLVED {
|
||
assert!(Isolation::parse(value).is_some(), "{value}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn writer_output_loads_without_unknown_or_invalid_keys() {
|
||
// 回归 P2-1(review r1):写入者(InstanceConfig + observe_mode)产出的键必须被 schema 全认。
|
||
// 之前 `lastModeChange.warned` 没登记,document 层会把真实文件标成 UnknownKey——
|
||
// 这正是「schema 只做写入者白名单视图」这条契约的裂缝。
|
||
let temp = TempDir::new("d20-writer-shape");
|
||
let game = GameLayout::new(temp.child("game"));
|
||
let mut config = InstanceConfig::new("11111111-2222-4333-8444-555555555555", "1.20.1");
|
||
config.isolation = Some(Isolation::Custom);
|
||
config.game_dir = Some(temp.child("elsewhere"));
|
||
config.overrides = vec![OverrideKey::MaxMemory];
|
||
observe_mode(
|
||
&mut config,
|
||
&game,
|
||
"2026-10-30T00:00:00Z",
|
||
Isolation::Custom,
|
||
)
|
||
.expect("观察");
|
||
|
||
let raw: serde_json::Value =
|
||
serde_json::from_str(&config.to_json_string().expect("序列化")).expect("合法 JSON");
|
||
// 写入者确实输出了 warned(P2-1 的前提)。
|
||
assert_eq!(raw["lastModeChange"]["warned"], json!(true));
|
||
|
||
let (document, problems) = SettingDocument::load(&instance_schema(), &raw);
|
||
let mut unknown: Vec<String> = Vec::new();
|
||
let mut invalid: Vec<String> = Vec::new();
|
||
for problem in &problems {
|
||
match problem {
|
||
LoadProblem::UnknownKey { key } => unknown.push(key.clone()),
|
||
LoadProblem::InvalidValue { key, reason } => invalid.push(format!("{key}: {reason}")),
|
||
_ => {}
|
||
}
|
||
}
|
||
assert!(unknown.is_empty(), "写入者的键没登记进 schema:{unknown:?}");
|
||
assert!(
|
||
invalid.is_empty(),
|
||
"写入者的值过不了 schema 校验:{invalid:?}"
|
||
);
|
||
assert_eq!(
|
||
document.get("lastModeChange.warned"),
|
||
Some(&json!(true)),
|
||
"记账键要能读回"
|
||
);
|
||
// 落盘视图不能把它当「未知键」重复输出。
|
||
let rendered = document.render(RenderMode::Full);
|
||
assert_eq!(rendered["lastModeChange"]["warned"], json!(true));
|
||
assert_eq!(rendered["overrides"], json!(["maxMemory"]));
|
||
}
|