| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "the-valley/flow/internal/run" |
| 14 | ) |
| 15 | |
| 16 | const dataRef = "refs/klaus/data" |
| 17 | |
| 18 | // refSHA returns the sha ref resolves to in the git dir, or "" if absent. |
| 19 | func (h *Harness) refSHA(gitDir, ref string) string { |
| 20 | h.t.Helper() |
| 21 | cmd := exec.Command("git", "--git-dir", gitDir, "rev-parse", "--verify", "--quiet", ref) |
| 22 | cmd.Env = append(os.Environ(), "HOME="+h.Home, "GIT_CONFIG_GLOBAL="+filepath.Join(h.Home, ".gitconfig"), "GIT_CONFIG_NOSYSTEM=1") |
| 23 | out, err := cmd.Output() |
| 24 | if err != nil { |
| 25 | return "" |
| 26 | } |
| 27 | return strings.TrimSpace(string(out)) |
| 28 | } |
| 29 | |
| 30 | // launchAndFinalize launches an agent, lets the fake claude finish, and waits |
| 31 | // for _finalize to complete (cost recorded, pane released). It returns the |
| 32 | // run id. |
| 33 | func (h *Harness) launchAndFinalize(prompt string) string { |
| 34 | h.t.Helper() |
| 35 | res := h.RunKlaus("launch", prompt) |
| 36 | if res.ExitCode != 0 { |
| 37 | h.t.Fatalf("launch exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 38 | } |
| 39 | ids := h.RunIDs() |
| 40 | if len(ids) != 1 { |
| 41 | h.t.Fatalf("expected 1 run, got %d: %v", len(ids), ids) |
| 42 | } |
| 43 | h.WaitForClaudeStart(30 * time.Second) |
| 44 | h.ReleaseClaude() |
| 45 | h.WaitForState(ids[0], func(s *run.State) bool { |
| 46 | return s.CostUSD != nil && s.TmuxPane == nil |
| 47 | }, 30*time.Second) |
| 48 | return ids[0] |
| 49 | } |
| 50 | |
| 51 | // TestDataRefStaysLocalByDefault: with the default config, finalize commits |
| 52 | // the run to the sandbox repo's data ref and origin never receives it. |
| 53 | // push-log publishes it only when given --push. |
| 54 | func TestDataRefStaysLocalByDefault(t *testing.T) { |
| 55 | t.Parallel() |
| 56 | h := NewHarness(t) |
| 57 | localGitDir := filepath.Join(h.RepoDir, ".git") |
| 58 | |
| 59 | runID := h.launchAndFinalize("add a health check endpoint") |
| 60 | |
| 61 | if h.refSHA(localGitDir, dataRef+":runs/"+runID+".json") == "" { |
| 62 | t.Fatalf("local %s has no runs/%s.json after finalize", dataRef, runID) |
| 63 | } |
| 64 | if sha := h.refSHA(h.OriginDir, dataRef); sha != "" { |
| 65 | t.Fatalf("origin has %s at %s after finalize; the default config must not push it", dataRef, sha) |
| 66 | } |
| 67 | |
| 68 | res := h.RunKlaus("push-log", runID) |
| 69 | if res.ExitCode != 0 { |
| 70 | t.Fatalf("push-log exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 71 | } |
| 72 | if !strings.Contains(res.Stdout, "Not pushed") { |
| 73 | t.Errorf("push-log should say it did not push; stdout:\n%s", res.Stdout) |
| 74 | } |
| 75 | if sha := h.refSHA(h.OriginDir, dataRef); sha != "" { |
| 76 | t.Fatalf("push-log without --push published %s", dataRef) |
| 77 | } |
| 78 | |
| 79 | res = h.RunKlaus("push-log", "--push", runID) |
| 80 | if res.ExitCode != 0 { |
| 81 | t.Fatalf("push-log --push exited %d\nstdout:\n%s\nstderr:\n%s", res.ExitCode, res.Stdout, res.Stderr) |
| 82 | } |
| 83 | if !strings.Contains(res.Stdout, "Pushed "+dataRef+" to origin") { |
| 84 | t.Errorf("push-log --push should report the push; stdout:\n%s", res.Stdout) |
| 85 | } |
| 86 | local := h.refSHA(localGitDir, dataRef) |
| 87 | if got := h.refSHA(h.OriginDir, dataRef); got == "" || got != local { |
| 88 | t.Errorf("origin %s = %q after push-log --push, want %s", dataRef, got, local) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // TestDataRefPushedWhenEnabled: push_data_ref: true makes finalize push the |
| 93 | // data ref to origin. It is set globally because a host-repo run's finalize |
| 94 | // reads the repo config from the agent's worktree, which holds the committed |
| 95 | // copy. |
| 96 | func TestDataRefPushedWhenEnabled(t *testing.T) { |
| 97 | t.Parallel() |
| 98 | h := NewHarness(t) |
| 99 | if err := os.MkdirAll(filepath.Join(h.Home, ".klaus"), 0o755); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | if err := os.WriteFile(filepath.Join(h.Home, ".klaus", "config.json"), []byte(`{"push_data_ref": true}`+"\n"), 0o644); err != nil { |
| 103 | t.Fatal(err) |
| 104 | } |
| 105 | |
| 106 | runID := h.launchAndFinalize("add a health check endpoint") |
| 107 | |
| 108 | local := h.refSHA(filepath.Join(h.RepoDir, ".git"), dataRef) |
| 109 | if local == "" { |
| 110 | t.Fatalf("local %s missing after finalize of %s", dataRef, runID) |
| 111 | } |
| 112 | if got := h.refSHA(h.OriginDir, dataRef); got != local { |
| 113 | t.Errorf("origin %s = %q, want the local commit %s", dataRef, got, local) |
| 114 | } |
| 115 | } |