fix(游戏分发领域): 派生 gameId 前先 trim projectKey

- derive_game_distribution_game_id 原先直接哈希原始字节," proj-1 " 与 "proj-1" 会派生出两个身份
- 与 game_distribution_version_key 的 trim 口径对齐,并补单测断言两侧空白归一化
This commit is contained in:
2026-10-07 19:18:45 +08:00
parent c90f14b9bc
commit 87dc80c750
@@ -69,8 +69,10 @@ pub fn game_distribution_version_key(
/// 首次发布的游戏身份:由 `(ownerUserId, project_key)` 确定性派生。
///
/// 确定性派生让媒体 objectKey 前缀在事务前就稳定,重试 / 并发不会落到不同命名空间;结果满足
/// gameId 的 `[A-Za-z0-9_-]` 约束。
/// gameId 的 `[A-Za-z0-9_-]` 约束。`project_key` 先 trim,与自然幂等键
/// [`game_distribution_version_key`] 的归一化口径一致,避免 `" proj-1 "` 与 `"proj-1"` 派生出两个身份。
pub fn derive_game_distribution_game_id(owner_user_id: &str, project_key: &str) -> String {
let project_key = project_key.trim();
let mut hasher = Sha256::new();
hasher.update(owner_user_id.as_bytes());
hasher.update([0]);
@@ -278,6 +280,11 @@ mod tests {
let other_owner = derive_game_distribution_game_id("owner-2", "proj-1");
let other_project = derive_game_distribution_game_id("owner-1", "proj-2");
assert_eq!(first, again);
assert_eq!(
first,
derive_game_distribution_game_id("owner-1", " proj-1 "),
"project_key 两侧空白必须归一化"
);
assert_ne!(first, other_owner);
assert_ne!(first, other_project);
assert!(first.starts_with(GAME_ID_PREFIX));