Warning, /firebird/packages/firebird-core/src/model/event-piece.spec.ts is written in an unsupported language. File is not indexed.
0001 // event-piece.spec.ts
0002
0003 import {
0004 EventPiece,
0005 EventPieceFactory,
0006 registerEventPieceFactory,
0007 getEventPieceFactory,
0008 _resetEventPieceRegistry,
0009 readPieceCount,
0010 readNumberColumn,
0011 readOptionalNumberColumn,
0012 } from './event-piece';
0013
0014 describe('EventPiece', () => {
0015 it('should be defined as an abstract class', () => {
0016 expect(EventPiece).toBeDefined();
0017 expect(typeof EventPiece).toBe('function');
0018 });
0019 });
0020
0021 describe('Piece Registry', () => {
0022 class TestPieceFactory implements EventPieceFactory {
0023 type: string = 'TestType';
0024
0025 fromDexObject(obj: any): EventPiece {
0026 return new TestEventPiece(obj['name'], obj['origin']);
0027 }
0028 }
0029
0030 class TestEventPiece extends EventPiece {
0031 override get timeRange(): [number, number] | null {
0032 return [0, 100];
0033 }
0034 constructor(name: string, origin?: string) {
0035 super(name, 'TestType', origin);
0036 }
0037
0038 toDexObject(): any {
0039 return {
0040 name: this.name,
0041 type: this.type,
0042 origin: this.origin,
0043 };
0044 }
0045 }
0046
0047 beforeEach(() => {
0048 _resetEventPieceRegistry();
0049 });
0050
0051 it('should register and retrieve piece factories correctly', () => {
0052 const factory = new TestPieceFactory();
0053 registerEventPieceFactory(factory);
0054 expect(getEventPieceFactory('TestType')).toBe(factory);
0055 });
0056
0057 it('should return undefined for unregistered piece types', () => {
0058 expect(getEventPieceFactory('UnknownType')).toBeUndefined();
0059 });
0060
0061 it('should overwrite existing factory when registering a factory with the same type', () => {
0062 const factory1 = new TestPieceFactory();
0063 const factory2 = new TestPieceFactory();
0064 registerEventPieceFactory(factory1);
0065 registerEventPieceFactory(factory2);
0066 expect(getEventPieceFactory('TestType')).toBe(factory2);
0067 });
0068
0069 it('should use the correct factory to create piece instances', () => {
0070 registerEventPieceFactory(new TestPieceFactory());
0071
0072 const dexObject = {
0073 name: 'TestEventPiece',
0074 type: 'TestType',
0075 origin: 'TestOrigin',
0076 };
0077
0078 const piece = getEventPieceFactory('TestType')!.fromDexObject(dexObject);
0079 expect(piece).toBeInstanceOf(EventPiece);
0080 expect(piece.name).toBe('TestEventPiece');
0081 expect(piece.type).toBe('TestType');
0082 expect(piece.origin).toBe('TestOrigin');
0083 });
0084 });
0085
0086 describe('column reading helpers', () => {
0087 const piece = {
0088 name: 'Hits',
0089 count: 2,
0090 columns: {
0091 pos: [1, 2, 3, 4, 5, 6],
0092 time: [10, 20],
0093 },
0094 };
0095
0096 it('reads counts and columns as typed arrays', () => {
0097 expect(readPieceCount(piece)).toBe(2);
0098 const pos = readNumberColumn(piece, 'pos', 2, 3);
0099 expect(pos).toBeInstanceOf(Float32Array);
0100 expect(Array.from(pos)).toEqual([1, 2, 3, 4, 5, 6]);
0101 expect(Array.from(readNumberColumn(piece, 'time', 2))).toEqual([10, 20]);
0102 });
0103
0104 it('returns null for omitted optional columns', () => {
0105 expect(readOptionalNumberColumn(piece, 'edep', 2)).toBeNull();
0106 });
0107
0108 it('throws loudly on bad counts', () => {
0109 expect(() => readPieceCount({ name: 'X', count: -1 })).toThrow(/count/);
0110 expect(() => readPieceCount({ name: 'X', count: 1.5 })).toThrow(/count/);
0111 expect(() => readPieceCount({ name: 'X' })).toThrow(/count/);
0112 });
0113
0114 it('throws loudly on length mismatches (id must equal index)', () => {
0115 expect(() => readNumberColumn(piece, 'pos', 3, 3)).toThrow(/pos/);
0116 expect(() => readNumberColumn(piece, 'time', 3)).toThrow(/time/);
0117 expect(() => readNumberColumn(piece, 'missing', 2)).toThrow(/missing/);
0118 });
0119 });