Repository navigation
test(windows): run history-recovery rows 1-3 against the real aw-server.exe in disposable profiles #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TimeToBuildBob
wants to merge
8
commits into
ActivityWatch:master
Choose a base branch
from
TimeToBuildBob:test/windows-recovery-matrix-rows-1-3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+662
−6
Open
test(windows): run history-recovery rows 1-3 against the real aw-server.exe in disposable profiles #864
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2a4ad5f
test(windows): recovery matrix row 1 (Local older, Roaming newer) ins…
TimeToBuildBob 4648f19
fix(test): tolerate empty event array and record bucket inventory in …
TimeToBuildBob 4ab0c88
fix(test): flush queued fixture inserts with a committing request, no…
TimeToBuildBob 21adb8b
test(windows): recovery matrix rows 2 and 3 with shared scenario helpers
TimeToBuildBob 788999a
fix(test): create row 2 fixtures as the default profile so no new-lay…
TimeToBuildBob 01d631e
fix(test): gate Windows fixtures on HTTP and verify recovery invariants
TimeToBuildBob 15c917a
fix(test): retry transient sentinel reads while recovery creates the …
TimeToBuildBob 4a45b32
test(windows): pin the URL the scenario hook polls for HTTP readiness
TimeToBuildBob File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| #Requires -Version 5.1 | ||
| # Hermetic regressions for the recovery harness; no Windows account or server. | ||
| Set-StrictMode -Version Latest | ||
| $ErrorActionPreference = 'Stop' | ||
| $mainPath = Join-Path $PSScriptRoot 'test-windows-disposable-profile.ps1' | ||
| $commonPath = Join-Path $PSScriptRoot 'windows-recovery-scenarios/common.ps1' | ||
| $tokens = $null | ||
| $errors = $null | ||
| $ast = [System.Management.Automation.Language.Parser]::ParseFile($mainPath, [ref]$tokens, [ref]$errors) | ||
| if ($errors.Count) { throw ($errors | Out-String) } | ||
| $waiter = $ast.Find({ param($node) | ||
| $node -is [System.Management.Automation.Language.FunctionDefinitionAst] -and | ||
| $node.Name -eq 'Wait-ServerHttpReady' | ||
| }, $true) | ||
| if ($null -eq $waiter) { throw 'missing HTTP readiness waiter' } | ||
| . ([scriptblock]::Create($waiter.Extent.Text)) | ||
| . $commonPath | ||
| function Assert-True { | ||
| param([bool]$Condition, [string]$Message) | ||
| if (-not $Condition) { throw $Message } | ||
| } | ||
| $root = Join-Path ([IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString('N')) | ||
| New-Item -ItemType Directory -Path $root | Out-Null | ||
| $savedPublic = $env:Public | ||
| $savedSystemRoot = $env:SystemRoot | ||
| try { | ||
| & { | ||
| $script:clock = [datetime]'2026-01-01' | ||
| $script:polls = 0 | ||
| function Get-Date { return $script:clock } | ||
| function Start-Sleep { param($Milliseconds) $script:clock = $script:clock.AddMilliseconds($Milliseconds) } | ||
| function Invoke-RestMethod { | ||
| param($Uri, $TimeoutSec) | ||
| $script:polls++ | ||
| if ($script:polls -lt 3) { throw 'HTTP not ready' } | ||
| return @{ version = 'test' } | ||
| } | ||
| Wait-ServerHttpReady -Process ([pscustomobject]@{ HasExited = $false }) -Port 1234 -TimeoutSeconds 2 | ||
| Assert-True ($script:polls -eq 3) 'HTTP readiness did not retry transient startup failures' | ||
| function Invoke-RestMethod { param($Uri, $TimeoutSec) throw 'never ready' } | ||
| $caught = $null | ||
| try { Wait-ServerHttpReady -Process ([pscustomobject]@{ HasExited = $false }) -Port 1234 -TimeoutSeconds 1 } | ||
| catch { $caught = $_.Exception.Message } | ||
| Assert-True ($null -ne $caught -and $caught -like '*HTTP*') 'HTTP timeout accepted' | ||
|
|
||
| # Execute the actual scenario hook: even with a valid SQLite file, | ||
| # a failed HTTP wait must stop it before the scenario writes fixtures. | ||
| $hook = $ast.Find({ param($node) | ||
| $node -is [System.Management.Automation.Language.IfStatementAst] -and | ||
| $node.Extent.Text -like 'if ($ScenarioPath)*' | ||
| }, $true) | ||
| $ScenarioPath = Join-Path $root 'scenario.ps1' | ||
| Set-Content -LiteralPath $ScenarioPath -Value '$script:scenarioRan = $true' | ||
| $script:scenarioRan = $false | ||
| # Pin the URL the hook polls: the mock used to ignore $Uri, so a | ||
| # harness polling the wrong port would have passed this guard. | ||
| $script:polledUris = @() | ||
| function Invoke-RestMethod { param($Uri, $TimeoutSec) $script:polledUris += $Uri; throw 'never ready' } | ||
| $serverProcess = [pscustomobject]@{ HasExited = $false } | ||
| $port = 1234 | ||
| $TimeoutSeconds = 1 | ||
| $evidence = @{} | ||
| $caught = $null | ||
| try { . ([scriptblock]::Create($hook.Extent.Text)) } | ||
|
TimeToBuildBob marked this conversation as resolved.
Resolved
|
||
| catch { $caught = $_.Exception.Message } | ||
| Assert-True ($null -ne $caught -and -not $script:scenarioRan) 'scenario ran before HTTP was ready' | ||
| Assert-True (($script:polledUris.Count -gt 0) -and -not ($script:polledUris | Where-Object { $_ -ne 'http://127.0.0.1:1234/api/0/info' })) ` | ||
| "scenario hook polled the wrong URL: $($script:polledUris -join ', ')" | ||
| } | ||
|
|
||
| & { | ||
| $env:Public = $root | ||
| $env:SystemRoot = $root | ||
| $exe = 'fake-server.exe' | ||
| $credential = $null | ||
| $runId = 'guard' | ||
| $scenario = @{} | ||
| $script:killed = $false | ||
| $script:waited = $false | ||
| $script:clock = [datetime]'2026-01-01' | ||
| function Get-Date { $script:clock = $script:clock.AddSeconds(121); return $script:clock } | ||
|
TimeToBuildBob marked this conversation as resolved.
Resolved
|
||
| function Start-Process { | ||
| param($FilePath, $ArgumentList, $Credential, [switch]$LoadUserProfile, [switch]$UseNewEnvironment, $WorkingDirectory, [switch]$PassThru) | ||
| Set-Content -LiteralPath (Join-Path $root 'aw-timeout-guard.log') -Value 'startup diagnostic' | ||
| $proc = [pscustomobject]@{ Id = 42; HasExited = $false } | ||
| $proc | Add-Member ScriptMethod WaitForExit { param($Timeout) $script:waited = $true; return $true } | ||
| return $proc | ||
| } | ||
| function taskkill.exe { | ||
| Assert-True (($args -join ' ') -eq '/PID 42 /T /F') 'wrong process tree killed' | ||
| $script:killed = $true | ||
| } | ||
| $caught = $null | ||
| try { Start-ScenarioServer -Name 'timeout' -Port 1234 -ExtraArgs '' } | ||
|
TimeToBuildBob marked this conversation as resolved.
Resolved
|
||
| catch { $caught = $_.Exception.Message } | ||
| Assert-True ($caught -like '*startup diagnostic*') 'startup diagnostic lost' | ||
| Assert-True ($script:killed -and $script:waited) 'timed-out process was leaked' | ||
| Assert-True (-not (Test-Path (Join-Path $root 'aw-timeout-guard.cmd'))) 'wrapper leaked' | ||
| Assert-True (-not (Test-Path (Join-Path $root 'aw-timeout-guard.log'))) 'log leaked' | ||
| Assert-True ($scenario['log_1234'] -contains 'startup diagnostic') 'cleanup discarded evidence' | ||
| } | ||
|
|
||
| & { | ||
| $scenario = @{} | ||
| $script:clock = [datetime]'2026-01-01' | ||
| $script:reads = 0 | ||
| function Get-Date { return $script:clock } | ||
| function Start-Sleep { param($Seconds) $script:clock = $script:clock.AddSeconds($Seconds) } | ||
| function Invoke-RestMethod { | ||
| param($Uri, $TimeoutSec) | ||
| if ($Uri -notlike '*/events*') { return [pscustomobject]@{ 'aw-guard' = 1 } } | ||
| $script:reads++ | ||
| if ($script:reads -lt 3) { throw 'bucket not found' } | ||
| return @([pscustomobject]@{ data = [pscustomobject]@{ sentinel = 'A-1' } }) | ||
| } | ||
| $got = Wait-ScenarioSentinels -Port 1234 -Expected @('A-1') -Bucket 'aw-guard' | ||
| Assert-True ((($got -join ',') -eq 'A-1') -and $script:reads -eq 3) 'sentinel polling aborted on a transient read failure' | ||
| function Invoke-RestMethod { param($Uri, $TimeoutSec) throw 'bucket never appeared' } | ||
| $caught = $null | ||
| try { $null = Wait-ScenarioSentinels -Port 1234 -Expected @('A-1') -Bucket 'aw-guard' -TimeoutSeconds 5 } | ||
| catch { $caught = $_.Exception.Message } | ||
| Assert-True ($caught -like '*never appeared*') 'persistent sentinel read failure was reported as empty' | ||
| } | ||
|
|
||
| & { | ||
| $scenario = @{} | ||
| foreach ($case in @('zero', 'inserted', 'skipped', 'missing')) { | ||
| $inserted = if ($case -eq 'inserted') { 1 } else { 0 } | ||
| $skipped = if ($case -eq 'skipped') { 3 } else { 0 } | ||
| $scenario['log_1234'] = if ($case -eq 'missing') { @('unrelated log') } else { | ||
| @("INFO History recovery from source: 0 buckets created, $inserted events inserted, $skipped duplicates skipped") | ||
| } | ||
| $caught = $null | ||
| try { Assert-NoRecoveryWork -Port 1234 } | ||
| catch { $caught = $_.Exception.Message } | ||
| Assert-True (($null -eq $caught) -eq ($case -eq 'zero')) "incorrect zero-work verdict for $case" | ||
| } | ||
| $scenario['log_1234'] = @('INFO History recovery from source: 0 buckets created, 0 events inserted, 3 duplicates skipped') | ||
| Assert-NoRecoveryWork -Port 1234 -AllowSkipped | ||
| } | ||
| & { | ||
| $rowPath = Join-Path $PSScriptRoot 'windows-recovery-scenarios/row1-local-older-roaming-newer.ps1' | ||
| $rowAst = [System.Management.Automation.Language.Parser]::ParseFile($rowPath, [ref]$tokens, [ref]$errors) | ||
| if ($errors.Count) { throw ($errors | Out-String) } | ||
| $preservation = $rowAst.Find({ param($node) | ||
|
TimeToBuildBob marked this conversation as resolved.
Resolved
|
||
| $node -is [System.Management.Automation.Language.ForEachStatementAst] -and | ||
| $node.Extent.Text -like '*pre-migration-*' | ||
| }, $true) | ||
| if ($null -eq $preservation) { throw 'missing original-tree assertions' } | ||
| $disposableProfile = $root | ||
| $localApp = Join-Path $root 'Local/aw-server-rust' | ||
| $roamApp = Join-Path $root 'Roaming/aw-server-rust' | ||
| $localBackup = "$localApp.pre-migration-test" | ||
| $roamBackup = "$roamApp.migrated-test" | ||
| foreach ($dir in @($localApp, $roamApp, $localBackup, $roamBackup)) { | ||
| New-Item -ItemType Directory -Path $dir -Force | Out-Null | ||
| Set-Content -LiteralPath (Join-Path $dir 'sqlite.db') -Value 'fixture' | ||
| } | ||
| $scenario = @{} | ||
| . ([scriptblock]::Create($preservation.Extent.Text)) | ||
| foreach ($backup in @($localBackup, $roamBackup)) { | ||
| $db = Join-Path $backup 'sqlite.db' | ||
| Remove-Item -LiteralPath $db | ||
| $caught = $null | ||
| try { . ([scriptblock]::Create($preservation.Extent.Text)) } | ||
| catch { $caught = $_.Exception.Message } | ||
| Assert-True ($null -ne $caught -and $caught -like '*not retained*') 'active database masked a missing original' | ||
| Set-Content -LiteralPath $db -Value 'fixture' | ||
| } | ||
| } | ||
| Write-Host 'PASS: HTTP readiness gates fixtures, startup timeout cleans its process tree and evidence, sentinel polling retries transient reads, restart counts fail closed, both originals required' | ||
| } | ||
| finally { | ||
| $env:Public = $savedPublic | ||
| $env:SystemRoot = $savedSystemRoot | ||
| Remove-Item -LiteralPath $root -Recurse -Force | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.