| 1 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import { Effect, Stream } from "effect" |
| 3 | import * as Ndjson from "effect/encoding/Ndjson" |
| 4 | import * as Schema from "effect/Schema" |
| 5 | |
| 6 | describe("Ndjson", () => { |
| 7 | it.effect("fails for values without a JSON representation", () => |
| 8 | Effect.gen(function*() { |
| 9 | const inputs = [undefined, () => {}, Symbol("x")] |
| 10 | |
| 11 | for (const input of inputs) { |
| 12 | const error = yield* Stream.make(input).pipe( |
| 13 | Stream.pipeThroughChannel(Ndjson.encodeString()), |
| 14 | Stream.runCollect, |
| 15 | Effect.flip |
| 16 | ) |
| 17 | |
| 18 | assert.instanceOf(error, Ndjson.NdjsonError) |
| 19 | assert.strictEqual(error.kind, "Pack") |
| 20 | } |
| 21 | })) |
| 22 | |
| 23 | it.effect("decodeSchema decodes records split across Uint8Array chunks", () => |
| 24 | Effect.gen(function*() { |
| 25 | const messages = yield* Stream.make( |
| 26 | new TextEncoder().encode("{\"foo\":\"bar\"}\n") |
| 27 | ).pipe( |
| 28 | Stream.pipeThroughChannel(Ndjson.decodeSchema(Schema.Struct({ foo: Schema.String }))()), |
| 29 | Stream.runCollect |
| 30 | ) |
| 31 | |
| 32 | assert.deepStrictEqual([...messages], [{ foo: "bar" }]) |
| 33 | })) |
| 34 | |
| 35 | it("identifies Ndjson errors", () => { |
| 36 | assert.isTrue(Ndjson.isNdjsonError(new Ndjson.NdjsonError({ kind: "Pack", cause: new Error("boom") }))) |
| 37 | assert.isFalse(Ndjson.isNdjsonError({ _tag: "NdjsonError" })) |
| 38 | assert.isFalse(Ndjson.isNdjsonError(null)) |
| 39 | }) |
| 40 | }) |