Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bindings-typescript/src/lib/algebraic_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ export const SumType = {
typespace
);
const serializeErr = AlgebraicType.makeSerializer(
ty.variants[0].algebraicType,
ty.variants[1].algebraicType,
typespace
);

Expand Down
69 changes: 69 additions & 0 deletions crates/bindings-typescript/tests/serde.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
BinaryWriter,
ConnectionId,
Identity,
Result,
ScheduleAt,
Uuid,
} from '../src';
Expand Down Expand Up @@ -184,4 +185,72 @@ describe('it correctly serializes and deserializes algebraic values', () => {

expect(deserializedValue).toEqual(value);
});

test('when it serializes and deserializes a Result with an ok payload', () => {
const value = { ok: 7 };

const algebraic_type = Result.getAlgebraicType(
AlgebraicType.U8,
AlgebraicType.String
);
const binaryWriter = new BinaryWriter(1024);
AlgebraicType.serializeValue(binaryWriter, algebraic_type, value);

const buffer = binaryWriter.getBuffer();
// Tag 0 (`ok`), then the `u8` payload.
expect(buffer).toEqual(new Uint8Array([0, 7]));

const deserializedValue = AlgebraicType.deserializeValue(
new BinaryReader(buffer),
algebraic_type
);

expect(deserializedValue).toEqual(value);
});

test('when it serializes and deserializes a Result with an err payload', () => {
const value = { err: 'boom' };

const algebraic_type = Result.getAlgebraicType(
AlgebraicType.U8,
AlgebraicType.String
);
const binaryWriter = new BinaryWriter(1024);
AlgebraicType.serializeValue(binaryWriter, algebraic_type, value);

const buffer = binaryWriter.getBuffer();
// Tag 1 (`err`), then the `string` payload: a u32 length and its bytes.
expect(buffer).toEqual(new Uint8Array([1, 4, 0, 0, 0, 98, 111, 111, 109]));

const deserializedValue = AlgebraicType.deserializeValue(
new BinaryReader(buffer),
algebraic_type
);

expect(deserializedValue).toEqual(value);
});

test('when it serializes and deserializes a Result with a product err payload', () => {
const value = { err: { code: 42 } };

const algebraic_type = Result.getAlgebraicType(
AlgebraicType.String,
AlgebraicType.Product({
elements: [{ name: 'code', algebraicType: AlgebraicType.I32 }],
})
);
const binaryWriter = new BinaryWriter(1024);
AlgebraicType.serializeValue(binaryWriter, algebraic_type, value);

const buffer = binaryWriter.getBuffer();
// Tag 1 (`err`), then the product payload: 42 as a little-endian i32.
expect(buffer).toEqual(new Uint8Array([1, 42, 0, 0, 0]));

const deserializedValue = AlgebraicType.deserializeValue(
new BinaryReader(buffer),
algebraic_type
);

expect(deserializedValue).toEqual(value);
});
});