A running task can call mcp__claudedo_run__AskUser(question) to block (up to 3 min) on a human answer. PendingQuestionRegistry holds the pending question + TaskCompletionSource; the tool broadcasts TaskQuestionAsked, awaits the answer (WorkerHub.AnswerTaskQuestion resolves it), and returns it as the tool result — or a 'proceed on your judgment' fallback on timeout. The run stays Running throughout (no status/schema change). ClaudeProcess raises MCP_TOOL_TIMEOUT so the 60s HTTP-MCP cap doesn't kill the wait; the run MCP is now wired for every task, not just standalone ones. System prompt updated to reconcile 'unattended'.
110 lines
3.7 KiB
C#
110 lines
3.7 KiB
C#
using ClaudeDo.Data.Models;
|
|
using ClaudeDo.Data.Repositories;
|
|
using ClaudeDo.Worker.Hub;
|
|
using ClaudeDo.Worker.Tests.Infrastructure;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Xunit;
|
|
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
|
|
|
namespace ClaudeDo.Worker.Tests.Hub;
|
|
|
|
public sealed class WorktreeStateHubTests : IDisposable
|
|
{
|
|
private readonly DbFixture _db = new();
|
|
|
|
public void Dispose() => _db.Dispose();
|
|
|
|
private WorkerHub CreateHub()
|
|
{
|
|
var broadcaster = new HubBroadcaster(new CapturingHubContext());
|
|
var hub = new WorkerHub(
|
|
null!, null!, null!, null!, broadcaster, _db.CreateFactory(),
|
|
null!, null!, null!, null!, null!, null!, null!, null!, null!, null!, null!, null!, null!,
|
|
null!, new ClaudeDo.Worker.Online.OnlineInboxConfig(), new ClaudeDo.Worker.Online.OnlineTokenStore(),
|
|
new ClaudeDo.Worker.Runner.PendingQuestionRegistry());
|
|
hub.Clients = new FakeHubCallerClients(new RecordingClientProxy());
|
|
hub.Context = new FakeHubCallerContext();
|
|
return hub;
|
|
}
|
|
|
|
private async Task<string> SeedWorktreeAsync(WorktreeState initial)
|
|
{
|
|
using var ctx = _db.CreateContext();
|
|
var listId = Guid.NewGuid().ToString();
|
|
var taskId = Guid.NewGuid().ToString();
|
|
await new ListRepository(ctx).AddAsync(new ListEntity
|
|
{
|
|
Id = listId, Name = "L", CreatedAt = DateTime.UtcNow,
|
|
});
|
|
await new TaskRepository(ctx).AddAsync(new TaskEntity
|
|
{
|
|
Id = taskId, ListId = listId, Title = "T",
|
|
Status = TaskStatus.Done, CreatedAt = DateTime.UtcNow, CommitType = "feat",
|
|
});
|
|
await new WorktreeRepository(ctx).AddAsync(new WorktreeEntity
|
|
{
|
|
TaskId = taskId, Path = "/tmp/x", BranchName = "claudedo/x",
|
|
BaseCommit = "deadbeef", State = initial, CreatedAt = DateTime.UtcNow,
|
|
});
|
|
return taskId;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetWorktreeState_Active_To_Discarded_Succeeds()
|
|
{
|
|
var taskId = await SeedWorktreeAsync(WorktreeState.Active);
|
|
var hub = CreateHub();
|
|
|
|
var ok = await hub.SetWorktreeState(taskId, WorktreeState.Discarded);
|
|
|
|
Assert.True(ok);
|
|
using var ctx = _db.CreateContext();
|
|
var row = await new WorktreeRepository(ctx).GetByTaskIdAsync(taskId);
|
|
Assert.Equal(WorktreeState.Discarded, row!.State);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetWorktreeState_Merged_To_Active_Throws()
|
|
{
|
|
var taskId = await SeedWorktreeAsync(WorktreeState.Merged);
|
|
var hub = CreateHub();
|
|
|
|
await Assert.ThrowsAsync<HubException>(() =>
|
|
hub.SetWorktreeState(taskId, WorktreeState.Active));
|
|
|
|
using var ctx = _db.CreateContext();
|
|
var row = await new WorktreeRepository(ctx).GetByTaskIdAsync(taskId);
|
|
Assert.Equal(WorktreeState.Merged, row!.State);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetWorktreeState_Discarded_To_Kept_Throws()
|
|
{
|
|
var taskId = await SeedWorktreeAsync(WorktreeState.Discarded);
|
|
var hub = CreateHub();
|
|
|
|
await Assert.ThrowsAsync<HubException>(() =>
|
|
hub.SetWorktreeState(taskId, WorktreeState.Kept));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetWorktreeState_SameState_IsNoOp()
|
|
{
|
|
var taskId = await SeedWorktreeAsync(WorktreeState.Active);
|
|
var hub = CreateHub();
|
|
|
|
var ok = await hub.SetWorktreeState(taskId, WorktreeState.Active);
|
|
|
|
Assert.True(ok);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task SetWorktreeState_Missing_Throws()
|
|
{
|
|
var hub = CreateHub();
|
|
|
|
await Assert.ThrowsAsync<HubException>(() =>
|
|
hub.SetWorktreeState("does-not-exist", WorktreeState.Discarded));
|
|
}
|
|
}
|