Files
Unlucky-Launcher/crates/presenter/src/mock_data.rs
T
AskaEth 79b796fd58 fix(m161-r1): 崩溃/静默失败不再冒充「启动进程失败」;补三处假绿的守卫
复审 P1-1(用户可见假文案):`LaunchEnd` 在 `failure == None`(跑起来之后崩的)时
临时造了 `Stage::Spawn + format!("exit {:?}")`,于是磁贴写「启动失败 · 启动游戏进程」、
详情页漏出 `exit Some(1)`、静默失败丢掉「exit 0 却没起来」,而同一时刻 Toast 说「崩溃」。
- 新增 `LaunchFailureCause{Stage|Crashed|SilentFailure}`(dto/launch_failure.rs),
  `LaunchFailureCause::of(outcome, failure, exit_code)` 是唯一组装点;`InstanceRuntime::Failed{cause}`。
  `LaunchFailureStage` 保持「只装装配阶段」(文档那句「崩溃是结局不是阶段」现在成立)。
- 界面:`InstancesFailed`(框架)+ `InstancesCrashed` + `InstancesSilentFailed`;
  删掉 `InstancesFailedStage`。退出码按数字出词,不再有 Debug 文本。
- 用例:presenter 断言**枚举变体**(三种形状逐条)、app 断言**具体句子**
  (「不许出现 Some(」「进程起来了不许说启动游戏进程」「静默失败要点出没起来」)。
  mock 夹具与 launch_state_tests 的期望值同步;launch-error 三张图重拍。

复审 P2-1(`convert::failure_stage` 零覆盖):新增 `convert/stage_tests.rs` 5 条(三档 +
「既没回滚也没宣称干净」+ 非致命无阶段 + `failed_report_with` ⇒ NotStarted)。
变异实测:改成恒 `RolledBack` ⇒ 3 条红。

复审 P2-2(矮窗口用例的算式三个数全错、`body_height` 零测试):用例改成直接调
`frame::body_height`,chrome 从 `Ui::metrics` + `WidthClass::gutter` 现取,并把门槛与真实
chrome 绑死(chrome+MIN ⇒ Split、-1px ⇒ ScrollWhole);新增 `frame` 自己的 `body_height` 用例。
变异实测:`2.0 * gutter` → `gutter` ⇒ 2 条红。

复审 P2-3(源指纹闸门 / `Refused` 零覆盖):新增两条 core 用例——闸门(原地留着 prefs.json
+ 预先写好的同源收据 ⇒ Nothing 且收据未被重写,反证:源变了允许重放)、`Refused`
(`$schema` 1.7.0 ⇒ ReadOnly ⇒ 不动旧文件、不写收据、理由含「不可写」)。
变异实测:删闸门 ⇒ 1 条红;`can_save` 守卫改 `if false` ⇒ 1 条红。

顺手收:
- CLI `pack import` 那句「已整包回滚」也按三档出词(`RollbackStage` + `rollback_line`),
  加 2 条单测。CLI 不依赖 presenter,判定暂与 presenter 各一份 → 已 file finding 建议下沉 core。
- 设置页 `status_text()` 抽出 applying 档,用例断言「正在安装…」且非错误色。

金表:新 key 加在 `golden.rs`(81 行),未动 `golden/tail.rs`(211 行),避免与 M162 的拆分冲突。
2026-11-07 17:01:13 +08:00

267 lines
9.3 KiB
Rust

//! P0 假数据:实例 / 版本 / 日志 / 诊断 / 工具调用样例。
//!
//! 只被 [`crate::mock`] 与阶段 B 的 main.rs 使用;真实后端接入后整文件可删。
use crate::dto::{
AncientKind, ChatMessage, ChatRole, Diagnosis, DiagnosisReport, InstanceRuntime,
InstanceSummary, LaunchArgsView, LoaderInfo, LoaderKind, LogLevel, LogLine, LogOutcome,
LogSession, SessionId, ToolCallView, VersionCategory, VersionKind, VersionSummary,
};
/// P0 演示实例:覆盖空闲 / 失败两种磁贴状态(运行中由启动脚本产生)。
pub fn mock_instances() -> Vec<InstanceSummary> {
vec![
InstanceSummary {
id: "survival".to_string(),
name: "生存整合包".to_string(),
mc_version: "1.20.4".to_string(),
loader: LoaderInfo {
kind: LoaderKind::Fabric,
version: "0.16.5".to_string(),
},
last_played: Some("2026-10-29T19:22:00Z".to_string()),
runtime: InstanceRuntime::Idle,
icon: None,
isolation: "isolated".to_string(),
version_id: "1.20.4".to_string(),
version_present: true,
launch: mock_launch(),
},
InstanceSummary {
id: "vanilla-latest".to_string(),
name: "原版最新".to_string(),
mc_version: "1.21.9".to_string(),
loader: LoaderInfo {
kind: LoaderKind::Vanilla,
version: String::new(),
},
last_played: Some("2026-10-28T21:03:00Z".to_string()),
runtime: InstanceRuntime::Idle,
icon: None,
isolation: "isolated".to_string(),
version_id: "1.21.9".to_string(),
version_present: true,
launch: mock_launch(),
},
InstanceSummary {
id: "ancient-1.7.10".to_string(),
name: "远古 1.7.10".to_string(),
mc_version: "1.7.10".to_string(),
loader: LoaderInfo {
kind: LoaderKind::Forge,
version: "10.13.4.1614".to_string(),
},
last_played: None,
runtime: InstanceRuntime::Idle,
icon: None,
isolation: "shared".to_string(),
version_id: "1.7.10".to_string(),
version_present: false,
launch: mock_launch(),
},
InstanceSummary {
id: "modded-dev".to_string(),
name: "开发测试".to_string(),
mc_version: "1.21.1".to_string(),
loader: LoaderInfo {
kind: LoaderKind::NeoForge,
version: "21.1.72".to_string(),
},
last_played: Some("2026-10-27T12:40:00Z".to_string()),
// 静默失败 = **起来过**但 exit 0 没真正跑起来(不是「进程起不来」,见复审 P1-1)。
runtime: InstanceRuntime::Failed {
cause: crate::dto::LaunchFailureCause::SilentFailure { exit_code: Some(0) },
},
icon: None,
isolation: "isolated".to_string(),
version_id: "1.21.1-neoforge-21.1.72".to_string(),
version_present: true,
launch: mock_launch(),
},
]
}
/// P0 演示版本列表(含远古版三子类,验证分类侧栏与警示徽标)。
pub fn mock_versions() -> Vec<VersionSummary> {
fn row(id: &str, kind: VersionKind, released: &str, installed: bool) -> VersionSummary {
VersionSummary {
id: id.to_string(),
kind,
released: released.to_string(),
installed,
}
}
vec![
row("1.21.9", VersionKind::Release, "2026-09-20", false),
row("1.21.8", VersionKind::Release, "2026-07-11", true),
row("1.20.4", VersionKind::Release, "2023-12-07", true),
row("1.21.9-pre3", VersionKind::Snapshot, "2026-09-01", false),
row(
"rd-132211",
VersionKind::Ancient(AncientKind::Classic),
"2009-05-13",
false,
),
row(
"a1.2.6",
VersionKind::Ancient(AncientKind::OldAlpha),
"2010-08-11",
false,
),
row(
"b1.7.3",
VersionKind::Ancient(AncientKind::OldBeta),
"2011-07-08",
false,
),
]
}
/// 日志会话样例(✓ / ✗ / ⚠ 三种结局各一条)。
pub fn mock_log_sessions() -> Vec<LogSession> {
vec![
LogSession {
id: "s-1029-1922".to_string(),
instance: "生存整合包".to_string(),
when: "10-29 19:22".to_string(),
outcome: LogOutcome::Ok,
duration_secs: 3_780,
},
LogSession {
id: "s-1029-1840".to_string(),
instance: "开发测试".to_string(),
when: "10-29 18:40".to_string(),
outcome: LogOutcome::Crashed,
duration_secs: 62,
},
LogSession {
id: "s-1029-1755".to_string(),
instance: "原版最新".to_string(),
when: "10-29 17:55".to_string(),
outcome: LogOutcome::SilentFail,
duration_secs: 8,
},
]
}
/// 日志行样例(含一条 ERROR 供高亮验证)。
pub fn mock_log_lines() -> Vec<LogLine> {
fn line(text: &str, level: LogLevel) -> LogLine {
LogLine {
text: text.to_string(),
level,
}
}
vec![
line(
"[19:22:01] [main/INFO] Launching Minecraft 1.20.4",
LogLevel::Info,
),
line("[19:22:02] [main/INFO] Loading 42 mods", LogLevel::Info),
line(
"[19:22:03] [Render thread/WARN] GL vendor: unknown",
LogLevel::Warn,
),
line(
"[19:22:05] [Render thread/ERROR] OpenGL 2.1 not supported",
LogLevel::Error,
),
line("[19:22:05] [main/INFO] Stopping!", LogLevel::Info),
]
}
/// 崩溃诊断样例(验证日志页诊断卡片 + 「让 AI 详解」入口)。
pub fn mock_diagnosis(session: SessionId) -> DiagnosisReport {
DiagnosisReport {
session,
diagnosis: Diagnosis {
rule: "gpu.driver_old".to_string(),
title: "显卡驱动过旧(OpenGL 版本不足)".to_string(),
confidence: 92,
hint: "升级显卡驱动,或在启动参数里改用软件渲染".to_string(),
fix: Some("switch_software_renderer".to_string()),
},
}
}
/// 工具调用条目样例(AI 面板静态填充用)。
pub fn mock_tool_message() -> ChatMessage {
ChatMessage {
role: ChatRole::Tool,
text: "读到 128 行,无 ERROR".to_string(),
tool: Some(ToolCallView {
name: "read_game_log".to_string(),
ok: true,
summary: "读到 128 行,无 ERROR".to_string(),
}),
streaming: false,
}
}
/// 分类 → 该分类下的假版本(下载页切换分类时用)。
pub fn mock_versions_for(category: VersionCategory) -> Vec<VersionSummary> {
mock_versions()
.into_iter()
.filter(|version| match category {
VersionCategory::Release => version.kind == VersionKind::Release,
VersionCategory::Snapshot => version.kind == VersionKind::Snapshot,
VersionCategory::Ancient(kind) => version.kind == VersionKind::Ancient(kind),
VersionCategory::Modpack | VersionCategory::Mod => false,
})
.collect()
}
/// 演示用的启动参数视图(M157):一条真实形状的实例(有 JVM 参数、Vulkan、门槛放行)。
fn mock_launch() -> LaunchArgsView {
LaunchArgsView {
instance_id: "survival".to_string(),
jvm_args: "-XX:+UseG1GC".to_string(),
game_args: String::new(),
memory_mb: Some(4096),
memory_effective_mb: 4096,
java_path: String::new(),
java_effective: "C:/Java/temurin-21/bin/java.exe".to_string(),
graphics_backend: "vulkan".to_string(),
graphics_backend_effective: "vulkan".to_string(),
graphics_backend_allowed: true,
mc_version: "26.2-snapshot-2".to_string(),
graphics_backend_min_version: "26.2-snapshot-2".to_string(),
graphics_backend_options: vec![
"default".to_string(),
"opengl".to_string(),
"vulkan".to_string(),
],
}
}
#[cfg(test)]
mod tests {
use crate::dto::{AncientKind, VersionCategory, VersionKind};
use crate::mock_data::{mock_instances, mock_versions_for};
#[test]
fn mock_instances_cover_the_tile_states() {
let instances = mock_instances();
assert_eq!(instances.len(), 4);
assert!(instances.iter().any(|instance| matches!(
instance.runtime,
crate::dto::InstanceRuntime::Failed { .. }
)));
}
#[test]
fn versions_are_filtered_by_category() {
let release = mock_versions_for(VersionCategory::Release);
assert!(release
.iter()
.all(|version| version.kind == VersionKind::Release));
assert_eq!(release.len(), 3);
let classic = mock_versions_for(VersionCategory::Ancient(AncientKind::Classic));
assert_eq!(classic.len(), 1);
assert_eq!(classic[0].id, "rd-132211");
assert!(mock_versions_for(VersionCategory::Mod).is_empty());
}
}