@@ -5,6 +5,8 @@ import { describe, expect, test as it } from "vitest";
55
66import { cbor } from "./cbor" ;
77import { bytesToFloat16 } from "./cbor-decode" ;
8+ import { tagSymbol } from "./cbor-types" ;
9+ import { dateToTag } from "./parseCborBody" ;
810
911// syntax is ESM but the test target is CJS.
1012const here = __dirname ;
@@ -179,6 +181,18 @@ describe("cbor", () => {
179181 161 , 103 , 109 , 101 , 115 , 115 , 97 , 103 , 101 , 108 , 104 , 101 , 108 , 108 , 111 , 44 , 32 , 119 , 111 , 114 , 108 , 100 ,
180182 ] ) ,
181183 } ,
184+ {
185+ name : "date=0" ,
186+ data : dateToTag ( new Date ( 0 ) ) ,
187+ // major tag (6 or 110), minor 1 (timestamp)
188+ cbor : allocByteArray ( [ 0b11000001 , 0 ] ) ,
189+ } ,
190+ {
191+ name : "date=turn of the millenium" ,
192+ data : dateToTag ( new Date ( 946684799999 ) ) ,
193+ // major tag (6 or 110), minor 1 (timestamp)
194+ cbor : allocByteArray ( [ 0b11000001 , 251 , 65 , 204 , 54 , 161 , 191 , 255 , 223 , 59 ] ) ,
195+ } ,
182196 {
183197 name : "complex object" ,
184198 data : {
@@ -202,7 +216,7 @@ describe("cbor", () => {
202216 ] ;
203217
204218 const toBytes = ( hex : string ) => {
205- const bytes = [ ] ;
219+ const bytes = [ ] as number [ ] ;
206220 hex . replace ( / ../ g, ( substr : string ) : string => {
207221 bytes . push ( parseInt ( substr , 16 ) ) ;
208222 return substr ;
@@ -211,6 +225,19 @@ describe("cbor", () => {
211225 } ;
212226
213227 describe ( "locally curated scenarios" , ( ) => {
228+ it ( "should throw an error if serializing a tag with missing properties" , ( ) => {
229+ expect ( ( ) =>
230+ cbor . serialize ( {
231+ myTag : {
232+ [ tagSymbol ] : true ,
233+ tag : 1 ,
234+ // value: undefined
235+ } ,
236+ } )
237+ ) . toThrowError ( "tag encountered with missing fields, need 'tag' and 'value', found: {\"tag\":1}" ) ;
238+ cbor . resizeEncodingBuffer ( 0 ) ;
239+ } ) ;
240+
214241 for ( const { name, data, cbor : cbor_representation } of examples ) {
215242 it ( `should encode for ${ name } ` , async ( ) => {
216243 const serialized = cbor . serialize ( data ) ;
@@ -292,6 +319,7 @@ describe("cbor", () => {
292319 return {
293320 tag : id ,
294321 value : translateTestData ( tagValue ) ,
322+ [ tagSymbol ] : true ,
295323 } ;
296324 default :
297325 throw new Error ( `Unrecognized test scenario <expect> type ${ type } .` ) ;
0 commit comments