Back to home page

EIC code displayed by LXR

 
 

    


Warning, /firebird/firebird-ng/src/app/data-pipelines/three-geometry.processor.spec.ts is written in an unsupported language. File is not indexed.

0001 import * as THREE from 'three';
0002 import { ruleSetsFromObj, DetectorThreeRuleSet } from './three-geometry.processor';
0003 import { EditThreeNodeRule } from '../utils/three-geometry-editor';
0004 
0005 describe('ruleSetsFromObj with materialJson', () => {
0006 
0007     it('should parse "materialJson" into a real THREE.Material', () => {
0008         const raw = [
0009             {
0010                 names: ['MyDetector'],
0011                 rules: [
0012                     {
0013                         color: '0xff00ff',
0014                         materialJson: {
0015                             // The minimal JSON structure for the built-in MaterialLoader
0016                             type: 'MeshStandardMaterial',
0017                             color: 16711680, // 0xff0000
0018                             roughness: 0.5
0019                         }
0020                     },
0021                     {
0022                         // a second rule with no materialJson => no parse attempt
0023                         color: '0x00ffff'
0024                     }
0025                 ]
0026             }
0027         ];
0028 
0029         const sets = ruleSetsFromObj(raw);
0030         expect(sets.length).toBe(1);
0031 
0032         const set = sets[0];
0033         expect(set.names).toEqual(['MyDetector']);
0034         expect(set.rules.length).toBe(2);
0035 
0036         // First rule
0037         const rule1: EditThreeNodeRule = set.rules[0];
0038         // color: '0xff00ff' => number
0039         expect(rule1.color).toBe(0xff00ff);
0040 
0041         // Check that a THREE.Material was created from materialJson
0042         expect(rule1.material).toBeDefined();
0043         expect(rule1.material).toBeInstanceOf(THREE.MeshStandardMaterial);
0044         const mat1 = rule1.material as THREE.MeshStandardMaterial;
0045         // color: 16711680 => 0xff0000
0046         expect(mat1.color.getHex()).toBe(0xff0000);
0047         expect(mat1.roughness).toBe(0.5);
0048 
0049         // Second rule => no materialJson, so no .material created
0050         const rule2: EditThreeNodeRule = set.rules[1];
0051         expect(rule2.color).toBe(0x00ffff);
0052         expect(rule2.material).toBeUndefined();
0053     });
0054 
0055     it('should handle parse errors gracefully', () => {
0056         const raw = [
0057             {
0058                 rules: [
0059                     {
0060                         materialJson: {
0061                             type: 'UnknownMaterialType', // invalid
0062                             color: 0xffffff
0063                         }
0064                     }
0065                 ]
0066             }
0067         ];
0068 
0069         vi.spyOn(console, 'error');
0070         const sets = ruleSetsFromObj(raw);
0071         expect(sets.length).toBe(1);
0072         expect(sets[0].rules[0].material).toBeUndefined(); // parse failed
0073 
0074         expect(console.error).toHaveBeenCalled(); // logs the parse error
0075     });
0076 
0077     it('should return an empty list if top-level is not array', () => {
0078         const result = ruleSetsFromObj({ nonsense: true });
0079         expect(result).toEqual([]);
0080     });
0081 
0082     it('should handle missing "rules" arrays by returning an empty "rules"', () => {
0083         const raw = [
0084             {
0085                 names: ['NoRulesHere']
0086             }
0087         ];
0088         const sets = ruleSetsFromObj(raw);
0089         expect(sets[0].rules.length).toBe(0);
0090     });
0091 });