|
| 1 | +import { NormalizedSchema, sim, struct } from "@smithy/core/schema"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +import { cbor } from "./cbor"; |
| 5 | +import { CborCodec, CborShapeSerializer } from "./CborCodec"; |
| 6 | + |
| 7 | +describe(CborShapeSerializer.name, () => { |
| 8 | + const codec = new CborCodec(); |
| 9 | + |
| 10 | + const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; |
| 11 | + |
| 12 | + const idempotencyTokenSchemas = [ |
| 13 | + NormalizedSchema.of(sim("", "StringWithTraits", 0, 0b0100)), |
| 14 | + NormalizedSchema.of(sim("", "StringWithTraits", 0, { idempotencyToken: 1 })), |
| 15 | + ]; |
| 16 | + |
| 17 | + const plainSchemas = [ |
| 18 | + NormalizedSchema.of(0), |
| 19 | + NormalizedSchema.of(sim("", "StringWithTraits", 0, 0)), |
| 20 | + NormalizedSchema.of(sim("", "StringWithTraits", 0, {})), |
| 21 | + ]; |
| 22 | + |
| 23 | + const serializer = codec.createSerializer(); |
| 24 | + |
| 25 | + describe("serialization", () => { |
| 26 | + it("should generate an idempotency token when the input for such a member is undefined", () => { |
| 27 | + for (const idempotencyTokenSchema of idempotencyTokenSchemas) { |
| 28 | + for (const plainSchema of plainSchemas) { |
| 29 | + const objectSchema = struct( |
| 30 | + "ns", |
| 31 | + "StructWithIdempotencyToken", |
| 32 | + 0, |
| 33 | + ["idempotencyToken", "plainString"], |
| 34 | + [idempotencyTokenSchema, plainSchema] |
| 35 | + ); |
| 36 | + |
| 37 | + serializer.write(objectSchema, { |
| 38 | + idempotencyToken: undefined, |
| 39 | + plainString: undefined, |
| 40 | + }); |
| 41 | + expect(cbor.deserialize(serializer.flush())).toMatchObject({ |
| 42 | + idempotencyToken: UUID_V4, |
| 43 | + }); |
| 44 | + |
| 45 | + serializer.write(objectSchema, { |
| 46 | + idempotencyToken: undefined, |
| 47 | + plainString: "abc", |
| 48 | + }); |
| 49 | + expect(cbor.deserialize(serializer.flush())).toMatchObject({ |
| 50 | + idempotencyToken: UUID_V4, |
| 51 | + plainString: /^abc$/, |
| 52 | + }); |
| 53 | + } |
| 54 | + } |
| 55 | + }); |
| 56 | + }); |
| 57 | +}); |
0 commit comments