Files
ClaudeDo/tests/ClaudeDo.Worker.Tests/External/HandoffMcpToolsTests.cs
T
mika kuns 4ef01274f9 feat(list-handler): hand off to a fresh session after Phase 2
The merge-helper ("Let Claude handle it") system prompt now calls a new
handoff_list_handler MCP tool once every surviving task is enhanced,
instead of continuing into Phases 3-5 in the same session -- avoiding
paying for Phases 0-2's dedupe/rewrite context on every polling round of
the run/review/merge phases.

The tool broadcasts HandoffRequested; Mission Control opens a second
ConPTY tile for the SAME handler task id (no new task, HandlerBaseCommit
untouched) running a fresh handoff brief that starts at Phase 3. The
original tile stays open. Adds PromptKind.MergeHelperHandoff,
InteractiveLaunchSpecService.BuildForMergeHelperHandoffAsync, and the
GetMergeHelperHandoffLaunchSpec hub method.
2026-08-05 20:27:39 +02:00

97 lines
3.3 KiB
C#

using ClaudeDo.Data;
using ClaudeDo.Data.Models;
using ClaudeDo.Data.Repositories;
using ClaudeDo.Worker.External;
using ClaudeDo.Worker.Hub;
using ClaudeDo.Worker.Tests.Infrastructure;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
namespace ClaudeDo.Worker.Tests.External;
public sealed class HandoffMcpToolsTests : IDisposable
{
private readonly DbFixture _db = new();
private readonly ClaudeDoDbContext _ctx;
private readonly TaskRepository _tasks;
private readonly ListRepository _lists;
private readonly CapturingHubContext _hubContext = new();
public HandoffMcpToolsTests()
{
_ctx = _db.CreateContext();
_tasks = new TaskRepository(_ctx);
_lists = new ListRepository(_ctx);
}
public void Dispose() { _ctx.Dispose(); _db.Dispose(); }
private HandoffMcpTools BuildSut() => new(_tasks, new HubBroadcaster(_hubContext));
private async Task<TaskEntity> SeedTaskAsync(string listId, TaskStatus status = TaskStatus.Idle, string title = "t")
{
var task = new TaskEntity
{
Id = Guid.NewGuid().ToString(), ListId = listId, Title = title,
Status = status, CreatedAt = DateTime.UtcNow, CommitType = "chore",
};
await _tasks.AddAsync(task);
return task;
}
private async Task<string> SeedListAsync()
{
var listId = Guid.NewGuid().ToString();
await _lists.AddAsync(new ListEntity { Id = listId, Name = "L", CreatedAt = DateTime.UtcNow });
return listId;
}
[Fact]
public async Task HandoffListHandler_ValidIds_BroadcastsAndReturnsCount()
{
var listId = await SeedListAsync();
var handlerTask = await SeedTaskAsync(listId, title: "List handler: Alpha");
var survivor = await SeedTaskAsync(listId, TaskStatus.WaitingForReview, title: "Survivor");
var sut = BuildSut();
var result = await sut.HandoffListHandler(handlerTask.Id, new[] { survivor.Id }, CancellationToken.None);
Assert.True(result.Requested);
Assert.Equal(handlerTask.Id, result.TaskId);
Assert.Equal(1, result.SurvivingCount);
var call = Assert.Single(_hubContext.Proxy.Calls);
Assert.Equal("HandoffRequested", call.Method);
Assert.Equal(handlerTask.Id, call.Args[0]);
}
[Fact]
public async Task HandoffListHandler_EmptySurvivingIds_Throws()
{
var listId = await SeedListAsync();
var handlerTask = await SeedTaskAsync(listId);
var sut = BuildSut();
await Assert.ThrowsAsync<InvalidOperationException>(() =>
sut.HandoffListHandler(handlerTask.Id, Array.Empty<string>(), CancellationToken.None));
}
[Fact]
public async Task HandoffListHandler_UnknownHandlerTask_Throws()
{
var sut = BuildSut();
await Assert.ThrowsAsync<InvalidOperationException>(() =>
sut.HandoffListHandler("missing", new[] { "x" }, CancellationToken.None));
}
[Fact]
public async Task HandoffListHandler_UnknownSurvivingTask_Throws()
{
var listId = await SeedListAsync();
var handlerTask = await SeedTaskAsync(listId);
var sut = BuildSut();
await Assert.ThrowsAsync<InvalidOperationException>(() =>
sut.HandoffListHandler(handlerTask.Id, new[] { "missing" }, CancellationToken.None));
}
}