feat(worker): add PlanningAggregator.BuildIntegrationBranchAsync
This commit is contained in:
@@ -116,4 +116,99 @@ public class PlanningAggregatorTests : IDisposable
|
||||
CreatedAt = DateTime.UtcNow,
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildIntegrationBranchAsync_NonConflictingChildren_ReturnsOkWithCombinedDiff()
|
||||
{
|
||||
var db = NewDb();
|
||||
var repo = NewRepo();
|
||||
GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");
|
||||
var (parentId, _, _) = await SeedPlanningWithTwoChildrenAsync(db, repo);
|
||||
|
||||
var git = new GitService();
|
||||
var svc = new PlanningAggregator(db.CreateFactory(), git, NullLogger<PlanningAggregator>.Instance);
|
||||
|
||||
var result = await svc.BuildIntegrationBranchAsync(parentId, targetBranch: "main", CancellationToken.None);
|
||||
|
||||
var ok = Assert.IsType<CombinedDiffResult.Ok>(result);
|
||||
Assert.EndsWith("-integration", ok.Value.IntegrationBranch);
|
||||
Assert.Contains("diff --git", ok.Value.UnifiedDiff);
|
||||
|
||||
var branches = await git.ListLocalBranchesAsync(repo.RepoDir, CancellationToken.None);
|
||||
Assert.Contains(branches, b => b == ok.Value.IntegrationBranch);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task BuildIntegrationBranchAsync_ConflictingChildren_ReturnsFailedAndResetsBranch()
|
||||
{
|
||||
var db = NewDb();
|
||||
var repo = NewRepo();
|
||||
GitRepoFixture.RunGit(repo.RepoDir, "branch", "-m", "main");
|
||||
var (parentId, subA, subB) = await SeedPlanningWithConflictingChildrenAsync(db, repo);
|
||||
|
||||
var git = new GitService();
|
||||
var svc = new PlanningAggregator(db.CreateFactory(), git, NullLogger<PlanningAggregator>.Instance);
|
||||
|
||||
var result = await svc.BuildIntegrationBranchAsync(parentId, targetBranch: "main", CancellationToken.None);
|
||||
|
||||
var failed = Assert.IsType<CombinedDiffResult.Failed>(result);
|
||||
Assert.Equal(subB, failed.Value.FirstConflictSubtaskId);
|
||||
Assert.NotEmpty(failed.Value.ConflictedFiles);
|
||||
|
||||
Assert.False(await git.IsMidMergeAsync(repo.RepoDir, CancellationToken.None));
|
||||
}
|
||||
|
||||
private async Task<(string parentId, string subA, string subB)> SeedPlanningWithConflictingChildrenAsync(
|
||||
DbFixture db, GitRepoFixture repo)
|
||||
{
|
||||
using var ctx = db.CreateContext();
|
||||
var listId = Guid.NewGuid().ToString();
|
||||
ctx.Lists.Add(new ListEntity
|
||||
{
|
||||
Id = listId, Name = "test", CreatedAt = DateTime.UtcNow, WorkingDir = repo.RepoDir,
|
||||
});
|
||||
var parentId = Guid.NewGuid().ToString();
|
||||
ctx.Tasks.Add(new TaskEntity
|
||||
{
|
||||
Id = parentId, ListId = listId, Title = "plan", CreatedAt = DateTime.UtcNow,
|
||||
Status = TaskStatus.Planning, SortOrder = 0,
|
||||
});
|
||||
var subA = Guid.NewGuid().ToString();
|
||||
var subB = Guid.NewGuid().ToString();
|
||||
ctx.Tasks.Add(new TaskEntity
|
||||
{
|
||||
Id = subA, ListId = listId, Title = "A", CreatedAt = DateTime.UtcNow,
|
||||
ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 1,
|
||||
});
|
||||
ctx.Tasks.Add(new TaskEntity
|
||||
{
|
||||
Id = subB, ListId = listId, Title = "B", CreatedAt = DateTime.UtcNow,
|
||||
ParentTaskId = parentId, Status = TaskStatus.Done, SortOrder = 2,
|
||||
});
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
SeedWorktreeWithFile(ctx, repo, subA, "README.md", "A wins\n");
|
||||
SeedWorktreeWithFile(ctx, repo, subB, "README.md", "B wins\n");
|
||||
await ctx.SaveChangesAsync();
|
||||
|
||||
return (parentId, subA, subB);
|
||||
}
|
||||
|
||||
private void SeedWorktreeWithFile(ClaudeDoDbContext ctx, GitRepoFixture repo, string taskId, string filename, string content)
|
||||
{
|
||||
var wtPath = Path.Combine(Path.GetTempPath(), $"wt_{Guid.NewGuid():N}");
|
||||
_wtCleanups.Add((repo.RepoDir, wtPath));
|
||||
var branch = $"claudedo/{taskId[..8]}";
|
||||
GitRepoFixture.RunGit(repo.RepoDir, "worktree", "add", "-b", branch, wtPath, repo.BaseCommit);
|
||||
File.WriteAllText(Path.Combine(wtPath, filename), content);
|
||||
GitRepoFixture.RunGit(wtPath, "add", filename);
|
||||
GitRepoFixture.RunGit(wtPath, "commit", "-m", $"edit {filename}");
|
||||
var head = GitRepoFixture.RunGit(wtPath, "rev-parse", "HEAD").Trim();
|
||||
ctx.Worktrees.Add(new WorktreeEntity
|
||||
{
|
||||
TaskId = taskId, Path = wtPath, BranchName = branch,
|
||||
BaseCommit = repo.BaseCommit, HeadCommit = head,
|
||||
DiffStat = null, State = WorktreeState.Active, CreatedAt = DateTime.UtcNow,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user