# Get-GiteaReleases.Tests.ps1 # Import the module $modulePath = Join-Path -Path $PSScriptRoot -ChildPath '..\PS-GiteaUtilities.psm1' Import-Module -Name $modulePath -Force Describe 'Get-GiteaReleases' { BeforeEach { # Mock configuration load Mock -CommandName Get-GiteaConfiguration -ModuleName PS-GiteaUtilities -MockWith { return @{ giteaURL = 'https://mock.gitea.com' defaultOwner = 'mockuser' token = 'mocktoken' } } # Mock API response Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith { return @( @{ id = 1 tag_name = "v1.0" target_commitish = "main" name = "Release 1" body = "Initial release" url = "https://mock.gitea.com/api/v1/releases/1" html_url = "https://mock.gitea.com/releases/v1.0" tarball_url = "https://mock.gitea.com/releases/v1.0.tar.gz" zipball_url = "https://mock.gitea.com/releases/v1.0.zip" upload_url = "https://mock.gitea.com/releases/uploads" draft = $false prerelease = $false created_at = "2024-01-01T00:00:00Z" published_at = "2024-01-01T01:00:00Z" author = @{ id = 101 username = "devuser" email = "dev@norwichct.org" avatar_url = "https://mock.gitea.com/avatar.jpg" html_url = "https://mock.gitea.com/devuser" } assets = @( @{ id = 201 name = "tool.exe" size = 123456 download_count = 42 created_at = "2024-01-01T00:30:00Z" uuid = "uuid-1234" browser_download_url = "https://mock.gitea.com/releases/assets/tool.exe" } ) } ) } Mock -CommandName Write-Error -ModuleName PS-GiteaUtilities -MockWith { } } Context 'Basic usage' { It 'Should return a list of release objects' { $result = Get-GiteaReleases -repoOwner 'mockuser' -repoName 'mockrepo' -token 'mocktoken' $result | Should -Not -BeNullOrEmpty $result[0].Name | Should -Be 'Release 1' $result[0].Author.Username | Should -Be 'devuser' $result[0].Assets.Count | Should -Be 1 $result[0].Assets[0].Name | Should -Be 'tool.exe' } } Context 'Optional switches affect URL' { It 'Should include draft=true and pre-release=true in URL if switches are set' { $calledUrl = $null Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith { param($Uri) $script:calledUrl = $Uri return @() } Get-GiteaReleases -repoOwner 'mockuser' -repoName 'mockrepo' -includeDrafts -includePreReleases -page 2 -limit 10 -token 'mocktoken' $script:calledUrl | Should -Match 'draft=true' $script:calledUrl | Should -Match 'pre-release=true' $script:calledUrl | Should -Match 'page=2' $script:calledUrl | Should -Match 'limit=10' } } Context 'When required params are missing and no config' { BeforeEach { Mock -CommandName Get-GiteaConfiguration -ModuleName PS-GiteaUtilities -MockWith { return $null } } It 'Should throw when required parameters are not supplied' { { Get-GiteaReleases -repoName 'mockrepo' } | Should -Throw } } Context 'When API call fails' { BeforeEach { Mock -CommandName Invoke-RestMethod -ModuleName PS-GiteaUtilities -MockWith { throw "API call failed" } Mock -CommandName Write-Error -ModuleName PS-GiteaUtilities -MockWith { } } It 'Should catch and handle the error' { $null = Get-GiteaReleases -repoOwner 'mockuser' -repoName 'mockrepo' -token 'mocktoken' # Expect no throw, Write-Error should catch it } } }