@@ -5,6 +5,8 @@ import { describe, expect, test as it } from "vitest";
5
5
6
6
import { cbor } from "./cbor" ;
7
7
import { bytesToFloat16 } from "./cbor-decode" ;
8
+ import { tagSymbol } from "./cbor-types" ;
9
+ import { dateToTag } from "./parseCborBody" ;
8
10
9
11
// syntax is ESM but the test target is CJS.
10
12
const here = __dirname ;
@@ -179,6 +181,18 @@ describe("cbor", () => {
179
181
161 , 103 , 109 , 101 , 115 , 115 , 97 , 103 , 101 , 108 , 104 , 101 , 108 , 108 , 111 , 44 , 32 , 119 , 111 , 114 , 108 , 100 ,
180
182
] ) ,
181
183
} ,
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
+ } ,
182
196
{
183
197
name : "complex object" ,
184
198
data : {
@@ -202,7 +216,7 @@ describe("cbor", () => {
202
216
] ;
203
217
204
218
const toBytes = ( hex : string ) => {
205
- const bytes = [ ] ;
219
+ const bytes = [ ] as number [ ] ;
206
220
hex . replace ( / ../ g, ( substr : string ) : string => {
207
221
bytes . push ( parseInt ( substr , 16 ) ) ;
208
222
return substr ;
@@ -211,6 +225,19 @@ describe("cbor", () => {
211
225
} ;
212
226
213
227
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
+
214
241
for ( const { name, data, cbor : cbor_representation } of examples ) {
215
242
it ( `should encode for ${ name } ` , async ( ) => {
216
243
const serialized = cbor . serialize ( data ) ;
@@ -292,6 +319,7 @@ describe("cbor", () => {
292
319
return {
293
320
tag : id ,
294
321
value : translateTestData ( tagValue ) ,
322
+ [ tagSymbol ] : true ,
295
323
} ;
296
324
default :
297
325
throw new Error ( `Unrecognized test scenario <expect> type ${ type } .` ) ;
0 commit comments