import { expect, test } from "vitest" import type { Either } from "#Source/result/index.ts" import { Controller, controllerFromEitherType, Left, Right } from "#Source/result/index.ts" test("Controller.isLeft identifies Left instances", () => { expect(Controller.isLeft(new Left("stop"))).toBe(true) expect(Controller.isLeft(new Right(2))).toBe(false) }) test("Controller.isRight identifies Right instances", () => { expect(Controller.isRight(new Right(2))).toBe(true) expect(Controller.isRight(new Left("stop"))).toBe(false) }) test("Controller.value exposes the same pending promise until the controller resolves", async () => { const controller = new Controller() const firstValue = controller.value const secondValue = controller.value expect(secondValue).toBe(firstValue) const returned = await controller.returnRight(2) const settled = await firstValue expect(settled).toBe(returned) expect(settled.isRight()).toBe(true) expect(settled.getRight()).toBe(2) }) test("Controller.returnRight resolves the controller with a Right value", async () => { const controller = new Controller() const returned = await controller.returnRight(2) const settled = await controller.value expect(returned).toBeInstanceOf(Right) expect(returned).toBe(settled) expect(returned.isRight()).toBe(true) expect(returned.getRight()).toBe(2) }) test("Controller.returnLeft resolves the controller with a Left value", async () => { const controller = new Controller() const returned = await controller.returnLeft("stop") const settled = await controller.value expect(returned).toBeInstanceOf(Left) expect(returned).toBe(settled) expect(returned.isLeft()).toBe(true) expect(returned.getLeft()).toBe("stop") }) test("Controller.throw resolves the controller with a Left value and rejects with the same Left", async () => { const controller = new Controller() let caught: unknown try { await controller.throw("stop") } catch (error) { caught = error } const settled = await controller.value expect(caught).toBeInstanceOf(Left) expect(caught).toBe(settled) expect(settled.isLeft()).toBe(true) expect(settled.getLeft()).toBe("stop") }) test("controllerFromEitherType creates a controller that matches the Either shape", async () => { const controller = controllerFromEitherType>() const typedController: Controller = controller const returned = await typedController.returnRight(3) const settled = await controller.value expect(controller).toBeInstanceOf(Controller) expect(returned).toBe(settled) expect(returned.isRight()).toBe(true) expect(returned.getRight()).toBe(3) })