t3-code-android-nightly/.repos/effect-smol/packages/effect/test/reactivity/AsyncResult.test.ts
Julius Marminge 6f9cea00ae
chore(refs): sync Effect and Alchemy references to 4.0.1 and beta.80 (#16170)
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 13:22:30 -07:00

50 lines
1.6 KiB
TypeScript

import { describe, expect, it } from "@effect/vitest"
import { Cause } from "effect"
import { AsyncResult } from "effect/reactivity"
describe("AsyncResult", () => {
it("waiting dispatches with optional options", () => {
const result = AsyncResult.success(1)
expect(AsyncResult.waiting(result).waiting).toBe(true)
expect(AsyncResult.waiting(result, { touch: false }).waiting).toBe(true)
expect(AsyncResult.waiting()(result).waiting).toBe(true)
expect(AsyncResult.waiting({ touch: false })(result).waiting).toBe(true)
})
describe("builder", () => {
it("onDefect handles defects", () => {
const defect = new Error("boom")
const result = AsyncResult.failure<number, string>(Cause.die(defect))
const handled = AsyncResult.builder(result)
.onDefect((received) => received)
.orElse(() => null)
expect(handled).toBe(defect)
})
it("onDefect does not handle typed errors", () => {
const handled = AsyncResult.builder(AsyncResult.fail("error"))
.onDefect(() => "defect")
.orElse(() => "fallback")
expect(handled).toEqual("fallback")
})
it("exhaustive returns output when typed errors are handled", () => {
const handled = AsyncResult.builder(
AsyncResult.fail<{ readonly _tag: "NotFoundError"; readonly resource: string }>({
_tag: "NotFoundError",
resource: "user"
})
)
.onErrorTag("NotFoundError", (error) => `missing:${error.resource}`)
.onDefect(() => "failure")
.onInterrupt(() => "interrupt")
.exhaustive()
expect(handled).toEqual("missing:user")
})
})
})