---
phase: 06-global-site-selection
plan: 05
subsystem: testing
tags: [xunit, moq, wpf, mvvm, weak-reference-messenger, global-sites]

# Dependency graph
requires:
  - phase: 06-global-site-selection/06-01
    provides: GlobalSitesChangedMessage, FeatureViewModelBase.GlobalSites, OnGlobalSitesChanged virtual hook
  - phase: 06-global-site-selection/06-02
    provides: MainWindowViewModel.GlobalSelectedSites, GlobalSitesSelectedLabel
  - phase: 06-global-site-selection/06-04
    provides: Tab VM OnGlobalSitesChanged overrides with local override protection
provides:
  - GlobalSiteSelectionTests (10 unit tests covering full global site selection contract)
  - Test coverage for message broadcast, base class reception, single/multi-site pre-fill
  - Test coverage for local override, override reset, tenant switch clearing, label update
affects: []

# Tech tracking
tech-stack:
  added: []
  patterns:
    - "TestFeatureViewModel inner class pattern — expose protected property for assertion via public accessor"
    - "WeakReferenceMessenger.Default.Reset() in test constructor — prevents cross-test message contamination"
    - "Reflection to set private bool flag (_hasLocalSiteOverride) for testing guard conditions without requiring a dialog to open"

key-files:
  created:
    - SharepointToolbox.Tests/ViewModels/GlobalSiteSelectionTests.cs
  modified: []

key-decisions:
  - "Test 8 (tenant switch) verifies override reset by sending new global sites after TenantSwitchedMessage — cleaner than asserting SiteUrl='' since OnSiteUrlChanged immediately re-applies global when SiteUrl is cleared and GlobalSites is non-empty"
  - "Used reflection to set _hasLocalSiteOverride in PermissionsViewModel override test — avoids needing a real SitePickerDialog; acceptable for unit test scenario coverage"
  - "MainWindowViewModel instantiated with real ProfileRepository (temp file path) and MsalClientFactory() — avoids needing to refactor VM for testability while still keeping test hermetic"

patterns-established:
  - "Messenger reset pattern: WeakReferenceMessenger.Default.Reset() in constructor prevents leakage between WeakReferenceMessenger-heavy tests"

requirements-completed:
  - SITE-01
  - SITE-02

# Metrics
duration: 3min
completed: 2026-04-07
---

# Phase 06 Plan 05: GlobalSiteSelectionTests Summary

**10 unit tests validating the full global site selection contract — message broadcast, base class GlobalSites property, single-site pre-fill, multi-site pre-populate, local override protection, override reset on clear, tenant switch clearing, and toolbar label count**

## Performance

- **Duration:** ~3 min
- **Started:** 2026-04-07T08:11:40Z
- **Completed:** 2026-04-07T08:14:30Z
- **Tasks:** 1
- **Files modified:** 1 created

## Accomplishments
- All 10 tests pass covering both SITE-01 (global consumption) and SITE-02 (local override) requirements
- Total test suite grows from 134 to 144 passing tests (22 skipped unchanged)
- Tests exercise the full flow: MainWindowViewModel broadcasts, FeatureViewModelBase receives, tab VMs react, local override blocks global, tenant switch resets state
- No regressions in any pre-existing test

## Task Commits

Each task was committed atomically:

1. **Task 1: Create GlobalSiteSelectionTests with comprehensive test coverage** - `80ef092` (test)

## Files Created/Modified
- `SharepointToolbox.Tests/ViewModels/GlobalSiteSelectionTests.cs` — 10 xUnit Fact tests covering all critical paths in the global site selection flow

## Decisions Made
- Test 8 revised to verify override-reset behavior indirectly: after `TenantSwitchedMessage`, sending new global sites verifies override was cleared (the simpler `Assert.Equal("", SiteUrl)` was wrong — `OnSiteUrlChanged` immediately re-applies GlobalSites when SiteUrl is cleared and GlobalSites is non-empty, which is correct designed behavior)
- Used `System.Reflection` to set `_hasLocalSiteOverride` on `PermissionsViewModel` for Test 7 — allows testing the guard without requiring a live dialog factory
- `MainWindowViewModel` instantiated via concrete `ProfileRepository(tempFile)` and `new MsalClientFactory()` — no refactoring needed, test remains hermetic

## Deviations from Plan

### Auto-fixed Issues

**1. [Rule 1 - Bug] Corrected Test 8 assertion to match actual StorageViewModel behavior**
- **Found during:** Task 1 (first test run)
- **Issue:** Initial Test 8 asserted `vm.SiteUrl == string.Empty` after tenant switch, but `OnSiteUrlChanged` immediately re-applies `GlobalSites[0].Url` when SiteUrl is cleared and GlobalSites is non-empty — this is correct, designed behavior (clearing = revert to global)
- **Fix:** Rewrote test to assert the real contract: after tenant switch, override flag is reset, so the next global sites message is applied to SiteUrl
- **Files modified:** SharepointToolbox.Tests/ViewModels/GlobalSiteSelectionTests.cs
- **Verification:** All 10 tests pass
- **Committed in:** 80ef092 (Task 1 commit)

---

**Total deviations:** 1 auto-fixed (1 bug — incorrect test assertion)
**Impact on plan:** Fix was necessary for test correctness; the assertion was wrong about the expected behavior, not the VM code.

## Issues Encountered
- First test run had 9/10 passing; Test 8 failed because the assertion tested an intermediate state that the VM immediately transitions through (SiteUrl clears then immediately re-fills from GlobalSites). Fixed by testing the stable end state instead.

## User Setup Required
None — no external service configuration required.

## Self-Check: PASSED

File exists: SharepointToolbox.Tests/ViewModels/GlobalSiteSelectionTests.cs
Commit 80ef092 exists in git log.
All 10 tests pass: `dotnet test --filter "GlobalSiteSelection"` → 10 Passed, 0 Failed.
No regressions: full suite → 144 Passed, 22 Skipped.

## Next Phase Readiness
- Phase 6 is complete — all 5 plans executed, all requirements SITE-01 and SITE-02 covered
- The global site selection feature is fully implemented and tested end-to-end
- No blockers for Phase 7

---
*Phase: 06-global-site-selection*
*Completed: 2026-04-07*
