test: retain committed stage diffs
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
# Committed and merged stage diffs remain retained
|
||||
|
||||
The server captures workspace contents around each stage visit rather than comparing
|
||||
Git status or HEAD. Regression coverage now exercises a stage that commits its edit,
|
||||
a later stage that resolves and commits a merge conflict, and an empty commit-only
|
||||
stage. After reopening SQLite, the first two patches remain available while the final
|
||||
stage correctly records no source-content change.
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -162,3 +163,154 @@ func TestRepeatedStageVisitsRetainIndependentDiffs(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRetainedStageDiffsSurviveCommittedMergeAndNoopVisit(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db := filepath.Join(t.TempDir(), "committed-diffs.db")
|
||||
store, err := pipeline.Open(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
source := pipeline.Template([]string{"commit", "merge", "noop"}, true)
|
||||
if _, err := store.Submit(ctx, "committed-diffs", source); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
items, err := store.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
workspace := t.TempDir()
|
||||
runGit(t, workspace, "init", "-q")
|
||||
runGit(t, workspace, "config", "user.email", "test@example.invalid")
|
||||
runGit(t, workspace, "config", "user.name", "test")
|
||||
path := filepath.Join(workspace, "source.txt")
|
||||
if err := os.WriteFile(path, []byte("base\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
runGit(t, workspace, "add", "source.txt")
|
||||
runGit(t, workspace, "commit", "-qm", "base")
|
||||
branch := runGitOutput(t, workspace, "branch", "--show-current")
|
||||
|
||||
r, err := newRunner(ctx, store)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
git := func(args ...string) error {
|
||||
cmd := exec.Command("git", append([]string{"-C", workspace}, args...)...)
|
||||
if output, err := cmd.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("git %v: %w: %s", args, err, output)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
gitOutput := func(args ...string) (string, error) {
|
||||
cmd := exec.Command("git", append([]string{"-C", workspace}, args...)...)
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("git %v: %w", args, err)
|
||||
}
|
||||
return strings.TrimSpace(string(output)), nil
|
||||
}
|
||||
r.execute = func(_ context.Context, def stage.Definition, _ stage.Values) (stage.Result, error) {
|
||||
switch def.Key {
|
||||
case "commit":
|
||||
if err := os.WriteFile(path, []byte("committed change\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("add", "source.txt"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("commit", "-qm", "stage commit"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
case "merge":
|
||||
if err := git("checkout", "-qb", "conflict-side"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("side change\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("add", "source.txt"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("commit", "-qm", "side"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("checkout", "-q", branch); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("main change\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("add", "source.txt"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("commit", "-qm", "main"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("merge", "--no-ff", "conflict-side", "-m", "merge"); err == nil {
|
||||
return stage.Result{}, errors.New("expected merge conflict")
|
||||
}
|
||||
conflicted, err := gitOutput("diff", "--name-only", "--diff-filter=U")
|
||||
if err != nil || conflicted != "source.txt" {
|
||||
return stage.Result{}, fmt.Errorf("merge did not leave the expected conflict: %q, %v", conflicted, err)
|
||||
}
|
||||
if err := os.WriteFile(path, []byte("resolved merge\n"), 0600); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("add", "source.txt"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
if err := git("commit", "-qm", "resolve merge"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
case "noop":
|
||||
if err := git("commit", "--allow-empty", "-qm", "no content change"); err != nil {
|
||||
return stage.Result{}, err
|
||||
}
|
||||
}
|
||||
return stage.Result{Outcome: "success"}, nil
|
||||
}
|
||||
|
||||
run, err := r.start(protocol.Message{PipelineID: items[0].ID, Version: 1, RequestKey: "committed-diffs", Workspace: workspace, Input: map[string]any{"prompt": "task"}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r.wg.Wait()
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store, err = pipeline.Open(db)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
detail, err := store.GetRun(ctx, run.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if detail.Status != "succeeded" || len(detail.Steps) != 3 {
|
||||
t.Fatal(detail)
|
||||
}
|
||||
for _, assertion := range []struct {
|
||||
step int
|
||||
old string
|
||||
new string
|
||||
}{
|
||||
{step: 0, old: "base", new: "committed change"},
|
||||
{step: 1, old: "committed change", new: "resolved merge"},
|
||||
} {
|
||||
changes := detail.Steps[assertion.step].FileChanges
|
||||
if changes == nil || len(changes.Files) != 1 || !strings.Contains(changes.Files[0].Patch, "-"+assertion.old) || !strings.Contains(changes.Files[0].Patch, "+"+assertion.new) {
|
||||
t.Fatalf("committed stage patch was not retained after reopen: %+v", changes)
|
||||
}
|
||||
}
|
||||
if changes := detail.Steps[2].FileChanges; changes == nil || len(changes.Files) != 0 {
|
||||
t.Fatalf("commit-only stage changed source evidence: %+v", changes)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil || string(data) != "resolved merge\n" {
|
||||
t.Fatalf("merge resolution was not preserved: %q, %v", data, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user