Skip to content

Commit

Permalink
Added basic tests with Pester
Browse files Browse the repository at this point in the history
  • Loading branch information
rrg92 committed Nov 15, 2024
1 parent b600c0c commit c789253
Show file tree
Hide file tree
Showing 6 changed files with 179 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ jobs:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
GOOGLE_API_KEY: ${{ secrets.GOOGLE_API_KEY }}
POWERSHAI_TEST_MODELS: ${{ vars.POWERSHAI_TEST_MODELS }}
run: ./util/publish-prepare.ps1 -BasicTest
run: ./util/publish-prepare.ps1 -Tests
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
HF_API_TOKEN: $HF_API_TOKEN
OPENAI_API_KEY: $OPENAI_API_KEY
GOOGLE_API_KEY: $GOOGLE_API_KEY
POWERSHAI_TEST_MODELS: google/gemini-1.5-flash,openai/gpt-4o-mini
command: pwsh -NonInteractive -File ./util/publish-prepare.ps1 -BasicTest
POWERSHAI_TEST_MODELS: google/gemini-1.5-flash-8b-latest,openai/gpt-4o-mini
command: pwsh -NonInteractive -File ./util/publish-prepare.ps1 -Tests


160 changes: 160 additions & 0 deletions tests/pester/basic.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
BeforeAll {
. "$(Get-Location)/util/UtilLib.ps1"
CheckPowershaiRoot

import-module ./powershai -force;

$PowerShaiModule = Get-module powershai;

if(!$m){
throw "MODULE_NOT_IMPORTED: $m";
}


}

BeforeDiscovery {
$ModelsParts = $Env:POWERSHAI_TEST_MODELS -Split ","
$TestModels = $ModelsParts | %{
$Parts = $_ -split "/",2
$Provider = $Parts[0];
$Model = $Parts[1];
@{
Provider = $Provider
Model = $Model
}
}

$ChatNum = 0;
}

Describe "Provider: <Provider>, model <model>" -Foreach $TestModels {

BeforeAll {
$ChatNum++
}

It "Set current provider to <provider>" {
Set-AiProvider $Provider;
(Get-AiCurrentProvider).name | Should -Be $Provider
}

It "List all available models and return name property" {
$Script:AllModels = Get-AiModels
$AllModels | gm | %{$_.name} | Should -Contain "name"
}

It "Default model <model> must be returned!" {
Set-AiDefaultModel $model;
$AllModels | ? { $_.name -like $model } | Should -Not -BeNullOrEmpty
}

Context "Chats" {
It "Garante chat default" -Tag "base" {
New-PowershaiChat -ChatId "default" -IfNotExists
Set-PowershaiActiveChat -ChatId "default"
}

It "Remover todos os demais chats" -Tag "base" {
$ChatAtivo = Get-PowershaiChat .
Get-PowershaiChat "*" | ? {$_.id -ne $ChatAtivo.id} | %{ Remove-PowershaiChat $_.id };
}

It "Chat Screen Output" {
Mock -ModuleName powershai write-host {}
Mock -ModuleName powershai write-warning {}

iat "Hi, this is a test from powershell ai module called Powershai";

Assert-MockCalled -ModuleName powershai write-host
Assert-MockCalled -ModuleName powershai write-warning
}

It "Pipeline context data" {
Mock -ModuleName powershai write-host {} -parameter { $Object -like "*two*" }
Mock -ModuleName powershai write-warning {}

1..10 | iat "write names of that numbers in english"

Assert-MockCalled -ModuleName powershai write-host
Assert-MockCalled -ModuleName powershai write-warning
}

It "Pipeline context foreach" {
Mock -ModuleName powershai write-host {}
Mock -ModuleName powershai write-warning {}

1..10 | iat -ForEach "write names of that numbers in english"

Assert-MockCalled -ModuleName powershai write-host -times 10
Assert-MockCalled -ModuleName powershai write-warning
}

It "Basic Chat history" {
ia "gere 5 palavras em pt-BR"
$lines = ia -Lines "Gere uma linha para cada nome, com a tradução para en-US" | %{ $_; write-host $_ }
$lines.count | Should -BeGreaterOrEqual 5
}

It "New Chat" -Tag "chatmemo" {
$Chat = New-PowershaiChat "Test-$ChatNum";

$null = Set-PowershaiChatParameter MaxTokens 100

@(Get-PowershaiChat .).id | Should -Be "Test-$ChatNum"

}

Context "Chat Memory" -Tag "chatmemo" {

BeforeAll {
$SecretGuid = [Guid]::NewGuid().Guid;
}

It "Send secret memory" {
ia "This is a secret data: $SecretGuid","I will ask about it later"
}

It "Return secret memory" {
$lines = ia -Lines "What is the secret data? Return just secret data in first line"
write-host $Lines;
@($lines -Join "`n") | Should -BeLike "*$SecretGuid*"
}

It "Forget secret" {
$Lines = ia -Lines -forget "What is the secret data? Return just secret data in first line"
@($lines -Join "`n") | Should -not -BeLike "*$SecretGuid*"
}
}

Context "Chat Params" -Tag "chatparams" {
It "Using MaxTokens from chat parameter" {
$null = Set-PowershaiChatParameter MaxTokens 50;
$null = iat "conte até 100" -lines
$Chat = Get-PowershaiChat .
$SentMaxTokens = $Chat.history[-1].Ret.interactions[0].sent.MaxTokens
$SentMaxTokens | Should -Be 50
}

It "Using MaxTokens from override" {
$null = iat "conte até 100" -lines -ChatParamsOverride @{ MaxTokens = 150 }
$Chat = Get-PowershaiChat .
$SentMaxTokens = $Chat.history[-1].Ret.interactions[0].sent.MaxTokens
$SentMaxTokens | Should -Be 150
}

It "MaxTokens default keep same" {
$null = Set-PowershaiChatParameter MaxTokens 50;
$null = iat "conte até 100" -lines
$Chat = Get-PowershaiChat .
$SentMaxTokens = $Chat.history[-1].Ret.interactions[0].sent.MaxTokens
$SentMaxTokens | Should -Be 50
}
}


}



}
4 changes: 0 additions & 4 deletions tests/pester/huggingface.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,4 @@ describe "Set-AiProvider HuggingFace" {
(Get-AiCurrentProvider).name | Should -Be "huggingface"
}





}
13 changes: 9 additions & 4 deletions util/publish-prepare.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
#
param()

if(-not(Get-Module -ListAvailable platyPS)){
write-host "Instaling PLatyPs module";
$m = Install-Module platyPS -force -PassThru
write-host " Installed: $($m.name) $($m.Version)"
"platyPS","pester" | %{
$ModName = $_

if(-not(Get-Module -ListAvailable $ModName)){
write-host "Instaling $ModName module";
$m = Install-Module $ModName -force -PassThru
write-host " Installed: $($m.name) $($m.Version)"
}

}

& ./util/publish.ps1 @Args
14 changes: 7 additions & 7 deletions util/publish.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
param(
$ApiKey = $Env:PSGALERY_KEY
,[switch]$CompileDoc
,[switch]$BasicTest
,[switch]$Tests
,[switch]$Publish
,[switch]$CheckVersion
)
Expand All @@ -27,6 +27,12 @@ if(!$POWERSHAI_PUBLISH_DATA.TempDir){

$TempDir = $POWERSHAI_PUBLISH_DATA.TempDir;

if($Tests){
write-host "Starting tests..."
Invoke-Pester -Output Detailed ./tests/pester
write-host " Test run!";
}

if($CompileDoc){
$PlatyDir = Join-Path $TempDir "platy"
$null = New-Item -Force -ItemType Directory -Path $PlatyDir
Expand All @@ -52,12 +58,6 @@ if($CheckVersion){
}
}

if($BasicTest){
write-host "Starting tests..."
./util/TestPowershai.ps1 -Basic;
write-host " Test run!";
}

if($Publish){
$PublishParams = @{
Path = $ModuleRoot
Expand Down

0 comments on commit c789253

Please sign in to comment.