1param([Parameter(Mandatory=$true)][string]$Root,
2 [Parameter(Mandatory=$true)][string]$SharedName,
3 [Parameter(Mandatory=$true)][string]$ExpectedText,
4 [Parameter(Mandatory=$true)][string]$Text)
5Set-StrictMode -Version Latest
6$ErrorActionPreference = 'Stop'
7if ($SharedName -notmatch '^stage[45]-[a-z0-9-]+$') { throw 'Choose a stage 4 or 5 test directory name.' }
8& "$PSScriptRoot\cold-current.ps1" -Root $Root
9$shared = "A:\one-tests\$SharedName"
10if (-not (Test-Path "$shared\synthetic.one")) { throw 'Create the shared fixture before opening OneNote.' }
11$app = New-Object -ComObject OneNote.Application
12$notebook = ''
13try {
14 $app.OpenHierarchy($shared, '', [ref]$notebook, 0)
15 $section = ''
16 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
17 $deadline = [DateTime]::UtcNow.AddSeconds(30)
18 $matches = @()
19 do {
20 $hierarchy = ''
21 $app.GetHierarchy($section, 4, [ref]$hierarchy, 1)
22 [xml]$tree = $hierarchy
23 $pages = @($tree.SelectNodes('//*[local-name()="Page"]'))
24 if ($pages.Count -gt 1) { throw 'Expected one shared test page.' }
25 if ($pages.Count -eq 1) {
26 $content = ''
27 $app.GetPageContent($pages[0].GetAttribute('ID'), [ref]$content, 3, 1)
28 [xml]$page = $content
29 $matches = @($page.SelectNodes('//*[local-name()="T"]') | Where-Object { $_.InnerText -eq $ExpectedText })
30 if ($matches.Count -eq 1) { break }
31 }
32 Start-Sleep -Milliseconds 250
33 } while ([DateTime]::UtcNow -lt $deadline)
34 if ($matches.Count -ne 1) { throw 'Expected one matching shared text object.' }
35 [IO.File]::WriteAllText("$Root\before.xml", $content, [Text.Encoding]::UTF8)
36 Start-Sleep -Seconds 3
37 $matches[0].InnerText = [Security.SecurityElement]::Escape($Text)
38 $app.UpdatePageContent($page.OuterXml, [DateTime]::MinValue, 1, $false)
39 $app.SyncHierarchy($notebook)
40 Start-Sleep -Seconds 5
41 $content = ''
42 $app.GetPageContent($pages[0].GetAttribute('ID'), [ref]$content, 3, 1)
43 [IO.File]::WriteAllText("$Root\after.xml", $content, [Text.Encoding]::UTF8)
44} finally {
45 try { if ($notebook) { $app.CloseNotebook($notebook, $false) } }
46 finally {
47 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
48 $app = $null
49 [GC]::Collect()
50 [GC]::WaitForPendingFinalizers()
51 Get-Process ONENOTE -ErrorAction SilentlyContinue | Wait-Process -Timeout 10
52 }
53}