import { HttpClient } from '@angular/common/http'; import { TestBed } from '@angular/core/testing'; import { of } from 'rxjs'; import { vi } from 'vitest'; import { LeaseFinalizationService } from './lease-finalization.service'; describe('LeaseFinalizationService', () => { const http = { get: vi.fn().mockReturnValue(of({})), post: vi.fn().mockReturnValue(of({})), }; beforeEach(() => { vi.clearAllMocks(); TestBed.configureTestingModule({ providers: [LeaseFinalizationService, { provide: HttpClient, useValue: http }], }); }); it('uses the finalization state endpoint', () => { TestBed.inject(LeaseFinalizationService).getState('lease-1').subscribe(); expect(http.get).toHaveBeenCalledWith('/api/leases/lease-1/finalization'); }); it('uploads an attested guarantee deed for one guarantor and tenant pair', () => { const file = new File(['deed'], 'cautionnement.pdf', { type: 'application/pdf' }); TestBed.inject(LeaseFinalizationService) .uploadGuaranteeDeed('lease-1', file, 'guarantor-1', 'tenant-1') .subscribe(); const formData = http.post.mock.calls[0][1] as FormData; expect(http.post.mock.calls[0][0]).toBe('/api/leases/lease-1/guarantee-deeds'); expect(formData.get('file')).toBe(file); expect(formData.get('guarantorId')).toBe('guarantor-1'); expect(formData.get('tenantId')).toBe('tenant-1'); expect(formData.get('attestation')).toBe('true'); }); it('sends the signed PDF and a native JSON metadata part', async () => { const file = new File(['lease'], 'bail-signe.pdf', { type: 'application/pdf' }); TestBed.inject(LeaseFinalizationService) .finalize('lease-1', { signedLease: file, revisionNumber: 4, lastSignatureDate: '2026-07-19', allPartiesSigned: true, revisionMatches: true, confirmedWarningCodes: ['DPE_OVERRIDE', 'RENT_OVERRIDE'], entryInventoryDiscrepancies: true, inventoryCorrections: [ { id: 'correction-1', inventoryLineId: null, privateAreaId: 'area-1', designation: 'Chaise', quantity: 0, description: 'Absente', }, ], allInventoryCorrectionsInitialed: true, }) .subscribe(); const formData = http.post.mock.calls[0][1] as FormData; expect(http.post.mock.calls[0][0]).toBe('/api/leases/lease-1/finalization'); expect(formData.get('signedLease')).toBe(file); const metadata = formData.get('metadata'); expect(metadata).toBeInstanceOf(Blob); await expect((metadata as Blob).text()).resolves.toBe( JSON.stringify({ revisionNumber: 4, lastSignatureDate: '2026-07-19', allPartiesSigned: true, revisionMatches: true, confirmedWarningCodes: ['DPE_OVERRIDE', 'RENT_OVERRIDE'], entryInventoryDiscrepancies: true, inventoryCorrections: [ { id: 'correction-1', inventoryLineId: null, privateAreaId: 'area-1', designation: 'Chaise', quantity: 0, description: 'Absente', }, ], allInventoryCorrectionsInitialed: true, }), ); }); });