Warning, /firebird/packages/firebird-core/src/model/box-hit.piece.spec.ts is written in an unsupported language. File is not indexed.
0001 // box-hit.piece.spec.ts
0002
0003 import { BoxHitPiece, BoxHitPieceFactory } from './box-hit.piece';
0004
0005 const DEX_PIECE = {
0006 name: 'BarrelHits',
0007 type: 'BoxHit',
0008 version: '1.0',
0009 origin: { by: 'spec' },
0010 count: 2,
0011 columns: {
0012 pos: [1, 2, 3, 4, 5, 6],
0013 dim: [0.1, 0.1, 0.1, 0.2, 0.2, 0.2],
0014 time: [4, 5],
0015 timeError: [1, 1],
0016 edep: [0.001, 0.002],
0017 edepError: [0.0001, 0.0002],
0018 },
0019 };
0020
0021 describe('BoxHitPieceFactory', () => {
0022 it('bulk-copies columns into typed arrays, id == index', () => {
0023 const piece = new BoxHitPieceFactory().fromDexObject(DEX_PIECE);
0024
0025 expect(piece.name).toBe('BarrelHits');
0026 expect(piece.type).toBe(BoxHitPiece.type);
0027 expect(piece.count).toBe(2);
0028 expect(piece.pos).toBeInstanceOf(Float32Array);
0029 // hit 1 is at pos[3..5]
0030 expect(Array.from(piece.pos.slice(3, 6))).toEqual([4, 5, 6]);
0031 expect(piece.time![1]).toBe(5);
0032 expect(piece.edep![0]).toBeCloseTo(0.001);
0033 });
0034
0035 it('leaves omitted optional columns null (sim writers omit error columns)', () => {
0036 const simPiece = new BoxHitPieceFactory().fromDexObject({
0037 name: 'SimHits', type: 'BoxHit', version: '1.0', count: 1,
0038 columns: { pos: [1, 2, 3], dim: [2, 2, 2], time: [7], edep: [0.001] },
0039 });
0040 expect(simPiece.timeError).toBeNull();
0041 expect(simPiece.edepError).toBeNull();
0042 expect(simPiece.time![0]).toBe(7);
0043 });
0044
0045 it('throws loudly on malformed columns', () => {
0046 const bad = { ...DEX_PIECE, columns: { ...DEX_PIECE.columns, pos: [1, 2, 3] } };
0047 expect(() => new BoxHitPieceFactory().fromDexObject(bad)).toThrow(/pos/);
0048 });
0049 });
0050
0051 describe('BoxHitPiece', () => {
0052 it('computes timeRange from the time column', () => {
0053 const piece = new BoxHitPieceFactory().fromDexObject(DEX_PIECE);
0054 expect(piece.timeRange).toEqual([4, 5]);
0055 });
0056
0057 it('has null timeRange without a time column', () => {
0058 const piece = new BoxHitPieceFactory().fromDexObject({
0059 name: 'X', type: 'BoxHit', version: '1.0', count: 1,
0060 columns: { pos: [0, 0, 0], dim: [1, 1, 1] },
0061 });
0062 expect(piece.timeRange).toBeNull();
0063 });
0064
0065 it('round-trips through toDexObject', () => {
0066 const factory = new BoxHitPieceFactory();
0067 const piece = factory.fromDexObject(DEX_PIECE);
0068 const dexObject = piece.toDexObject();
0069
0070 expect(dexObject.type).toBe('BoxHit');
0071 expect(dexObject.version).toBe('1.0');
0072 expect(dexObject.count).toBe(2);
0073 expect(dexObject.columns.pos).toEqual([1, 2, 3, 4, 5, 6]);
0074 expect(dexObject.columns.time).toEqual([4, 5]);
0075
0076 const reparsed = factory.fromDexObject(dexObject);
0077 expect(Array.from(reparsed.pos)).toEqual(Array.from(piece.pos));
0078 expect(Array.from(reparsed.time!)).toEqual(Array.from(piece.time!));
0079 });
0080 });