同步主线并沿用已隔离的会话发现测试
- 作者
- Lee-xeo
- 时间
- 2026/11/09 23:25
- 提交
- 87c28c2
这是一个合并提交,下面是和第一个上一提交相比的改动。
这是一个合并提交,下面是和第一个上一提交相比的改动。
| @@ -223,6 +223,8 @@ internal sealed class MainWindow : Form | ||
| 223 | 223 | using var shutdown = CancellationTokenSource.CreateLinkedTokenSource(lifetime.Token); |
| 224 | 224 | shutdown.CancelAfter(TimeSpan.FromSeconds(20)); |
| 225 | 225 | var errors = new List<string>(); |
| 226 | 新增: int? prepareExit = null; | |
| 227 | 新增: string? prepareStatus = null; | |
| 226 | 228 | if (failure != null) errors.Add(failure); |
| 227 | 229 | try |
| 228 | 230 | { |
| @@ -242,6 +244,8 @@ internal sealed class MainWindow : Form | ||
| 242 | 244 | { |
| 243 | 245 | if (nativeBridgeReady) smokePhase = "stopping_backend"; |
| 244 | 246 | var (exit, data) = await Backend.Json(["prepare-update", "--json"], shutdown.Token, TimeSpan.FromSeconds(20)); |
| 247 | 新增: prepareExit = exit; | |
| 248 | 新增: prepareStatus = data.Text("status") is "prepared" or "stop_required" or "error" ? data.Text("status") : "unexpected"; | |
| 245 | 249 | if (exit != 0 || data.Text("status") != "prepared") throw new IOException("自检结束时无法确认后台已停止"); |
| 246 | 250 | await Task.WhenAll(controllers.Select(process => process.WaitForExitAsync(shutdown.Token))); |
| 247 | 251 | } |
| @@ -257,7 +261,7 @@ internal sealed class MainWindow : Form | ||
| 257 | 261 | environment.BrowserProcessExited += BrowserExited; |
| 258 | 262 | try |
| 259 | 263 | { |
| 260 | 删除: if (nativeBridgeReady) smokePhase = "stopping_browser"; | |
| 264 | 新增: if (nativeBridgeReady && errors.Count == 0) smokePhase = "stopping_browser"; | |
| 261 | 265 | web.Dispose(); |
| 262 | 266 | await exited.Task.WaitAsync(shutdown.Token); |
| 263 | 267 | } |
| @@ -269,7 +273,7 @@ internal sealed class MainWindow : Form | ||
| 269 | 273 | if (nativeBridgeReady && errors.Count == 0) smokePhase = "completed"; |
| 270 | 274 | File.WriteAllText(Program.SmokeOutput, JsonSerializer.Serialize(new { |
| 271 | 275 | success = nativeBridgeReady && errors.Count == 0, nativeBridgeReady, version = AppUpdater.Version, |
| 272 | 删除: phase = smokePhase, nativeStatus = message.Text, errors, page, | |
| 276 | 新增: phase = smokePhase, nativeStatus = message.Text, prepareExit, prepareStatus, errors, page, | |
| 273 | 277 | pageURL, webviewVersion, |
| 274 | 278 | windowBounds = Bounds, workingArea = Screen.FromHandle(Handle).WorkingArea, deviceDpi = DeviceDpi, |
| 275 | 279 | nativeMenuBar = MainMenuStrip != null, |
| @@ -138,12 +138,12 @@ func TestArchivedCLIHistoryIsNotRediscovered(t *testing.T) { | ||
| 138 | 138 | s, id, project := metadataFixture(t) |
| 139 | 139 | home, _ := filepath.EvalSymlinks(t.TempDir()) |
| 140 | 140 | bin := filepath.Join(home, "bin") |
| 141 | 删除: if err := os.Mkdir(bin, 0700); err != nil { | |
| 141 | 新增: if err := os.MkdirAll(bin, 0700); err != nil { | |
| 142 | 142 | t.Fatal(err) |
| 143 | 143 | } |
| 144 | 删除: // Discovery queries the CLI through the guest login shell. Keep that | |
| 145 | 删除: // command local to the fixture while exercising the real discovery path. | |
| 146 | 删除: const cli = "#!/bin/sh\n[ \"$#\" -eq 2 ] && [ \"$1\" = agents ] && [ \"$2\" = --json ] || exit 2\nprintf '[]\\n'\n" | |
| 144 | 新增: // This archived history has no live CLI process. Keep the real discovery | |
| 145 | 新增: // helper and SSH path independent of a developer's installed Claude CLI. | |
| 146 | 新增: cli := "#!/bin/sh\n[ \"$#\" -eq 2 ] && [ \"$1\" = agents ] && [ \"$2\" = --json ] || exit 64\nprintf '[]\\n'\n" | |
| 147 | 147 | if err := os.WriteFile(filepath.Join(bin, "claude"), []byte(cli), 0700); err != nil { |
| 148 | 148 | t.Fatal(err) |
| 149 | 149 | } |
| @@ -194,3 +194,71 @@ func TestPrepareUpdateDoesNotAcceptUnresponsiveLockedController(t *testing.T) { | ||
| 194 | 194 | t.Fatal("locked offline service accepted as safe to replace") |
| 195 | 195 | } |
| 196 | 196 | } |
| 197 | 新增: | |
| 198 | 新增:func TestPrepareUpdateWaitsForRoutineProbeAndPreservesActiveWork(t *testing.T) { | |
| 199 | 新增: for _, activeJob := range []bool{false, true} { | |
| 200 | 新增: t.Run(strconv.FormatBool(activeJob), func(t *testing.T) { | |
| 201 | 新增: s := testServer(t) | |
| 202 | 新增: var err error | |
| 203 | 新增: s.identity, err = currentController() | |
| 204 | 新增: if err != nil { | |
| 205 | 新增: t.Fatal(err) | |
| 206 | 新增: } | |
| 207 | 新增: lock, err := openControllerLock(s.profile, os.O_CREATE|os.O_RDWR) | |
| 208 | 新增: if err != nil { | |
| 209 | 新增: t.Fatal(err) | |
| 210 | 新增: } | |
| 211 | 新增: defer lock.Close() | |
| 212 | 新增: if err := lockController(lock); err != nil { | |
| 213 | 新增: t.Fatal(err) | |
| 214 | 新增: } | |
| 215 | 新增: defer unlockController(lock) | |
| 216 | 新增: var stopped atomic.Bool | |
| 217 | 新增: shutdownDone := make(chan struct{}) | |
| 218 | 新增: s.shutdown = func() { | |
| 219 | 新增: stopped.Store(true) | |
| 220 | 新增: _ = unlockController(lock) | |
| 221 | 新增: close(shutdownDone) | |
| 222 | 新增: } | |
| 223 | 新增: server := httptest.NewServer(s.handler()) | |
| 224 | 新增: defer server.Close() | |
| 225 | 新增: _, portString, _ := net.SplitHostPort(server.Listener.Addr().String()) | |
| 226 | 新增: port, _ := strconv.Atoi(portString) | |
| 227 | 新增: s.origin = "http://localhost:" + portString | |
| 228 | 新增: if err := writePrivate(filepath.Join(s.profile, "control.json"), controlFile{Port: port, Token: s.token, PID: os.Getpid(), Controller: s.identity}); err != nil { | |
| 229 | 新增: t.Fatal(err) | |
| 230 | 新增: } | |
| 231 | 新增: if activeJob { | |
| 232 | 新增: s.setJobForTest(Job{ID: "active", Running: true}) | |
| 233 | 新增: } | |
| 234 | 新增: // The first handoff reaches a real occupied operation lock. A | |
| 235 | 新增: // routine probe releases it without creating a user job or VM. | |
| 236 | 新增: s.op.Lock() | |
| 237 | 新增: released := make(chan struct{}) | |
| 238 | 新增: go func() { | |
| 239 | 新增: time.Sleep(100 * time.Millisecond) | |
| 240 | 新增: s.op.Unlock() | |
| 241 | 新增: close(released) | |
| 242 | 新增: }() | |
| 243 | 新增: result, err := PrepareUpdate(s.configPath, false) | |
| 244 | 新增: <-released | |
| 245 | 新增: if result.Status == "prepared" { | |
| 246 | 新增: select { | |
| 247 | 新增: case <-shutdownDone: | |
| 248 | 新增: case <-time.After(time.Second): | |
| 249 | 新增: t.Fatal("prepared update did not finish shutting down") | |
| 250 | 新增: } | |
| 251 | 新增: } | |
| 252 | 新增: if err != nil { | |
| 253 | 新增: t.Fatal(err) | |
| 254 | 新增: } | |
| 255 | 新增: if activeJob { | |
| 256 | 新增: if result.Status != "stop_required" || stopped.Load() { | |
| 257 | 新增: t.Fatalf("active work was not preserved: %+v stopped=%v", result, stopped.Load()) | |
| 258 | 新增: } | |
| 259 | 新增: } else if result.Status != "prepared" || !stopped.Load() { | |
| 260 | 新增: t.Fatalf("routine probe prevented idle update: %+v stopped=%v", result, stopped.Load()) | |
| 261 | 新增: } | |
| 262 | 新增: }) | |
| 263 | 新增: } | |
| 264 | 新增:} | |
| @@ -276,15 +276,35 @@ func PrepareUpdate(configPath string, stop bool) (DesktopResult, error) { | ||
| 276 | 276 | return DesktopResult{Status: "prepared"}, nil |
| 277 | 277 | } |
| 278 | 278 | if c.launch.Controller.Protocol == controlProtocol { |
| 279 | 删除: status, err := c.call("POST", "/api/handoff", map[string]string{"token": c.record.Token}, nil) | |
| 280 | 删除: if err != nil { | |
| 281 | 删除: return DesktopResult{}, err | |
| 282 | 删除: } | |
| 283 | 删除: if status == http.StatusOK { | |
| 284 | 删除: if err = c.waitForExit(); err != nil { | |
| 279 | 新增: // Routine status probes also hold the operation lock. Retry briefly. | |
| 280 | 新增: // every request still checks running jobs, terminals and VM ownership. | |
| 281 | 新增: handoffCtx, cancelHandoff := context.WithTimeout(ctx, 3*time.Second) | |
| 282 | 新增: defer cancelHandoff() | |
| 283 | 新增: conflicted := false | |
| 284 | 新增: handoff: | |
| 285 | 新增: for { | |
| 286 | 新增: status, err := c.callContext(handoffCtx, "POST", "/api/handoff", map[string]string{"token": c.record.Token}, nil) | |
| 287 | 新增: if err != nil { | |
| 288 | 新增: if conflicted && handoffCtx.Err() != nil { | |
| 289 | 新增: break | |
| 290 | 新增: } | |
| 285 | 291 | return DesktopResult{}, err |
| 286 | 292 | } |
| 287 | 删除: return DesktopResult{Status: "prepared"}, nil | |
| 293 | 新增: if status == http.StatusOK { | |
| 294 | 新增: if err = c.waitForExit(); err != nil { | |
| 295 | 新增: return DesktopResult{}, err | |
| 296 | 新增: } | |
| 297 | 新增: return DesktopResult{Status: "prepared"}, nil | |
| 298 | 新增: } | |
| 299 | 新增: if status != http.StatusConflict { | |
| 300 | 新增: break | |
| 301 | 新增: } | |
| 302 | 新增: conflicted = true | |
| 303 | 新增: select { | |
| 304 | 新增: case <-handoffCtx.Done(): | |
| 305 | 新增: break handoff | |
| 306 | 新增: case <-time.After(50 * time.Millisecond): | |
| 307 | 新增: } | |
| 288 | 308 | } |
| 289 | 309 | } |
| 290 | 310 | return DesktopResult{Status: "stop_required", Message: "请保存文件后确认停止环境并安装更新。终端任务会结束,项目和登录状态会保留。"}, nil |
| @@ -5,7 +5,6 @@ param([Parameter(Mandatory)][string]$Installer) | ||
| 5 | 5 | . (Join-Path $PSScriptRoot 'windows-common.ps1') |
| 6 | 6 | if (-not $IsWindows) { throw 'Installation smoke tests must run on Windows.' } |
| 7 | 7 | $target = Join-Path $env:LOCALAPPDATA ('Programs/Cage-test-' + [guid]::NewGuid().ToString('N')) |
| 8 | 删除:$report = Join-Path ([IO.Path]::GetTempPath()) ('cage-smoke-' + [guid]::NewGuid().ToString('N') + '.json') | |
| 9 | 8 | $savedKey = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\ClaudeCage' |
| 10 | 9 | if (Test-Path $savedKey) { throw 'Use a disposable Windows user without an existing Cage installation.' } |
| 11 | 10 | $fixture = Join-Path $PSScriptRoot 'personal-data-fixture.mjs' |
| @@ -74,17 +73,10 @@ Invoke-BuildTool (Join-Path $target 'resources/cage.exe') @('verify-web-assets') | ||
| 74 | 73 | Invoke-BuildTool (Join-Path $target 'resources/mihomo.exe') @('-v') |
| 75 | 74 | Invoke-BuildTool (Join-Path $target 'resources/runtime/qemu/qemu-system-x86_64.exe') @('--version') |
| 76 | 75 | Invoke-BuildTool (Join-Path $target 'resources/runtime/openssh/ssh.exe') @('-V') |
| 77 | 删除:$savedProfile = $env:CLAUDE_CAGE_CONFIG | |
| 78 | 删除:try { | |
| 79 | 删除: # The UI test owns a separate profile; the installer must preserve the | |
| 80 | 删除: # pre-existing default profile without asking the app to parse test bytes. | |
| 81 | 删除: $env:CLAUDE_CAGE_CONFIG = Join-Path ([IO.Path]::GetTempPath()) ('cage-ui-profile-' + [guid]::NewGuid().ToString('N') + '/config.json') | |
| 82 | 删除: $window = Start-Process -FilePath (Join-Path $target 'Cage.exe') -ArgumentList @('--smoke-test', "`"$report`"") -PassThru | |
| 83 | 删除: if (-not $window.WaitForExit(180000)) { throw 'Cage WebView2/workbench smoke test timed out.' } | |
| 84 | 删除: if ($window.ExitCode -ne 0 -or -not (Test-Path -LiteralPath $report)) { throw "Cage desktop smoke test failed ($($window.ExitCode))." } | |
| 85 | 删除:} finally { $env:CLAUDE_CAGE_CONFIG = $savedProfile } | |
| 76 | 新增:# Use the same isolated profile, process lifetime and retained diagnostics as | |
| 77 | 新增:# build acceptance. The pre-existing default profile holds preservation markers. | |
| 78 | 新增:Assert-WindowsWorkbench -AppDirectory $target | |
| 86 | 79 | Assert-PersonalData |
| 87 | 删除:Get-Content -LiteralPath $report | |
| 88 | 80 | # A locked installed executable must fail before replacing the installation. |
| 89 | 81 | $before = (Get-FileHash -LiteralPath (Join-Path $target 'Cage.exe') -Algorithm SHA256).Hash |
| 90 | 82 | $locked = [IO.File]::Open((Join-Path $target 'Cage.exe'), [IO.FileMode]::Open, [IO.FileAccess]::Read, [IO.FileShare]::Read) |
| @@ -109,11 +101,15 @@ Assert-PersonalData | ||
| 109 | 101 | $uninstall = Start-Process -FilePath (Join-Path $target 'Uninstall.exe') -ArgumentList @('/S') -PassThru |
| 110 | 102 | if (-not $uninstall.WaitForExit(120000) -or $uninstall.ExitCode -ne 0) { throw 'Uninstall failed.' } |
| 111 | 103 | # NSIS may relaunch its uninstaller from TEMP so that it can delete the original. |
| 104 | 新增:# The launcher can disappear before the worker clears registration and shortcuts. | |
| 112 | 105 | $deadline = [DateTime]::UtcNow.AddSeconds(120) |
| 113 | 删除:while ((Test-Path -LiteralPath (Join-Path $target 'Uninstall.exe')) -and [DateTime]::UtcNow -lt $deadline) { Start-Sleep -Milliseconds 200 } | |
| 106 | 新增:while (((Test-Path -LiteralPath $target) -or (Test-Path $savedKey) -or | |
| 107 | 新增: (Test-Path -LiteralPath $desktopShortcut) -or (Test-Path -LiteralPath $startMenuDirectory)) -and | |
| 108 | 新增: [DateTime]::UtcNow -lt $deadline) { Start-Sleep -Milliseconds 200 } | |
| 114 | 109 | if (Test-Path -LiteralPath (Join-Path $target 'Cage.exe')) { throw 'Uninstall left Cage.exe behind.' } |
| 115 | 110 | if (Test-Path -LiteralPath (Join-Path $target 'Uninstall.exe')) { throw 'Uninstall left its launcher behind.' } |
| 116 | 111 | if (Test-Path -LiteralPath $target) { throw 'Uninstall left packaged files or directories that would block installation into the same folder.' } |
| 112 | 新增:if (Test-Path $savedKey) { throw 'Uninstall left the Cage registration behind.' } | |
| 117 | 113 | Assert-PersonalData |
| 118 | 114 | Assert-Shortcuts $false $false |
| 119 | 115 | Write-Host 'Install, WebView2 workbench, reinstall, uninstall and user-data preservation passed. VM startup was not tested.' |