/* oxlint-disable typescript/no-floating-promises -- expectTypeOf() is a sync type-assertion library that the type-aware linter misidentifies as a promise */ import { strict as assert } from "node:assert"; import { test } from "node:test"; import { expectTypeOf } from "expect-type"; import { type Matcher, P, match } from "./index.ts"; test("match returns a builder", () => { const builder = match("x"); expectTypeOf(builder).toHaveProperty("with"); expectTypeOf(builder).toHaveProperty("exhaustive"); expectTypeOf(builder).toHaveProperty("otherwise"); }); test("P.literal narrows to its literal type", () => { const matcher = P.literal("yes"); expectTypeOf(matcher).toMatchTypeOf>(); assert.equal(matcher.matches("yes"), true); assert.equal(matcher.matches("no"), false); }); test("P.type narrows to the typeof target", () => { const matcher = P.type("string"); expectTypeOf(matcher).toMatchTypeOf>(); assert.equal(matcher.matches("hi"), true); assert.equal(matcher.matches(42), false); }); test("exhaustive() returns the union of handler return types", () => { const result = match<"a" | "b">("a") .with(P.literal("a"), () => 1 as const) .with(P.literal("b"), () => "two" as const) .exhaustive(); expectTypeOf(result).toEqualTypeOf<1 | "two">(); assert.equal(result, 1); }); test("otherwise() falls back when no case matches", () => { const result = match<"x" | "y" | "z">("z") .with(P.literal("x"), (v): string => `got ${v}`) .otherwise((v): string => `fallback ${v}`); assert.equal(result, "fallback z"); }); test("exhaustive throws when no case matches", () => { assert.throws( () => match<"a" | "b" | "c">("c") .with(P.literal("a"), () => "A") .with(P.literal("b"), () => "B") .exhaustive(), /no matching case/, ); });