| 1 | import { describe, it } from "@effect/vitest" |
| 2 | import { assertFalse, assertTrue, strictEqual, throws } from "@effect/vitest/utils" |
| 3 | import { Chunk, Equal, Hash, Redacted } from "effect" |
| 4 | |
| 5 | describe("Redacted", () => { |
| 6 | it("chunk constructor", () => { |
| 7 | const redacted = Redacted.make(Chunk.fromIterable("redacted".split(""))) |
| 8 | assertTrue(Equal.equals(redacted, Redacted.make(Chunk.fromIterable("redacted".split(""))))) |
| 9 | }) |
| 10 | |
| 11 | it("value", () => { |
| 12 | const redacted = Redacted.make(Chunk.fromIterable("redacted".split(""))) |
| 13 | const value = Redacted.value(redacted) |
| 14 | assertTrue(Equal.equals(value, Chunk.fromIterable("redacted".split("")))) |
| 15 | }) |
| 16 | |
| 17 | it("pipe supports extracting the protected value", () => { |
| 18 | const value = { asd: 123 } |
| 19 | const redacted = Redacted.make(value) |
| 20 | const extractedValue = redacted.pipe(Redacted.value) |
| 21 | strictEqual(value, extractedValue) |
| 22 | }) |
| 23 | |
| 24 | it("toString returns a redacted placeholder", () => { |
| 25 | const redacted = Redacted.make("redacted") |
| 26 | strictEqual(`${redacted}`, "<redacted>") |
| 27 | }) |
| 28 | |
| 29 | it("toJSON serializes the redacted placeholder", () => { |
| 30 | const redacted = Redacted.make("redacted") |
| 31 | strictEqual(JSON.stringify(redacted), "\"<redacted>\"") |
| 32 | }) |
| 33 | |
| 34 | it("label appears in redacted output and wipe errors", () => { |
| 35 | const redacted = Redacted.make("redacted", { label: "MY_LABEL" }) |
| 36 | strictEqual(redacted.label, "MY_LABEL") |
| 37 | strictEqual(redacted.toString(), "<redacted:MY_LABEL>") |
| 38 | strictEqual(JSON.stringify(redacted), `"<redacted:MY_LABEL>"`) |
| 39 | |
| 40 | assertTrue(Redacted.wipeUnsafe(redacted)) |
| 41 | throws(() => Redacted.value(redacted), new Error("Unable to get redacted value with label: \"MY_LABEL\"")) |
| 42 | }) |
| 43 | |
| 44 | it("wipeUnsafe removes access to the protected value", () => { |
| 45 | const redacted = Redacted.make("redacted") |
| 46 | assertTrue(Redacted.wipeUnsafe(redacted)) |
| 47 | throws(() => Redacted.value(redacted), new Error("Unable to get redacted value")) |
| 48 | }) |
| 49 | |
| 50 | it("Equal compares the protected values of two Redacted values", () => { |
| 51 | assertTrue(Equal.equals(Redacted.make(1), Redacted.make(1))) |
| 52 | assertFalse(Equal.equals(Redacted.make(1), Redacted.make(2))) |
| 53 | }) |
| 54 | |
| 55 | it("Hash is derived from the protected value", () => { |
| 56 | strictEqual(Hash.hash(Redacted.make(1)), Hash.hash(Redacted.make(1))) |
| 57 | assertTrue(Hash.hash(Redacted.make(1)) !== Hash.hash(Redacted.make(2))) |
| 58 | }) |
| 59 | }) |