Files
TioxyAi/tests/ps/test_strataModels.ps1
tisachi e50a3fd996 Migracja Strata do terminala 0.1.47 - start przez CLI, bez deskryptora
Strata odpala sie teraz przez CLI terminala (strata serve --config). Jeden wpis STRATA_CONTROL w .env, zero znajomosci formatu configu, zero plikow wspoldzielonych.

Launcher nie pisze deskryptora host/state/strata.json (Strata go nie ma); proxy bierze wszystko z zywej API: /health, /props, /v1/models, a kwantyzacja jest odgadywana ze sciezki modelu.

Lista modeli i strata.bat czytaja 4 pola z CLI (id|nazwa|kontekst|status). Walidacja tylko przez -DryRun. Zatrzymywanie waskie po galazi serve i naszym porcie; obcej instancji nie zabijamy.

Testy przerobione na atrape terminala przez STRATA_CONTROL; protectedPaths dodaje D:\AI\Silniki\Strata.

Naprawa: Get-StrataModels zwracal pusta liste jako wartosc pusta (PowerShell zapada pusta tablice do pustki) i blednie kierowal do strata doctor zamiast strata launch.

Przy okazji: test_launcherContract mial nieaktualny czas (3 s) przy choice /T 5 w killList.bat.

README, AGENTS.md, docs/architektura.md oraz .env.example zaktualizowane.
2026-10-21 22:21:52 +02:00

222 lines
11 KiB
PowerShell

# =============================================================================
# test_strataModels.ps1 - lista modeli Strata z CLI terminala.
#
# CO TU JEST NAPRAWDE SPRAWDZANE
#
# Od terminala 0.1.47 TioxyAi NIE zna formatu configu Straty ani jej katalogow.
# Lista modeli przychodzi z CLI ('models list --output json'), a TioxyAi tylko
# przeklada ja na kontrakt strata.bat. Atrapa terminala = katalog z bin\strata.cmd
# (fixture.ps1 wypisuje gotowy JSON) + strata.py + python\python.exe + VERSION,
# podstawiany przez STRATA_CONTROL.
#
# Dwie rzeczy musza byc twarde, bo od nich zalezy caly launcher:
#
# 1. KONTRAKT WYJSCIA: na stdout WYLACZNIE linie modeli, cztery pola
# <id>|<nazwa>|<kontekst>|<status>. Jeden log za duzo i strata.bat mialby
# dodatkowy "model". Sprawdzamy StdOut osobno, nie polaczony strumien.
#
# 2. TOLERANCJA WPISU BLEDNEGO: 'invalid' nie ma pol 'name' ani 'context_tokens'
# (strata_control/models.py) - lista NIE moze sie na tym wylozyc ani zgubic
# pozostalych modeli; wpis zostaje oznaczony 'blad-statusu'.
# =============================================================================
. "$PSScriptRoot\..\lib\testHelper.ps1"
Initialize-Sandbox
$sandbox = $script:Sandbox
# --- Repo piaskownicy --------------------------------------------------------
$repoCopy = Join-Path $sandbox 'repo'
New-Item -ItemType Directory -Path (Join-Path $repoCopy 'host\lib') -Force | Out-Null
foreach ($name in @('common.ps1', 'strataBackend.ps1', 'strataModels.ps1')) {
Copy-Item -LiteralPath (Join-Path $script:RepoRoot "host\lib\$name") `
-Destination (Join-Path $repoCopy "host\lib\$name") -Force
}
# --- Atrapa terminala (CLI + Python) -----------------------------------------
# bin\strata.cmd odpala fixture.ps1, ktory wypisuje gotowy JSON dla 'models list'
# (i 'serve --plan'). Nic nie jest uruchamiane naprawde.
function New-TioxyFixtureTerminal {
param([Parameter(Mandatory = $true)][string]$Terminal)
New-Item -ItemType Directory -Path (Join-Path $Terminal 'bin') -Force | Out-Null
New-Item -ItemType Directory -Path (Join-Path $Terminal 'python') -Force | Out-Null
Set-Content -LiteralPath (Join-Path $Terminal 'bin\strata.cmd') -Encoding ASCII -Value @(
'@echo off',
'powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0fixture.ps1" %*',
'exit /b %errorlevel%'
)
Set-Content -LiteralPath (Join-Path $Terminal 'bin\fixture.ps1') -Encoding ASCII -Value @(
'param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Rest)',
'$text = ($Rest -join " ")',
'function Emit([string]$Path) {',
' if ($Path -and (Test-Path -LiteralPath $Path)) {',
' [Console]::Out.Write([System.IO.File]::ReadAllText($Path))',
' }',
'}',
'if ($text -match "serve") {',
' if ($text -match "--plan") {',
' if ($env:STRATA_FIXTURE_CASE -eq "plan_fail") { [Console]::Error.WriteLine("plan: blad"); exit 4 }',
' Emit $env:STRATA_FIXTURE_PLAN_FILE',
' exit 0',
' }',
' [Console]::Error.WriteLine("fixture: pelny start nie jest obslugiwany"); exit 5',
'}',
'if ($text -match "models") {',
' if ($env:STRATA_FIXTURE_CASE -eq "list_fail") { [Console]::Error.WriteLine("models: blad"); exit 3 }',
' Emit $env:STRATA_FIXTURE_MODELS_FILE',
' exit 0',
'}',
'exit 0'
)
Set-Content -LiteralPath (Join-Path $Terminal 'strata.py') -Value '# atrapa' -Encoding ASCII
Set-Content -LiteralPath (Join-Path $Terminal 'python\python.exe') -Value 'atrapa' -Encoding ASCII
Set-Content -LiteralPath (Join-Path $Terminal 'VERSION') -Value '0.1.47' -Encoding ASCII
}
$terminal = Join-Path $sandbox 'strata-terminal'
New-TioxyFixtureTerminal $terminal
function Write-ModelsJson {
param([Parameter(Mandatory = $true)]$Models)
$path = Join-Path $sandbox 'models.json'
$payload = [ordered]@{ schema_version = 1; ok = $true; scope = 'local_run_configs'; models = $Models }
Set-Content -LiteralPath $path -Encoding ASCII -Value ($payload | ConvertTo-Json -Depth 8)
return $path
}
function Invoke-Models {
param([hashtable]$Extra = @{}, [string]$Control = $terminal)
$env = @{
'STRATA_CONTROL' = $Control
'STRATA_PORT' = '7995'
}
foreach ($k in $Extra.Keys) { $env[$k] = $Extra[$k] }
$command = "powershell -NoProfile -ExecutionPolicy Bypass -File `"$repoCopy\host\lib\strataModels.ps1`""
return Invoke-Child -Command $command -WorkingDirectory $repoCopy -Environment $env
}
function Get-ModelLine {
param([string[]]$Lines, [string]$Id)
foreach ($line in $Lines) { if ($line.StartsWith("$Id|")) { return $line } }
return $null
}
function Get-Field {
param([string]$Line, [int]$Index)
if ($null -eq $Line) { return $null }
return @($Line -split '\|')[$Index]
}
# --- Cane JSON z ksztaltow z prawdziwego CLI ---------------------------------
$modelsFile = Write-ModelsJson @(
[ordered]@{ id = 'swift-iq2_xs'; name = 'swift-1.5-iq2_xs';
source_config = 'C:\Strata\install\strata-swift-iq2_xs.json';
context_tokens = 262144; component_status = 'present'; backend = $null },
[ordered]@{ id = 'swift-iq3_xxs'; name = 'swift-1.5-iq3_xxs';
source_config = 'C:\Strata\install\strata-swift-iq3_xxs.json';
context_tokens = 131072; component_status = 'present'; backend = $null },
# Komponenty niekompletne - wpis pozostaje, menu musi to pokazac.
[ordered]@{ id = 'bez-komponentow'; name = 'model-bez-komponentow';
source_config = 'C:\Strata\install\strata-bez-komponentow.json';
context_tokens = 32768; component_status = 'missing_or_invalid'; backend = $null },
# Config bledny: CLI NIE doklada 'name' ani 'context_tokens' (trasa wyjatku).
[ordered]@{ id = 'zepsuty-config';
source_config = 'C:\Strata\install\strata-zepsuty-config.json';
component_status = 'invalid'; error = 'run config changed'; execution_compatibility = 'unverified' }
)
# =============================================================================
Start-Test 'Lista modeli: kontrakt czterech pol na stdout'
$result = Invoke-Models @{ 'STRATA_FIXTURE_MODELS_FILE' = $modelsFile }
Assert-Equal 0 $result.ExitCode 'kod wyjscia 0'
$lines = @($result.StdOut -split "`r?`n" | Where-Object { $_ -ne '' })
Assert-Equal 4 $lines.Count 'dokladnie cztery modele na stdout'
# stdout jest KONTRAKTEM - ani jednego logu, ostrzezenia ani smiecia wiecej.
Assert-NotContains $result.StdOut '[DEBUG]' 'na stdout nie ma logow pomocniczych'
Assert-NotContains $result.StdOut '[WARN]' 'ostrzezenia ida na stderr, nie na stdout'
Assert-NotContains $result.StdOut '[ERROR]' 'bledy ida na stderr'
Assert-NotContains $result.StdOut 'atrapa' 'na stdout nie ma tresci plikow silnika'
Assert-NotContains $result.StdOut 'source_config' 'sciezki do configu nie trafia na stdout'
foreach ($line in $lines) {
Assert-Equal 3 (@($line -split '\|').Count - 1) "linia ma 4 pola (3 separatory): $line"
}
# =============================================================================
Start-Test 'Pola: id, nazwa z CLI, kontekst, status'
$iq2 = Get-ModelLine $lines 'swift-iq2_xs'
Assert-True ($null -ne $iq2) 'model swift-iq2_xs jest na liscie'
Assert-Equal 'swift-1.5-iq2_xs' (Get-Field $iq2 1) 'nazwa = name z CLI (model_name)'
Assert-Equal '262144' (Get-Field $iq2 2) 'kontekst z context_tokens'
Assert-Equal 'ok' (Get-Field $iq2 3) 'status ok dla component_status=present'
$brak = Get-ModelLine $lines 'bez-komponentow'
Assert-Equal 'brak-komponentow' (Get-Field $brak 3) 'missing_or_invalid = brak-komponentow'
# =============================================================================
Start-Test 'Wpis bledny nie ma nazwy/kontekstu - lista go toleruje'
$zepsuty = Get-ModelLine $lines 'zepsuty-config'
Assert-True ($null -ne $zepsuty) 'bledny config zostaje na liscie, nie znika'
Assert-Equal 'blad-statusu' (Get-Field $zepsuty 3) 'invalid = blad-statusu'
Assert-Equal 'zepsuty-config' (Get-Field $zepsuty 1) 'bez name nazwa spada na id'
Assert-Equal '0' (Get-Field $zepsuty 2) 'bez context_tokens kontekst = 0'
# =============================================================================
Start-Test 'Brak terminala - exit 1 i wskazanie STRATA_CONTROL'
$result = Invoke-Models -Control (Join-Path $sandbox 'czego-nie-ma')
Assert-Equal 1 $result.ExitCode 'kod wyjscia 1'
Assert-Contains $result.StdErr 'STRATA_CONTROL' 'komunikat podpowiada, ktora zmienna poprawic'
Assert-Equal '' $result.StdOut.Trim() 'przy bledzie na stdout nie ma nic'
# =============================================================================
Start-Test 'Terminal jest, ale niepelny (brak bin\strata.cmd) - exit 1'
$broken = Join-Path $sandbox 'terminal-niepelnny'
New-Item -ItemType Directory -Path (Join-Path $broken 'python') -Force | Out-Null
Set-Content -LiteralPath (Join-Path $broken 'strata.py') -Value '# atrapa' -Encoding ASCII
Set-Content -LiteralPath (Join-Path $broken 'python\python.exe') -Value 'atrapa' -Encoding ASCII
Set-Content -LiteralPath (Join-Path $broken 'VERSION') -Value '0.1.47' -Encoding ASCII
$result = Invoke-Models -Control $broken
Assert-Equal 1 $result.ExitCode 'kod wyjscia 1'
Assert-Contains $result.StdErr 'bin\strata.cmd' 'komunikat wskazuje, czego brakuje w terminalu'
# =============================================================================
Start-Test 'Pusta lista modelow - exit 1 ze wskazaniem strata launch'
$emptyFile = Write-ModelsJson @()
$result = Invoke-Models @{ 'STRATA_FIXTURE_MODELS_FILE' = $emptyFile }
Assert-Equal 1 $result.ExitCode 'kod wyjscia 1'
Assert-Contains $result.StdErr 'strata launch' 'komunikat mowi, czym pobrac model'
Assert-Equal '' $result.StdOut.Trim() 'przy braku modelow stdout pozostaje pusty'
# =============================================================================
Start-Test 'Blad CLI (models list nie wrocil 0) - exit 1 ze wskazaniem doctor'
$result = Invoke-Models @{ 'STRATA_FIXTURE_MODELS_FILE' = $modelsFile; 'STRATA_FIXTURE_CASE' = 'list_fail' }
Assert-Equal 1 $result.ExitCode 'kod wyjscia 1'
Assert-Contains $result.StdErr 'strata doctor' 'komunikat kieruje do diagnostyki silnika'
Assert-Equal '' $result.StdOut.Trim() 'przy bledzie CLI stdout pozostaje pusty'
# =============================================================================
Start-Test 'Ten sam id w dwoch miejscach - jeden wpis'
$dupFile = Write-ModelsJson @(
[ordered]@{ id = 'dwa-miejsca'; name = 'model-z-glownego'; source_config = 'C:\a\x.json';
context_tokens = 8192; component_status = 'present' },
[ordered]@{ id = 'dwa-miejsca'; name = 'model-z-uzupebnienia'; source_config = 'C:\b\x.json';
context_tokens = 8192; component_status = 'present' }
)
$result = Invoke-Models @{ 'STRATA_FIXTURE_MODELS_FILE' = $dupFile }
Assert-Equal 0 $result.ExitCode 'kod wyjscia 0'
$dupLines = @($result.StdOut -split "`r?`n" | Where-Object { $_.StartsWith('dwa-miejsca|') })
Assert-Equal 1 $dupLines.Count 'zduplikowany id dawaja jeden wpis na stdout'
Assert-Equal 'model-z-glownego' (Get-Field $dupLines[0] 1) 'wygrywa glowny wpis (pierwszy)'
exit (Get-SuiteSummary 'test_strataModels')