import { ClaimRequestManagementService } from "./claim-request-management.service"; function setup(accessories: unknown) { const service = Object.create(ClaimRequestManagementService.prototype) as any; service.logger = { warn: jest.fn(), log: jest.fn() }; service.getFanavaranLookupRows = jest.fn().mockImplementation( async (_client: string, name: string) => ({ "vehicle-hull-dmg-kind": [{ Id: 8001, Caption: "تخريب", IsActive: 1 }], "vehicle-hull-dmg-cost-kinds": [ { Id: 8002, Caption: "مجموع لوازم", IsActive: 1 }, ], "car-components": [{ Id: 9 }, { Id: 13 }, { Id: 112 }], })[name] ?? [], ); service.fanavaranLookupService = { bodyPolicyById: jest.fn().mockResolvedValue({ VehicleValue: 15000000000 }), vehicleHullDmgAccessoriesByPolicyId: jest .fn() .mockResolvedValue(accessories), }; const build = (policyId = 123) => service.buildCarBodyFanavaranExpertisePayload({ clientKey: "parsian", claimCase: { fanavaranSync: { baseClaim: { policyId } } }, blame: { parties: [] }, profile: {}, parts: [ { partId: 9, partName: "سپر جلو", price: 50000000, salary: 25000000 }, { partId: 13, partName: "چراغ جلو چپ", price: 0, salary: 2000000 }, ], claimExpertId: 4044, submittedAt: new Date("2026-11-11T09:09:00Z"), priceDropTotal: 0, priceDropCarPrice: null, warnings: [], infoWarnings: [], }); return { service, build }; } describe("hull expertise policy accessory mapping", () => { it("uses the current policy accessory IDs for the bumper and headlamp", async () => { const { service, build } = setup([ { Id: 900001, AccessoryKindId: 9, AccessoryDesc: "سپر جلو" }, { Id: 900002, AccessoryKindId: 13, AccessoryDesc: "چراغ جلو چپ" }, ]); const { payload, warnings } = await build(); expect( payload.DmgSections.map((row: any) => row.VehicleHullAccessoryId), ).toEqual([900001, 900002]); expect(warnings).toEqual([]); expect(payload.DmgSections[0]).toMatchObject({ DmgKindId: 8001, DmgSectionCosts: [ { DmgCostKindId: 8002, Caption: expect.any(String), Amount: expect.any(Number), }, ], }); expect( service.fanavaranLookupService.vehicleHullDmgAccessoriesByPolicyId, ).toHaveBeenCalledWith( "parsian", 123, expect.objectContaining({ contractIdOverride: expect.any(String) }), ); }); it("maps unmatched parts onto the policy factory-bundle VehicleHullAccessoryId", async () => { const { build } = setup([ { Id: 3061139, AccessoryKindId: 112, AccessoryDesc: "کليه قطعات فابريک", }, ]); const { payload, warnings, infoWarnings } = await build(); expect( payload.DmgSections.map((row: any) => row.VehicleHullAccessoryId), ).toEqual([3061139, 3061139]); expect(payload.DmgSections.map((row: any) => row.AccessoryKindId)).toEqual([ 112, 112, ]); expect(warnings).toEqual([]); expect(infoWarnings).toEqual( expect.arrayContaining([ expect.stringContaining("mapped to factory bundle VehicleHullAccessoryId=3061139 AccessoryKindId=112"), ]), ); }); it("does not replace unavailable lookup values with 5485 or 1", async () => { const { service, build } = setup([ { Id: 2810613, AccessoryKindId: 9, AccessoryDesc: "سپر جلو" }, ]); service.getFanavaranLookupRows.mockResolvedValue([]); const { payload, warnings } = await build(); expect(payload.DmgSections[0]).toMatchObject({ DmgKindId: null }); expect(payload.DmgSections[0].DmgSectionCosts[0].DmgCostKindId).toBeNull(); expect(warnings).toEqual( expect.arrayContaining([ expect.stringContaining("DmgKindId"), expect.stringContaining("DmgCostKindId"), ]), ); }); it("reports unavailable lookup data without reusing the old hardcoded ID", async () => { const { service, build } = setup([]); service.fanavaranLookupService.vehicleHullDmgAccessoriesByPolicyId.mockRejectedValue( new Error("lookup unavailable"), ); const { payload, warnings } = await build(); expect( payload.DmgSections.map((row: any) => row.VehicleHullAccessoryId), ).toEqual([null, null]); expect(warnings).toEqual( expect.arrayContaining([ expect.stringContaining("dmg-accessories lookup failed"), expect.stringContaining("Leaving VehicleHullAccessoryId null"), ]), ); }); it("reports a missing hull PolicyId instead of using the claim ID as a policy", async () => { const { service, build } = setup([]); const { warnings } = await build(null); expect( service.fanavaranLookupService.vehicleHullDmgAccessoriesByPolicyId, ).not.toHaveBeenCalled(); expect(warnings).toEqual( expect.arrayContaining([expect.stringContaining("missing PolicyId")]), ); }); it.each([ { rows: [] }, { rows: [{ Id: 900004, AccessoryKindId: 99, AccessoryDesc: "unrelated" }] }, ])( "reports unmapped accessories instead of guessing an ID: %j", async ({ rows }) => { const { build } = setup(rows); const { payload, warnings } = await build(); expect( payload.DmgSections.map((row: any) => row.VehicleHullAccessoryId), ).toEqual([null, null]); expect(warnings).toEqual( expect.arrayContaining([ expect.stringMatching( /VehicleHullAccessoryId|Leaving VehicleHullAccessoryId null/, ), ]), ); }, ); it("matches by part caption and keeps AccessoryKindId from that policy row", async () => { const { build } = setup([ { Id: 900010, AccessoryKindId: 44, AccessoryDesc: "سپر جلو", }, ]); const { payload, warnings } = await build(); expect(payload.DmgSections[0]).toMatchObject({ VehicleHullAccessoryId: 900010, AccessoryKindId: 44, }); expect(payload.DmgSections[1].VehicleHullAccessoryId).toBeNull(); expect(warnings.some((w) => w.includes("چراغ جلو چپ"))).toBe(true); }); });