Files
ClaudeDo/tests/ClaudeDo.Ui.Tests/ViewModels/DetailsIslandReviewActionsTests.cs
T
mika kuns e1807fd53b fix(review): propagate HubException from ApproveReviewAsync so blocked merges surface errors
TryInvokeAsync swallowed all exceptions including HubException, so a
blocked merge (uncommitted changes in target, mid-merge state, inactive
worktree) returned null silently — the task stayed WaitingForReview with
no feedback shown.  Switch to a direct _hub.InvokeAsync so both VM
catch blocks (TasksIsland ErrorReported, DetailsIsland ShowErrorAsync)
actually fire.

Add regression tests for both call sites verifying that a throwing
worker causes the error to be reported.
2026-07-29 09:08:19 +02:00

165 lines
6.1 KiB
C#

using ClaudeDo.Data;
using ClaudeDo.Data.Models;
using ClaudeDo.Ui.Services;
using ClaudeDo.Ui.ViewModels.Islands;
using Microsoft.EntityFrameworkCore;
using TaskStatus = ClaudeDo.Data.Models.TaskStatus;
namespace ClaudeDo.Ui.Tests.ViewModels;
public class DetailsIslandReviewActionsTests : IDisposable
{
private readonly string _dbPath;
public DetailsIslandReviewActionsTests()
{
_dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_review_actions_test_{Guid.NewGuid():N}.db");
using var ctx = NewContext();
ctx.Database.EnsureCreated();
}
public void Dispose()
{
try { File.Delete(_dbPath); } catch { }
try { File.Delete(_dbPath + "-wal"); } catch { }
try { File.Delete(_dbPath + "-shm"); } catch { }
}
private ClaudeDoDbContext NewContext()
{
var opts = new DbContextOptionsBuilder<ClaudeDoDbContext>()
.UseSqlite($"Data Source={_dbPath}")
.Options;
return new ClaudeDoDbContext(opts);
}
private sealed class TestDbFactory : IDbContextFactory<ClaudeDoDbContext>
{
private readonly Func<ClaudeDoDbContext> _create;
public TestDbFactory(Func<ClaudeDoDbContext> create) => _create = create;
public ClaudeDoDbContext CreateDbContext() => _create();
}
private sealed class NullServiceProvider : IServiceProvider
{
public object? GetService(Type serviceType) => null;
}
private sealed class StubNotesApi : ClaudeDo.Ui.Services.Interfaces.INotesApi
{
public Task<List<DailyNoteDto>> ListAsync(DateOnly day) =>
Task.FromResult(new List<DailyNoteDto>());
public Task<DailyNoteDto?> AddAsync(DateOnly day, string text) =>
Task.FromResult<DailyNoteDto?>(null);
public Task UpdateAsync(string id, string text) => Task.CompletedTask;
public Task DeleteAsync(string id) => Task.CompletedTask;
}
private sealed class RecordingWorkerClient : StubWorkerClient
{
public override bool IsConnected => true;
public string? ParkedTaskId;
public override Task RejectReviewToIdleAsync(string taskId)
{
ParkedTaskId = taskId;
return Task.CompletedTask;
}
}
private DetailsIslandViewModel BuildVm(StubWorkerClient worker)
{
var factory = new TestDbFactory(NewContext);
return new DetailsIslandViewModel(factory, worker, new NullServiceProvider(), new StubNotesApi(), new ClaudeDo.Ui.Services.MergeCoordinator());
}
[Fact]
public async Task ParkReview_CallsRejectReviewToIdle_ForTheBoundTask()
{
var worker = new RecordingWorkerClient();
var vm = BuildVm(worker);
vm.Bind(new TaskRowViewModel { Id = "task-park-1", Status = TaskStatus.WaitingForReview });
await vm.ParkReviewCommand.ExecuteAsync(null);
Assert.Equal("task-park-1", worker.ParkedTaskId);
}
[Fact]
public void SendBack_IsDisabledUntilFeedbackIsEntered()
{
var vm = BuildVm(new RecordingWorkerClient());
vm.Bind(new TaskRowViewModel { Id = "task-fb-1", Status = TaskStatus.WaitingForReview });
Assert.False(vm.RejectReviewCommand.CanExecute(null));
vm.ReviewFeedback = "tighten the error handling";
Assert.True(vm.RejectReviewCommand.CanExecute(null));
vm.ReviewFeedback = " ";
Assert.False(vm.RejectReviewCommand.CanExecute(null));
}
[Fact]
public void Approve_IsEnabled_WhenThereIsNothingToReview()
{
// A childless sandbox run has no worktree diff to inspect, so the gate must
// not block it — it approves straight through.
var vm = BuildVm(new RecordingWorkerClient());
vm.Bind(new TaskRowViewModel { Id = "task-nodiff-1", Status = TaskStatus.WaitingForReview });
vm.Monitor.ApplyState(TaskStatus.WaitingForReview);
Assert.False(vm.Merge.HasReviewableDiff);
Assert.False(vm.ShowReviewDiffHint);
Assert.True(vm.ApproveReviewCommand.CanExecute(null));
}
[Fact]
public void Approve_IsGatedUntilDiffOpened_AndReLocksOnNewRun()
{
var vm = BuildVm(new RecordingWorkerClient());
vm.Bind(new TaskRowViewModel { Id = "task-diff-1", Status = TaskStatus.WaitingForReview });
vm.Merge.SyncWorktree("/tmp/wt", null, null, "Active", null);
vm.Monitor.ApplyState(TaskStatus.WaitingForReview);
// There is a diff to read, but it has not been opened → merge is blocked.
Assert.True(vm.Merge.HasReviewableDiff);
Assert.True(vm.ShowReviewDiffHint);
Assert.False(vm.ApproveReviewCommand.CanExecute(null));
// Opening the diff records the inspection and unlocks the merge.
vm.Merge.DiffViewed?.Invoke();
Assert.False(vm.ShowReviewDiffHint);
Assert.True(vm.ApproveReviewCommand.CanExecute(null));
// A new run means a fresh diff — the gate re-engages.
vm.Monitor.ApplyState(TaskStatus.Running);
vm.Monitor.ApplyState(TaskStatus.WaitingForReview);
Assert.True(vm.ShowReviewDiffHint);
Assert.False(vm.ApproveReviewCommand.CanExecute(null));
}
private sealed class ThrowingWorkerClient : StubWorkerClient
{
public override bool IsConnected => true;
public string ExceptionMessage { get; init; } = "blocked: target working tree has uncommitted changes";
public override Task<MergeResultDto?> ApproveReviewAsync(string taskId, string targetBranch) =>
throw new Exception(ExceptionMessage);
}
[Fact]
public async Task ApproveReview_WhenWorkerThrows_CallsShowErrorAsync()
{
var worker = new ThrowingWorkerClient();
var vm = BuildVm(worker);
vm.Bind(new TaskRowViewModel { Id = "task-err-1", Status = TaskStatus.WaitingForReview });
vm.Monitor.ApplyState(TaskStatus.WaitingForReview);
string? reportedError = null;
vm.ShowErrorAsync = msg => { reportedError = msg; return Task.CompletedTask; };
await vm.ApproveReviewCommand.ExecuteAsync(null);
Assert.Equal(worker.ExceptionMessage, reportedError);
}
}