import { test } from "node:test"; import assert from "node:assert/strict"; import { CADFiler, Database, Entity, PhysicalMaterialRecord, TextureTableRecord } from "webcad_ue4_api"; import { MaterialIn, MaterialOut } from "../src/common/MaterialSerializer"; import fixture from "./fixtures/material-v24-v16.json"; import { doubleSidedFixture, extendedTail, textureIdSlots, versionedFixture } from "./materialFixtures"; import { reactive } from "vue"; // 使用真实 API 的数据库、读写和克隆;仅关闭渲染更新,避免 Node 中加载图片/WebGL。 PhysicalMaterialRecord.prototype.Update = async () => {}; const roundTrip = (material: PhysicalMaterialRecord) => MaterialIn(JSON.parse(MaterialOut(material))); const recordData = (material: PhysicalMaterialRecord) => { const file = new CADFiler(); material.WriteFile(file); return file.Data; }; test("导入用户的数据库 v24 / 材质 v16,历史记录和公共字段保持对齐", () => { const input = structuredClone(fixture); const material = MaterialIn(input); assert.deepEqual(input, fixture); assert.equal(material.Name, "材质1"); assert.equal(material.type, "木纹"); assert.equal(material.UVType, 0); assert.equal(material.sharpen, 1); assert.equal(material.roughness, 0.2); assert.equal(material.map.Object.Id.Index, 100); assert.equal((material.map.Object as TextureTableRecord).imageUrl, fixture[44]); assert.equal(material.Db.hm.curIndex, -1); assert.equal(material.Db.hm.historyRecord.length, 0); assert.equal(material.Db.hm.Enable, false); assert.deepEqual(recordData(material), fixture.slice(54, 149)); }); test("编辑后导出并再次导入,保留 v16 参数和纹理编辑结果", () => { const material = MaterialIn(fixture); material.Name = "新版材质"; material.matalness = 0.42; material.roughness = 0.67; const texture = material.map.Object as TextureTableRecord; texture.repeatX = 3.5; texture.moveY = 0.25; const imported = roundTrip(material); assert.equal(imported.Name, "新版材质"); assert.equal(imported.matalness, 0.42); assert.equal(imported.roughness, 0.67); assert.equal((imported.map.Object as TextureTableRecord).repeatX, 3.5); assert.equal((imported.map.Object as TextureTableRecord).moveY, 0.25); assert.equal(recordData(imported)[6], 16); assert.deepEqual(recordData(imported).slice(-4), [false, false, 0, true]); }); test("旧 API 新建的 v13 材质仍可导入导出", () => { const db = new Database(false, false, true); const material = new PhysicalMaterialRecord(); material.Name = "旧版材质"; material.opacityMaximum = 0.8; material.opacityMinimum = 0.15; db.MaterialTable.Add(material); const imported = roundTrip(material); assert.equal(imported.Name, material.Name); assert.equal(imported.opacityMaximum, 0.8); assert.equal(imported.opacityMinimum, 0.15); assert.equal(recordData(imported)[6], 13); }); for (const version of [14, 15] as const) { test(`材质 v${version} 按原协议导出,不增加或丢失字段`, () => { const material = MaterialIn(versionedFixture(version)); const imported = roundTrip(material); assert.equal(recordData(imported)[6], version); assert.deepEqual(recordData(imported), recordData(material)); assert.equal(imported.Db.hm.curIndex, -1); }); } test("双面材质的全部扩展参数和仅被扩展字段引用的纹理随克隆保留", () => { const material = MaterialIn(doubleSidedFixture()); assert.deepEqual(recordData(material).slice(-48), extendedTail); // sceneStore 中的材质经过 Vue reactive,不能依赖原始对象作为 WeakMap 的键。 const output = MaterialOut(reactive(material) as PhysicalMaterialRecord); const serialized = JSON.parse(output); assert.equal(serialized.filter((value: unknown) => value === "TextureTableRecord").length, 6); assert.ok(!output.includes("CompatibleMaterialRecord")); const imported = MaterialIn(serialized); assert.equal(imported.Db.TextureTable.Symbols.size, 6); const tail = recordData(imported).slice(-48); const normalize = (values: any[], getUrl: (id: number) => string) => values.map((value, index) => textureIdSlots.includes(index) ? getUrl(value) : value); assert.deepEqual( normalize(tail, id => (imported.Db.GetObjectId(id).Object as TextureTableRecord).imageUrl), normalize(extendedTail, id => `texture-${id}.png`), ); assert.equal(imported.map.Object, imported.Db.GetObjectId(imported.map.Index).Object); }); test("可变长自定义属性不影响后续材质字段及历史记录", () => { const data: any[] = structuredClone(fixture); data.splice(133, 1, 2, "长度", "100", "品牌", "测试"); const imported = roundTrip(MaterialIn(data)); assert.deepEqual(imported.HardwareAttributeInfo.DataList.value.slice(0, 2), [["长度", "100"], ["品牌", "测试"]]); assert.deepEqual(recordData(imported).slice(-4), [false, false, 0, true]); assert.equal(imported.Db.hm.curIndex, -1); }); test("协议类名不替换用户字符串,多个纹理记录均使用稳定类名", () => { const material = MaterialIn(doubleSidedFixture()); material.Name = `${PhysicalMaterialRecord.name} / ${TextureTableRecord.name}`; const output = MaterialOut(material); const imported = MaterialIn(JSON.parse(output)); assert.equal(imported.Name, material.Name); assert.equal(imported.Db.TextureTable.Symbols.size, 6); }); test("导出成功或失败均恢复调用前的 API 读取状态", () => { const material = MaterialIn(fixture); const initial = Entity.__ReadFileIng__; try { Entity.__ReadFileIng__ = true; MaterialOut(material); assert.equal(Entity.__ReadFileIng__, true); Entity.__ReadFileIng__ = false; material.WriteFile = () => { throw new Error("模拟序列化失败"); }; assert.throws(() => MaterialOut(material), /模拟序列化失败/); assert.equal(Entity.__ReadFileIng__, false); } finally { Entity.__ReadFileIng__ = initial; } }); test("拒绝未知协议和空材质,并恢复 API 的读取状态", () => { const future = structuredClone(fixture); future[60] = 17; Entity.__ReadFileIng__ = false; assert.throws(() => MaterialIn(future), /不支持的材质版本: 17/); assert.equal(Entity.__ReadFileIng__, false); assert.throws(() => MaterialIn([25]), /不支持的材质数据库版本/); assert.throws(() => MaterialIn([]), /不支持的材质数据库版本/); const empty = new Database(false, false, true).FileWrite().Data; assert.throws(() => MaterialIn(empty), /没有材质记录/); assert.equal(MaterialIn(fixture).Name, "材质1"); });