Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * PROJECT METADATA8 * -----------------------------------------------------------------------------9 * Project : Vrai-Prix Android10 * File : app/src/test/kotlin/com/spboucher/vraiprix/LibraryTest.kt11 * Description : bibliothèque locale : favoris, comparateur limité à huit,12 * récents, recherches, persistance sur disque, fichier13 * illisible, effacement.14 * Module : test15 * Repository : https://git.spboucher.ai/vrai-prix-android16 * Project Website : https://www.vrai-prix.com17 * Created : 2026-11-2918 * Last Modified : 2026-11-2919 *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: dev@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 * =============================================================================91 */9293package com.spboucher.vraiprix9495import com.spboucher.vraiprix.core.store.CompareResult96import com.spboucher.vraiprix.core.store.Library97import com.spboucher.vraiprix.core.store.SavedKind98import com.spboucher.vraiprix.core.store.SavedProperty99import com.spboucher.vraiprix.core.store.SearchRecord100import org.junit.Assert.assertEquals101import org.junit.Assert.assertFalse102import org.junit.Assert.assertTrue103import org.junit.Rule104import org.junit.Test105import org.junit.rules.TemporaryFolder106import java.io.File107108class LibraryTest {109 @get:Rule val tmp = TemporaryFolder()110111 private fun p(n: Int, value: Double? = 400_000.0) =112 SavedProperty(SavedKind.unit, "id$n", "Propriété $n", value = value, low = value?.minus(40_000), high = value?.plus(40_000), regime = "valeur")113114 @Test fun favoritesToggle() {115 val lib = Library(null)116 assertTrue(lib.toggleFavorite(p(1)))117 assertTrue(lib.isFavorite("unit:id1"))118 assertTrue(lib.toggleFavorite(p(2)))119 // le plus récent en tête120 assertEquals(listOf("id2", "id1"), lib.state.value.favorites.map { it.ref })121 assertFalse(lib.toggleFavorite(p(1)))122 assertFalse(lib.isFavorite("unit:id1"))123 lib.removeFavorite("unit:id2")124 assertTrue(lib.state.value.favorites.isEmpty())125 }126127 @Test fun comparatorIsLimitedToEight() {128 val lib = Library(null)129 for (i in 1..8) assertEquals(CompareResult.ADDED, lib.toggleCompare(p(i)))130 assertEquals(CompareResult.FULL, lib.toggleCompare(p(9)))131 assertEquals(8, lib.state.value.compare.size)132 assertEquals(CompareResult.REMOVED, lib.toggleCompare(p(3)))133 assertEquals(CompareResult.ADDED, lib.toggleCompare(p(9)))134 lib.clearCompare()135 assertTrue(lib.state.value.compare.isEmpty())136 }137138 @Test fun recentsAreCappedAndRefreshSavedCopies() {139 var clock = 1000L140 val lib = Library(null) { clock++ }141 lib.toggleFavorite(p(1, 400_000.0))142 lib.toggleCompare(p(1, 400_000.0))143 val savedAt = lib.state.value.favorites.first().savedAt144 for (i in 1..30) lib.visit(p(i))145 assertEquals(Library.RECENT_LIMIT, lib.state.value.recents.size)146 assertEquals("id30", lib.state.value.recents.first().ref)147 // revoir une fiche la remonte sans doublon et met à jour le favori (sa date d'ajout est gardée)148 lib.visit(p(1, 455_000.0))149 assertEquals("id1", lib.state.value.recents.first().ref)150 assertEquals(1, lib.state.value.recents.count { it.ref == "id1" })151 assertEquals(455_000.0, lib.state.value.favorites.first().value!!, 0.0)152 assertEquals(savedAt, lib.state.value.favorites.first().savedAt)153 assertEquals(455_000.0, lib.state.value.compare.first().value!!, 0.0)154 lib.clearRecents()155 assertTrue(lib.state.value.recents.isEmpty())156 }157158 @Test fun searchesKeepTheLastEightWithoutDuplicates() {159 val lib = Library(null)160 for (i in 1..10) lib.remember(SearchRecord("s$i", "Recherche $i"))161 lib.remember(SearchRecord("s5", "Recherche 5"))162 val s = lib.state.value.searches163 assertEquals(8, s.size)164 assertEquals("s5", s.first().id)165 assertEquals(1, s.count { it.id == "s5" })166 lib.clearSearches()167 assertTrue(lib.state.value.searches.isEmpty())168 }169170 @Test fun persistsToDiskAndSurvivesACorruptFile() {171 val file = File(tmp.root, "bibliotheque.json")172 val lib = Library(file)173 lib.toggleFavorite(p(1))174 lib.toggleCompare(p(2))175 lib.visit(p(3))176 val again = Library(file)177 assertEquals(listOf("id1"), again.state.value.favorites.map { it.ref })178 assertEquals(listOf("id2"), again.state.value.compare.map { it.ref })179 assertEquals(listOf("id3"), again.state.value.recents.map { it.ref })180 assertTrue(again.state.value.favorites.first().valueBlock.isFirm)181 // un champ inconnu (version future) est ignoré ; un fichier illisible donne une bibliothèque vide, sans plantage182 file.writeText(file.readText().replaceFirst("{", "{\"futur\":1,"))183 assertEquals(1, Library(file).state.value.favorites.size)184 file.writeText("{ceci n'est pas du JSON")185 assertTrue(Library(file).state.value.favorites.isEmpty())186 }187188 @Test fun eraseAllLeavesNothing() {189 val file = File(tmp.root, "b.json")190 val lib = Library(file)191 lib.toggleFavorite(p(1)); lib.toggleCompare(p(1)); lib.visit(p(1)); lib.remember(SearchRecord("s", "S"))192 lib.eraseAll()193 val s = Library(file).state.value194 assertTrue(s.favorites.isEmpty() && s.compare.isEmpty() && s.recents.isEmpty() && s.searches.isEmpty())195 }196}197