feat(scripts): emitPrefabJson - 按 Login_Layer.prefab schema 输出 prefab JSON

This commit is contained in:
2026-09-02 08:28:45 +08:00
parent 2e2a6b0025
commit 9742509d32
2 changed files with 495 additions and 0 deletions
@@ -0,0 +1,51 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { emitPrefabJson } from '../../scripts/legacy-layer-to-cocos-prefab.mjs';
test('emitPrefabJson 输出是 JSON 数组', () => {
const layer = { layerId: '2', layerName: 'Login_Layer', spirits: [] };
const json = emitPrefabJson(layer);
assert.ok(Array.isArray(json), '输出必须是数组');
assert.ok(json.length >= 1, '至少含 cc.Prefab 头');
assert.equal(json[0].__type__, 'cc.Prefab');
assert.equal(json[0]._name, 'Login_Layer');
});
test('emitPrefabJson 含 CompPrefabInfo 引用块', () => {
const layer = { layerId: '2', layerName: 'Login_Layer', spirits: [] };
const json = emitPrefabJson(layer);
const compInfo = json.filter(o => o.__type__ === 'cc.CompPrefabInfo');
assert.ok(compInfo.length > 0, '每个组件应有 CompPrefabInfo 引用块');
});
test('emitPrefabJson 含 PrefabInfo 块', () => {
const layer = { layerId: '2', layerName: 'Login_Layer', spirits: [] };
const json = emitPrefabJson(layer);
const prefabInfo = json.find(o => o.__type__ === 'cc.PrefabInfo');
assert.ok(prefabInfo, '必须有 PrefabInfo 块');
assert.equal(prefabInfo.root.__id__, 1); // 根节点 id=1
assert.equal(prefabInfo.asset.__id__, 0);
});
test('emitPrefabJson 渠道logo _lpos 误差 < 0.001', () => {
// 注: brief 原版用 spirit name='Spirit405' 但断言搜 /渠道/; 实际 fixture 里 id=405 的 spirit
// 坐标 (734, 143, 59, 148) 严格对应 Login_Layer '渠道logo' 节点; 这里 name 用 '渠道logo'
// 与断言一致, 否则 node 找不到。
const layer = {
layerId: '2', layerName: 'Login_Layer',
spirits: [
{ id: '2', name: '微信登录背景', type: '0', imgResID: '20',
x: '0', y: '0', w: '1280', h: '720', group: '2', indexOfLayer: '1' },
{ id: '405', name: '渠道logo', type: '0', imgResID: '5',
x: '734', y: '143', w: '59', h: '148', group: '2', indexOfLayer: '2' },
{ id: '285', name: '版本显示', type: '1', imgResID: '',
x: '16', y: '9', w: '96', h: '31', group: '2', indexOfLayer: '3' },
],
};
const json = emitPrefabJson(layer);
// Find 渠道logo node
const genLogo = json.find(o => o.__type__ === 'cc.Node' && o._name && o._name.includes('渠道'));
assert.ok(genLogo, '应有渠道logo node');
assert.ok(Math.abs(genLogo._lpos.x - 123.5) < 0.001, `x expected 123.5 got ${genLogo._lpos.x}`);
assert.ok(Math.abs(genLogo._lpos.y - 143) < 0.001, `y expected 143 got ${genLogo._lpos.y}`);
});