| 1 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import { Schema } from "effect" |
| 3 | import { EntityAddress, EntityId, EntityType, Envelope, Reply, ShardId, Snowflake } from "effect/cluster" |
| 4 | import { SchemaBinary } from "effect/encoding" |
| 5 | import { Headers } from "effect/http" |
| 6 | |
| 7 | const request = { |
| 8 | _tag: "Request", |
| 9 | requestId: Snowflake.Snowflake(BigInt(1)), |
| 10 | address: EntityAddress.make({ |
| 11 | shardId: ShardId.make("default", 1), |
| 12 | entityType: EntityType.make("TestEntity"), |
| 13 | entityId: EntityId.make("1") |
| 14 | }), |
| 15 | tag: "Test", |
| 16 | payload: { id: 1 }, |
| 17 | headers: Headers.empty |
| 18 | } as Envelope.PartialRequest |
| 19 | |
| 20 | describe("Envelope.OpaqueHole", () => { |
| 21 | it("leaves the already-encoded payload untouched under the JSON codec", () => { |
| 22 | const encoded = Schema.encodeSync(Envelope.PartialJson)(request) as Envelope.PartialRequestEncoded |
| 23 | assert.deepStrictEqual(encoded.payload, { id: 1 }) |
| 24 | |
| 25 | const decoded = Schema.decodeSync(Envelope.PartialJson)(encoded) |
| 26 | assert.strictEqual(decoded._tag, "Request") |
| 27 | assert.deepStrictEqual((decoded as Envelope.PartialRequest).payload, { id: 1 }) |
| 28 | }) |
| 29 | |
| 30 | it("carries a non-JSON payload through the hole", () => { |
| 31 | const bytes = Uint8Array.of(1, 2, 3) |
| 32 | const encoded = Schema.encodeSync(Envelope.PartialJson)( |
| 33 | { ...request, payload: bytes } as Envelope.PartialRequest |
| 34 | ) as Envelope.PartialRequestEncoded |
| 35 | assert.strictEqual(encoded.payload, bytes) |
| 36 | }) |
| 37 | |
| 38 | it("compiles the hole and Reply.Encoded as a bytes leaf under SchemaBinary", () => { |
| 39 | const bytes = Uint8Array.of(1, 2, 3) |
| 40 | const encodedBytes = Schema.encodeSync(SchemaBinary.toCodec(Schema.Uint8Array))(bytes) |
| 41 | const encodedHole = Schema.encodeSync(SchemaBinary.toCodec(Envelope.OpaqueHole))(bytes) |
| 42 | const encodedReply = Schema.encodeSync(SchemaBinary.toCodec(Reply.Encoded))(bytes as unknown as Reply.Encoded) |
| 43 | |
| 44 | assert.deepStrictEqual(encodedHole, encodedBytes) |
| 45 | assert.deepStrictEqual(encodedReply, encodedBytes) |
| 46 | assert.deepStrictEqual(Schema.decodeSync(SchemaBinary.toCodec(Envelope.OpaqueHole))(encodedHole), bytes) |
| 47 | assert.deepStrictEqual( |
| 48 | Schema.decodeSync(SchemaBinary.toCodec(Reply.Encoded))(encodedReply) as unknown, |
| 49 | bytes |
| 50 | ) |
| 51 | }) |
| 52 | |
| 53 | it("reports non-byte OpaqueHole inputs as schema failures", () => { |
| 54 | const encode = Schema.encodeUnknownSync(SchemaBinary.toCodec(Envelope.OpaqueHole)) |
| 55 | |
| 56 | assert.throws(() => encode({ id: 1 }), /Uint8Array/) |
| 57 | }) |
| 58 | }) |