Files
Redflag/aggregator-agent/internal/scanner/winget_path_test.go
jpetree331 8901f22a96 fix(windows): C-1 Windows-specific bug fixes
- Apply B-2 jitter and backoff fixes to Windows service (F-C1-5)
  Proportional jitter and exponential backoff now in service polling loop
- Add known winget install location search for SYSTEM account (F-C1-1)
  Checks PATH then system-wide WindowsApps locations
- Fix winget text parser for package names with spaces (F-C1-2)
  Column-position parsing from header keywords replaces whitespace split
- Add ghost update post-install state verification (F-C1-3)
  RebootRequired flag on InstallResult marks pending reboot
- Replace fmt.Printf with log.Printf in winget scanner (F-C1-6)
- Remove emoji from Windows service log messages (F-C1-7)

GOOS=linux build: PASS. All tests pass, no regressions.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-29 09:13:21 -04:00

76 lines
2.5 KiB
Go

package scanner
// winget_path_test.go — Pre-fix tests for winget path detection.
// [SHARED] — no build tag, compiles on all platforms.
//
// F-C1-1 HIGH: Winget located via exec.LookPath (PATH only).
// When running as SYSTEM service, winget is not in PATH.
//
// Run: cd aggregator-agent && go test ./internal/scanner/... -v -run TestWinget
import (
"os"
"strings"
"testing"
)
// ---------------------------------------------------------------------------
// Test 1.1 — Documents PATH-only winget search (F-C1-1)
//
// Category: PASS-NOW (documents the bug)
// ---------------------------------------------------------------------------
func TestWingetSearchesPathOnly(t *testing.T) {
// F-C1-1 HIGH: Winget located via PATH only. Windows service
// runs as SYSTEM which does not have %LOCALAPPDATA% in PATH.
// Per-user winget installs are invisible to the SYSTEM account.
content, err := os.ReadFile("winget.go")
if err != nil {
t.Fatalf("failed to read winget.go: %v", err)
}
src := string(content)
// Uses exec.LookPath for discovery
if !strings.Contains(src, `exec.LookPath("winget")`) {
t.Error("[ERROR] [agent] [scanner] expected exec.LookPath in winget discovery")
}
// POST-FIX: Also checks known system-wide install paths
hasKnownPaths := strings.Contains(src, "WindowsApps") ||
strings.Contains(src, `winget.exe`)
if !hasKnownPaths {
t.Error("[ERROR] [agent] [scanner] F-C1-1 NOT FIXED: no known path search")
}
t.Log("[INFO] [agent] [scanner] F-C1-1 FIXED: winget checks PATH and known locations")
}
// ---------------------------------------------------------------------------
// Test 1.2 — Winget must check known install locations (assert fix)
//
// Category: FAIL-NOW / PASS-AFTER-FIX
// ---------------------------------------------------------------------------
func TestWingetChecksKnownInstallLocations(t *testing.T) {
// F-C1-1: After fix, winget discovery must check known
// system-wide install paths in addition to PATH search.
content, err := os.ReadFile("winget.go")
if err != nil {
t.Fatalf("failed to read winget.go: %v", err)
}
src := string(content)
hasKnownPaths := strings.Contains(src, "WindowsApps") ||
strings.Contains(src, "LOCALAPPDATA") ||
strings.Contains(src, "ProgramFiles") ||
strings.Contains(src, `winget.exe`)
if !hasKnownPaths {
t.Errorf("[ERROR] [agent] [scanner] winget does not check known install locations.\n" +
"F-C1-1: must check known paths for SYSTEM service compatibility.")
}
}