| 1 | //go:build e2e |
| 2 | |
| 3 | package e2e |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "the-valley/flow/internal/run" |
| 15 | "the-valley/flow/internal/selfupdate" |
| 16 | ) |
| 17 | |
| 18 | func writeE2EFile(t *testing.T, path, data string) { |
| 19 | t.Helper() |
| 20 | if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | if err := os.WriteFile(path, []byte(data), 0o644); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | // TestOperatesStateUnderFlowNames: a session, a project registry, a host |
| 29 | // config, a repo config and self-update state that a later build put under |
| 30 | // the flow names (~/.valley/flow, <repo>/.valley/flow) are found and kept |
| 31 | // there by every command, whatever the naming generation, so a rollback to |
| 32 | // this build keeps working on them. |
| 33 | func TestOperatesStateUnderFlowNames(t *testing.T) { |
| 34 | t.Parallel() |
| 35 | h := NewHarness(t) |
| 36 | h.UseFlowNames() |
| 37 | flow := filepath.Join(h.Home, ".valley", "flow") |
| 38 | legacy := filepath.Join(h.Home, ".klaus") |
| 39 | |
| 40 | writeE2EFile(t, filepath.Join(flow, "config.json"), `{"default_agent_model": "flow-host-model"}`+"\n") |
| 41 | writeE2EFile(t, filepath.Join(flow, "projects.json"), `{"projects_dir": "~/src", "projects": {"widget": "`+h.RepoDir+`"}}`+"\n") |
| 42 | writeE2EFile(t, filepath.Join(flow, "self-update.json"), `{"current": {"commit": "1111111", "store_path": "/nix/store/x-klaus"}, "updated_at": "2026-11-01T00:00:00Z"}`+"\n") |
| 43 | if err := os.Mkdir(filepath.Join(h.RepoDir, ".valley"), 0o755); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | h.git(h.RepoDir, "mv", ".klaus", filepath.Join(".valley", "flow")) |
| 47 | h.git(h.RepoDir, "commit", "-qm", "keep flow config under .valley/flow") |
| 48 | h.git(h.RepoDir, "push", "-q", "origin", "main") |
| 49 | |
| 50 | run1 := func(args ...string) RunResult { |
| 51 | t.Helper() |
| 52 | res := h.RunKlaus(args...) |
| 53 | if res.ExitCode != 0 { |
| 54 | t.Fatalf("klaus %v exited %d\nstdout:\n%s\nstderr:\n%s", args, res.ExitCode, res.Stdout, res.Stderr) |
| 55 | } |
| 56 | return res |
| 57 | } |
| 58 | |
| 59 | if res := run1("project", "list"); !strings.Contains(res.Stdout, "widget") { |
| 60 | t.Errorf("project list read no registry under the flow names:\n%s", res.Stdout) |
| 61 | } |
| 62 | other := filepath.Join(h.E2EDir, "other") |
| 63 | h.git("", "init", "-q", "-b", "main", other) |
| 64 | h.RegisterProject("acme/other", other) |
| 65 | if data, err := os.ReadFile(filepath.Join(flow, "projects.json")); err != nil || !strings.Contains(string(data), "other") { |
| 66 | t.Errorf("project add did not write the registry back where it read it: %s, %v", data, err) |
| 67 | } |
| 68 | |
| 69 | run1("launch", "work under the flow names") |
| 70 | ids := h.RunIDs() |
| 71 | if len(ids) != 1 { |
| 72 | t.Fatalf("runs in the session under the flow names: %v", ids) |
| 73 | } |
| 74 | id := ids[0] |
| 75 | h.WaitForClaudeStart(15 * time.Second) |
| 76 | h.ReleaseClaude() |
| 77 | st := h.WaitForState(id, func(s *run.State) bool { return s.CostUSD != nil }, 60*time.Second) |
| 78 | if st.Model == nil || *st.Model != "flow-host-model" { |
| 79 | t.Errorf("run model %v: the host config under the flow names was not read", st.Model) |
| 80 | } |
| 81 | if st.TmuxSession != h.AgentsSession() { |
| 82 | t.Errorf("run recorded tmux session %q, want %q", st.TmuxSession, h.AgentsSession()) |
| 83 | } |
| 84 | if st.LogFile == nil || !strings.HasPrefix(*st.LogFile, h.SessionDir()+string(filepath.Separator)) { |
| 85 | t.Errorf("run log %v is outside its session %s", st.LogFile, h.SessionDir()) |
| 86 | } |
| 87 | |
| 88 | if res := run1("status"); !strings.Contains(res.Stdout, id) { |
| 89 | t.Errorf("status does not list %s:\n%s", id, res.Stdout) |
| 90 | } |
| 91 | |
| 92 | var report struct { |
| 93 | Places []struct { |
| 94 | Kind string `json:"kind"` |
| 95 | Paths []string `json:"paths"` |
| 96 | } `json:"places"` |
| 97 | Blockers []struct{ What string } `json:"blockers"` |
| 98 | } |
| 99 | res := run1("naming", "status", "--json") |
| 100 | if err := json.Unmarshal([]byte(res.Stdout), &report); err != nil { |
| 101 | t.Fatalf("naming status --json: %v\n%s", err, res.Stdout) |
| 102 | } |
| 103 | places := map[string][]string{} |
| 104 | for _, p := range report.Places { |
| 105 | places[p.Kind] = p.Paths |
| 106 | } |
| 107 | for kind, want := range map[string]string{ |
| 108 | "host config": filepath.Join(flow, "config.json"), |
| 109 | "project registry": filepath.Join(flow, "projects.json"), |
| 110 | "self-update state": filepath.Join(flow, "self-update.json"), |
| 111 | "sessions": filepath.Join(flow, "sessions"), |
| 112 | } { |
| 113 | if got := places[kind]; len(got) != 1 || got[0] != want { |
| 114 | t.Errorf("naming status puts %s at %v, want %s", kind, got, want) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Every self-update operation lists the profile first: one holding klaus. |
| 119 | h.WriteStub("nix", `#!/usr/bin/env bash |
| 120 | case "$1 $2" in |
| 121 | "profile list") echo '{"elements":{"klaus":{"storePaths":["/nix/store/x-klaus"]}},"version":3}';; |
| 122 | *) echo "nix stub: unexpected $*" >&2; exit 2;; |
| 123 | esac |
| 124 | `) |
| 125 | run1("self-update", "--pin") |
| 126 | if su, err := selfupdate.LoadFile(filepath.Join(flow, "self-update.json")); err != nil || !su.Pinned || su.Current == nil { |
| 127 | t.Errorf("self-update --pin did not write the state where it found it: %+v, %v", su, err) |
| 128 | } |
| 129 | |
| 130 | run1("cleanup", id) |
| 131 | if _, err := h.ReadState(id); !errors.Is(err, os.ErrNotExist) { |
| 132 | t.Errorf("cleanup left the run's state: %v", err) |
| 133 | } |
| 134 | |
| 135 | // Nothing of these moved to, or was copied under, the legacy names. |
| 136 | for _, p := range []string{ |
| 137 | filepath.Join(legacy, "sessions"), |
| 138 | filepath.Join(legacy, "projects.json"), |
| 139 | filepath.Join(legacy, "config.json"), |
| 140 | filepath.Join(h.Home, ".local", "state", "klaus", "self-update.json"), |
| 141 | filepath.Join(h.RepoDir, ".klaus"), |
| 142 | } { |
| 143 | if _, err := os.Stat(p); !errors.Is(err, os.ErrNotExist) { |
| 144 | t.Errorf("%s exists: state under the flow names was copied or moved", p) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // A registry under both names stops launch, naming both, rather than |
| 149 | // launching as if neither were there. |
| 150 | writeE2EFile(t, filepath.Join(legacy, "projects.json"), `{"projects": {}}`+"\n") |
| 151 | res = h.RunKlaus("launch", "with two registries") |
| 152 | if res.ExitCode == 0 || !strings.Contains(res.Stderr, filepath.Join(legacy, "projects.json")) || !strings.Contains(res.Stderr, filepath.Join(flow, "projects.json")) { |
| 153 | t.Errorf("launch with a registry under both names exited %d, want a refusal naming both\nstderr:\n%s", res.ExitCode, res.Stderr) |
| 154 | } |
| 155 | } |