Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/lib/engine.test.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-08-0912 * Last Modified : 2026-08-0913 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142import { describe, expect, it } from "vitest";43import {44 adjustComps,45 ageAdjustment,46 areaAdjustment,47 estimate,48 haversineM,49 timeFactor,50 weightedMedian,51 type CompInput,52 type MarketIndexPoint,53 type Subject,54} from "./engine";5556const NOW = "2026-08-08";5758const INDEX: MarketIndexPoint[] = [59 { month: "2024-01", idx: 0.8 },60 { month: "2025-01", idx: 0.9 },61 { month: "2026-01", idx: 1.0 },62];6364function comp(over: Partial<CompInput>): CompInput {65 return {66 id: "c1",67 date: "2026-02-15",68 amount: 400000,69 lat: 46.8,70 lng: -71.2,71 propertyType: "unifamilial",72 yearBuilt: 1990,73 floorArea: 120,74 street: "1 rue Test",75 city: "Québec",76 ...over,77 };78}7980const SUBJECT: Subject = {81 lat: 46.8,82 lng: -71.2,83 typeProp: "unifamilial",84 floorArea: 120,85 yearBuilt: 1990,86 modelEstimate: 410000,87 modelP10: 340000,88 modelP90: 480000,89};9091describe("haversineM", () => {92 it("est nul à distance nulle et ~111 km par degré de latitude", () => {93 expect(haversineM(46.8, -71.2, 46.8, -71.2)).toBe(0);94 expect(haversineM(46.0, -71.2, 47.0, -71.2)).toBeGreaterThan(110000);95 expect(haversineM(46.0, -71.2, 47.0, -71.2)).toBeLessThan(112000);96 });97});9899describe("timeFactor", () => {100 it("majore une vente ancienne selon l'indice", () => {101 expect(timeFactor("2024-01", INDEX)).toBeCloseTo(1.0 / 0.8, 5);102 expect(timeFactor("2026-03", INDEX)).toBe(1);103 });104 it("retourne 1 sans indice", () => {105 expect(timeFactor("2024-01", [])).toBe(1);106 });107});108109describe("areaAdjustment", () => {110 it("ajuste à 50 % du $/m² du comparable", () => {111 // comp 100 m² à 400 000 $ => 4 000 $/m² ; sujet 110 m² => +10 × 2 000 = +20 000112 expect(areaAdjustment(110, { floorArea: 100, amount: 400000 })).toBe(20000);113 expect(areaAdjustment(90, { floorArea: 100, amount: 400000 })).toBe(-20000);114 });115 it("borne à ±25 % du prix", () => {116 expect(areaAdjustment(300, { floorArea: 100, amount: 400000 })).toBe(100000);117 });118 it("retourne 0 si superficie manquante", () => {119 expect(areaAdjustment(null, { floorArea: 100, amount: 400000 })).toBe(0);120 expect(areaAdjustment(110, { floorArea: null, amount: 400000 })).toBe(0);121 });122});123124describe("ageAdjustment", () => {125 it("0,5 % par année d'écart, borné à ±10 %", () => {126 expect(ageAdjustment(2000, { yearBuilt: 1990, amount: 400000 })).toBeCloseTo(20000);127 expect(ageAdjustment(1900, { yearBuilt: 2020, amount: 400000 })).toBeCloseTo(-40000);128 });129});130131describe("weightedMedian", () => {132 it("respecte les poids", () => {133 expect(weightedMedian([100, 200, 300], [1, 1, 10])).toBe(300);134 expect(weightedMedian([100, 200, 300], [1, 1, 1])).toBe(200);135 });136});137138describe("adjustComps", () => {139 it("filtre par type et superficie, trie par poids", () => {140 const candidates = [141 comp({ id: "proche", lat: 46.801, floorArea: 118 }),142 comp({ id: "loin", lat: 46.9, floorArea: 118 }),143 comp({ id: "condo", propertyType: "condo" }),144 comp({ id: "trop-grand", floorArea: 400 }),145 comp({ id: "a", floorArea: 125 }),146 comp({ id: "b", floorArea: 115 }),147 comp({ id: "c", floorArea: 130 }),148 comp({ id: "d", floorArea: 110 }),149 ];150 const res = adjustComps(SUBJECT, candidates, INDEX, NOW);151 const ids = res.map((c) => c.id);152 expect(ids).not.toContain("condo");153 expect(ids).not.toContain("trop-grand");154 expect(res[0].id).toBe("proche"); // le plus proche pèse le plus155 });156157 it("relâche le filtre de type quand le marché est mince", () => {158 const candidates = [159 comp({ id: "x", propertyType: "indéterminé" }),160 comp({ id: "y", propertyType: "indéterminé" }),161 ];162 const res = adjustComps(SUBJECT, candidates, INDEX, NOW);163 expect(res.length).toBe(2);164 });165});166167describe("estimate", () => {168 const candidates = Array.from({ length: 8 }, (_, i) =>169 comp({170 id: `c${i}`,171 lat: 46.8 + i * 0.001,172 amount: 380000 + i * 10000,173 date: "2026-01-10",174 })175 );176177 it("combine modèle (65 %) et comparables (35 %)", () => {178 const r = estimate(SUBJECT, candidates, INDEX, NOW);179 expect(r.modelWeight).toBe(0.65);180 expect(r.modelEstimate).toBe(410000);181 expect(r.compsEstimate).not.toBeNull();182 const expected = 0.65 * 410000 + 0.35 * r.compsEstimate!;183 expect(Math.abs(r.estimate - expected)).toBeLessThanOrEqual(100);184 expect(r.low).toBeLessThan(r.estimate);185 expect(r.high).toBeGreaterThan(r.estimate);186 });187188 it("retombe sur le modèle seul sans comparables", () => {189 const r = estimate(SUBJECT, [], INDEX, NOW);190 expect(r.modelWeight).toBe(1);191 expect(r.estimate).toBe(410000);192 expect(r.nCompsUsed).toBe(0);193 });194195 it("fonctionne aux comparables seuls (sans modèle)", () => {196 const r = estimate({ ...SUBJECT, modelEstimate: null, modelP10: null, modelP90: null },197 candidates, INDEX, NOW);198 expect(r.modelEstimate).toBeNull();199 expect(r.estimate).toBeGreaterThan(300000);200 expect(r.confidenceLevel).toMatch(/[A-D]/);201 });202203 it("la confiance augmente avec le nombre de comparables", () => {204 const few = estimate({ ...SUBJECT, modelEstimate: null }, candidates.slice(0, 3), INDEX, NOW);205 const many = estimate({ ...SUBJECT, modelEstimate: null }, candidates, INDEX, NOW);206 expect(many.confidencePct).toBeGreaterThanOrEqual(few.confidencePct);207 });208209 it("jamais d'estimation sans niveau de confiance", () => {210 const r = estimate(SUBJECT, candidates, INDEX, NOW);211 expect(r.confidencePct).toBeGreaterThan(0);212 expect(["A", "B", "C", "D"]).toContain(r.confidenceLevel);213 });214});215