| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "the-valley/flow/internal/project" |
| 11 | "the-valley/flow/internal/run" |
| 12 | ) |
| 13 | |
| 14 | func TestTargetCommand(t *testing.T) { |
| 15 | tmpDir := t.TempDir() |
| 16 | sessionDir := filepath.Join(tmpDir, "sessions", "test-session") |
| 17 | store := run.NewHomeDirStoreFromPath(sessionDir) |
| 18 | if err := store.EnsureDirs(); err != nil { |
| 19 | t.Fatalf("EnsureDirs: %v", err) |
| 20 | } |
| 21 | |
| 22 | baseDir := store.BaseDir() |
| 23 | |
| 24 | t.Run("set target", func(t *testing.T) { |
| 25 | if err := run.SaveTarget(baseDir, "myorg/myrepo"); err != nil { |
| 26 | t.Fatalf("SaveTarget: %v", err) |
| 27 | } |
| 28 | repo, err := run.LoadTarget(baseDir) |
| 29 | if err != nil { |
| 30 | t.Fatalf("LoadTarget: %v", err) |
| 31 | } |
| 32 | if repo != "myorg/myrepo" { |
| 33 | t.Errorf("expected myorg/myrepo, got %q", repo) |
| 34 | } |
| 35 | }) |
| 36 | |
| 37 | t.Run("show target", func(t *testing.T) { |
| 38 | repo, err := run.LoadTarget(baseDir) |
| 39 | if err != nil { |
| 40 | t.Fatalf("LoadTarget: %v", err) |
| 41 | } |
| 42 | if repo != "myorg/myrepo" { |
| 43 | t.Errorf("expected myorg/myrepo, got %q", repo) |
| 44 | } |
| 45 | }) |
| 46 | |
| 47 | t.Run("clear target", func(t *testing.T) { |
| 48 | if err := run.ClearTarget(baseDir); err != nil { |
| 49 | t.Fatalf("ClearTarget: %v", err) |
| 50 | } |
| 51 | repo, err := run.LoadTarget(baseDir) |
| 52 | if err != nil { |
| 53 | t.Fatalf("LoadTarget: %v", err) |
| 54 | } |
| 55 | if repo != "" { |
| 56 | t.Errorf("expected empty after clear, got %q", repo) |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | func TestTargetCommandIntegration(t *testing.T) { |
| 62 | // Test the target command end-to-end by setting up a session environment |
| 63 | // and using the run package directly (same path the command takes). |
| 64 | tmpHome := t.TempDir() |
| 65 | sessionID := "test-session-integration" |
| 66 | sessionDir := filepath.Join(tmpHome, ".klaus", "sessions", sessionID) |
| 67 | if err := os.MkdirAll(filepath.Join(sessionDir, "runs"), 0o755); err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if err := os.MkdirAll(filepath.Join(sessionDir, "logs"), 0o755); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | |
| 74 | // The target command does: |
| 75 | // 1. sessionStore() → HomeDirStore via KLAUS_SESSION_ID |
| 76 | // 2. hds.BaseDir() → session base dir |
| 77 | // 3. run.SaveTarget/LoadTarget/ClearTarget on that dir |
| 78 | |
| 79 | // Simulate the same flow |
| 80 | store := run.NewHomeDirStoreFromPath(sessionDir) |
| 81 | baseDir := store.BaseDir() |
| 82 | |
| 83 | // Initially no target |
| 84 | repo, err := run.LoadTarget(baseDir) |
| 85 | if err != nil { |
| 86 | t.Fatalf("initial LoadTarget: %v", err) |
| 87 | } |
| 88 | if repo != "" { |
| 89 | t.Errorf("expected no target initially, got %q", repo) |
| 90 | } |
| 91 | |
| 92 | // Set target (the command normalizes via ParseRepoRef, which we test separately) |
| 93 | if err := run.SaveTarget(baseDir, "owner/myrepo"); err != nil { |
| 94 | t.Fatalf("SaveTarget: %v", err) |
| 95 | } |
| 96 | |
| 97 | // Verify it persists |
| 98 | repo, err = run.LoadTarget(baseDir) |
| 99 | if err != nil { |
| 100 | t.Fatalf("LoadTarget after set: %v", err) |
| 101 | } |
| 102 | if repo != "owner/myrepo" { |
| 103 | t.Errorf("expected owner/myrepo, got %q", repo) |
| 104 | } |
| 105 | |
| 106 | // Verify the file is at the expected path |
| 107 | targetPath := filepath.Join(sessionDir, "target.json") |
| 108 | if _, err := os.Stat(targetPath); err != nil { |
| 109 | t.Errorf("target.json not found at %s: %v", targetPath, err) |
| 110 | } |
| 111 | |
| 112 | // Clear |
| 113 | if err := run.ClearTarget(baseDir); err != nil { |
| 114 | t.Fatalf("ClearTarget: %v", err) |
| 115 | } |
| 116 | |
| 117 | // Verify cleared |
| 118 | repo, err = run.LoadTarget(baseDir) |
| 119 | if err != nil { |
| 120 | t.Fatalf("LoadTarget after clear: %v", err) |
| 121 | } |
| 122 | if repo != "" { |
| 123 | t.Errorf("expected empty after clear, got %q", repo) |
| 124 | } |
| 125 | |
| 126 | // File should be removed |
| 127 | if _, err := os.Stat(targetPath); !os.IsNotExist(err) { |
| 128 | t.Errorf("target.json should be removed after clear") |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func TestTargetResolvesProjectName(t *testing.T) { |
| 133 | // Create a temporary git repo to simulate a registered project with a remote |
| 134 | tmpDir := t.TempDir() |
| 135 | repoDir := filepath.Join(tmpDir, "my-project") |
| 136 | |
| 137 | // Init a git repo with a remote |
| 138 | if err := os.MkdirAll(repoDir, 0o755); err != nil { |
| 139 | t.Fatal(err) |
| 140 | } |
| 141 | cmds := [][]string{ |
| 142 | {"git", "init", repoDir}, |
| 143 | {"git", "-C", repoDir, "remote", "add", "origin", "https://github.com/testowner/my-project.git"}, |
| 144 | } |
| 145 | for _, args := range cmds { |
| 146 | cmd := exec.Command(args[0], args[1:]...) |
| 147 | if out, err := cmd.CombinedOutput(); err != nil { |
| 148 | t.Fatalf("running %v: %v\n%s", args, err, out) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Create a registry with this project |
| 153 | regPath := filepath.Join(tmpDir, "projects.json") |
| 154 | reg := &project.Registry{ |
| 155 | Projects: map[string]string{ |
| 156 | "my-project": repoDir, |
| 157 | }, |
| 158 | } |
| 159 | if err := reg.SaveTo(regPath); err != nil { |
| 160 | t.Fatalf("SaveTo: %v", err) |
| 161 | } |
| 162 | |
| 163 | // Simulate the target resolution logic |
| 164 | repoRef := "my-project" |
| 165 | loaded, err := project.LoadFrom(regPath) |
| 166 | if err != nil { |
| 167 | t.Fatalf("LoadFrom: %v", err) |
| 168 | } |
| 169 | |
| 170 | if !strings.Contains(repoRef, "/") { |
| 171 | if localPath, ok := loaded.Get(repoRef); ok { |
| 172 | remote := gitRemoteURL(localPath) |
| 173 | if remote == "" { |
| 174 | t.Fatal("expected remote URL from project repo") |
| 175 | } |
| 176 | if !strings.Contains(remote, "testowner/my-project") { |
| 177 | t.Errorf("expected remote to contain testowner/my-project, got %q", remote) |
| 178 | } |
| 179 | } else { |
| 180 | t.Error("project my-project should be found in registry") |
| 181 | } |
| 182 | } |
| 183 | } |