import { describe, expect, it } from "vitest"; import { AssertEqual, Expect, } from "inferred-types/types"; import { createKindError, KindError, } from "~"; describe("KindErrorType.partial()", () => { it("Runtime & Type: basic partial application", () => { // Arrange const BaseErr = createKindError("my-error", { region: "string", code: "number", user: "string" }); // Act // We expect this to return a new KindErrorType where region and code are fixed const SpecificErr = BaseErr.partial({ region: "us-east", code: 404 }); // Instantiate the error const err = SpecificErr("not found", { user: "ken" }); // Assert Runtime expect(SpecificErr.kind).toBe("my-error"); expect(SpecificErr.context).toEqual({ region: "us-east", code: 404, user: "string" }); expect(err.kind).toBe("my-error"); expect(err.context).toEqual({ region: "us-east", code: 404, user: "ken" }); // Assert Types type SpecificParams = Parameters; // Check that the resulting error has the correct context shape (all values resolved) type ErrContext = typeof err.context; type cases = [ // The partial error type should require 'user' in its context Expect>, // The resulting error context should have literal values for region/code // and string for user Expect >> ]; }); it("Runtime & Type: chained partial application", () => { // Arrange const BaseErr = createKindError("my-error", { region: "string", code: "number", user: "string" }); // Act const Step1 = BaseErr.partial({ region: "us-west" }); const Step2 = Step1.partial({ code: 500 }); // Instantiate const err = Step2("server error", { user: "admin" }); // Assert Runtime expect(Step2.context).toEqual({ region: "us-west", code: 500, user: "string" }); expect(err.context).toEqual({ region: "us-west", code: 500, user: "admin" }); // Assert Types type Step1Params = Parameters; type Step2Params = Parameters; type cases = [ // Step1 still needs code and user Expect>, // Step2 only needs user Expect>, // Resulting error has everything Expect >> ]; }); it("Runtime & Type: partial with optional property", () => { // Arrange const BaseErr = createKindError("my-error", { req: "string", opt: "number|undefined" }); // Act const FixedOpt = BaseErr.partial({ opt: 123 }); const FixedReq = BaseErr.partial({ req: "hello" }); // Assert Runtime // ... (omitted for brevity as we focus on types mostly here) // Assert Types type P1 = Parameters; type P2 = Parameters; type cases = [ // FixedOpt has opt fixed, so req is required Expect>, // FixedReq has req fixed, so opt is optional (ctx is optional) Expect> ]; }); });