Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : Immo-Ka iOS9// File : ImmoKaTests/MortgageTests.swift10// Description : calculs de financement — versement, mise de fonds,11// assurance prêt, droits de mutation — vérifiés contre les12// exemples officiels et les barèmes 2026 (sources dans13// Mortgage.swift).14// Module : ImmoKaTests15// Repository : https://git.spboucher.ai/immo-ka-ios-v316// Project Website : https://www.immo-ka.com17// Created : 2026-11-2518// Last Modified : 2026-11-2519//20// =============================================================================21// AUTHOR & PROFESSIONAL AFFILIATION22// =============================================================================23//24// Author : Simon-Pierre Boucher25// Position : Professor of Finance26// Institution : Université du Québec en Outaouais (UQO)27// Department : Département des sciences administratives28// Organization : SP Boucher29//30// Website : https://spboucher.ai31// Git : https://git.spboucher.ai32//33// =============================================================================34// OFFICIAL CONTACT DIRECTORY35// =============================================================================36//37// General : info@spboucher.ai38// Professional : contact@spboucher.ai39// Direct : simon@spboucher.ai40// Research : research@spboucher.ai41// Development : dev@spboucher.ai42// Support : support@spboucher.ai43// Security/Admin : admin@spboucher.ai44// Notifications : notifications@spboucher.ai45//46// Preferred Contact: support@spboucher.ai47//48// =============================================================================49// COPYRIGHT & INTELLECTUAL PROPERTY50// =============================================================================51//52// Copyright (c) 202653// Simon-Pierre Boucher54//55// All rights reserved where applicable.56//57// This file is governed by the applicable project license,58// copyright notices, institutional agreements and relevant law.59//60// Unless expressly authorized by the applicable license or61// rights holder, copying, redistribution, sublicensing,62// publication and commercial exploitation are not permitted.63//64// Existing third-party rights and notices remain unaffected.65//66// =============================================================================67// CONFIDENTIALITY & SECURITY68// =============================================================================69//70// Classification : Public71// Distribution : Public repository; use is governed by the project license72// Security Contact : admin@spboucher.ai73//74// Access to this source does not independently grant permission75// to redistribute or disclose confidential materials.76//77// Do not embed credentials, private keys, access tokens,78// certificates or confidential personal information.79//80// =============================================================================81// LICENSE & MAINTENANCE82// =============================================================================83//84// License : None specified (no license file in this repository)85// Maintainer : Simon-Pierre Boucher86// Technical Contact: dev@spboucher.ai87//88// =============================================================================89// END OF SP BOUCHER SOURCE HEADER90// =============================================================================9192import Foundation93import Testing94@testable import ImmoKa9596@Suite("Financement")97struct MortgageTests {98 private func day(_ y: Int, _ m: Int, _ d: Int) -> Date {99 var c = DateComponents(); c.year = y; c.month = m; c.day = d100 return Calendar(identifier: .gregorian).date(from: c)!101 }102103 @Test("taux périodique : capitalisation semestrielle (fixe), mensuelle (variable)")104 func periodicRate() {105 // 6 % nominal, capitalisé deux fois l'an : (1,03)^(1/6) − 1106 #expect(abs(Mortgage.monthlyRate(annualPct: 6) - 0.00493862) < 1e-8)107 #expect(abs(Mortgage.monthlyRate(annualPct: 6, fixed: false) - 0.005) < 1e-12)108 #expect(Mortgage.monthlyRate(annualPct: 0) == 0)109 }110111 @Test("versement mensuel")112 func payment() {113 // 400 000 $, 5 %, 25 ans, taux fixe canadien : 2 326,42 $ par mois (valeur de référence des calculateurs bancaires)114 #expect(abs(Mortgage.monthlyPayment(principal: 400_000, annualPct: 5, years: 25) - 2326.42) < 0.01)115 // 300 000 $, 6 %, 25 ans : 1 919,42 $116 #expect(abs(Mortgage.monthlyPayment(principal: 300_000, annualPct: 6, years: 25) - 1919.42) < 0.01)117 // taux nul : simple division118 #expect(abs(Mortgage.monthlyPayment(principal: 120_000, annualPct: 0, years: 10) - 1000) < 1e-9)119 #expect(Mortgage.monthlyPayment(principal: 0, annualPct: 5, years: 25) == 0)120 // capitalisation mensuelle (variable) : versement un peu plus élevé au même taux nominal121 #expect(Mortgage.monthlyPayment(principal: 400_000, annualPct: 5, years: 25, fixed: false) > 2326.42)122 }123124 @Test("solde : de la totalité du prêt à zéro, toujours décroissant")125 func balance() {126 let b = Mortgage.yearlyBalances(principal: 400_000, annualPct: 5, years: 25)127 #expect(b.count == 26 && b[0] == 400_000 && b[25] < 0.01)128 #expect(zip(b, b.dropFirst()).allSatisfy { $0 > $1 })129 // après 5 ans (fin d'un terme de 60 mois), il reste environ 354 500 $130 #expect(abs(b[5] - 354_500) < 600)131 }132133 @Test("test de résistance : max(taux + 2, 5,25 %)")134 func qualifying() {135 #expect(Mortgage.qualifyingRate(4.74) == 6.74)136 #expect(Mortgage.qualifyingRate(2.9) == 5.25)137 #expect(Mortgage.qualifyingRate(3.25) == 5.25)138 }139140 @Test("mise de fonds minimale")141 func minDown() {142 #expect(Mortgage.minDownPayment(price: 400_000) == 20_000) // 5 %143 #expect(Mortgage.minDownPayment(price: 500_000) == 25_000)144 #expect(Mortgage.minDownPayment(price: 700_000) == 45_000) // 25 000 + 10 % de 200 000145 #expect(abs(Mortgage.minDownPayment(price: 1_499_999) - 124_999.9) < 1e-6)146 #expect(Mortgage.minDownPayment(price: 1_500_000) == 300_000) // 20 %147 #expect(Mortgage.minDownPayment(price: 0) == 0)148 }149150 @Test("assurance prêt : barème par rapport prêt-valeur, surprime de 30 ans, taxe du Québec")151 func insurance() {152 let d2026 = day(2026, 11, 25)153 // 20 % de mise de fonds : pas d'assurance154 let none = Mortgage.insurance(price: 500_000, downPayment: 100_000, years: 25, date: d2026)155 #expect(!none.required && none.eligible && none.premium == 0 && none.tax == 0)156 // 5 % : rapport de 95 %, prime de 4,00 % du prêt ; taxe de 9 % en 2026157 let five = Mortgage.insurance(price: 400_000, downPayment: 20_000, years: 25, date: d2026)158 #expect(five.required && five.eligible && five.premiumRate == 0.04)159 #expect(five.premium == 15_200 && five.tax == 1_368)160 // 10 % : 3,10 % ; 15 % : 2,80 %161 #expect(Mortgage.insurance(price: 400_000, downPayment: 40_000, years: 25, date: d2026).premiumRate == 0.031)162 #expect(Mortgage.insurance(price: 400_000, downPayment: 60_000, years: 25, date: d2026).premiumRate == 0.028)163 // 30 ans : surprime de 0,20 point164 let thirty = Mortgage.insurance(price: 400_000, downPayment: 40_000, years: 30, date: d2026)165 #expect(abs(thirty.premiumRate - 0.033) < 1e-12 && thirty.eligible && thirty.issue != nil)166 // la taxe passe à 9,975 % pour les primes payées après le 31 décembre 2026167 #expect(Mortgage.premiumTaxRate(on: day(2026, 12, 31)) == 0.09)168 #expect(Mortgage.premiumTaxRate(on: day(2027, 1, 1)) == 0.09975)169 #expect(Mortgage.insurance(price: 400_000, downPayment: 20_000, years: 25, date: day(2027, 3, 1)).tax == 1_516.2)170 // sous le minimum légal, ou à 1,5 M$ et plus : non assurable, signalé171 let low = Mortgage.insurance(price: 700_000, downPayment: 40_000, years: 25, date: d2026)172 #expect(low.required && !low.eligible && low.premium == 0 && low.issue != nil)173 let big = Mortgage.insurance(price: 1_600_000, downPayment: 200_000, years: 25, date: d2026)174 #expect(big.required && !big.eligible)175 }176177 @Test("droits de mutation : barème de base 2026 (exemple officiel de Québec.ca)")178 func transferBase() {179 // base de 350 000 $ → 3 360,50 $ (62 900 × 0,5 % + 252 100 × 1 % + 35 000 × 1,5 %)180 let t = Mortgage.transferTax(base: 350_000, city: "Rimouski-Est")181 #expect(abs(t.total - 3_360.50) < 0.005 && t.slices.count == 3 && !t.municipalScale)182 #expect(Mortgage.transferTax(base: 60_000, city: "").total == 300)183 #expect(Mortgage.transferTax(base: 4_999, city: "").total == 0) // exonération184 // sans barème municipal propre : 1,5 % sans plafond185 let plain: Double = 314.5 + 2_521 + 685_000 * 0.015186 #expect(abs(Mortgage.transferTax(base: 1_000_000, city: "Saint-Élie-de-Caxton").total - plain) < 0.005)187 }188189 @Test("droits de mutation : Montréal (exemple officiel de la Ville), arrondissements compris")190 func transferMontreal() {191 // 700 000 $ → 9 349,00 $192 let t = Mortgage.transferTax(base: 700_000, city: "Montréal")193 #expect(abs(t.total - 9_349.00) < 0.005 && t.municipalScale && t.slices.count == 4)194 #expect(Mortgage.transferTax(base: 700_000, city: "Montréal (Rosemont/La Petite-Patrie)").total == t.total)195 #expect(Mortgage.municipalityKey("Montréal-Est") == "montreal-est" && Mortgage.municipalityKey("Montréal (Verdun)") == "montreal")196 // toutes les tranches : 4 M$197 let all = Mortgage.transferTax(base: 4_000_000, city: "Montréal")198 var expected: Double = 314.5 + 2_521199 expected += Double(552_300 - 315_000) * 0.015200 expected += Double(1_104_700 - 552_300) * 0.02201 expected += Double(2_136_500 - 1_104_700) * 0.025202 expected += Double(3_113_000 - 2_136_500) * 0.035203 expected += Double(4_000_000 - 3_113_000) * 0.04204 #expect(abs(all.total - expected) < 0.01 && all.slices.count == 7)205 }206207 @Test("droits de mutation : autres villes à barème propre, et Gatineau selon la date")208 func transferCities() {209 let base: Double = 314.5 + 2_521.0 + 185_000 * 0.015210 // 3 % au-delà de 500 000 $211 #expect(abs(Mortgage.transferTax(base: 800_000, city: "Laval").total - (base + 300_000 * 0.03)) < 0.005)212 #expect(abs(Mortgage.transferTax(base: 800_000, city: "Sherbrooke").total - (base + 300_000 * 0.03)) < 0.005)213 // Québec : 2,5 % de 500 000 à 750 000, puis 3 %214 let quebec: Double = base + 250_000 * 0.025 + 50_000 * 0.03215 #expect(abs(Mortgage.transferTax(base: 800_000, city: "Québec").total - quebec) < 0.005)216 // Longueuil : 1,5 % jusqu'à 630 100 $217 let longueuil: Double = 314.5 + 2_521 + 315_100 * 0.015 + 169_900 * 0.03218 #expect(abs(Mortgage.transferTax(base: 800_000, city: "Longueuil").total - longueuil) < 0.005)219 // Gatineau : barème modifié le 25 février 2026220 let before = Mortgage.transferTax(base: 800_000, city: "Gatineau", date: day(2026, 2, 1))221 let after = Mortgage.transferTax(base: 800_000, city: "Gatineau", date: day(2026, 11, 25))222 let gatineauBefore: Double = 314.5 + 2_521 + 435_000 * 0.015 + 50_000 * 0.025223 #expect(abs(before.total - gatineauBefore) < 0.005)224 #expect(abs(after.total - (base + 300_000 * 0.03)) < 0.005 && after.total > before.total)225 // sous 500 000 $, toutes les villes paient le barème de base226 #expect(Mortgage.transferTax(base: 450_000, city: "Laval").total == Mortgage.transferTax(base: 450_000, city: "Amos").total)227 }228229 @Test("scénario complet : la fiche de Gatineau à 999 900 $")230 func scenario() {231 let date = day(2026, 11, 25)232 let r = Mortgage.compute(.init(price: 999_900, downPayment: 199_980, annualPct: 4.74, years: 25, city: "Gatineau"), date: date)233 #expect(r.loan == 799_920 && !r.insurance.required && r.principal == r.loan)234 #expect(abs(r.monthly - Mortgage.monthlyPayment(principal: 799_920, annualPct: 4.74, years: 25)) < 1e-9)235 #expect(r.monthly > 4_500 && r.monthly < 4_600)236 let tax: Double = 314.5 + 2_521 + 185_000 * 0.015 + 499_900 * 0.03237 #expect(abs(r.transferTax.total - tax) < 0.005)238 #expect(abs(r.cashAtClosing - (199_980 + r.transferTax.total)) < 0.005)239 #expect(r.qualifyingRate == 6.74 && r.qualifyingMonthly > r.monthly)240 #expect(abs(r.totalInterest - (r.monthly * 300 - 799_920)) < 1e-6)241 // 10 % de mise de fonds : prime ajoutée au prêt, taxe à la clôture242 let ins = Mortgage.compute(.init(price: 500_000, downPayment: 50_000, annualPct: 5, years: 25, city: "Amos"), date: date)243 #expect(ins.insurance.premium == 13_950 && ins.principal == 463_950 && ins.insurance.tax == 1_255.5)244 #expect(abs(ins.cashAtClosing - (50_000 + ins.transferTax.total + 1_255.5)) < 0.005)245 }246}247