1param([Parameter(Mandatory=$true)][string]$Root,
2 [Parameter(Mandatory=$true)][string]$SharedPath,
3 [Parameter(Mandatory=$true)][string]$CloneHost,
4 [switch]$ResumeCache,
5 [int]$StartSequence = 1)
6function Keep-Container($page, $target) {
7 $container = $target
8 while ($container.ParentNode -ne $page.DocumentElement) { $container = $container.ParentNode }
9 foreach ($sibling in @($page.DocumentElement.ChildNodes)) {
10 if ($sibling -ne $container -and $sibling.LocalName -in @('Outline', 'Title', 'Image', 'InkDrawing')) {
11 [void]$page.DocumentElement.RemoveChild($sibling)
12 }
13 }
14}
15Set-StrictMode -Version Latest
16$ErrorActionPreference = 'Stop'
17. "$PSScriptRoot\text.ps1"
18if ($SharedPath -notmatch '^\\\\192\.168\.77\.1\\agent\\m6-[a-z0-9-]+$') { throw 'Choose a disposable Linux lab notebook.' }
19if ($env:COMPUTERNAME -ne $CloneHost) { throw 'The collaboration controller requires its owned clone.' }
20if (-not $ResumeCache) { & "$PSScriptRoot\cold-current.ps1" -Root $Root -CloneHost $CloneHost }
21$shared = $SharedPath
22if (-not (Test-Path "$shared\synthetic.one")) { throw 'Create the shared fixture first.' }
23New-Item -ItemType Directory "$Root\inbox", "$Root\outbox" -Force | Out-Null
24$user = 'HKCU:\Software\Microsoft\Office\Common\UserInfo'
25New-Item $user -Force | Out-Null
26New-ItemProperty $user -Name UserName -Value $CloneHost -PropertyType String -Force | Out-Null
27New-ItemProperty $user -Name UserInitials -Value $CloneHost.Substring($CloneHost.Length - 3) -PropertyType String -Force | Out-Null
28$app = New-Object -ComObject OneNote.Application
29$notebook = ''
30try {
31 $app.OpenHierarchy($shared, '', [ref]$notebook, 0)
32 $section = ''
33 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
34 [IO.File]::WriteAllText("$Root\ready", 'ready')
35 $deadline = [DateTime]::UtcNow.AddMinutes(30)
36 $sequence = $StartSequence
37 while ([DateTime]::UtcNow -lt $deadline) {
38 $name = '{0:d4}.json' -f $sequence
39 $input = "$Root\inbox\$name"
40 if (-not (Test-Path $input)) { Start-Sleep -Milliseconds 100; continue }
41 $command = Get-Content -Raw -Encoding UTF8 $input | ConvertFrom-Json
42 $output = "$Root\outbox\$sequence"
43 New-Item -ItemType Directory $output | Out-Null
44 try {
45 if ($command.action -in @('prepare-stress', 'stress')) {
46 . "$PSScriptRoot\stress.ps1"
47 Invoke-Stress $app $section $command $output $shared
48 }
49 elseif ($command.action -eq 'sync') { $app.SyncHierarchy($notebook) }
50 elseif ($command.action -eq 'kill-process') {
51 Get-Process ONENOTE | Stop-Process -Force
52 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
53 $app = New-Object -ComObject OneNote.Application
54 $app.OpenHierarchy($shared, '', [ref]$notebook, 0)
55 $app.OpenHierarchy('synthetic.one', $notebook, [ref]$section, 0)
56 }
57 elseif ($command.action -eq 'edit' -or $command.action -eq 'snapshot') {
58 $hierarchy = ''
59 $app.GetHierarchy($section, 4, [ref]$hierarchy, 1)
60 [IO.File]::WriteAllText("$output\hierarchy.xml", $hierarchy, [Text.Encoding]::UTF8)
61 [xml]$tree = $hierarchy
62 $matches = 0
63 $editedPage = $null
64 $i = 0
65 foreach ($node in $tree.SelectNodes('//*[local-name()="Page"]')) {
66 $content = ''
67 $app.GetPageContent($node.GetAttribute('ID'), [ref]$content, 1, 1)
68 [IO.File]::WriteAllText("$output\page-$i.xml", $content, [Text.Encoding]::UTF8)
69 [xml]$page = $content
70 if ($command.action -eq 'edit') {
71 $targets = @($page.SelectNodes('//*[local-name()="T"]') | Where-Object {
72 (Get-ParagraphText $_.InnerText) -eq $command.expected
73 })
74 if ($targets.Count -gt 1) { throw 'Expected one matching text object.' }
75 if ($targets.Count -eq 1) {
76 $matches++
77 if ($matches -gt 1) { throw 'The text occurs on multiple pages.' }
78 $targets[0].InnerText = [Security.SecurityElement]::Escape($command.text)
79 Keep-Container $page $targets[0]
80 $editedPage = $page
81 }
82 }
83 $i++
84 }
85 if ($command.action -eq 'edit' -and $matches -ne 1) { throw 'Expected one matching text object.' }
86 if ($editedPage) { $app.UpdatePageContent($editedPage.OuterXml, [DateTime]::MinValue, 1, $false) }
87 } elseif ($command.action -ne 'close') { throw 'Unknown collaboration command.' }
88 [IO.File]::WriteAllText("$output\done", 'ok')
89 } catch {
90 [IO.File]::WriteAllText("$output\error", $_.Exception.ToString())
91 }
92 if ($command.action -eq 'close') { break }
93 $sequence++
94 }
95} finally {
96 try { if ($notebook) { $app.CloseNotebook($notebook, $false) } }
97 finally {
98 [void][Runtime.InteropServices.Marshal]::FinalReleaseComObject($app)
99 $app = $null
100 [GC]::Collect()
101 [GC]::WaitForPendingFinalizers()
102 Get-Process ONENOTE -ErrorAction SilentlyContinue | Wait-Process -Timeout 10
103 [IO.File]::WriteAllText("$Root\closed", 'closed')
104 }
105}