Fossil.Tests.ps1

using module '.\Fossil.psm1'

Describe 'Write-FossilLog function tests' {
  BeforeAll {
    # Create Temp File
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'The variable ''Temp_File'' is assigned but never used.')]
    $Temp_File = New-TemporaryFile
  }

  Context 'When creating the header section' {
    It "should write the header section and provided header title: `" $HeaderTitle`"" {
      # Arrange/Act: Call the function with an input
      $HeaderTitle = "Pester Test"
      Write-FossilLog -Path $Temp_File -Header -HeaderTitle $HeaderTitle

      # Assert: Verify the result
      $Temp_File | Should -FileContentMatchMultiline "------------------------------------------------------------------------------\r\n $HeaderTitle\r\n------------------------------------------------------------------------------\r\n Started: .+\r\n Executed By: .+\r\n------------------------------------------------------------------------------"
    }

    It "should create the Fossil_Meta_StartDate environment variable" {
      # Arrange/Act: Call the function with an input
      $Result = Test-Path -Path env:Fossil_Meta_StartDate

      # Assert: Verify the result
      $Result | Should -be $true
    }

    It "should store a properly formatted string in Fossil_Meta_StartDate environment variable that can be converted to DatTime object" {
      # Arrange/Act: Call the function with an input
      $Result = [datetime]::Parse($env:Fossil_Meta_StartDate)

      # Assert: Verify the result
      $Result.GetType().Name | Should -be "DateTime"
    }
  }

  Context 'When writing a message' {
    It "should write the provided message `"$Message`"" {
      $Message = "Pester is currently running"
      # Arrange/Act: Call the function with an input
      Write-FossilLog -Path $Temp_File -Message $Message

      # Assert: Verify the result
      $Temp_File | Should -FileContentMatch "$Message"
    }
  }

  Context 'When creating the footer section' {
    It "should write the footer section and metadata" {
      # Arrange/Act: Call the function with an input
      Write-FossilLog -Path $Temp_File -Footer

      # Assert: Verify the result
      $Temp_File | Should -FileContentMatchMultiline "------------------------------------------------------------------------------\r\n Completed: .+\r\n Total Time: .+\r\n------------------------------------------------------------------------------\r\n End of Logging\r\n------------------------------------------------------------------------------"
    }
  }

  AfterAll {
    # Cleanup Temp File
    $Temp_File | Remove-Item -Force
  }
}

Describe 'Show-FossilStatusWindow function' {
  BeforeAll {
    # Create Temp File
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'The variable ''Temp_File'' is assigned but never used.')]
    $Temp_File = New-TemporaryFile
  }

  Context 'Invalid input scenarios' {
    It 'should throw an error when the log file does not exist at the path provided' {
      # Arrange/Act: Call the function with an input

      # Assert: Verify the result
      { Show-FossilStatusWindow -Path "C:\DoesNotExist\DoesNotExist.log" } | Should -Throw -ExpectedMessage 'The path provided for the log file is invalid.'
    }

    It 'should throw an error when the console name provided does not match the validate set restrictions' {
      # Arrange/Act: Call the function with an input

      # Assert: Verify the result
      { Show-FossilStatusWindow -Path $Temp_File -Console "InvalidConsoleName" } | Should -Throw
    }
  }

  Context 'Using Terminal as status window' {
    It 'should be able to get the process that was created by the function using the window title' {
      # Arrange/Act: Call the function with an input
      $WindowTitle = "Fossil (Pester Test) | VividRock"
      Show-FossilStatusWindow -Path $Temp_File -Console Terminal -Title $WindowTitle
      do {
        Start-Sleep -Seconds 1
        $Result = Get-Process -Name WindowsTerminal | Where-Object { ($_.MainWindowTitle -eq $WindowTitle) -or ($_.MainWindowTitle -eq "Administrator: $($WindowTitle)") }
      } until (
        $Result
      )

      # Assert: Verify the result
      $Result | Should -BeOfType System.Diagnostics.Process

      # Cleanup
      Stop-Process -InputObject $Result
    }
  }

  AfterAll {
    # Cleanup Temp File
    $Temp_File | Remove-Item -Force
  }
}