Files

75 lines
3.6 KiB
JavaScript

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 uses XML Text for labels and targetName overrides the output name', () => {
const layer = {
layerId: '10', layerName: 'Protol_Layer', spirits: [{
id: '1', name: 'Spirit1', text: '登录', type: '1', fontSize: '18', fontColor: '16777215',
alignHorz: '0', alignVert: '1', fontBold: '0', wordWrap: '0', lineSpace: '0', rowSpace: '0',
x: '100', y: '100', w: '100', h: '30', group: '37', indexOfLayer: '1',
}],
};
const json = emitPrefabJson(layer, { targetName: 'Layer10_Protocol' });
assert.equal(json[0]._name, 'Layer10_Protocol');
const textNode = json.find(o => o.__type__ === 'cc.Node' && o._name === 'Spirit1');
assert.ok(textNode);
const label = json.filter(o => o.__type__ === 'cc.Label')[0];
assert.equal(label._string, '登录');
assert.equal(label._fontSize, 18);
assert.equal(label._color.r, 255);
assert.equal(label._color.g, 255);
assert.equal(label._color.b, 255);
assert.equal(label._horizontalAlign, 0);
assert.equal(label._verticalAlign, 1);
assert.equal(label._isBold, false);
assert.equal(label._enableWrapText, false);
});
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}`);
});