t-stage.test.mjs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* tests/t-stage.test.mjs —— 阶段 F:八款 STAGE 建模回归(Node,无需浏览器)
  2. * 用极简 BABYLON 替身只跑几何路径:验证每款都能建模、零件数 ≥4、每个零件带 ex、
  3. * 顶点无 NaN、法线数量一致、索引不越界,并且参数变化会改变几何。
  4. * 运行:node --test site/tests/t-stage.test.mjs
  5. */
  6. import { test } from 'node:test';
  7. import assert from 'node:assert/strict';
  8. /* ---------- Babylon 替身(只实现建模路径) ---------- */
  9. export class V3 {
  10. constructor(x = 0, y = 0, z = 0) { this.x = x; this.y = y; this.z = z; }
  11. clone() { return new this.constructor(this.x, this.y, this.z); }
  12. add(o) { return new this.constructor(this.x + o.x, this.y + o.y, this.z + o.z); }
  13. subtract(o) { return new this.constructor(this.x - o.x, this.y - o.y, this.z - o.z); }
  14. scale(s) { return new this.constructor(this.x * s, this.y * s, this.z * s); }
  15. length() { return Math.hypot(this.x, this.y, this.z); }
  16. normalize() { const l = this.length() || 1; return new this.constructor(this.x / l, this.y / l, this.z / l); }
  17. static Cross(a, b) { return new V3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); }
  18. static TransformCoordinates(v) { return new V3(v.x, v.y, v.z); }
  19. static Project() { return new V3(0, 0, 0.5); }
  20. }
  21. class Node {
  22. constructor(n) { this.name = n; this.position = new V3(); this.rotation = new V3(); this.scaling = new V3(1, 1, 1); this.children = []; this.metadata = null; }
  23. getChildMeshes() { return this.children.filter((c) => c.vertices !== undefined); }
  24. }
  25. class Mesh extends Node {
  26. constructor(n) { super(n); this.vertices = 0; }
  27. set material(m) { this._mat = m; }
  28. get material() { return this._mat || null; }
  29. set parent(p) { this._p = p; if (p) p.children.push(this); }
  30. get parent() { return this._p; }
  31. getTotalVertices() { return this.vertices; }
  32. computeWorldMatrix() {}
  33. }
  34. const stats = { tris: 0, verts: 0 };
  35. class VertexData {
  36. positions = null; indices = null; normals = null;
  37. applyToMesh(m) {
  38. assert.ok(this.positions && this.indices, 'vertexdata 缺位置或索引');
  39. if (m.name === 'body' && !globalThis.__d) { globalThis.__d = 1; console.log('DBG5', typeof this.positions[0], this.positions[0].constructor.name, this.positions[1], typeof this.positions[1], Object.keys(this.positions[0])); }
  40. assert.equal(this.normals.length, this.positions.length, m.name + ' 法线数量与顶点不一致');
  41. for (let k = 0; k < this.positions.length; k += 3) assert.ok(Number.isFinite(this.positions[k]) && Number.isFinite(this.positions[k + 1]) && Number.isFinite(this.positions[k + 2]), m.name + ' 顶点含 NaN');
  42. for (let k = 0; k < this.normals.length; k += 3) assert.ok(Number.isFinite(this.normals[k]) && Number.isFinite(this.normals[k + 1]) && Number.isFinite(this.normals[k + 2]), m.name + ' 法线含 NaN');
  43. assert.equal(this.positions.length % 3, 0); assert.equal(this.normals.length % 3, 0);
  44. for (const i of this.indices) assert.ok(i >= 0 && i < this.positions.length, m.name + ' 索引越界');
  45. stats.tris += this.indices.length / 3; stats.verts += this.positions.length;
  46. m.vertices = this.positions.length;
  47. }
  48. }
  49. class Light { constructor() { this.groundColor = null; this.intensity = 1; } }
  50. class ArcRotateCamera { constructor() { this.target = new V3(); this.viewport = {}; } attachControl() {} }
  51. class Scene { constructor() { this.onPointerObservable = { add() {} }; } render() {} }
  52. class Engine { runRenderLoop() {} setHardwareScalingLevel() {} resize() {} getRenderWidth() { return 800; } getRenderHeight() { return 600; } getDeltaTime() { return 16; } }
  53. class DynamicTexture {
  54. getContext() { return { createImageData: (w, h) => ({ data: new Uint8ClampedArray(w * h * 4) }), createRadialGradient: () => ({ addColorStop() {} }), putImageData() {}, fillRect() {}, fillStyle: '' }; }
  55. update() {}
  56. }
  57. const MeshBuilder = {
  58. CreateCylinder(n) { const m = new Mesh(n); m.vertices = 64; return m; },
  59. CreateTorus(n) { const m = new Mesh(n); m.vertices = 64; return m; },
  60. CreateBox(n) { const m = new Mesh(n); m.vertices = 24; return m; },
  61. CreateLines(n, o) { const m = new Mesh(n); m.vertices = (o.points || []).length; m.color = null; return m; },
  62. CreateGround(n) { const m = new Mesh(n); m.vertices = 4; return m; }
  63. };
  64. globalThis.BABYLON = {
  65. Vector3: V3, Mesh, TransformNode: Node, VertexData, MeshBuilder, Scene, Engine, DynamicTexture,
  66. Light, HemisphericLight: Light, DirectionalLight: Light, ArcRotateCamera,
  67. Color3: { FromHexString: () => ({ r: 0, g: 0, b: 0 }) },
  68. PBRMaterial: class { constructor(n) { this.name = n; this.clearCoat = { isEnabled: false }; } },
  69. StandardMaterial: class {}, Material: { MATERIAL_ALPHABLEND: 2 }, PointerEventTypes: { POINTERPICK: 1, POINTERDOUBLETAP: 2 }
  70. };
  71. const { STAGE, PROJECTS, MATERIALS } = await import('../core/stage/STAGE.js');
  72. const BUILDERS = STAGE.getBuilders();
  73. const IDS = ['silicone', 'recorder', 'round', 'ai-pin', 'eink-tag', 'pocket-ai', 'bone-headset', 'smart-ring'];
  74. /** 与 site/data/products 的零件 id 对齐的新产品映射 */
  75. const DATA_MAP = {
  76. 'ai-pin': ['mid-frame', 'back-cover', 'screen', 'pin'],
  77. 'eink-tag': ['front-shell', 'screen', 'kick-stand', 'back-cover'],
  78. 'pocket-ai': ['top-lid', 'keycap-set', 'hinge', 'bottom-cover'],
  79. 'bone-headset': ['neckband', 'vibrator-l', 'titanium-core', 'ctrl-module'],
  80. 'smart-ring': ['ti-outer', 'resin-inner', 'dock-lid', 'sensor-window']
  81. };
  82. function build(pid, params) {
  83. const out = STAGE._build(pid, params);
  84. assert.ok(out && out.root && Array.isArray(out.parts), pid + ' 建模返回结构不完整');
  85. return out;
  86. }
  87. test('八款产品全部注册且有专属建模函数', () => {
  88. for (const id of IDS) {
  89. assert.ok(PROJECTS[id], '缺少项目注册: ' + id);
  90. assert.equal(typeof BUILDERS[id], 'function', id + ' 没有专属建模函数');
  91. }
  92. });
  93. for (const id of IDS) {
  94. test(`建模 ${id}:零件 ≥4、每件带 ex、无非有限顶点`, () => {
  95. const out = build(id);
  96. const ids = new Set(out.parts.map((p) => p.id));
  97. assert.ok(out.parts.length >= 4, id + ' 零件数不足');
  98. assert.ok(ids.size >= 4, id + ' 去重零件 id 不足 4 个');
  99. for (const p of out.parts) {
  100. assert.ok(p.ex && Number.isFinite(p.ex.x) && Number.isFinite(p.ex.y) && Number.isFinite(p.ex.z), id + '/' + p.id + ' 缺爆炸向量 ex');
  101. assert.ok(p.node && p.node.metadata && p.node.metadata.role, id + '/' + p.id + ' 缺 metadata.role');
  102. }
  103. for (const k of ['x', 'y', 'z']) assert.ok(out.size[k] > 0, id + ' 包络尺寸非法');
  104. });
  105. }
  106. test('新产品零件 id 与产品数据一致', () => {
  107. for (const [pid, want] of Object.entries(DATA_MAP)) {
  108. const ids = new Set(build(pid).parts.map((p) => p.id));
  109. for (const w of want) assert.ok(ids.has(w), `${pid} 缺少数据契约中的零件 id: ${w}`);
  110. }
  111. });
  112. test('参数变化会改变几何(键列数 / 戒指内径 / 支架角)', () => {
  113. const a = build('pocket-ai');
  114. const b = build('pocket-ai', { cols: 8 });
  115. assert.notEqual(a.parts.length, b.parts.length, '键列数变化应改变键帽数量');
  116. const r1 = build('smart-ring');
  117. const r2 = build('smart-ring', { ringD: 22, dockD: 46 });
  118. assert.ok(r2.size.x > r1.size.x, '戒指内径变大,包络应变化');
  119. });
  120. test('材质表:每款至少 3 种可选 PBR 材质且都在 MATERIALS 中', () => {
  121. for (const id of Object.keys(PROJECTS)) {
  122. const ms = PROJECTS[id].materials || [];
  123. assert.ok(ms.length >= 3, id + ' 可选材质不足');
  124. for (const m of ms) assert.ok(MATERIALS[m] && MATERIALS[m].albedo, id + ' 引用了不存在的材质: ' + m);
  125. }
  126. });