Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : Immo-Ka Android10 * File : app/src/test/kotlin/com/spboucher/immoka/MortgageTest.kt11 * Description : simulateur de financement — taux périodique, versement,12 * solde, test de résistance, mise de fonds minimale,13 * assurance prêt, droits de mutation (base, Montréal, autres14 * villes), scénario complet. Mêmes cas et mêmes chiffres que15 * l'app iOS.16 * Module : test17 * Repository : https://git.spboucher.ai/immo-ka-android18 * Project Website : https://www.immo-ka.com19 * Created : 2026-11-2920 * Last Modified : 2026-11-2921 *22 * =============================================================================23 * AUTHOR & PROFESSIONAL AFFILIATION24 * =============================================================================25 *26 * Author : Simon-Pierre Boucher27 * Position : Professor of Finance28 * Institution : Université du Québec en Outaouais (UQO)29 * Department : Département des sciences administratives30 * Organization : SP Boucher31 *32 * Website : https://spboucher.ai33 * Git : https://git.spboucher.ai34 *35 * =============================================================================36 * OFFICIAL CONTACT DIRECTORY37 * =============================================================================38 *39 * General : info@spboucher.ai40 * Professional : contact@spboucher.ai41 * Direct : simon@spboucher.ai42 * Research : research@spboucher.ai43 * Development : dev@spboucher.ai44 * Support : support@spboucher.ai45 * Security/Admin : admin@spboucher.ai46 * Notifications : notifications@spboucher.ai47 *48 * Preferred Contact: contact@spboucher.ai49 *50 * =============================================================================51 * COPYRIGHT & INTELLECTUAL PROPERTY52 * =============================================================================53 *54 * Copyright (c) 202655 * Simon-Pierre Boucher56 *57 * All rights reserved where applicable.58 *59 * This file is governed by the applicable project license,60 * copyright notices, institutional agreements and relevant law.61 *62 * Unless expressly authorized by the applicable license or63 * rights holder, copying, redistribution, sublicensing,64 * publication and commercial exploitation are not permitted.65 *66 * Existing third-party rights and notices remain unaffected.67 *68 * =============================================================================69 * CONFIDENTIALITY & SECURITY70 * =============================================================================71 *72 * Classification : Public73 * Distribution : Public repository; use is governed by the project license74 * Security Contact : admin@spboucher.ai75 *76 * Access to this source does not independently grant permission77 * to redistribute or disclose confidential materials.78 *79 * Do not embed credentials, private keys, access tokens,80 * certificates or confidential personal information.81 *82 * =============================================================================83 * LICENSE & MAINTENANCE84 * =============================================================================85 *86 * License : None specified (no license file in this repository)87 * Maintainer : Simon-Pierre Boucher88 * Technical Contact: dev@spboucher.ai89 *90 * =============================================================================91 * END OF SP BOUCHER SOURCE HEADER92 * =============================================================================93 */9495package com.spboucher.immoka9697import com.spboucher.immoka.core.finance.Mortgage98import com.spboucher.immoka.core.finance.Mortgage.Day99import org.junit.Assert.assertEquals100import org.junit.Assert.assertFalse101import org.junit.Assert.assertNotNull102import org.junit.Assert.assertTrue103import org.junit.Test104105class MortgageTest {106 private val d2026 = Day(2026, 11, 25)107108 @Test fun `taux periodique - capitalisation semestrielle (fixe), mensuelle (variable)`() {109 // 6 % nominal, capitalisé deux fois l'an : (1,03)^(1/6) − 1110 assertEquals(0.00493862, Mortgage.monthlyRate(6.0), 1e-8)111 assertEquals(0.005, Mortgage.monthlyRate(6.0, fixed = false), 1e-12)112 assertEquals(0.0, Mortgage.monthlyRate(0.0), 0.0)113 }114115 @Test fun `versement mensuel`() {116 // 400 000 $, 5 %, 25 ans, taux fixe canadien : 2 326,42 $ par mois117 assertEquals(2326.42, Mortgage.monthlyPayment(400_000.0, 5.0, 25), 0.01)118 assertEquals(1919.42, Mortgage.monthlyPayment(300_000.0, 6.0, 25), 0.01)119 assertEquals(1000.0, Mortgage.monthlyPayment(120_000.0, 0.0, 10), 1e-9)120 assertEquals(0.0, Mortgage.monthlyPayment(0.0, 5.0, 25), 0.0)121 assertTrue(Mortgage.monthlyPayment(400_000.0, 5.0, 25, fixed = false) > 2326.42)122 }123124 @Test fun `solde - de la totalite du pret a zero, toujours decroissant`() {125 val b = Mortgage.yearlyBalances(400_000.0, 5.0, 25)126 assertEquals(26, b.size); assertEquals(400_000.0, b[0], 0.0); assertTrue(b[25] < 0.01)127 assertTrue(b.zipWithNext().all { (x, y) -> x > y })128 assertEquals(354_500.0, b[5], 600.0)129 }130131 @Test fun `test de resistance - max(taux + 2, 5,25)`() {132 assertEquals(6.74, Mortgage.qualifyingRate(4.74), 1e-12)133 assertEquals(5.25, Mortgage.qualifyingRate(2.9), 0.0)134 assertEquals(5.25, Mortgage.qualifyingRate(3.25), 0.0)135 }136137 @Test fun `mise de fonds minimale`() {138 assertEquals(20_000.0, Mortgage.minDownPayment(400_000.0), 0.0)139 assertEquals(25_000.0, Mortgage.minDownPayment(500_000.0), 0.0)140 assertEquals(45_000.0, Mortgage.minDownPayment(700_000.0), 0.0)141 assertEquals(124_999.9, Mortgage.minDownPayment(1_499_999.0), 1e-6)142 assertEquals(300_000.0, Mortgage.minDownPayment(1_500_000.0), 0.0)143 assertEquals(0.0, Mortgage.minDownPayment(0.0), 0.0)144 }145146 @Test fun `assurance pret - bareme, surprime de 30 ans, taxe du Quebec`() {147 val none = Mortgage.insurance(500_000.0, 100_000.0, 25, d2026)148 assertTrue(!none.required && none.eligible && none.premium == 0.0 && none.tax == 0.0)149 // 5 % : rapport de 95 %, prime de 4,00 % du prêt ; taxe de 9 % en 2026150 val five = Mortgage.insurance(400_000.0, 20_000.0, 25, d2026)151 assertTrue(five.required && five.eligible); assertEquals(0.04, five.premiumRate, 0.0)152 assertEquals(15_200.0, five.premium, 0.0); assertEquals(1_368.0, five.tax, 0.0)153 assertEquals(0.031, Mortgage.insurance(400_000.0, 40_000.0, 25, d2026).premiumRate, 0.0)154 assertEquals(0.028, Mortgage.insurance(400_000.0, 60_000.0, 25, d2026).premiumRate, 0.0)155 val thirty = Mortgage.insurance(400_000.0, 40_000.0, 30, d2026)156 assertEquals(0.033, thirty.premiumRate, 1e-12); assertTrue(thirty.eligible); assertEquals(Mortgage.InsuranceIssue.THIRTY_YEARS, thirty.issue)157 // la taxe passe à 9,975 % pour les primes payées après le 31 décembre 2026158 assertEquals(0.09, Mortgage.premiumTaxRate(Day(2026, 12, 31)), 0.0)159 assertEquals(0.09975, Mortgage.premiumTaxRate(Day(2027, 1, 1)), 0.0)160 assertEquals(1_516.2, Mortgage.insurance(400_000.0, 20_000.0, 25, Day(2027, 3, 1)).tax, 0.0)161 // sous le minimum légal, ou à 1,5 M$ et plus : non assurable, signalé162 val low = Mortgage.insurance(700_000.0, 40_000.0, 25, d2026)163 assertTrue(low.required && !low.eligible && low.premium == 0.0); assertEquals(Mortgage.InsuranceIssue.DOWN_TOO_LOW, low.issue)164 val big = Mortgage.insurance(1_600_000.0, 200_000.0, 25, d2026)165 assertTrue(big.required && !big.eligible); assertEquals(Mortgage.InsuranceIssue.PRICE_TOO_HIGH, big.issue)166 }167168 @Test fun `droits de mutation - bareme de base 2026`() {169 // base de 350 000 $ → 3 360,50 $ (62 900 × 0,5 % + 252 100 × 1 % + 35 000 × 1,5 %)170 val t = Mortgage.transferTax(350_000.0, "Rimouski-Est", d2026)171 assertEquals(3_360.50, t.total, 0.005); assertEquals(3, t.slices.size); assertFalse(t.municipalScale)172 assertEquals(300.0, Mortgage.transferTax(60_000.0, "", d2026).total, 0.0)173 assertEquals(0.0, Mortgage.transferTax(4_999.0, "", d2026).total, 0.0)174 assertEquals(314.5 + 2_521 + 685_000 * 0.015, Mortgage.transferTax(1_000_000.0, "Saint-Élie-de-Caxton", d2026).total, 0.005)175 }176177 @Test fun `droits de mutation - Montreal, arrondissements compris`() {178 val t = Mortgage.transferTax(700_000.0, "Montréal", d2026)179 assertEquals(9_349.00, t.total, 0.005); assertTrue(t.municipalScale); assertEquals(4, t.slices.size)180 assertEquals(t.total, Mortgage.transferTax(700_000.0, "Montréal (Rosemont/La Petite-Patrie)", d2026).total, 0.0)181 assertEquals("montreal-est", Mortgage.municipalityKey("Montréal-Est")); assertEquals("montreal", Mortgage.municipalityKey("Montréal (Verdun)"))182 val all = Mortgage.transferTax(4_000_000.0, "Montréal", d2026)183 val expected = 314.5 + 2_521 + (552_300 - 315_000) * 0.015 + (1_104_700 - 552_300) * 0.02 + (2_136_500 - 1_104_700) * 0.025 +184 (3_113_000 - 2_136_500) * 0.035 + (4_000_000 - 3_113_000) * 0.04185 assertEquals(expected, all.total, 0.01); assertEquals(7, all.slices.size)186 }187188 @Test fun `droits de mutation - autres villes et Gatineau selon la date`() {189 val base = 314.5 + 2_521.0 + 185_000 * 0.015190 assertEquals(base + 300_000 * 0.03, Mortgage.transferTax(800_000.0, "Laval", d2026).total, 0.005)191 assertEquals(base + 300_000 * 0.03, Mortgage.transferTax(800_000.0, "Sherbrooke", d2026).total, 0.005)192 assertEquals(base + 250_000 * 0.025 + 50_000 * 0.03, Mortgage.transferTax(800_000.0, "Québec", d2026).total, 0.005)193 assertEquals(314.5 + 2_521 + 315_100 * 0.015 + 169_900 * 0.03, Mortgage.transferTax(800_000.0, "Longueuil", d2026).total, 0.005)194 // Gatineau : barème modifié le 25 février 2026195 val before = Mortgage.transferTax(800_000.0, "Gatineau", Day(2026, 2, 1))196 val after = Mortgage.transferTax(800_000.0, "Gatineau", d2026)197 assertEquals(314.5 + 2_521 + 435_000 * 0.015 + 50_000 * 0.025, before.total, 0.005)198 assertEquals(base + 300_000 * 0.03, after.total, 0.005); assertTrue(after.total > before.total)199 assertEquals(Mortgage.transferTax(450_000.0, "Amos", d2026).total, Mortgage.transferTax(450_000.0, "Laval", d2026).total, 0.0)200 }201202 @Test fun `scenario complet - la fiche de Gatineau a 999 900`() {203 val r = Mortgage.compute(Mortgage.Scenario(999_900.0, 199_980.0, 4.74, 25, city = "Gatineau"), d2026)204 assertEquals(799_920.0, r.loan, 0.0); assertFalse(r.insurance.required); assertEquals(r.loan, r.principal, 0.0)205 assertEquals(Mortgage.monthlyPayment(799_920.0, 4.74, 25), r.monthly, 1e-9); assertTrue(r.monthly > 4_500 && r.monthly < 4_600)206 val tax = 314.5 + 2_521 + 185_000 * 0.015 + 499_900 * 0.03207 assertEquals(tax, r.transferTax.total, 0.005); assertEquals(199_980 + r.transferTax.total, r.cashAtClosing, 0.005)208 assertEquals(6.74, r.qualifyingRate, 1e-12); assertTrue(r.qualifyingMonthly > r.monthly)209 assertEquals(r.monthly * 300 - 799_920, r.totalInterest, 1e-6)210 // 10 % de mise de fonds : prime ajoutée au prêt, taxe à la clôture211 val ins = Mortgage.compute(Mortgage.Scenario(500_000.0, 50_000.0, 5.0, 25, city = "Amos"), d2026)212 assertEquals(13_950.0, ins.insurance.premium, 0.0); assertEquals(463_950.0, ins.principal, 0.0); assertEquals(1_255.5, ins.insurance.tax, 0.0)213 assertEquals(50_000 + ins.transferTax.total + 1_255.5, ins.cashAtClosing, 0.005)214 assertNotNull(ins.minDown)215 }216}217