Skip to content

Commit bc53485

Browse files
yotsudaclaude
andcommitted
test(naming): regression for get_current_location title-on-claim fix
Two xUnit cases pinned to PowerShellTools.GetCurrentLocation: - GetCurrentLocation_ConsoleSwitched_SetsWindowTitle verifies that SetWindowTitleAsync is called when FindReadyPipeAsync reports a newly-claimed (unowned) pipe. Without 5b3ae35's fix this test fails with Moq.MockException ("Expected invocation on the mock at least once, but was never performed"). - GetCurrentLocation_NotSwitched_DoesNotSetWindowTitle is the companion guard: established consoles must not re-trigger TryAssignNameToPid + SetWindowTitleAsync on every call, otherwise the round-trip is wasted (TryAssignNameToPid is idempotent and returns null on the second call) and the established title would be at risk of accidental rename if the queue logic ever changes. Verified locally by toggling the if (consoleSwitched) block in PowerShellTools.cs:109 — the first test fails as designed when the guard is removed and passes when restored. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent dbf3d57 commit bc53485

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

Tests/Unit/Proxy/PowerShellToolsTests.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,77 @@ public async Task GetCurrentLocation_PipeError_ReturnsErrorMessage()
135135
Assert.Contains("Pipe communication failed", result);
136136
}
137137

138+
[Fact]
139+
public async Task GetCurrentLocation_ConsoleSwitched_SetsWindowTitle()
140+
{
141+
// Regression: GetCurrentLocation previously discarded FindReadyPipeAsync's
142+
// consoleSwitched flag with `_`, so a get_current_location call that
143+
// claimed an unowned (Import-Module-only) console left the window title
144+
// at the OnImport placeholder "#PID ____" instead of assigning a name.
145+
// Arrange: discovery reports a switched (newly-claimed) pipe.
146+
_mockPipeDiscoveryService
147+
.Setup(s => s.FindReadyPipeAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
148+
.ReturnsAsync(new PipeDiscoveryResult(TestPipeName, true, new List<string>(), null));
149+
150+
_mockPowerShellService
151+
.Setup(s => s.GetCurrentLocationFromPipeAsync(TestPipeName, It.IsAny<CancellationToken>()))
152+
.ReturnsAsync("Location [FileSystem]: C:\\test");
153+
154+
_mockPowerShellService
155+
.Setup(s => s.SetWindowTitleAsync(TestPipeName, It.IsAny<string>(), It.IsAny<CancellationToken>()))
156+
.Returns(Task.CompletedTask);
157+
158+
_mockPipeDiscoveryService
159+
.Setup(s => s.CollectAllCachedOutputsAsync(It.IsAny<string>(), TestPipeName, It.IsAny<CancellationToken>()))
160+
.ReturnsAsync(new CachedOutputResult("", ""));
161+
162+
// Act
163+
var result = await PowerShellTools.GetCurrentLocation(
164+
_mockPowerShellService.Object,
165+
_mockPipeDiscoveryService.Object,
166+
agent_id: TestAgentId);
167+
168+
// Assert: SetWindowTitleAsync must be invoked at least once for the
169+
// switched pipe. Title text itself is generated by ConsoleSessionManager
170+
// and varies per run, so accept any non-empty string.
171+
_mockPowerShellService.Verify(
172+
s => s.SetWindowTitleAsync(TestPipeName, It.IsAny<string>(), It.IsAny<CancellationToken>()),
173+
Times.AtLeastOnce);
174+
Assert.Contains("Location [FileSystem]", result);
175+
}
176+
177+
[Fact]
178+
public async Task GetCurrentLocation_NotSwitched_DoesNotSetWindowTitle()
179+
{
180+
// Companion guard: an established pipe (consoleSwitched=false) must not
181+
// re-trigger the title-assignment path. Setting the title every call
182+
// would either redo the round-trip wastefully (TryAssignNameToPid is
183+
// idempotent so it returns null) or — if the name dictionary ever
184+
// changes — risk renaming a console mid-session.
185+
_mockPipeDiscoveryService
186+
.Setup(s => s.FindReadyPipeAsync(It.IsAny<string>(), It.IsAny<CancellationToken>()))
187+
.ReturnsAsync(new PipeDiscoveryResult(TestPipeName, false, new List<string>(), null));
188+
189+
_mockPowerShellService
190+
.Setup(s => s.GetCurrentLocationFromPipeAsync(TestPipeName, It.IsAny<CancellationToken>()))
191+
.ReturnsAsync("Location [FileSystem]: C:\\test");
192+
193+
_mockPipeDiscoveryService
194+
.Setup(s => s.CollectAllCachedOutputsAsync(It.IsAny<string>(), TestPipeName, It.IsAny<CancellationToken>()))
195+
.ReturnsAsync(new CachedOutputResult("", ""));
196+
197+
// Act
198+
await PowerShellTools.GetCurrentLocation(
199+
_mockPowerShellService.Object,
200+
_mockPipeDiscoveryService.Object,
201+
agent_id: TestAgentId);
202+
203+
// Assert
204+
_mockPowerShellService.Verify(
205+
s => s.SetWindowTitleAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<CancellationToken>()),
206+
Times.Never);
207+
}
208+
138209
#endregion
139210

140211
#region InvokeExpression Tests

0 commit comments

Comments
 (0)