| 1 | import { assert, describe, it } from "@effect/vitest" |
| 2 | import { Result } from "effect" |
| 3 | import * as Base64 from "effect/encoding/Base64" |
| 4 | |
| 5 | describe("Base64", () => { |
| 6 | it("encodes strings and bytes", () => { |
| 7 | assert.strictEqual(Base64.encode("hello"), "aGVsbG8=") |
| 8 | assert.strictEqual(Base64.encode(new Uint8Array([72, 101, 108, 108, 111])), "SGVsbG8=") |
| 9 | }) |
| 10 | |
| 11 | it("decodes bytes and UTF-8 strings", () => { |
| 12 | assert.deepStrictEqual( |
| 13 | Result.getOrThrow(Base64.decode("SGVs\r\nbG8=")), |
| 14 | new Uint8Array([72, 101, 108, 108, 111]) |
| 15 | ) |
| 16 | assert.strictEqual(Result.getOrThrow(Base64.decodeString("8J+Riw==")), "👋") |
| 17 | }) |
| 18 | |
| 19 | it("rejects invalid input with format information", () => { |
| 20 | const invalidLength = Base64.decode("abc") |
| 21 | assert.isTrue(Result.isFailure(invalidLength)) |
| 22 | if (Result.isFailure(invalidLength)) { |
| 23 | assert.strictEqual(invalidLength.failure.kind, "Decode") |
| 24 | assert.strictEqual(invalidLength.failure.module, "Base64") |
| 25 | assert.strictEqual(invalidLength.failure.input, "abc") |
| 26 | } |
| 27 | |
| 28 | assert.isTrue(Result.isFailure(Base64.decode("ab=c"))) |
| 29 | assert.isTrue(Result.isFailure(Base64.decode("!!!!"))) |
| 30 | }) |
| 31 | }) |