11import { NormalizedSchema } from "@smithy/core/schema" ;
2- import type { StaticSimpleSchema , StaticStructureSchema , StringSchema } from "@smithy/types" ;
2+ import type { StaticSimpleSchema , StaticStructureSchema , StringSchema , TimestampDefaultSchema } from "@smithy/types" ;
33import { describe , expect , it } from "vitest" ;
44
55import { cbor } from "./cbor" ;
66import { CborCodec , CborShapeSerializer } from "./CborCodec" ;
7+ import { tagSymbol } from "./cbor-types" ;
78
89describe ( CborShapeSerializer . name , ( ) => {
910 const codec = new CborCodec ( ) ;
@@ -22,6 +23,16 @@ describe(CborShapeSerializer.name, () => {
2223 ] ;
2324
2425 const serializer = codec . createSerializer ( ) ;
26+ const deserializer = codec . createDeserializer ( ) ;
27+
28+ const dateSchema = [
29+ 3 ,
30+ "ns" ,
31+ "DateContainer" ,
32+ 0 ,
33+ [ "timestamp" ] ,
34+ [ 4 satisfies TimestampDefaultSchema ] ,
35+ ] satisfies StaticStructureSchema ;
2536
2637 describe ( "serialization" , ( ) => {
2738 it ( "should generate an idempotency token when the input for such a member is undefined" , ( ) => {
@@ -69,12 +80,25 @@ describe(CborShapeSerializer.name, () => {
6980 }
7081 }
7182 } ) ;
83+
84+ it ( "should serialize Dates to tags if the schema is a timestamp" , ( ) => {
85+ serializer . write ( dateSchema , { timestamp : new Date ( 1 ) } ) ;
86+ const serialization = serializer . flush ( ) ;
87+
88+ const parsedWithoutSchema = cbor . deserialize ( serialization ) ;
89+ expect ( parsedWithoutSchema ) . toEqual ( {
90+ timestamp : {
91+ tag : 1 ,
92+ value : 0.001 ,
93+ [ tagSymbol ] : true ,
94+ } ,
95+ } ) ;
96+ } ) ;
7297 } ) ;
7398
7499 describe ( "deserialization" , ( ) => {
75100 it ( "should not create undefined values" , async ( ) => {
76101 const struct = [ 3 , "ns" , "Struct" , 0 , [ "sessionId" , "tokenId" ] , [ 0 , 0 ] ] satisfies StaticStructureSchema ;
77- const deserializer = codec . createDeserializer ( ) ;
78102
79103 const data = cbor . serialize ( {
80104 sessionId : "abcd" ,
@@ -88,5 +112,21 @@ describe(CborShapeSerializer.name, () => {
88112
89113 expect ( "tokenId" in deserialized ) . toEqual ( false ) ;
90114 } ) ;
115+
116+ it ( "should deserialize tags to dates if the schema is a timestamp" , async ( ) => {
117+ const decoded = {
118+ timestamp : {
119+ tag : 1 ,
120+ value : 0.001 ,
121+ [ tagSymbol ] : true ,
122+ } ,
123+ } ;
124+
125+ const deserialized = await deserializer . read ( dateSchema , cbor . serialize ( decoded ) ) ;
126+
127+ expect ( deserialized ) . toEqual ( {
128+ timestamp : new Date ( 1 ) ,
129+ } ) ;
130+ } ) ;
91131 } ) ;
92132} ) ;
0 commit comments