Files
ClaudeDo/tests/ClaudeDo.Ui.Tests/ViewModels/DetailsIslandResetAndRetryTests.cs
T
mika kuns 166021049a fix(ui): close interactive-session gate bypasses in reset-and-retry and plan queueing
Reset & Retry discarded the branch and queued an autonomous run even while the
user had an interactive ConPTY pane open on the task, and finalizing a plan
queued every child unconditionally (the hub has no notion of a UI-hosted
session) — both bypassed the HasInteractiveSession gate added for
CanSendToQueue. CanResetAndRetry now checks it too, with a subscription on the
bound task so the command re-evaluates when the flag flips without Task
itself changing; SendToQueueAsync now blocks queuing the whole plan and
surfaces the affected child titles when any child has an open session.
2026-08-06 14:30:24 +02:00

137 lines
5.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;
// Covers the Reset & Retry gate: it discards the branch/uncommitted work and queues an
// autonomous run into the same worktree, so it must stay off while an interactive ConPTY
// pane is open on the task (mirrors TaskRowViewModel.CanSendToQueue's HasInteractiveSession gate).
public class DetailsIslandResetAndRetryTests : IDisposable
{
private readonly string _dbPath;
public DetailsIslandResetAndRetryTests()
{
_dbPath = Path.Combine(Path.GetTempPath(), $"claudedo_reset_retry_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;
}
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 void ResetAndRetry_IsDisabled_WhileTaskHasInteractiveSession()
{
var vm = BuildVm(new RecordingWorkerClient());
var row = new TaskRowViewModel { Id = "task-reset-1", Status = TaskStatus.Failed, HasInteractiveSession = true };
vm.Bind(row);
vm.Monitor.ApplyState(TaskStatus.Failed);
Assert.True(vm.ShowResetAndRetry);
Assert.False(vm.ResetAndRetryCommand.CanExecute(null));
}
[Fact]
public void ResetAndRetry_ReEnables_WhenInteractiveSessionCloses()
{
var vm = BuildVm(new RecordingWorkerClient());
var row = new TaskRowViewModel { Id = "task-reset-2", Status = TaskStatus.Failed, HasInteractiveSession = true };
vm.Bind(row);
vm.Monitor.ApplyState(TaskStatus.Failed);
Assert.False(vm.ResetAndRetryCommand.CanExecute(null));
var canExecuteChangedRaised = false;
vm.ResetAndRetryCommand.CanExecuteChanged += (_, _) => canExecuteChangedRaised = true;
// The row is the same instance Mission Control flips when a ConPTY pane closes —
// the command's CanExecute must react without the bound Task itself changing.
row.HasInteractiveSession = false;
Assert.True(canExecuteChangedRaised);
Assert.True(vm.ResetAndRetryCommand.CanExecute(null));
}
[Fact]
public void ResetAndRetry_IsEnabled_ForTerminalTaskWithoutInteractiveSession()
{
var vm = BuildVm(new RecordingWorkerClient());
var row = new TaskRowViewModel { Id = "task-reset-3", Status = TaskStatus.Cancelled };
vm.Bind(row);
vm.Monitor.ApplyState(TaskStatus.Cancelled);
Assert.True(vm.ResetAndRetryCommand.CanExecute(null));
}
[Fact]
public void ResetAndRetry_StaysDisabled_AfterSwitchingToAnotherInteractiveTask()
{
var vm = BuildVm(new RecordingWorkerClient());
var firstRow = new TaskRowViewModel { Id = "task-reset-4", Status = TaskStatus.Failed };
vm.Bind(firstRow);
vm.Monitor.ApplyState(TaskStatus.Failed);
Assert.True(vm.ResetAndRetryCommand.CanExecute(null));
var secondRow = new TaskRowViewModel { Id = "task-reset-5", Status = TaskStatus.Failed, HasInteractiveSession = true };
vm.Bind(secondRow);
vm.Monitor.ApplyState(TaskStatus.Failed);
Assert.False(vm.ResetAndRetryCommand.CanExecute(null));
// Flipping the old (unsubscribed) row must not resurrect the command for the new task.
firstRow.HasInteractiveSession = true;
Assert.False(vm.ResetAndRetryCommand.CanExecute(null));
}
}