@@ -79,6 +79,35 @@ test('NbsDecoder constructor throws for invalid arguments', () => {
7979 ) ;
8080} ) ;
8181
82+ test ( 'NbsDecoder.getTypeIndex() returns the correct index for the given type subtype' , ( ) => {
83+ const pingIndices = decoder . getTypeIndex ( { type : pingType , subtype : 0 } ) ;
84+ assert . equal ( pingIndices . length , 300 ) ;
85+
86+ const pongIndices = decoder . getTypeIndex ( { type : pongType , subtype : 0 } ) ;
87+ assert . equal ( pongIndices . length , 300 ) ;
88+
89+ const pang100Indices = decoder . getTypeIndex ( { type : pangType , subtype : 100 } ) ;
90+ assert . equal ( pang100Indices . length , 150 ) ;
91+
92+ const pang200Indices = decoder . getTypeIndex ( { type : pangType , subtype : 200 } ) ;
93+ assert . equal ( pang200Indices . length , 150 ) ;
94+ } ) ;
95+
96+ test ( 'NbsDecoder.getTypeIndex() returns an empty array for types not in the decoder' , ( ) => {
97+ const fakePacketIndices = decoder . getTypeIndex ( { type : 'fakeIndex' , subtype : 0 } ) ;
98+ assert . equal ( fakePacketIndices . length , 0 ) ;
99+ } ) ;
100+
101+ test ( 'NbsDecoder.getTypeIndex() returns the correct index with the correct packet timestamps' , ( ) => {
102+ const pingIndices = decoder . getTypeIndex ( { type : pingType , subtype : 0 } ) ;
103+
104+ assert . equal ( pingIndices [ 0 ] , { seconds : 1000 , nanos : 0 } ) ;
105+ assert . equal ( pingIndices [ 1 ] , { seconds : 1003 , nanos : 0 } ) ;
106+
107+ const lastIndex = pingIndices . length - 1 ;
108+ assert . equal ( pingIndices [ lastIndex ] , { seconds : 1897 , nanos : 0 } ) ;
109+ } ) ;
110+
82111test ( 'NbsDecoder.getAvailableTypes() returns a list of all the types available in the nbs files' , ( ) => {
83112 const types = decoder . getAvailableTypes ( ) ;
84113
@@ -438,6 +467,78 @@ test('NbsDecoder.getPackets() returns the last packet of each type when given a
438467 } ) ;
439468} ) ;
440469
470+ test ( 'NbsDecoder.getPacketByIndex() throws for invalid arguments' , ( ) => {
471+ assert . throws (
472+ ( ) => {
473+ decoder . getPacketByIndex ( ) ;
474+ } ,
475+ / i n v a l i d t y p e f o r a r g u m e n t ` i n d e x ` : e x p e c t e d i n t e g e r / ,
476+ 'NbsDecoder.getPacketByIndex() throws for missing `index` argument'
477+ ) ;
478+
479+ assert . throws (
480+ ( ) => {
481+ decoder . getPacketByIndex ( true ) ;
482+ } ,
483+ / i n v a l i d t y p e f o r a r g u m e n t ` i n d e x ` : e x p e c t e d i n t e g e r / ,
484+ 'NbsDecoder.getPacketByIndex() throws for incorrect `index` argument type'
485+ ) ;
486+
487+ assert . throws (
488+ ( ) => {
489+ decoder . getPacketByIndex ( 0 ) ;
490+ } ,
491+ / i n v a l i d t y p e f o r a r g u m e n t ` t y p e S u b t y p e ` : e x p e c t e d o b j e c t / ,
492+ 'NbsDecoder.getPacketByIndex() throws for missing `type` argument'
493+ ) ;
494+
495+ assert . throws (
496+ ( ) => {
497+ decoder . getPacketByIndex ( 0 , { } ) ;
498+ } ,
499+ / i n v a l i d t y p e f o r a r g u m e n t ` t y p e S u b t y p e ` : e x p e c t e d o b j e c t w i t h ` t y p e ` a n d ` s u b t y p e ` k e y s / ,
500+ 'NbsDecoder.getPacketByIndex() throws for invalid `type` argument'
501+ ) ;
502+ } ) ;
503+
504+ test ( 'NbsDecoder.getPacketByIndex() returns the correct packet for a valid index' , ( ) => {
505+ const packet0 = decoder . getPacketByIndex ( 0 , { type : pingType , subtype : 0 } ) ;
506+ const packet1 = decoder . getPacketByIndex ( 1 , { type : pingType , subtype : 0 } ) ;
507+ const packetLast = decoder . getPacketByIndex ( 299 , { type : pingType , subtype : 0 } ) ;
508+
509+ assert . equal ( packet0 , {
510+ timestamp : { seconds : 1000 , nanos : 0 } ,
511+ type : pingType ,
512+ subtype : 0 ,
513+ payload : Buffer . from ( 'ping.0' , 'utf8' ) ,
514+ } ) ;
515+
516+ assert . equal ( packet1 , {
517+ timestamp : { seconds : 1003 , nanos : 0 } ,
518+ type : pingType ,
519+ subtype : 0 ,
520+ payload : Buffer . from ( 'ping.1' , 'utf8' ) ,
521+ } ) ;
522+
523+ assert . equal ( packetLast , {
524+ timestamp : { seconds : 1897 , nanos : 0 } ,
525+ type : pingType ,
526+ subtype : 0 ,
527+ payload : Buffer . from ( 'ping.699' , 'utf8' ) ,
528+ } ) ;
529+ } ) ;
530+
531+ test ( 'NbsDecoder.getPacketByIndex() returns undefined for indices outside the valid range' , ( ) => {
532+ // Negative indices
533+ assert . equal ( decoder . getPacketByIndex ( - 1 , { type : pingType , subtype : 0 } ) , undefined ) ;
534+
535+ // Indices one above the max
536+ assert . equal ( decoder . getPacketByIndex ( 300 , { type : pingType , subtype : 0 } ) , undefined ) ;
537+ assert . equal ( decoder . getPacketByIndex ( 300 , { type : pongType , subtype : 0 } ) , undefined ) ;
538+ assert . equal ( decoder . getPacketByIndex ( 150 , { type : pangType , subtype : 100 } ) , undefined ) ;
539+ assert . equal ( decoder . getPacketByIndex ( 150 , { type : pangType , subtype : 200 } ) , undefined ) ;
540+ } ) ;
541+
441542const multiTypeNextTimestampArray = [
442543 { type : pongType , subtype : 0 } ,
443544 { type : pingType , subtype : 0 } ,
0 commit comments