# ============================================================================= # test_syncOpenCode.ps1 - synchronizacja OpenCode (Windows). # # Skrypt rozmawia wylacznie z proxy, wiec testujemy go przeciwko mockowi # odpowiadajacemu zgodnie z kontraktem - bez Dockera i bez zadnego modelu. # # Najwazniejsze, co musi byc udowodnione: # - kazdy dzialajacy silnik dostaje osobnego providera, nic sie nie nadpisuje # - brak modeli NIE kasuje dzialajacej konfiguracji # - uszkodzona odpowiedz serwera NIE kasuje dzialajacej konfiguracji # - powstaje kopia zapasowa # ============================================================================= . "$PSScriptRoot\..\lib\testHelper.ps1" Initialize-Sandbox $sandbox = $script:Sandbox $syncPath = Join-Path $sandbox 'bundle\syncOpenCode.ps1' $port = Get-TestPort 7891 $key = 'tioxyai_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' $server = "http://127.0.0.1:$port" $mockPath = Join-Path $script:TestsRoot 'mocks\mockProxy.py' function Invoke-Sync { param([hashtable]$Environment = @{}, [switch]$KeepHome) if (-not $KeepHome) { $null = New-SandboxHome } $sandboxHome = Join-Path $sandbox 'home' # SYNC_ANYWAY=1: ta suite bada logike synchronizacji, nie bramke klienta # (bramka ma wlasna suite). Bez tego czysta piaskownica konczylaby sie # pominieciem juz na starcie. $childEnv = @{ 'USERPROFILE' = $sandboxHome; 'SYNC_ANYWAY' = '1'; 'NO_RESTART' = '1' } foreach ($name in $Environment.Keys) { $childEnv[$name] = $Environment[$name] } $command = "powershell -NoProfile -ExecutionPolicy Bypass -File `"$syncPath`" -Server `"$server`" -Key `"$key`" -NoRestart" return Invoke-Child -Command $command -Environment $childEnv } function Get-ConfigPath { return (Join-Path $sandbox 'home\.config\opencode\opencode.json') } # ============================================================================= Start-Test 'Dwa dzialajace silniki - obaj providerzy w konfiguracji' $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'ok' } try { $result = Invoke-Sync $config = Get-ConfigPath Assert-Equal 10 $result.ExitCode 'kod wyjscia 10 (konfiguracja zmieniona)' Assert-FileExists $config 'plik opencode.json powstal' Assert-ValidJson $config 'zapisany plik jest poprawnym JSON-em' Assert-JsonPath $config 'provider.tioxyai-vllm.name' 'TioxyAi vLLM' 'provider vLLM ma poprawna nazwe' Assert-JsonPath $config 'provider.tioxyai-llamacpp.name' 'TioxyAi llamacpp' 'provider llamacpp ma poprawna nazwe' # Sedno wymagania: kazdy model jest widoczny osobno. Silnik niesie NAZWA # PROVIDERA, a nie nazwa modelu - inaczej ta sama informacja dublowalaby sie # w jednej linii pickera ("TioxyAi vLLM" + "TioxyAi vLLM/Qwen..."). Assert-JsonPath $config 'provider.tioxyai-vllm.models[Qwen3.8-27B].name' 'Qwen3.8-27B' ` 'nazwa modelu vLLM bez powtorzonego dopisku silnika' Assert-JsonPath $config 'provider.tioxyai-llamacpp.models[gemma-4-12b-it].name' 'gemma-4-12b-it' ` 'nazwa modelu llamacpp bez powtorzonego dopisku silnika' # Rozroznialnosc zapewnia osobny provider dla kazdego silnika. Assert-True ($null -ne (Get-JsonPath $config 'provider.tioxyai-vllm.models[Qwen3.8-27B]')) ` 'model vLLM siedzi pod swoim providerem' Assert-True ($null -ne (Get-JsonPath $config 'provider.tioxyai-llamacpp.models[gemma-4-12b-it]')) ` 'model llamacpp siedzi pod swoim providerem' Assert-JsonPath $config 'provider.tioxyai-vllm.models[Qwen3.8-27B].limit.context' 60127 'kontekst z serwera' Assert-JsonPath $config 'provider.tioxyai-vllm.models[Qwen3.8-27B].tool_call' 'True' 'wywolywanie narzedzi wlaczone' Assert-JsonPath $config 'provider.tioxyai-vllm.models[Qwen3.8-27B].attachment' 'True' 'obsluga obrazow wlaczona' Assert-JsonPath $config 'provider.tioxyai-vllm.options.apiKey' $key 'klucz zapisany w konfiguracji' Assert-Contains (Get-JsonPath $config 'provider.tioxyai-vllm.options.baseURL') '/vllm/v1' 'adres wskazuje endpoint silnika' Assert-JsonPath $config 'mcp.searxng.enabled' 'True' 'wyszukiwarka wlaczona' Assert-JsonPath $config 'mcp.searxng.environment.AUTH_PASSWORD' $key 'wyszukiwarka dostaje klucz jako haslo' Assert-Contains (Get-JsonPath $config 'model') 'tioxyai-' 'ustawiony model domyslny' Assert-Contains $result.All 'TioxyAi vLLM/Qwen3.8-27B' 'lista modeli pokazana uzytkownikowi' } finally { Stop-Mock $mock } # ============================================================================= Start-Test 'Kopia zapasowa poprzedniej konfiguracji' $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'ok' } try { $null = Invoke-Sync $config = Get-ConfigPath $before = Get-Content -LiteralPath $config -Raw $result = Invoke-Sync -KeepHome Assert-Equal 10 $result.ExitCode 'drugie uruchomienie tez konczy sie kodem 10' $backups = @(Get-ChildItem -Path "$config.bak.*" -ErrorAction SilentlyContinue) Assert-True ($backups.Count -ge 1) 'powstala kopia zapasowa' Assert-Equal $before (Get-Content -LiteralPath $backups[0].FullName -Raw) 'kopia zawiera poprzednia tresc' } finally { Stop-Mock $mock } # ============================================================================= Start-Test 'Brak dzialajacych modeli - konfiguracja NIE jest ruszana' $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'ok' } try { $null = Invoke-Sync } finally { Stop-Mock $mock } $config = Get-ConfigPath $snapshot = Get-Content -LiteralPath $config -Raw $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'brak-modeli' } try { $result = Invoke-Sync -KeepHome Assert-Equal 0 $result.ExitCode 'kod wyjscia 0 (bez zmian)' Assert-Equal $snapshot (Get-Content -LiteralPath $config -Raw) 'poprzednia konfiguracja zostala nietknieta' Assert-Contains $result.All 'zaden model' 'komunikat wyjasnia przyczyne' } finally { Stop-Mock $mock } # ============================================================================= Start-Test 'Uszkodzona odpowiedz serwera - konfiguracja NIE jest ruszana' $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'uszkodzone-ustawienia' } try { $result = Invoke-Sync -KeepHome Assert-Equal 1 $result.ExitCode 'kod wyjscia 1 (blad)' Assert-Equal $snapshot (Get-Content -LiteralPath $config -Raw) 'poprzednia konfiguracja zostala nietknieta' Assert-Contains $result.All 'uszkodzone' 'komunikat wyjasnia przyczyne' } finally { Stop-Mock $mock } # ============================================================================= Start-Test 'Odrzucony klucz - czytelny komunikat, brak zmian' $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = 'tioxyai_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'; SCENARIO = 'ok' } try { $result = Invoke-Sync -KeepHome Assert-Equal 1 $result.ExitCode 'kod wyjscia 1 (blad)' Assert-Contains $result.All 'klucz' 'komunikat mowi o kluczu' Assert-Equal $snapshot (Get-Content -LiteralPath $config -Raw) 'konfiguracja nietknieta' } finally { Stop-Mock $mock } # ============================================================================= Start-Test 'Serwer nieosiagalny - czytelny komunikat, brak zmian' $result = Invoke-Sync -KeepHome Assert-Equal 1 $result.ExitCode 'kod wyjscia 1 (blad)' Assert-Contains $result.All 'serwer' 'komunikat mowi o serwerze' Assert-Equal $snapshot (Get-Content -LiteralPath $config -Raw) 'konfiguracja nietknieta' # ============================================================================= Start-Test 'Klucz i adres czytane z pliku config.json' $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'ok' } try { New-ClientConfigFile -Directory (Join-Path $sandbox 'bundle') -Key $key -Server $server $sandboxHome = New-SandboxHome $command = "powershell -NoProfile -ExecutionPolicy Bypass -File `"$syncPath`" -NoRestart" $result = Invoke-Child -Command $command -Environment @{ 'USERPROFILE' = $sandboxHome; 'SYNC_ANYWAY' = '1' } Assert-Equal 10 $result.ExitCode 'skrypt sam znalazl klucz i adres' Assert-FileExists (Get-ConfigPath) 'konfiguracja zapisana' } finally { Stop-Mock $mock Remove-Item -LiteralPath (Join-Path $sandbox 'bundle\config.json') -Force -ErrorAction SilentlyContinue } # ============================================================================= # MODELE, KTORYCH PROXY NIE WIDZI - lokalne (rola klient) i zdalne (rola host). # Skrypt je wykrywa i przysyla proxy, a proxy renderuje je do opencode.json. # ============================================================================= $mockLmsPath = Join-Path $script:TestsRoot 'mocks\mockLmStudio.py' $pythonBin = $null foreach ($kandydat in @('python', 'python3', 'py')) { $cmd = Get-Command $kandydat -ErrorAction SilentlyContinue if (-not $cmd) { continue } $probe = & $cmd.Source -c "print('ok')" 2>$null if ($probe -eq 'ok') { $pythonBin = $cmd.Source; break } } function Start-LmStudio { param([int]$Port) $env:PORT = "$Port" $proc = Start-Process -FilePath $pythonBin -ArgumentList $mockLmsPath ` -PassThru -WindowStyle Hidden for ($i = 0; $i -lt 50; $i++) { try { Invoke-RestMethod -Uri "http://127.0.0.1:$Port/v1/models" -TimeoutSec 1 | Out-Null break } catch { Start-Sleep -Milliseconds 200 } } return $proc } function Stop-LmStudio { param($Process) Remove-Item Env:PORT -ErrorAction SilentlyContinue if ($null -ne $Process) { Stop-Process -Id $Process.Id -Force -ErrorAction SilentlyContinue } } # ============================================================================= Start-Test 'Host: zdalny komputer z modelami jako provider' if (-not $pythonBin) { Assert-True $true 'POMINIETE - brak dzialajacego Pythona do atrapy' } else { $portZdalny = Get-TestPort 18236 $procLms = Start-LmStudio -Port $portZdalny $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'ok' } try { $null = New-SandboxHome $sandboxHome = Join-Path $sandbox 'home' $childEnv = @{ 'USERPROFILE' = $sandboxHome 'SYNC_ANYWAY' = '1' 'NO_RESTART' = '1' # Lista kandydatow: najpierw martwy adres, potem atrapa - jak # w prawdziwym skanowaniu LAN/VPN. 'REMOTE_LLM_SCAN' = "127.0.0.1:19,127.0.0.1:$portZdalny" 'REMOTE_LLM_NAME' = 'mac' } $command = "powershell -NoProfile -ExecutionPolicy Bypass -File `"$syncPath`" -Server `"$server`" -Key `"$key`" -NoRestart" $result = Invoke-Child -Command $command -Environment $childEnv $config = Get-ConfigPath Assert-Equal 10 $result.ExitCode 'kod wyjscia 10 (konfiguracja zmieniona)' Assert-Contains $result.All 'Zdalny komputer odpowiada' 'skrypt melduje znalezienie komputera' Assert-JsonPath $config 'provider.tioxyai-zdalny-mac.options.baseURL' "http://127.0.0.1:$portZdalny/v1" ` 'baseURL providera zdalnego' Assert-JsonPath $config 'provider.tioxyai-zdalny-mac.options.apiKey' 'lokalny' ` 'provider zdalny bez klucza proxy' Assert-JsonPath $config 'provider.tioxyai-zdalny-mac.models[qwen3.8-27b-instruct].limit.context' 32768 ` 'kontekst modelu zdalnego' Assert-JsonPath $config 'provider.tioxyai-zdalny-mac.models[qwen3.8-27b-instruct].limit.output' 8192 ` 'limit wyjscia wyliczony z kontekstu' Assert-JsonPath $config 'provider.tioxyai-vllm.name' 'TioxyAi vLLM' 'providery z proxy zostaly' Assert-Contains $result.All 'modele zdalne: dopisane' 'podsumowanie melduje modele zdalne' } finally { Stop-Mock $mock Stop-LmStudio $procLms } } # ============================================================================= Start-Test 'Klient: model lokalny jako provider' if (-not $pythonBin) { Assert-True $true 'POMINIETE - brak dzialajacego Pythona do atrapy' } else { $portLokalny = 4891 $procLms = Start-LmStudio -Port $portLokalny $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'ok' } try { $null = New-SandboxHome $sandboxHome = Join-Path $sandbox 'home' # Rola "klient" wymuszona w config.json - serwer stoi na localhost, # wiec bez tego skrypt traktowalby maszyne jak hosc. Set-Content -LiteralPath (Join-Path $sandbox 'bundle\config.json') ` -Value ('{{"machine_key":"{0}","server":"{1}","rola":"klient"}}' -f $key, $server) -Encoding ASCII $command = "powershell -NoProfile -ExecutionPolicy Bypass -File `"$syncPath`" -NoRestart" $result = Invoke-Child -Command $command -Environment @{ 'USERPROFILE' = $sandboxHome; 'SYNC_ANYWAY' = '1'; 'NO_RESTART' = '1' } $config = Get-ConfigPath Assert-Equal 10 $result.ExitCode 'kod wyjscia 10 (konfiguracja zmieniona)' Assert-Contains $result.All 'Modele uruchomione na tym komputerze' 'skrypt melduje model lokalny' Assert-JsonPath $config 'provider.tioxyai-lokalny-4891.options.baseURL' "http://127.0.0.1:$portLokalny/v1" ` 'baseURL providera lokalnego' Assert-JsonPath $config 'provider.tioxyai-lokalny-4891.options.apiKey' 'lokalny' ` 'provider lokalny bez klucza proxy' Assert-JsonPath $config 'provider.tioxyai-lokalny-4891.models[qwen3.8-27b-instruct].limit.context' 32768 ` 'kontekst modelu lokalnego' Assert-JsonPath $config 'provider.tioxyai-vllm.name' 'TioxyAi vLLM' 'providery z proxy zostaly' Assert-Contains $result.All 'modele lokalne: dopisane' 'podsumowanie melduje modele lokalne' } finally { Stop-Mock $mock Stop-LmStudio $procLms Remove-Item -LiteralPath (Join-Path $sandbox 'bundle\config.json') -Force -ErrorAction SilentlyContinue } } # ============================================================================= Start-Test 'Brak modeli serwera + model lokalny - plik powstaje' if (-not $pythonBin) { Assert-True $true 'POMINIETE - brak dzialajacego Pythona do atrapy' } else { $portLokalny = 4891 $procLms = Start-LmStudio -Port $portLokalny $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'brak-modeli' } try { $null = New-SandboxHome $sandboxHome = Join-Path $sandbox 'home' Set-Content -LiteralPath (Join-Path $sandbox 'bundle\config.json') ` -Value ('{{"machine_key":"{0}","server":"{1}","rola":"klient"}}' -f $key, $server) -Encoding ASCII $command = "powershell -NoProfile -ExecutionPolicy Bypass -File `"$syncPath`" -NoRestart" $result = Invoke-Child -Command $command -Environment @{ 'USERPROFILE' = $sandboxHome; 'SYNC_ANYWAY' = '1'; 'NO_RESTART' = '1' } $config = Get-ConfigPath Assert-Equal 10 $result.ExitCode 'kod wyjscia 10 (konfiguracja zmieniona)' Assert-FileExists $config 'plik powstal mimo pustego katalogu' Assert-JsonPath $config 'provider.tioxyai-lokalny-4891.name' 'This device' 'provider lokalny w konfiguracji' Assert-True ($null -eq (Get-JsonPath $config 'provider.tioxyai-vllm')) 'providera serwera nie ma' Assert-Contains (Get-JsonPath $config 'model') 'tioxyai-lokalny-4891' 'model domyslny wskazuje model lokalny' } finally { Stop-Mock $mock Stop-LmStudio $procLms Remove-Item -LiteralPath (Join-Path $sandbox 'bundle\config.json') -Force -ErrorAction SilentlyContinue } } # ============================================================================= Start-Test 'Serwer nieosiagalny + model lokalny - bez zmian, kod 0' if (-not $pythonBin) { Assert-True $true 'POMINIETE - brak dzialajacego Pythona do atrapy' } else { $portLokalny = 4891 $procLms = Start-LmStudio -Port $portLokalny try { # Najpierw zapisz dzialajaca konfiguracje (proxy zyje), potem sprawdz # zachowanie, gdy proxy znika. $mock = Start-Mock -Script $mockPath -Port $port -Environment @{ PORT = $port; KEY = $key; SCENARIO = 'ok' } $null = Invoke-Sync Stop-Mock $mock $config = Get-ConfigPath $snapshot = Get-Content -LiteralPath $config -Raw $result = Invoke-Sync -KeepHome -Environment @{ 'TIOXYAI_ROLA' = 'klient' } Assert-Equal 0 $result.ExitCode 'kod wyjscia 0 (bez zmian)' Assert-Equal $snapshot (Get-Content -LiteralPath $config -Raw) 'konfiguracja nietknieta' Assert-Contains $result.All 'serwer nieosiagalny' 'podsumowanie wyjasnia przyczyne' } finally { Stop-LmStudio $procLms } } exit (Get-SuiteSummary 'test_syncOpenCode')