test(worker): cover planning worktree lifecycle and self-heal
Adds four tests to PlanningSessionManagerTests: worktree removal on discard, error on non-git working dir, self-heal when branch already exists, and resume returning the correct token and session id. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -218,4 +218,72 @@ public sealed class PlanningSessionManagerTests : IDisposable
|
|||||||
Assert.Equal(TaskStatus.Manual, loaded!.Status);
|
Assert.Equal(TaskStatus.Manual, loaded!.Status);
|
||||||
Assert.Null(loaded.PlanningSessionToken);
|
Assert.Null(loaded.PlanningSessionToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DiscardAsync_RemovesWorktreeAndBranch()
|
||||||
|
{
|
||||||
|
var (listId, wd) = await SeedListAsync();
|
||||||
|
var parent = await SeedManualTaskAsync(listId);
|
||||||
|
|
||||||
|
var ctx = await _sut.StartAsync(parent.Id, CancellationToken.None);
|
||||||
|
Assert.True(Directory.Exists(ctx.WorktreePath));
|
||||||
|
|
||||||
|
await _sut.DiscardAsync(parent.Id, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.False(Directory.Exists(ctx.WorktreePath));
|
||||||
|
// branch deleted
|
||||||
|
var paths = await _git.ListWorktreePathsForBranchAsync(wd, ctx.BranchName);
|
||||||
|
Assert.Empty(paths);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task StartAsync_ThrowsWhenWorkingDirIsNotGitRepo()
|
||||||
|
{
|
||||||
|
var listId = Guid.NewGuid().ToString();
|
||||||
|
var wd = Path.Combine(Path.GetTempPath(), $"cd_nogit_{Guid.NewGuid():N}");
|
||||||
|
Directory.CreateDirectory(wd);
|
||||||
|
await _lists.AddAsync(new ListEntity { Id = listId, Name = "NoGit", WorkingDir = wd, CreatedAt = DateTime.UtcNow });
|
||||||
|
|
||||||
|
var t = await SeedManualTaskAsync(listId);
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<InvalidOperationException>(() => _sut.StartAsync(t.Id, CancellationToken.None));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task StartAsync_SelfHealsWhenBranchAlreadyExists()
|
||||||
|
{
|
||||||
|
var (listId, wd) = await SeedListAsync();
|
||||||
|
var parent = await SeedManualTaskAsync(listId);
|
||||||
|
|
||||||
|
// Pre-create a colliding branch.
|
||||||
|
var branch = $"claudedo/planning/{parent.Id.Replace("-", "")}";
|
||||||
|
var head = await _git.RevParseHeadAsync(wd);
|
||||||
|
var procInfo = new System.Diagnostics.ProcessStartInfo("git") { WorkingDirectory = wd };
|
||||||
|
procInfo.ArgumentList.Add("branch");
|
||||||
|
procInfo.ArgumentList.Add(branch);
|
||||||
|
procInfo.ArgumentList.Add(head);
|
||||||
|
var p = System.Diagnostics.Process.Start(procInfo)!;
|
||||||
|
p.WaitForExit();
|
||||||
|
|
||||||
|
var ctx = await _sut.StartAsync(parent.Id, CancellationToken.None);
|
||||||
|
Assert.True(Directory.Exists(ctx.WorktreePath));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task ResumeAsync_ReturnsContextWithTokenAndWorktree()
|
||||||
|
{
|
||||||
|
var (listId, wd) = await SeedListAsync();
|
||||||
|
var parent = await SeedManualTaskAsync(listId);
|
||||||
|
|
||||||
|
var startCtx = await _sut.StartAsync(parent.Id, CancellationToken.None);
|
||||||
|
|
||||||
|
// Simulate the claude session capturing its session id.
|
||||||
|
await _tasks.UpdatePlanningSessionIdAsync(parent.Id, "session-abc");
|
||||||
|
|
||||||
|
var resumeCtx = await _sut.ResumeAsync(parent.Id, CancellationToken.None);
|
||||||
|
|
||||||
|
Assert.Equal(startCtx.Token, resumeCtx.Token);
|
||||||
|
Assert.Equal(startCtx.WorktreePath, resumeCtx.WorktreePath);
|
||||||
|
Assert.Equal("session-abc", resumeCtx.ClaudeSessionId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user