Skip to content

Commit

Permalink
Renaming tests files, and add more tests (tools, get-achat retry logi…
Browse files Browse the repository at this point in the history
…c, etc.)
  • Loading branch information
rrg92 committed Dec 12, 2024
1 parent 27ca4f1 commit 961edb7
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ Describe "Provider Commands" -Tag "basic","core:providers" {
# Enter-AiProvider $provider.name


}

}
File renamed without changes.
File renamed without changes.
208 changes: 208 additions & 0 deletions tests/pester/001-core/004-util.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
Describe "Util Commands" -Tag "basic","utils" {
BeforeAll {

function GetOtherProvider {
$CurrentProvider = Get-AiCurrentProvider
$NewProvider = @(Get-AiProviders | ? {$_.name -ne $CurrentProvider.name} | Get-Random)[0]

return @{
current = $CurrentProvider.name
other = $NewProvider.name
}
}

}

Context "HashTableMerge" {

InModuleScope powershai {
It "Merge two tables" {
$tab = HashTableMerge @{a = 1} @{b = 2}

$tab.a,$tab.b | Should -Be 1,2;
}

It "Merge two tables, overwrite" {
$tab = HashTableMerge @{a = 1} @{a = 3}

$tab.a | Should -Be 3;
}

It "Merge two tables, filter" {
$tab = HashTableMerge @{a = 1; c = 5} @{b = 2; c = "mudar"} -filter {
param($k)

if($k.path -eq "/c"){

if(!$k.current){
return $true;
}

return $false;
}

return $true;
}

$tab.c | Should -Be 5;
}
}

}



Context "Confirm-PowershaiObjectSchema" {


It "Simple Object, Valid Object" {
$object = @{ a = 1; b = "teste" }
$schema = @{
a = [int]
b = [string]
}
$result = Confirm-PowershaiObjectSchema $object $schema
$result.valid | Should -Be $true
}

It "Simple Object, Invalid Object" {
$object = @{ a = 1; b = "teste" }
$schema = @{
a = [int]
b = [int]
}
$result = Confirm-PowershaiObjectSchema $object $schema


$ValidationA = $result.validations | ? { $_.path -eq "/a" }
$ValidationB = $result.validations | ? { $_.path -eq "/b" }

$result.valid | Should -Be $false
$ValidationA.valid | Should -Be $true
$ValidationB.valid | Should -Be $false
$ValidationB.IsTypeValid | Should -Be $false
}


It "Nested Object, Valid Object" {
$object = @{
a = 1
b = @{
b1 = 123
b2 = (Get-Date)
}
}

$schema = @{
a = [int]
b = @{
b1 = [int]
b2 = [datetime]
}
}

$result = Confirm-PowershaiObjectSchema $object $schema
$result.valid | Should -Be $true
}

It "Nested Object, Invalid Object, b.b2 type" {
$object = @{
a = 1
b = @{
b1 = 123
b2 = (Get-Date)
}
}

$schema = @{
a = [int]
b = @{
b1 = [int]
b2 = [int]
}
}

$result = Confirm-PowershaiObjectSchema $object $schema
$result.valid | Should -Be $false
}

It "Nested Object, Invalid Object, b type" {
$object = @{
a = 1
b = @{
b1 = 123
b2 = (Get-Date)
}
}

$schema = @{
a = [int]
b = [int]
}

$result = Confirm-PowershaiObjectSchema $object $schema
$result.valid | Should -Be $false
}

It "Nested Object With Array, valid" {
$object = @{
a = 1
b = @{
b1 = 123
b2 = @(
@{ itemNum = 0; itemName = "obj1" }
@{ itemNum = 1; itemName = "obj2" }
)
}
}

$schema = @{
a = [int]
b = @{
b1 = [int]
b2 = @{
'$schema' = [array]
itemNum = [int]
itemName = [string]
}
}
}

$result = Confirm-PowershaiObjectSchema $object $schema

$result.valid | Should -Be $true

}

It "Nested Object With Array, forgot `$schema" {
$object = @{
a = 1
b = @{
b1 = 123
b2 = @(
@{ itemNum = 0; itemName = "obj1" }
@{ itemNum = 1; itemName = "obj2" }
)
}
}

$schema = @{
a = [int]
b = @{
b1 = [int]
b2 = @{
#'$schema' = "array" --> simulating forgot!
itemNum = [int]
itemName = [string]
}
}
}

$result = Confirm-PowershaiObjectSchema $object $schema
$result.valid | Should -Be $false
}

}
}


138 changes: 138 additions & 0 deletions tests/pester/001-core/005-cmdlets.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
BeforeAll {
import-module ./powershai -force;
}

Describe "Get-AiChat" -Tags "get-aichat" {

BeforeAll {
Set-AiProvider openai;

$Control = @{
num = 0;
}

$CustomMessaege = @{
model = "pester-test"
choices = @(
@{
message = @{
content = "test123";
}
}
)
}

# Control output!
Mock -module powershai openai_Chat {

return $CustomMessaege
}

}


It "Returning test message" {
$resp = Get-AiChat;

$resp.choices[0].message.content | Should -be "test123"
}


It "Returning, content only" {
$resp = Get-AiChat -ContentOnly;

$resp | Should -be "test123"
}

It "Retry Logic retries" {
$MaxTries = Get-Random -Min 2 -Max 5;
{ Get-AiChat -Check "test456" -Retries $MaxTries} | Should -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times $MaxTries;
}

It "Retry Logic retries" {
$MaxTries = Get-Random -Min 2 -Max 5;
{ Get-AiChat -Check "test456" -Retries $MaxTries} | Should -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times $MaxTries;
}

It "Retry Logic, check like" {
$CustomMessaege.choices[0].message.content = "Powershai Test 123";

{$resp = Get-AiChat -CheckLike "*Test*" -Retries 2} | Should -Not -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times 1;

{$resp = Get-AiChat -CheckLike "*NotTest*" -Retries 2} | Should -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times 2;
}

It "Retry Logic, check regex" {
$CustomMessaege.choices[0].message.content = "Powershai Test 123";

{$resp = Get-AiChat -CheckRegex 'Test \d+' -Retries 2} | Should -Not -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times 1;

{$resp = Get-AiChat -CheckRegex 'Test$' -Retries 2} | Should -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times 2;
}

It "Retry Logic, check json" {
$CustomMessaege.choices[0].message.content = @{
a = 1
b = "String"
c = @{
c1 = "c.c1"
c2 = "c.c2"
}
d = @(
@{ id = 0 }
@{ id = 1 }
)
} | ConvertTo-Json


$schema = @{
a = [int]
b = [string]
c = @{
c1 = [string]
c2 = [string]
}
d = @{
'$schema' = [array]
id = [int]
}
}

{$resp = Get-AiChat -CheckJson $schema -Retries 2} | Should -Not -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times 1;

$schema.a = [string];

{$resp = Get-AiChat -CheckJson $schema -Retries 5} | Should -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times 5;
}


It "Retry Logic, custom script" {
$CustomMessaege.choices[0].message.content = "Test123";

$CheckScript = {
$_.choices[0].message.content -eq "Test123";
}

{$resp = Get-AiChat -CheckJson $CheckScript -Retries 2} | Should -Not -Throw


$CheckScript = {
$_.model -ne $CustomMessaege.model
}
{$resp = Get-AiChat -CheckJson $CheckScript -Retries 5} | Should -Throw
Assert-MockCalled -ModuleName powershai openai_Chat -Times 5;


}


}

File renamed without changes.
Loading

0 comments on commit 961edb7

Please sign in to comment.