import { BadRequestException } from "@nestjs/common"; import { selectLatestActiveFanavaranPolicy } from "./fanavaran-policy-selection"; describe("selectLatestActiveFanavaranPolicy", () => { const today = "2026-06-30"; it("selects the policy with the latest EndDate when newest is first", () => { const selected = selectLatestActiveFanavaranPolicy( [ { PolicyId: 4826286, EndDate: "1405/09/23" }, { PolicyId: 3719458, EndDate: "1404/09/06" }, { PolicyId: 2800731, EndDate: "1403/09/05" }, ], today, ); expect(selected.policyId).toBe(4826286); expect(selected.endDate).toBe("1405/09/23"); }); it("selects the policy with the latest EndDate when newest is last", () => { const selected = selectLatestActiveFanavaranPolicy( [ { PolicyId: 2800731, EndDate: "1403/09/05" }, { PolicyId: 3719458, EndDate: "1404/09/06" }, { PolicyId: 4826286, EndDate: "1405/09/23" }, ], today, ); expect(selected.policyId).toBe(4826286); }); it("rejects when no policies are returned", () => { expect(() => selectLatestActiveFanavaranPolicy([], today)).toThrow( BadRequestException, ); }); it("rejects when the latest policy is expired", () => { expect(() => selectLatestActiveFanavaranPolicy( [ { PolicyId: 3719458, EndDate: "1404/09/06" }, { PolicyId: 2800731, EndDate: "1403/09/05" }, ], today, ), ).toThrow(BadRequestException); }); it("rejects when the latest policy has no valid PolicyId", () => { expect(() => selectLatestActiveFanavaranPolicy( [ { PolicyId: 3719458, EndDate: "1404/09/06" }, { PolicyId: null, EndDate: "1405/09/23" }, ], today, ), ).toThrow(BadRequestException); }); });