QueuePicker's raw-SQL Queued->Running claim bypasses TaskStateService.StartRunningAsync, the only other place that broadcasts TaskUpdated on this transition, so the task-list badge stayed on "Queued" until the run finished. Send TaskUpdated for alreadyClaimed dispatches in TaskRunner.RunAsync/ContinueAsync to close that gap.
83 lines
3.5 KiB
C#
83 lines
3.5 KiB
C#
using ClaudeDo.Data;
|
|
using ClaudeDo.Data.Git;
|
|
using ClaudeDo.Data.Models;
|
|
using ClaudeDo.Data.Repositories;
|
|
using ClaudeDo.Worker.Config;
|
|
using ClaudeDo.Worker.Hub;
|
|
using ClaudeDo.Worker.Runner;
|
|
using ClaudeDo.Worker.Tests.Infrastructure;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
|
|
using Xunit;
|
|
|
|
namespace ClaudeDo.Worker.Tests.Runner;
|
|
|
|
/// Regression test for the queue-claim badge bug: QueuePicker's raw-SQL Queued->Running claim
|
|
/// bypasses TaskStateService.StartRunningAsync (which normally broadcasts TaskUpdated), so
|
|
/// alreadyClaimed=true dispatches must broadcast it themselves — otherwise the UI task-list
|
|
/// badge stays on "Queued" until the run finishes instead of flipping live.
|
|
public sealed class QueueClaimTaskUpdatedBroadcastTests : IDisposable
|
|
{
|
|
private readonly DbFixture _db = new();
|
|
private readonly string _tempDir;
|
|
private readonly WorkerConfig _cfg;
|
|
private readonly CapturingHubContext _hubContext = new();
|
|
|
|
public QueueClaimTaskUpdatedBroadcastTests()
|
|
{
|
|
_tempDir = Path.Combine(Path.GetTempPath(), $"cd_queueclaim_{Guid.NewGuid():N}");
|
|
Directory.CreateDirectory(_tempDir);
|
|
_cfg = new WorkerConfig { SandboxRoot = _tempDir, LogRoot = _tempDir };
|
|
}
|
|
|
|
public void Dispose() { _db.Dispose(); try { Directory.Delete(_tempDir, true); } catch { } }
|
|
|
|
private TaskRunner BuildRunner(IClaudeProcess claude)
|
|
{
|
|
var dbFactory = _db.CreateFactory();
|
|
var state = TaskStateServiceBuilder.Build(dbFactory).State;
|
|
var wt = new WorktreeManager(new GitService(), dbFactory, _cfg, NullLogger<WorktreeManager>.Instance);
|
|
return new TaskRunner(claude, dbFactory, new HubBroadcaster(_hubContext), wt,
|
|
new ClaudeArgsBuilder(), _cfg, NullLogger<TaskRunner>.Instance, state, new TaskRunTokenRegistry(),
|
|
new AttachmentStore(), new FakeSessionSkillSeeder(), new FakeTranscriptUsageReader());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task RunAsync_with_alreadyClaimed_broadcasts_TaskUpdated_before_the_run_finishes()
|
|
{
|
|
string listId = Guid.NewGuid().ToString(), taskId = Guid.NewGuid().ToString();
|
|
using (var ctx = _db.CreateContext())
|
|
{
|
|
ctx.Lists.Add(new ListEntity { Id = listId, Name = "L", WorkingDir = null, CreatedAt = DateTime.UtcNow });
|
|
// Mirrors what QueuePicker's raw SQL claim already did before dispatch.
|
|
ctx.Tasks.Add(new TaskEntity
|
|
{
|
|
Id = taskId, ListId = listId, Title = "T", Status = TaskStatus.Running,
|
|
StartedAt = DateTime.UtcNow, CreatedAt = DateTime.UtcNow,
|
|
});
|
|
await ctx.SaveChangesAsync();
|
|
}
|
|
|
|
var processStarted = new TaskCompletionSource();
|
|
var releaseProcess = new TaskCompletionSource();
|
|
var fake = new FakeClaudeProcess(async (_, _, _, _, _) =>
|
|
{
|
|
processStarted.TrySetResult();
|
|
await releaseProcess.Task;
|
|
return new RunResult { ExitCode = 0, ResultMarkdown = "ok" };
|
|
});
|
|
var runner = BuildRunner(fake);
|
|
|
|
Task runTask;
|
|
using (var ctx = _db.CreateContext())
|
|
runTask = runner.RunAsync((await new TaskRepository(ctx).GetByIdAsync(taskId))!, "queue", CancellationToken.None, alreadyClaimed: true);
|
|
|
|
await processStarted.Task;
|
|
|
|
Assert.Contains(_hubContext.Proxy.Calls, c => c.Method == "TaskUpdated" && (string)c.Args[0]! == taskId);
|
|
|
|
releaseProcess.TrySetResult();
|
|
await runTask;
|
|
}
|
|
}
|