import { describe, it, expect, expectTypeOf } from "vitest"; import type { ShippoConfidenceScore, ShippoValidationValue, ShippoAddressType, ShippoValidationReason, RecommendedAddress, AddressValidationResult, } from "./checkout"; describe("AddressValidationResult types", () => { it("ShippoConfidenceScore accepts valid literals", () => { expectTypeOf<"high">().toMatchTypeOf(); expectTypeOf<"medium">().toMatchTypeOf(); expectTypeOf<"low">().toMatchTypeOf(); }); it("ShippoValidationValue accepts valid literals", () => { expectTypeOf<"valid">().toMatchTypeOf(); expectTypeOf<"partially_valid">().toMatchTypeOf(); expectTypeOf<"invalid">().toMatchTypeOf(); }); it("ShippoAddressType accepts valid literals", () => { expectTypeOf<"residential">().toMatchTypeOf(); expectTypeOf<"commercial">().toMatchTypeOf(); expectTypeOf<"unknown">().toMatchTypeOf(); expectTypeOf<"po_box">().toMatchTypeOf(); expectTypeOf<"military">().toMatchTypeOf(); }); it("ShippoValidationReason has code and description", () => { expectTypeOf().toHaveProperty("code"); expectTypeOf().toHaveProperty("description"); }); it("RecommendedAddress has required fields and no state", () => { expectTypeOf().toHaveProperty("addressLine1"); expectTypeOf().toHaveProperty("city"); expectTypeOf().toHaveProperty("postalCode"); expectTypeOf().toHaveProperty("country"); expectTypeOf().toHaveProperty("confidenceScore"); expectTypeOf().toHaveProperty("confidenceCode"); expectTypeOf().toHaveProperty("confidenceDescription"); type Keys = keyof RecommendedAddress; expectTypeOf<"state" extends Keys ? true : false>().toEqualTypeOf(); expectTypeOf<"addressLine2" extends Keys ? true : false>().toEqualTypeOf(); }); it("AddressValidationResult is structurally complete", () => { expectTypeOf().toHaveProperty("isValid"); expectTypeOf().toHaveProperty("validationValue"); expectTypeOf().toHaveProperty("reasons"); expectTypeOf().toHaveProperty("addressType"); expectTypeOf().toHaveProperty("changedAttributes"); expectTypeOf().toHaveProperty("originalAddress"); }); it("recommendedAddress is optional on AddressValidationResult", () => { const withoutRecommended: AddressValidationResult = { isValid: true, validationValue: "valid", reasons: [], addressType: "unknown", changedAttributes: [], originalAddress: { addressLine1: "10 Downing Street", city: "London", postalCode: "SW1A 2AA", country: "GB", }, }; expect(withoutRecommended.recommendedAddress).toBeUndefined(); }); it("AddressValidationResult accepts a full object with recommended address", () => { const full: AddressValidationResult = { isValid: false, validationValue: "partially_valid", reasons: [{ code: "postal_data_match", description: "Postal code matched" }], addressType: "unknown", changedAttributes: ["postalCode"], recommendedAddress: { addressLine1: "10 Downing Street", city: "London", postalCode: "SW1A 2AA", country: "GB", completeAddress: "10 Downing Street;LONDON;SW1A 2AA;UNITED KINGDOM", confidenceScore: "high", confidenceCode: "postal_data_match", confidenceDescription: "Matched via postal data", }, originalAddress: { addressLine1: "10 Downing St", city: "London", postalCode: "SW1A2AA", country: "GB", }, }; expect(full.isValid).toBe(false); expect(full.recommendedAddress).toBeDefined(); expect(full.recommendedAddress!.confidenceScore).toBe("high"); }); it("originalAddress uses additionalInformation instead of addressLine2", () => { const result: AddressValidationResult = { isValid: true, validationValue: "valid", reasons: [], addressType: "unknown", changedAttributes: [], originalAddress: { addressLine1: "10 Downing Street", additionalInformation: "Flat 1", city: "London", postalCode: "SW1A 2AA", country: "GB", }, }; expect(result.originalAddress.additionalInformation).toBe("Flat 1"); type OrigKeys = keyof AddressValidationResult["originalAddress"]; expectTypeOf<"state" extends OrigKeys ? true : false>().toEqualTypeOf(); expectTypeOf<"addressLine2" extends OrigKeys ? true : false>().toEqualTypeOf(); }); });