/* 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 path from "node:path"; import { test } from "node:test"; import { expectTypeOf } from "expect-type"; import { type CompletionTarget, LspSession, } from "#test-utils/lsp-completion.ts"; import { getMatcher, getMatcherW } from "./primitive.ts"; // ============================================================================ // API: getMatcher — ✔️ Exhaustive / ✔️ ReturnsStrict // ============================================================================ test("getMatcher: exhaustive pattern infers one common return type", () => { // Arrange const factory = getMatcher<"a" | "b">(); // Act const matcher = factory({ a: (s) => { expectTypeOf(s).toEqualTypeOf<"a">(); return 1; }, b: (s) => { expectTypeOf(s).toEqualTypeOf<"b">(); return 2; }, }); // Assert // ✔️ ReturnsStrict: R is the best common return type, not a widening union. expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b") => number>(); // ❌ a value outside T must not be accepted by the matcher expectTypeOf<"c">().not.toExtend[0]>(); assert.equal(matcher("a"), 1); assert.equal(matcher("b"), 2); }); test("getMatcher: dispatches on numeric literal keys", () => { // Arrange const factory = getMatcher<1 | 2>(); // Act const matcher = factory({ 1: (n) => { expectTypeOf(n).toEqualTypeOf<1>(); return n + 1; }, 2: (n) => { expectTypeOf(n).toEqualTypeOf<2>(); return n * 10; }, }); // Assert expectTypeOf(matcher).toEqualTypeOf<(shape: 1 | 2) => number>(); assert.equal(matcher(1), 2); assert.equal(matcher(2), 20); }); test("getMatcher: dispatches on mixed string and numeric keys", () => { // Arrange const factory = getMatcher<"a" | "b" | 1 | 2>(); // Act const matcher = factory({ a: (s) => { expectTypeOf(s).toEqualTypeOf<"a">(); return 1; }, b: (s) => { expectTypeOf(s).toEqualTypeOf<"b">(); return 2; }, 1: (n) => { expectTypeOf(n).toEqualTypeOf<1>(); return 10; }, 2: (n) => { expectTypeOf(n).toEqualTypeOf<2>(); return 20; }, }); // Assert expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b" | 1 | 2) => number>(); assert.equal(matcher("a"), 1); assert.equal(matcher("b"), 2); assert.equal(matcher(1), 10); assert.equal(matcher(2), 20); }); test("getMatcher: dispatches on boolean literals", () => { // Arrange const factory = getMatcher(); // Act const matcher = factory({ true: (s) => { expectTypeOf(s).toEqualTypeOf(); return 1; }, false: (s) => { expectTypeOf(s).toEqualTypeOf(); return 2; }, }); // Assert expectTypeOf(matcher).toEqualTypeOf<(shape: boolean) => number>(); assert.equal(matcher(true), 1); assert.equal(matcher(false), 2); }); test("getMatcher: dispatches on null and undefined", () => { // Arrange const factory = getMatcher(); // Act const matcher = factory({ null: (s) => { expectTypeOf(s).toEqualTypeOf(); return "null"; }, undefined: (s) => { expectTypeOf(s).toEqualTypeOf(); return "undefined"; }, }); // Assert expectTypeOf(matcher).toEqualTypeOf<(shape: null | undefined) => string>(); assert.equal(matcher(null), "null"); assert.equal(matcher(undefined), "undefined"); }); // ============================================================================ // API: getMatcher — ❌ Exhaustive (fallback) / ✔️ ReturnsStrict // ============================================================================ test("getMatcher: a `_` fallback receives the unhandled keys", () => { // Arrange const factory = getMatcher<"a" | "b" | "c">(); // Act const matcher = factory( { a: (s) => { expectTypeOf(s).toEqualTypeOf<"a">(); return 1 as const; }, }, (s) => { // The fallback sees only the keys `a` did not handle. expectTypeOf(s).toEqualTypeOf<"b" | "c">(); return 2 as const; }, ); // Assert expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b" | "c") => 1 | 2>(); // ❌ a value outside T must not be accepted by the matcher expectTypeOf<"d">().not.toExtend[0]>(); assert.equal(matcher("a"), 1); assert.equal(matcher("b"), 2); assert.equal(matcher("c"), 2); }); test("getMatcher: a `_` fallback routes mixed string and numeric gaps", () => { // Arrange const factory = getMatcher<"a" | "b" | 1 | 2>(); // Act const matcher = factory( { a: (s) => { expectTypeOf(s).toEqualTypeOf<"a">(); return "A"; }, 1: (n) => { expectTypeOf(n).toEqualTypeOf<1>(); return "one"; }, }, (s) => { expectTypeOf(s).toEqualTypeOf<"b" | 2>(); return "fallback"; }, ); // Assert expectTypeOf(matcher).toEqualTypeOf<(shape: "a" | "b" | 1 | 2) => string>(); assert.equal(matcher("a"), "A"); assert.equal(matcher("b"), "fallback"); assert.equal(matcher(1), "one"); assert.equal(matcher(2), "fallback"); }); test("getMatcher: a `_` fallback receives unhandled boolean and nullish keys", () => { // Arrange const factory = getMatcher<"a" | true | false | null | undefined>(); // Act const matcher = factory( { a: (s) => { expectTypeOf(s).toEqualTypeOf<"a">(); return 1 as const; }, null: (s) => { expectTypeOf(s).toEqualTypeOf(); return 2 as const; }, }, (s) => { // `true` and `false` are keyed as `"true"`/`"false"` but the // fallback still sees them as booleans. expectTypeOf(s).toEqualTypeOf(); return 3 as const; }, ); // Assert expectTypeOf(matcher).toEqualTypeOf< (shape: "a" | true | false | null | undefined) => 1 | 2 | 3 >(); assert.equal(matcher("a"), 1); assert.equal(matcher(true), 3); assert.equal(matcher(false), 3); assert.equal(matcher(null), 2); assert.equal(matcher(undefined), 3); }); // ============================================================================ // API: getMatcherW — ✔️ Exhaustive / ❌ ReturnsStrict // ============================================================================ test("getMatcherW: exhaustive pattern widens to the union of handler returns", () => { // Arrange const factory = getMatcherW<"x" | "y">(); // Act const matcher = factory({ x: (s) => { expectTypeOf(s).toEqualTypeOf<"x">(); return 1 as const; }, y: (s) => { expectTypeOf(s).toEqualTypeOf<"y">(); return "two" as const; }, }); // Assert // ❌ ReturnsStrict: mixed handler returns widen to their union. expectTypeOf(matcher).toEqualTypeOf<(shape: "x" | "y") => 1 | "two">(); // ❌ a value outside T must not be accepted by the matcher expectTypeOf<"z">().not.toExtend[0]>(); assert.equal(matcher("x"), 1); assert.equal(matcher("y"), "two"); }); test("getMatcherW: dispatches on mixed string and numeric keys", () => { // Arrange const factory = getMatcherW<"a" | "b" | 1 | 2>(); // Act const matcher = factory({ a: (s) => { expectTypeOf(s).toEqualTypeOf<"a">(); return "A" as const; }, b: (s) => { expectTypeOf(s).toEqualTypeOf<"b">(); return "B" as const; }, 1: (n) => { expectTypeOf(n).toEqualTypeOf<1>(); return 10 as const; }, 2: (n) => { expectTypeOf(n).toEqualTypeOf<2>(); return 20 as const; }, }); // Assert expectTypeOf(matcher).toEqualTypeOf< (shape: "a" | "b" | 1 | 2) => "A" | "B" | 10 | 20 >(); assert.equal(matcher("a"), "A"); assert.equal(matcher("b"), "B"); assert.equal(matcher(1), 10); assert.equal(matcher(2), 20); }); test("getMatcherW: exhaustive boolean and nullish widen to the union", () => { // Arrange const factory = getMatcherW(); // Act const matcher = factory({ true: (s) => { expectTypeOf(s).toEqualTypeOf(); return "yes" as const; }, false: (s) => { expectTypeOf(s).toEqualTypeOf(); return "no" as const; }, null: (s) => { expectTypeOf(s).toEqualTypeOf(); return 0 as const; }, undefined: (s) => { expectTypeOf(s).toEqualTypeOf(); return 1 as const; }, }); // Assert expectTypeOf(matcher).toEqualTypeOf< (shape: boolean | null | undefined) => "yes" | "no" | 0 | 1 >(); assert.equal(matcher(true), "yes"); assert.equal(matcher(false), "no"); assert.equal(matcher(null), 0); assert.equal(matcher(undefined), 1); }); // ============================================================================ // API: getMatcherW — ❌ Exhaustive (fallback) / ❌ ReturnsStrict // ============================================================================ test("getMatcherW: a `_` fallback widens gaps into the union", () => { // Arrange const factory = getMatcherW<"x" | "y" | "z">(); // Act const matcher = factory( { x: (s) => { expectTypeOf(s).toEqualTypeOf<"x">(); return 1 as const; }, }, (s) => { // The fallback sees only the keys `x` did not handle. expectTypeOf(s).toEqualTypeOf<"y" | "z">(); return "fallback" as const; }, ); // Assert expectTypeOf(matcher).toEqualTypeOf< (shape: "x" | "y" | "z") => 1 | "fallback" >(); // ❌ a value outside T must not be accepted by the matcher expectTypeOf<"w">().not.toExtend[0]>(); assert.equal(matcher("x"), 1); assert.equal(matcher("y"), "fallback"); assert.equal(matcher("z"), "fallback"); }); test("getMatcherW: a `_` fallback widens mixed string and numeric returns", () => { // Arrange const factory = getMatcherW<"a" | "b" | 1 | 2>(); // Act const matcher = factory( { a: (s) => { expectTypeOf(s).toEqualTypeOf<"a">(); return "A" as const; }, 1: (n) => { expectTypeOf(n).toEqualTypeOf<1>(); return 10 as const; }, }, (s) => { expectTypeOf(s).toEqualTypeOf<"b" | 2>(); return "fallback" as const; }, ); // Assert // ❌ ReturnsStrict: mixed handler returns widen to their union. expectTypeOf(matcher).toEqualTypeOf< (shape: "a" | "b" | 1 | 2) => "A" | 10 | "fallback" >(); assert.equal(matcher("a"), "A"); assert.equal(matcher("b"), "fallback"); assert.equal(matcher(1), 10); assert.equal(matcher(2), "fallback"); }); // ============================================================================ // Factory contracts — calls that must not compile // ============================================================================ test("getMatcher factory rejects patterns outside its contract", () => { // Arrange const factory = getMatcher<"a" | "b">(); // Act / Assert — the calls below must not compile. // `Parameters[0]` resolves only the *last* overload, so it // is not a sound "rejected" oracle (an accepted partial pattern does not // extend it either); the call sites are the oracle instead. factory({ a: () => 1, b: () => 2, // @ts-expect-error `c` is not part of the universe `"a" | "b"` c: () => 3, }); // @ts-expect-error a gap without a `_` fallback is not exhaustive factory({ a: () => 1 }); // @ts-expect-error a `string` fallback does not fit the `number` handler factory({ a: () => 1 }, () => "x"); // @ts-expect-error a fallback is redundant once the map covers the universe factory({ a: () => 1, b: () => 2 }, () => 0); }); test("getMatcher factory rejects non-exhaustive boolean and nullish maps", () => { // Arrange const factory = getMatcher(); // Act / Assert — the calls below must not compile // @ts-expect-error only `true` is handled; `false`, `null`, `undefined` are not factory({ true: () => 1 }); // @ts-expect-error `null` and `undefined` are not handled factory({ true: () => 1, false: () => 2 }); factory( // @ts-expect-error a fallback is redundant once the map covers the universe { true: () => 1, false: () => 2, null: () => 3, undefined: () => 4, }, () => 0, ); }); test("getMatcher: an open universe keeps the fallback's remainder open", () => { // Arrange const factory = getMatcher(); // Act const matcher = factory({ a: () => 1 }, (s) => { // The map's literal keys do not close an open universe, so the // remainder stays `string` and the fallback is not redundant. expectTypeOf(s).toEqualTypeOf(); return 2 as const; }); // Assert expectTypeOf(matcher).toEqualTypeOf<(shape: string) => number>(); assert.equal(matcher("a"), 1); assert.equal(matcher("b"), 2); }); test("getMatcherW factory rejects patterns outside its contract", () => { // Arrange const factory = getMatcherW<"x" | "y">(); // Act / Assert — the calls below must not compile factory({ x: () => 1 as const, y: () => 2 as const, // @ts-expect-error `z` is not part of the universe `"x" | "y"` z: () => 3 as const, }); // @ts-expect-error a gap without a `_` fallback is not exhaustive factory({ x: () => 1 as const }); // @ts-expect-error a fallback is redundant once the map covers the universe factory({ x: () => 1 as const, y: () => 2 as const }, () => 0); }); // ============================================================================ // Dispatch — runtime behavior // ============================================================================ test("getMatcher: an unhandled shape throws without a fallback", () => { // Arrange — an open universe types its handler map as an index signature, // so the type system cannot prove the runtime map is exhaustive. const handlers: Record number> = { a: () => 1 }; const matcher = getMatcher()(handlers); // Act / Assert assert.equal(matcher("a"), 1); assert.throws(() => matcher("b"), /Unhandled shape: b/); }); test("getMatcher: an unhandled boolean shape throws without a fallback", () => { // Arrange — an `string | boolean` universe widens its handler map to an // index signature, so the runtime map's exhaustiveness is not provable. const handlers: Record number> = { true: () => 1 }; const matcher = getMatcher()(handlers); // Act / Assert assert.equal(matcher(true), 1); assert.throws(() => matcher(false), /Unhandled shape: false/); }); // ============================================================================ // Autocomplete — the language server is the oracle, not the type system // ============================================================================ // Completion is a contextual-type property that the type system cannot observe, // so the cases below read the popup from the repo's language server (via the // test helper) rather than pairing `expectTypeOf` with `assert` — see // development/testing.md § Autocomplete. They probe the *matcher's* overloads, // which is why they live with the matcher and not with the helper. const REPO_ROOT = path.resolve(import.meta.dirname, ".."); const UNIVERSE = `"a" | "b" | "c"`; // One session per probe: the tests share no language-server state (open // documents, project membership), so they pass in any order. interface LabelsProbe { readonly name: string; readonly factory: "getMatcher" | "getMatcherW"; readonly body: string; readonly universe?: string; readonly tail?: string; } const labelsFor = ({ name, factory, body, universe = UNIVERSE, tail = "", }: LabelsProbe): Promise => { const session = new LspSession(REPO_ROOT); const target: CompletionTarget = { file: `src/__autocomplete_${name}.ts`, source: [ `import { ${factory} } from "./index.ts";`, `const m = ${factory}<${universe}>()({`, body, `}${tail});`, "", ].join("\n"), }; return session .completionLabelsAt(target) .then((result) => result.labels) .finally(() => session.close()); }; test("autocomplete: an exhaustive pattern requires the universe", () => { // Arrange const name = "getMatcher_fresh"; // Act const labels = labelsFor({ name, factory: "getMatcher", body: " /*COMPLETE*/", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["a", "b", "c"]); }); }); test("autocomplete: handled keys drop out of the popup", () => { // Arrange const name = "getMatcher_after_key"; // Act const labels = labelsFor({ name, factory: "getMatcher", body: " a: () => 1,\n /*COMPLETE*/", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["b", "c"]); }); }); test("autocomplete: a fallback makes the remaining keys optional", () => { // Arrange const name = "getMatcher_with_fallback"; // Act const labels = labelsFor({ name, factory: "getMatcher", body: " /*COMPLETE*/", tail: ", () => 0", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["a?", "b?", "c?"]); }); }); test("autocomplete: with a fallback, handled keys stay optional", () => { // Arrange const name = "getMatcher_with_fallback_after_key"; // Act const labels = labelsFor({ name, factory: "getMatcher", body: " a: () => 1,\n /*COMPLETE*/", tail: ", () => 0", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["b?", "c?"]); }); }); test("autocomplete: `getMatcherW` offers the same popup as `getMatcher`", () => { // Arrange const name = "getMatcherW_fresh"; // Act const labels = labelsFor({ name, factory: "getMatcherW", body: " /*COMPLETE*/", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["a", "b", "c"]); }); }); test("autocomplete: `getMatcherW` also offers optional keys with a fallback", () => { // Arrange const name = "getMatcherW_with_fallback"; // Act const labels = labelsFor({ name, factory: "getMatcherW", body: " /*COMPLETE*/", tail: ", () => 0", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["a?", "b?", "c?"]); }); }); test("autocomplete: boolean and nullish keys are offered by name", () => { // Arrange const name = "getMatcher_boolean_nullish"; // Act const labels = labelsFor({ name, factory: "getMatcher", universe: "boolean | null | undefined", body: " /*COMPLETE*/", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["false", "null", "true", "undefined"]); }); }); test("autocomplete: a handled boolean literal drops out of the popup", () => { // Arrange const name = "getMatcher_boolean_after_key"; // Act const labels = labelsFor({ name, factory: "getMatcher", universe: "boolean | null | undefined", body: " true: () => 1,\n /*COMPLETE*/", }); // Assert return labels.then((result) => { assert.deepEqual([...result], ["false", "null", "undefined"]); }); });