# ============================================================ # Omika - Tests de scripts\Completer-Packs-Langues.ps1 # ============================================================ # AUCUN acces reseau : la recuperation des fichiers est injectee (scriptblock), # ce qui permet de simuler un modele identique, un modele different et une # panne reseau — les trois seuls cas qui decident du sort d'un pack. # # Ce que ces tests protegent : # - le nom du depot deduit du code et du sens (une inversion produirait # opus-mt-fr-es au lieu d'opus-mt-es-fr, donc le vocabulaire d'un AUTRE # modele, c'est-a-dire un charabia silencieux) ; # - le garde-fou : rien n'est ecrit quand le source.spm telecharge differe ; # - le fait que -Verifier n'ecrive jamais. # ============================================================ $ErrorActionPreference = 'Stop' $scriptTeste = Join-Path (Split-Path -Parent $PSScriptRoot) 'Completer-Packs-Langues.ps1' $tests = 0 $fails = 0 function Assert([bool]$condition, [string]$label) { $script:tests++ if ($condition) { Write-Host ("PASS {0}" -f $label) -ForegroundColor Green } else { Write-Host ("FAIL {0}" -f $label) -ForegroundColor Red $script:fails++ } } # Les fonctions du script sont chargees sans executer le traitement. . $scriptTeste $bac = Join-Path ([System.IO.Path]::GetTempPath()) ('omika-completion-test-' + [guid]::NewGuid().ToString('N').Substring(0, 8)) $packs = Join-Path $bac 'packs' try { # --- Un pack par cas ----------------------------------------------------- foreach ($couple in @( @{ Code = 'es'; Sens = 'versFrancais'; ContenuSpm = 'source-espagnol' }, @{ Code = 'pt'; Sens = 'versFrancais'; ContenuSpm = 'source-portugais' }, @{ Code = 'it'; Sens = 'depuisFrancais'; ContenuSpm = 'source-italien' })) { $dossier = Join-Path (Join-Path $packs $couple.Code) $couple.Sens $null = New-Item -ItemType Directory -Path $dossier -Force Set-Content -LiteralPath (Join-Path $dossier 'source.spm') -Value $couple.ContenuSpm -NoNewline Set-Content -LiteralPath (Join-Path $dossier 'target.spm') -Value 'cible' -NoNewline Set-Content -LiteralPath (Join-Path $dossier 'encoder_model.onnx') -Value 'faux' -NoNewline } # Dossier technique : ce n'est pas une langue, il doit etre ignore. $null = New-Item -ItemType Directory -Path (Join-Path $packs '.hf-cache\hub') -Force # --- 1. Nom du depot deduit --------------------------------------------- Assert ((Get-NomModele -Code 'es' -Sens 'versFrancais') -eq 'Helsinki-NLP/opus-mt-es-fr') 'versFrancais -> opus-mt-es-fr' Assert ((Get-NomModele -Code 'it' -Sens 'depuisFrancais') -eq 'Helsinki-NLP/opus-mt-fr-it') 'depuisFrancais -> opus-mt-fr-it' Assert ((Get-NomModele -Code 'ES' -Sens 'versFrancais') -eq 'Helsinki-NLP/opus-mt-es-fr') 'code mis en minuscules' Assert ((Get-NomModele -Code 'pt-br' -Sens 'depuisFrancais') -eq 'Helsinki-NLP/opus-mt-fr-pt-br') 'code compose conserve' # --- 2. Inventaire ------------------------------------------------------- $presents = @(Get-PacksPresents -Racine $packs) Assert ($presents.Count -eq 3) 'trois couples (code, sens) trouves' Assert (@($presents | Where-Object { $_.Code -eq '.hf-cache' }).Count -eq 0) 'dossier technique ignore' $filtres = @(Get-PacksPresents -Racine $packs -CodesFiltres @('pt')) Assert ($filtres.Count -eq 1 -and $filtres[0].Code -eq 'pt') 'filtre par code applique' # --- 3. Faux recuperateur : es identique, pt different, it en panne ------ $appels = New-Object 'System.Collections.Generic.List[string]' $recuperer = { param($modele, $fichier, $destination) $script:appels.Add(('{0}|{1}' -f $modele, $fichier)) if ($modele -like '*fr-it') { return $false } # it : panne reseau $contenu = if ($fichier -eq 'source.spm') { # pt : le depot deduit n'est pas celui du pack. if ($modele -like '*pt-fr') { 'source-autre-modele' } else { 'source-espagnol' } } elseif ($fichier -eq 'vocab.json') { '{ "piece": 584 }' } else { '{ "decoder_start_token_id": 74821, "eos_token_id": 0 }' } Set-Content -LiteralPath $destination -Value $contenu -NoNewline return $true } # --- 4. Verification seule : rien n'est ecrit ---------------------------- $bilanVerif = Invoke-Completion -Racine $packs -Recuperer $recuperer -SansEcrire Assert ($bilanVerif.Packs -eq 3) 'verification : trois packs examines' Assert ($bilanVerif.Alignes -eq 1) 'verification : un pack aligne (es)' Assert ($bilanVerif.Ecartes -eq 1) 'verification : un pack ecarte (pt)' Assert ($bilanVerif.Echecs -eq 1) 'verification : un echec (it)' Assert ($bilanVerif.Ecrits -eq 0) 'verification : aucune ecriture' Assert (-not (Test-Path -LiteralPath (Join-Path $packs 'es\versFrancais\vocab.json'))) 'verification : vocab.json absent du pack aligne' # --- 5. Ecriture reelle -------------------------------------------------- $bilan = Invoke-Completion -Racine $packs -Recuperer $recuperer Assert ($bilan.Ecrits -eq 1) 'ecriture : un pack complete' Assert (Test-Path -LiteralPath (Join-Path $packs 'es\versFrancais\vocab.json')) 'ecriture : vocab.json pose' Assert (Test-Path -LiteralPath (Join-Path $packs 'es\versFrancais\config.json')) 'ecriture : config.json pose' Assert (-not (Test-Path -LiteralPath (Join-Path $packs 'pt\versFrancais\vocab.json'))) 'ecart : aucun fichier ecrit' Assert (-not (Test-Path -LiteralPath (Join-Path $packs 'it\depuisFrancais\vocab.json'))) 'echec : aucun fichier ecrit' # --- 6. Le vocabulaire n'est jamais pris pour la configuration ----------- $config = Get-Content -LiteralPath (Join-Path $packs 'es\versFrancais\config.json') -Raw Assert ($config -like '*decoder_start_token_id*') 'config.json recopie tel quel' $vocab = Get-Content -LiteralPath (Join-Path $packs 'es\versFrancais\vocab.json') -Raw Assert ($vocab -like '*584*') 'vocab.json recopie tel quel' # --- 7. Les bons fichiers ont ete demandes -------------------------------- Assert ($appels -contains 'Helsinki-NLP/opus-mt-es-fr|vocab.json') 'vocabulaire demande au bon depot (es)' Assert ($appels -contains 'Helsinki-NLP/opus-mt-fr-it|source.spm') 'source demandee au bon depot (it)' # --- 8. Idempotence : relancer ne casse rien ----------------------------- $bilan2 = Invoke-Completion -Racine $packs -Recuperer $recuperer Assert ($bilan2.Ecrits -eq 1 -and $bilan2.Packs -eq 3) 'deuxieme passage : meme resultat' # --- 9. Un pack familial (commun/) n'est pas un pack a completer --------- # Un modele familial range ses poids dans \commun\ et ne laisse qu'un # modele.json par sens : il n'a AUCUN source.spm par sens, donc rien a # aligner. Le completer fabriquerait sinon un [ECART] trompeur (la paire # bilingue deduite n'existe pas) et ferait croire le pack casse. $dossierFamille = Join-Path (Join-Path $packs 'de') 'commun' $null = New-Item -ItemType Directory -Path $dossierFamille -Force Set-Content -LiteralPath (Join-Path $dossierFamille 'source.spm') -Value 'source-famille' -NoNewline Set-Content -LiteralPath (Join-Path $dossierFamille 'encoder_model.onnx') -Value 'faux' -NoNewline foreach ($sens in @('versFrancais', 'depuisFrancais')) { $dossierSens = Join-Path (Join-Path $packs 'de') $sens $null = New-Item -ItemType Directory -Path $dossierSens -Force Set-Content -LiteralPath (Join-Path $dossierSens 'modele.json') -Value '{ "jetonCible": ">>fra<<" }' -NoNewline } $presentsFamille = @(Get-PacksPresents -Racine $packs) Assert ($presentsFamille.Count -eq 3) 'pack familial : aucun couple (code, sens) a completer' $bilanFamille = Invoke-Completion -Racine $packs -Recuperer $recuperer -SansEcrire Assert ($bilanFamille.Packs -eq 3 -and $bilanFamille.Ecartes -eq 1 -and $bilanFamille.Echecs -eq 1) ` 'pack familial : le bilan ne change pas (aucun ecart invente)' } finally { Remove-Item -LiteralPath $bac -Recurse -Force -ErrorAction SilentlyContinue } Write-Host '' if ($fails -eq 0) { Write-Host ("{0} tests, 0 echec" -f $tests) -ForegroundColor Green exit 0 } Write-Host ("{0} tests, {1} echec(s)" -f $tests, $fails) -ForegroundColor Red exit 1