fix(游戏共创): 非法游标回归稳定码 THEME_INVALID_CURSOR(契约码此前在 HTTP 面不可达)
Project CI / Backend tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust crates (pull_request) Has been cancelled
Project CI / Native shell tests (pull_request) Has been cancelled
Project CI / Frontend tests (pull_request) Has been cancelled
Project CI / Repository checks (pull_request) Has been cancelled
Project CI / AI game creator shell web tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust lane 1/2 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust lane 2/2 (pull_request) Has been cancelled
Project CI / Backend tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust crates (pull_request) Has been cancelled
Project CI / Native shell tests (pull_request) Has been cancelled
Project CI / Frontend tests (pull_request) Has been cancelled
Project CI / Repository checks (pull_request) Has been cancelled
Project CI / AI game creator shell web tests (pull_request) Has been cancelled
Project CI / AI game creator shell Rust lane 1/2 (pull_request) Has been cancelled
Project CI / AI game creator shell Rust lane 2/2 (pull_request) Has been cancelled
端到端实测(54 项里唯一红的那条):`GET /api/game-distribution/themes?cursor=abc` 回 400,但
`error.code = "BAD_REQUEST"`——技术方案 §3.4 与主题 e2e 脚本承诺的稳定码 `THEME_INVALID_CURSOR`
在 HTTP 面上拿不到,客户端只能去匹配中文文案。
根因:`map_spacetime_error` 的 `THEME_` 分支前置条件是「消息含 `THEME_`」,而模块侧游标解析只回
中文串「主题列表游标格式无效」⇒ 落兜底分支成 400 + 通用 `BAD_REQUEST`;契约常量与那条映射分支
因此是死代码。且 api-server 原有单测**明确要求**文案不含 `THEME_`(口径留了两套)。
口径二选一,选**让码可达**(模块产出前缀),删掉「要求不含 `THEME_`」的断言:
- `module-game-distribution/src/errors.rs`:新增 6 个主题码常量
`GAME_DISTRIBUTION_THEME_{NOT_FOUND,BAD_REQUEST,INVALID_CURSOR,IDEMPOTENCY_CONFLICT,MEMBER_NOT_ROOT,MEMBER_GAME_NOT_FOUND}_CODE`,
并把 `Display` 从字面量改为**复用同一批常量**(消灭「常量改了、`Display` 没改」这类只有真栈才发现的漂移)。
- `theme.rs`:`parse_game_distribution_theme_cursor` 产出 `THEME_INVALID_CURSOR: 主题列表游标格式无效`;
文档注释更新为「只保留 `FORK_` 这个排在 `THEME_` 分支之前的子串禁忌」(其余子串已抢不到)。
- `lib.rs`:导出新增的 6 个码常量。
- 测试:
· `domain.rs::theme_errors_are_prefixed_with_their_machine_code` 改为**枚举全部 6 个主题码**(含唯一
不走领域变体的游标路径——正是当初漏掉它的原因),并加「任何新增码都必须进这张表」的覆盖完整性断言;
· `theme.rs::invalid_cursor_message_stays_in_the_bad_request_bucket` 增加「必须以稳定码开头」;
· api-server 新增 `theme_error_codes_are_reachable_from_module_messages`:用**模块真实产出的消息**
逐码断言 `(状态码, 稳定码)` 可达,并比对模块常量与 `shared-contracts` 常量是同一组字符串;
· api-server `theme_list_invalid_cursor_maps_to_bad_request` 改为断言映射出 `THEME_INVALID_CURSOR`,
只把 `FORK_` 留在禁忌列表里(并写明理由)。
门禁:`cargo test -p module-game-distribution` **100 passed**;`cargo test -p api-server game_distribution`
**86 passed**;`cargo check --all-targets` 0;DTO parity 58 组 / 10 构建器;`check:encoding` 0;
`cargo fmt --all -- --check` 0;`git diff --check` 0。
This commit is contained in:
@@ -8678,33 +8678,109 @@ mod tests {
|
||||
assert_eq!(GAME_DISTRIBUTION_THEME_PAGE_LIMIT_DEFAULT, 20);
|
||||
}
|
||||
|
||||
/// 主题列表非法游标必须落 400:模块侧文案经 `map_spacetime_error` 的兜底分支,
|
||||
/// 不得被 404 / 409 的子串规则抢先命中(与 `/my-collections` 同一条纪律)。
|
||||
/// 主题列表非法游标必须落 400 **且带稳定码 `THEME_INVALID_CURSOR`**。
|
||||
///
|
||||
/// 这条测试是那次真缺陷的回归网:模块侧曾经只回中文串「主题列表游标格式无效」,不含 `THEME_`,
|
||||
/// 于是 `THEME_` 映射分支不命中,落兜底 400 + 通用 `BAD_REQUEST`——契约里的稳定码在 HTTP 面上
|
||||
/// **不可达**,客户端只能匹配中文文案。现在两头都钉住:模块产出前缀(模块侧枚举测试),
|
||||
/// 这里钉「HTTP 面真的映射出那个码」。
|
||||
///
|
||||
/// 文案不得含 `FORK_`:`FORK_` 分支排在 `THEME_` **之前**,只要文案里出现 `FORK_`,这条错误
|
||||
/// 就会被 FORK 分支抢走(其余「不存在」「状态」等子串都在 `THEME_` 之后,抢不到了)。
|
||||
#[test]
|
||||
fn theme_list_invalid_cursor_maps_to_bad_request() {
|
||||
let message = module_game_distribution::parse_game_distribution_theme_cursor("不是游标")
|
||||
.expect_err("非法游标必须报错");
|
||||
assert!(message.contains("格式无效"), "{message}");
|
||||
for forbidden in [
|
||||
"不存在",
|
||||
"已被删除",
|
||||
"状态",
|
||||
"不匹配",
|
||||
"幂等",
|
||||
"已存在",
|
||||
"FORK_",
|
||||
"THEME_",
|
||||
] {
|
||||
assert!(
|
||||
!message.contains("FORK_"),
|
||||
"游标错误文案不得含 `FORK_`(FORK 分支排在 THEME 之前,会被它抢走):{message}"
|
||||
);
|
||||
let error = map_spacetime_error(SpacetimeClientError::Procedure(message.clone()));
|
||||
assert_eq!(error.status_code(), StatusCode::BAD_REQUEST, "{message}");
|
||||
assert_eq!(
|
||||
error.code(),
|
||||
GAME_DISTRIBUTION_THEME_INVALID_CURSOR,
|
||||
"非法游标必须映射出契约里的稳定码,否则客户端只能匹配中文文案:{message}"
|
||||
);
|
||||
}
|
||||
|
||||
/// **可达性**:6 个主题码里每一个都必须能从**模块真实产出的文案**映射出 (状态码, 稳定码)。
|
||||
///
|
||||
/// 这是那次真缺陷的根因所在的另一面:`theme_errors_map_to_documented_status_codes` 用的是
|
||||
/// `format!("{code}: 具体原因")` 这类**合成**文案,只能证明「映射表里有这条」,证明不了「模块
|
||||
/// 真的会产出这个前缀」——`THEME_INVALID_CURSOR` 就此成了死代码。这条测试改用模块真实产出的
|
||||
/// 消息(5 个领域变体的 `Display` + 游标解析的裸字符串),顺带比对模块常量与 `shared-contracts`
|
||||
/// 常量是同一组字符串(两侧改字都会红)。
|
||||
#[test]
|
||||
fn theme_error_codes_are_reachable_from_module_messages() {
|
||||
use module_game_distribution::GameDistributionError as DomainError;
|
||||
|
||||
let cases = [
|
||||
(
|
||||
DomainError::ThemeNotFound.to_string(),
|
||||
module_game_distribution::GAME_DISTRIBUTION_THEME_NOT_FOUND_CODE,
|
||||
GAME_DISTRIBUTION_THEME_NOT_FOUND,
|
||||
StatusCode::NOT_FOUND,
|
||||
),
|
||||
(
|
||||
DomainError::ThemeBadRequest {
|
||||
reason: "主题名不能为空".to_string(),
|
||||
}
|
||||
.to_string(),
|
||||
module_game_distribution::GAME_DISTRIBUTION_THEME_BAD_REQUEST_CODE,
|
||||
GAME_DISTRIBUTION_THEME_BAD_REQUEST,
|
||||
StatusCode::BAD_REQUEST,
|
||||
),
|
||||
(
|
||||
module_game_distribution::parse_game_distribution_theme_cursor("不是游标")
|
||||
.expect_err("非法游标必须报错"),
|
||||
module_game_distribution::GAME_DISTRIBUTION_THEME_INVALID_CURSOR_CODE,
|
||||
GAME_DISTRIBUTION_THEME_INVALID_CURSOR,
|
||||
StatusCode::BAD_REQUEST,
|
||||
),
|
||||
(
|
||||
DomainError::ThemeIdempotencyConflict.to_string(),
|
||||
module_game_distribution::GAME_DISTRIBUTION_THEME_IDEMPOTENCY_CONFLICT_CODE,
|
||||
GAME_DISTRIBUTION_THEME_IDEMPOTENCY_CONFLICT,
|
||||
StatusCode::CONFLICT,
|
||||
),
|
||||
(
|
||||
DomainError::ThemeMemberNotRoot {
|
||||
game_id: "game_child".to_string(),
|
||||
}
|
||||
.to_string(),
|
||||
module_game_distribution::GAME_DISTRIBUTION_THEME_MEMBER_NOT_ROOT_CODE,
|
||||
GAME_DISTRIBUTION_THEME_MEMBER_NOT_ROOT,
|
||||
StatusCode::CONFLICT,
|
||||
),
|
||||
(
|
||||
DomainError::ThemeMemberGameNotFound {
|
||||
game_id: "game_missing".to_string(),
|
||||
}
|
||||
.to_string(),
|
||||
module_game_distribution::GAME_DISTRIBUTION_THEME_MEMBER_GAME_NOT_FOUND_CODE,
|
||||
GAME_DISTRIBUTION_THEME_MEMBER_GAME_NOT_FOUND,
|
||||
StatusCode::NOT_FOUND,
|
||||
),
|
||||
];
|
||||
for (message, module_code, contract_code, expected_status) in &cases {
|
||||
assert_eq!(
|
||||
module_code, contract_code,
|
||||
"模块常量与 shared-contracts 常量必须是同一组字符串:{message}"
|
||||
);
|
||||
assert!(
|
||||
!message.contains(forbidden),
|
||||
"游标错误文案不得含「{forbidden}」:{message}"
|
||||
message.starts_with(contract_code),
|
||||
"模块产出的文案必须以契约码开头(否则 HTTP 面不可达):{message}"
|
||||
);
|
||||
let error = map_spacetime_error(SpacetimeClientError::Procedure(message.clone()));
|
||||
assert_eq!(error.status_code(), *expected_status, "{message}");
|
||||
assert_eq!(
|
||||
error.code(),
|
||||
*contract_code,
|
||||
"稳定码必须原样透传到 HTTP 面:{message}"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
map_spacetime_error(SpacetimeClientError::Procedure(message.clone())).status_code(),
|
||||
StatusCode::BAD_REQUEST,
|
||||
"{message}"
|
||||
);
|
||||
}
|
||||
|
||||
/// 主题列表响应形状:`themes` / `nextCursor` 一定发出;游标是真实值,最后一页为 `null`;
|
||||
|
||||
Reference in New Issue
Block a user