import { BadRequestException } from "@nestjs/common"; import { selectLatestActiveFanavaranPolicy, selectFanavaranPolicyForAccident, } 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); }); }); describe("car-body accident policy selection", () => { const policies = [ { PolicyId: 2, BeginDate: "1401/01/02", EndDate: "1402/01/02" }, { PolicyId: 1, BeginDate: "1400/01/01", EndDate: "1401/01/01" }, ]; it("selects the previous renewal covering the accident, even when both expired today", () => { expect( selectFanavaranPolicyForAccident(policies, "1400/12/28").policyId, ).toBe(1); }); it.each(["1400/01/01", "1401/01/01"])( "includes the coverage boundary %s", (date) => { expect(selectFanavaranPolicyForAccident(policies, date).policyId).toBe(1); }, ); it.each(["1399/12/29", "1402/01/03", "invalid", undefined])( "rejects an uncovered or missing accident date %s", (date) => { expect(() => selectFanavaranPolicyForAccident(policies, date)).toThrow( BadRequestException, ); }, ); });