Skip to content

Commit 0db674e

Browse files
committed
Ajustes para pruebas en pester de github
Ajustes para pruebas en pester de github
1 parent c8bafb1 commit 0db674e

3 files changed

Lines changed: 121 additions & 27 deletions

File tree

scripts/dark.ps1

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,91 @@
1-
function ToggleDarkMode {
2-
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
3-
4-
$appsMode = Get-ItemProperty -Path $regPath -Name "AppsUseLightTheme"
5-
$systemMode = Get-ItemProperty -Path $regPath -Name "SystemUsesLightTheme"
6-
7-
# Si está en modo claro, cambia ambos a oscuro
8-
if ($appsMode.AppsUseLightTheme -eq 1 -or $systemMode.SystemUsesLightTheme -eq 1) {
9-
Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 0
10-
Set-ItemProperty -Path $regPath -Name "SystemUsesLightTheme" -Value 0
11-
Write-Host "🖤 Cambiado a modo oscuro para apps y sistema"
1+
<#
2+
███████╗ █████╗ ██████╗ ██╗██████╗ ███████╗██╗ ██╗ ██╗██████╗
3+
██╔════╝██╔══██╗██╔══██╗██║██╔══██╗██╔════╝██║ ██║ ██╔╝╚════██╗
4+
█████╗ ███████║██████╔╝██║██║ ██║█████╗ ██║ ██║██╔╝ █████╔╝
5+
██╔══╝ ██╔══██║██╔══██╗██║██║ ██║██╔══╝ ╚██╗ ██╔╝╚██╗ ╚═══██╗
6+
██║ ██║ ██║██████╔╝██║██████╔╝███████╗ ╚████╔╝ ╚██╗██████╔╝
7+
╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝╚═════╝ ╚══════╝ ╚═══╝ ╚═╝╚═════╝
8+
🧠 Automation CLI by: FabiDev<3
9+
#>
10+
11+
param(
12+
[ValidateSet('Dark','Light','Toggle')]
13+
[string]$Mode = 'Toggle', # <— al llamar el archivo sin args entra aquí
14+
[switch]$PassThru # ← útil para las pruebas
15+
)
16+
17+
function Toggle-DarkMode {
18+
[CmdletBinding()]
19+
param(
20+
[ValidateSet('Dark','Light','Toggle')]
21+
[string]$Mode = 'Toggle',
22+
[switch]$PassThru
23+
)
24+
25+
$regPath = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize'
26+
27+
# Lee los valores actuales (0 = dark, 1 = light)
28+
$currentApps = (Get-ItemProperty -Path $regPath -Name AppsUseLightTheme).AppsUseLightTheme
29+
$currentSystem = (Get-ItemProperty -Path $regPath -Name SystemUsesLightTheme).SystemUsesLightTheme
30+
31+
switch ($Mode) {
32+
'Dark' { $new = 0 }
33+
'Light' { $new = 1 }
34+
'Toggle' { $new = if ($currentApps -eq 1 -or $currentSystem -eq 1) { 0 } else { 1 } }
35+
}
36+
37+
# Solo escribe si hay cambio
38+
if ($currentApps -ne $new -or $currentSystem -ne $new) {
39+
Set-ItemProperty -Path $regPath -Name AppsUseLightTheme -Value $new
40+
Set-ItemProperty -Path $regPath -Name SystemUsesLightTheme -Value $new
1241
}
13-
else {
14-
Set-ItemProperty -Path $regPath -Name "AppsUseLightTheme" -Value 1
15-
Set-ItemProperty -Path $regPath -Name "SystemUsesLightTheme" -Value 1
42+
43+
# Mensaje amigable
44+
if ($new -eq 0) {
45+
Write-Host "🖤 Cambiado a modo oscuro para apps y sistema"
46+
} else {
1647
Write-Host "🤍 Cambiado a modo claro para apps y sistema"
1748
}
1849

19-
## Option Reinicia el explorador para aplicar los cambios
20-
# Stop-Process -Name explorer -Force
21-
# Start-Process explorer
50+
# Notifica al shell sin cerrar sesión (silencioso si falla en pruebas)
51+
try {
52+
if (-not ('NativeMethods' -as [type])) {
53+
Add-Type @"
54+
using System;
55+
using System.Runtime.InteropServices;
56+
public class NativeMethods {
57+
[DllImport("user32.dll", SetLastError=true)]
58+
public static extern IntPtr SendMessageTimeout(
59+
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
60+
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
61+
}
62+
"@
63+
}
64+
[UIntPtr]$dummy = [UIntPtr]::Zero
65+
[NativeMethods]::SendMessageTimeout([IntPtr]::Zero, 0x1A,
66+
[UIntPtr]::Zero, 'ImmersiveColorSet', 0x0000, 100, [ref]$dummy) | Out-Null
67+
} catch {
68+
# Ignorar en entornos sin GUI (p.ej., GitHub Actions)
69+
}
70+
71+
if ($PassThru) {
72+
[PSCustomObject]@{
73+
Mode = if ($new -eq 0) { 'Dark' } else { 'Light' }
74+
Timestamp = Get-Date
75+
}
76+
}
77+
}
78+
79+
# ------------------------------------------------------------------
80+
# Si este archivo se ejecuta **directamente** (no dot-sourced), llama
81+
# a la función con los parámetros recibidos.
82+
# ------------------------------------------------------------------
83+
if ($MyInvocation.InvocationName -eq $MyInvocation.MyCommand.Path) {
84+
Toggle-DarkMode @PSBoundParameters
85+
}
86+
87+
# Exporta la función si alguien dot-sourcó el archivo o lo importó
88+
if ($ExecutionContext.SessionState.Module) {
89+
Export-ModuleMember -Function Toggle-DarkMode
2290
}
2391

24-
# Ejecutar la función
25-
ToggleDarkMode

tests/Pester/DarkMode.Tests.ps1

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# tests\Pester\DarkMode.Tests.ps1
2+
BeforeAll {
3+
$ScriptPath = Join-Path $PSScriptRoot '..\..\scripts\dark.ps1'
4+
5+
# Evita que el script escriba en el registro
6+
Mock -CommandName Set-ItemProperty -MockWith {}
7+
8+
# Carga dark.ps1 y expone la función Toggle-DarkMode
9+
. $ScriptPath
10+
}
11+
12+
Describe 'DarkMode' {
13+
14+
Context 'Modo forzado' {
15+
16+
It 'Devuelve Dark al usar -Mode Dark' {
17+
(Toggle-DarkMode -Mode Dark -PassThru).Mode | Should -Be 'Dark'
18+
}
19+
20+
It 'Devuelve Light al usar -Mode Light' {
21+
(Toggle-DarkMode -Mode Light -PassThru).Mode | Should -Be 'Light'
22+
}
23+
}
24+
25+
Context 'Cambio automático (Toggle)' {
26+
27+
It 'Alterna de Light a Dark' {
28+
# Simula que el sistema está en modo claro
29+
Mock -CommandName Get-ItemProperty -MockWith {
30+
@{ SystemUsesLightTheme = 1; AppsUseLightTheme = 1 }
31+
}
32+
33+
(Toggle-DarkMode -Mode Toggle -PassThru).Mode | Should -Be 'Dark'
34+
}
35+
}
36+
}

tests/Pester/ToggleDarkMode.Tests.ps1

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)