写入侧产出 canonical kind,读取侧读时重派生自愈存量分类
- 写入侧:infer_canvas_export_asset_kind 直接返回 canonical kind(animation → character-animation、ui → ui-design、asset → image),不再让别名表替写入侧兜底 - 写入侧补防御性归一,并新增 canvas_export_asset_kind_is_always_canonical 逐分支钉死返回值为 canonical、且不再写回 ui / animation / asset - 读取侧:gameCreationAppAssetCategory 加收窄覆盖规则——落盘 category 为 unclassified 且 kind 能派生出明确非 unclassified 分类时采用派生值 - 该规则不写迁移脚本、永久自愈:真机 57 条 ui 资产待归类从 58 降到 1,UI 交互从 2 升到 59,仅剩 code 类 game-entry 在待归类(分类表设计口径) - 收窄条件把覆盖窗口压到最小:落盘值本身是明确分类时仍信任落盘值(保留用户在分类与标签面板手动设置的权威性) - kind 派生结果本身即 unclassified 的 image / video / code / publication-material 不受影响,补断言钉住 - resourceCardPreviewRealManifest 由「记录缺陷」改为「守住修复」:断言归位后的 58 → 1 分布与 57 条 ui 全部落 UI 交互 - PRD 与 pitfalls 补记分类取值优先级、读时自愈规则、唯一盲区(手动把可明确分类的资产设为待归类会被覆盖)及写入侧 canonical 化
This commit is contained in:
@@ -1635,27 +1635,34 @@ pub(crate) fn normalized_zip_entry_name(name: &str) -> Result<String, String> {
|
||||
normalize_relative_path(&normalized)
|
||||
}
|
||||
|
||||
/// 画板导出图层推断出的资源 kind。
|
||||
///
|
||||
/// 返回值必须是 **canonical kind**:这个值会被 `register_local_asset_entry` 原样写入
|
||||
/// manifest,并据以派生落盘 `category`。历史上这里写过非 canonical 的
|
||||
/// `ui` / `animation` / `asset`,只能靠别名表兜底,等于同时维护两套词汇;别名表只用于
|
||||
/// 兼容存量数据,不作为新写入值的来源。
|
||||
pub(crate) fn infer_canvas_export_asset_kind(
|
||||
layer: &CanvasExportLayerMetadata,
|
||||
file: &str,
|
||||
) -> &'static str {
|
||||
let layer_type = layer.visible.layer_type.as_str();
|
||||
if file.starts_with("sequences/") || contains_any(layer_type, &["序列", "动画", "动作"]) {
|
||||
return "animation";
|
||||
}
|
||||
if file.starts_with("media/") || contains_any(layer_type, &["音频", "音乐", "音效"]) {
|
||||
return "audio";
|
||||
}
|
||||
if contains_any(layer_type, &["角色"]) {
|
||||
return "character";
|
||||
}
|
||||
if contains_any(layer_type, &["场景", "背景"]) {
|
||||
return "scene";
|
||||
}
|
||||
if contains_any(layer_type, &["UI", "界面", "图标"]) {
|
||||
return "ui";
|
||||
}
|
||||
"asset"
|
||||
let inferred = if file.starts_with("sequences/")
|
||||
|| contains_any(layer_type, &["序列", "动画", "动作"])
|
||||
{
|
||||
"character-animation"
|
||||
} else if file.starts_with("media/") || contains_any(layer_type, &["音频", "音乐", "音效"]) {
|
||||
"audio"
|
||||
} else if contains_any(layer_type, &["角色"]) {
|
||||
"character"
|
||||
} else if contains_any(layer_type, &["场景", "背景"]) {
|
||||
"scene"
|
||||
} else if contains_any(layer_type, &["UI", "界面", "图标"]) {
|
||||
"ui-design"
|
||||
} else {
|
||||
"image"
|
||||
};
|
||||
// 防御性归一:分支字面量写错时由覆盖测试暴露,这里再兜一层。
|
||||
shared_contracts::game_creation_app::canonical_game_creation_app_asset_kind(inferred)
|
||||
}
|
||||
|
||||
pub(crate) fn infer_canvas_export_media_type(file: &str) -> &'static str {
|
||||
@@ -1975,6 +1982,67 @@ mod tests {
|
||||
use super::*;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
/// 画板导出推断出的 kind 必须已经是 canonical 值。
|
||||
///
|
||||
/// 这个值会被原样写进 manifest 并据以派生落盘 `category`;一旦写出非 canonical 值
|
||||
/// (历史上曾写 `ui` / `animation` / `asset`),就只能靠别名表兜底,等于同时维护
|
||||
/// 两套词汇,且已落盘的 category 无法随别名修复自愈。这里逐分支钉死。
|
||||
#[test]
|
||||
fn canvas_export_asset_kind_is_always_canonical() {
|
||||
fn layer(layer_type: &str) -> CanvasExportLayerMetadata {
|
||||
serde_json::from_value(serde_json::json!({
|
||||
"title": "测试图层",
|
||||
"file": "layer.png",
|
||||
"exportError": null,
|
||||
"visible": {
|
||||
"type": layer_type,
|
||||
"model": "-",
|
||||
"task": "-",
|
||||
"object": "-",
|
||||
},
|
||||
}))
|
||||
.expect("canvas export layer metadata")
|
||||
}
|
||||
|
||||
// 每个分支的代表输入 → 期望的 canonical kind。
|
||||
let cases: [(&str, &str, &str); 6] = [
|
||||
("序列", "sequences/01.png", "character-animation"),
|
||||
("动画", "layer.png", "character-animation"),
|
||||
("音频", "layer.png", "audio"),
|
||||
("角色", "layer.png", "character"),
|
||||
("场景", "layer.png", "scene"),
|
||||
("UI", "layer.png", "ui-design"),
|
||||
// 无匹配时落到 image。
|
||||
];
|
||||
for (layer_type, file, expected) in cases {
|
||||
let kind = infer_canvas_export_asset_kind(&layer(layer_type), file);
|
||||
assert_eq!(kind, expected, "layer_type={layer_type} file={file}");
|
||||
}
|
||||
// media/ 前缀同样走音频分支。
|
||||
assert_eq!(
|
||||
infer_canvas_export_asset_kind(&layer("图层"), "media/bgm.mp3"),
|
||||
"audio"
|
||||
);
|
||||
// 兜底分支是 image 而不是旧的 "asset"。
|
||||
assert_eq!(
|
||||
infer_canvas_export_asset_kind(&layer("图层"), "layer.png"),
|
||||
"image"
|
||||
);
|
||||
|
||||
// 所有分支的返回值都必须在 canonical 目录里,且不能是旧的非 canonical 写法。
|
||||
for layer_type in ["序列", "音频", "角色", "场景", "UI", "图层", "其他"] {
|
||||
let kind = infer_canvas_export_asset_kind(&layer(layer_type), "layer.png");
|
||||
assert!(
|
||||
shared_contracts::game_creation_app::GAME_CREATION_APP_CANONICAL_ASSET_KINDS
|
||||
.contains(&kind),
|
||||
"非 canonical kind: {kind}(layer_type={layer_type})"
|
||||
);
|
||||
for legacy in ["ui", "animation", "asset"] {
|
||||
assert_ne!(kind, legacy, "写回了非 canonical 的 {legacy}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn infer_file_extension_preserves_supported_local_resource_extensions() {
|
||||
assert_eq!(
|
||||
|
||||
@@ -118,63 +118,59 @@ describe('真机 manifest 取证:占位卡片计数与栏目分布', () => {
|
||||
expect(resources).toHaveLength(61);
|
||||
});
|
||||
|
||||
test('存量 category 与新 kind 口径的差异必须显式记录(别名救不了存量)', () => {
|
||||
test('存量 57 条 ui 的落盘 unclassified 被读时重派生自愈', () => {
|
||||
const assets = realManifest().assets;
|
||||
const drifted = assets
|
||||
.map((asset) => ({
|
||||
kind: asset.kind,
|
||||
onDisk: asset.category,
|
||||
// 只按 kind 重新派生(丢弃存量 category),这就是加别名之后新登记资产会拿到的值。
|
||||
derivedFromKind: gameCreationAppAssetCategory({
|
||||
kind: asset.kind,
|
||||
category: undefined,
|
||||
}),
|
||||
}))
|
||||
.filter((entry) => entry.onDisk !== entry.derivedFromKind);
|
||||
const uiAssets = assets.filter((asset) => asset.kind === 'ui');
|
||||
|
||||
const driftedByKind = new Map<
|
||||
string,
|
||||
{ onDisk: string; derivedFromKind: string; count: number }
|
||||
>();
|
||||
for (const entry of drifted) {
|
||||
const key = `${entry.kind}|${entry.onDisk}|${entry.derivedFromKind}`;
|
||||
const existing = driftedByKind.get(key);
|
||||
if (existing) {
|
||||
existing.count += 1;
|
||||
} else {
|
||||
driftedByKind.set(key, { ...entry, count: 1 });
|
||||
}
|
||||
}
|
||||
// 前置事实:这 57 条落盘值确实是 unclassified(历史误判现场)。
|
||||
expect(uiAssets).toHaveLength(57);
|
||||
expect(uiAssets.every((asset) => asset.category === 'unclassified')).toBe(
|
||||
true,
|
||||
);
|
||||
// 读取侧已经把它重派生成 ui-interaction:读时自愈,不回写 manifest。
|
||||
expect(
|
||||
uiAssets.every(
|
||||
(asset) => gameCreationAppAssetCategory(asset) === 'ui-interaction',
|
||||
),
|
||||
).toBe(true);
|
||||
|
||||
// 真机现状:57 条 ui 的落盘 category 是 unclassified,而按新 kind 口径应是
|
||||
// ui-interaction。这条差异是「别名只救新登记、不救存量」的直接证据:读取侧
|
||||
// `gameCreationAppAssetCategory` 优先信任落盘 category,所以别名加完,
|
||||
// 这 57 条仍然留在「待归类」。
|
||||
expect(drifted).toHaveLength(57);
|
||||
expect(Object.fromEntries(driftedByKind)).toEqual({
|
||||
'ui|unclassified|ui-interaction': {
|
||||
kind: 'ui',
|
||||
onDisk: 'unclassified',
|
||||
derivedFromKind: 'ui-interaction',
|
||||
count: 57,
|
||||
},
|
||||
});
|
||||
// 直接钉住「读时重派生」这一行为本身:落盘 unclassified + kind 能派生明确分类
|
||||
// → 用派生值;这正是修复前那 57 条留在「待归类」的原因。
|
||||
expect(
|
||||
gameCreationAppAssetCategory({ kind: 'ui', category: 'unclassified' }),
|
||||
).toBe('ui-interaction');
|
||||
// 落盘值本身就是明确分类时仍然信任落盘值(用户手动设置的权威性)。
|
||||
expect(
|
||||
gameCreationAppAssetCategory({ kind: 'ui', category: 'scene' }),
|
||||
).toBe('scene');
|
||||
// 落盘值缺失时按 kind 派生(历史 manifest 兼容)。
|
||||
expect(
|
||||
gameCreationAppAssetCategory({ kind: 'ui', category: undefined }),
|
||||
).toBe('ui-interaction');
|
||||
});
|
||||
|
||||
test('别名落地后存量仍落待归类:58 不变(根治需要读时重派生或一次性回填)', () => {
|
||||
test('落盘 unclassified 且 kind 派生也是 unclassified 时不受影响', () => {
|
||||
// image / video / code 的 canonical 分类本来就是 unclassified,
|
||||
// 这类资产必须继续信任落盘值,不被重派生规则误伤。
|
||||
for (const kind of ['image', 'video', 'code', 'publication-material']) {
|
||||
expect(
|
||||
gameCreationAppAssetCategory({ kind, category: 'unclassified' }),
|
||||
).toBe('unclassified');
|
||||
}
|
||||
});
|
||||
|
||||
test('读时重派生后:57 条 ui 归位 UI 交互,待归类只剩 code 类 game-entry', () => {
|
||||
const resources = projectResourcesFromReadModels(realManifest(), [], []);
|
||||
const byCategory = countBy(resources, (resource) =>
|
||||
projectResourceCanvasCategory(resource),
|
||||
);
|
||||
|
||||
// 期望分布是加别名**之后**的实际结果,不是目标态。
|
||||
// 存量自愈后的真实分布:57 条 ui 归位到 UI 交互,只剩 game-entry(code)在待归类。
|
||||
expect(byCategory).toEqual({
|
||||
unclassified: 58,
|
||||
'ui-interaction': 2,
|
||||
unclassified: 1,
|
||||
'ui-interaction': 59,
|
||||
scene: 1,
|
||||
});
|
||||
// 若哪天真做了存量回填,这两个数字应当变成 unclassified:1 / ui-interaction:59,
|
||||
// 届时这条断言会失败并提醒更新口径。
|
||||
const uiResources = resources.filter(
|
||||
(resource) => resource.subtype === 'ui',
|
||||
);
|
||||
@@ -182,9 +178,18 @@ describe('真机 manifest 取证:占位卡片计数与栏目分布', () => {
|
||||
expect(
|
||||
uiResources.every(
|
||||
(resource) =>
|
||||
projectResourceCanvasCategory(resource) === 'unclassified',
|
||||
projectResourceCanvasCategory(resource) === 'ui-interaction',
|
||||
),
|
||||
).toBe(true);
|
||||
// 唯一留在待归类的就是 game-entry:code → unclassified 是分类表的设计口径。
|
||||
expect(
|
||||
resources
|
||||
.filter(
|
||||
(resource) =>
|
||||
projectResourceCanvasCategory(resource) === 'unclassified',
|
||||
)
|
||||
.map((resource) => resource.subtype),
|
||||
).toEqual(['game-entry']);
|
||||
});
|
||||
|
||||
test('按预览分支计数:只有 JSON 规格会落占位,52 张 PNG 走图片分支', () => {
|
||||
@@ -213,15 +218,15 @@ describe('真机 manifest 取证:占位卡片计数与栏目分布', () => {
|
||||
expect(countBy(placeholders, (e) => e.resource.subtype)).toEqual({ ui: 8 });
|
||||
});
|
||||
|
||||
test('按画布栏目计数:58 条落待归类,与真机截图一致', () => {
|
||||
test('按画布栏目计数:待归类从 58 降到 1,UI 交互升到 59', () => {
|
||||
const resources = projectResourcesFromReadModels(realManifest(), [], []);
|
||||
const byCategory = countBy(resources, (resource) =>
|
||||
projectResourceCanvasCategory(resource),
|
||||
);
|
||||
|
||||
expect(byCategory).toEqual({
|
||||
unclassified: 58,
|
||||
'ui-interaction': 2,
|
||||
unclassified: 1,
|
||||
'ui-interaction': 59,
|
||||
scene: 1,
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user