| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/hex" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "io/fs" |
| 11 | "net/http" |
| 12 | "os" |
| 13 | "path/filepath" |
| 14 | "slices" |
| 15 | "strings" |
| 16 | "testing" |
| 17 | "time" |
| 18 | |
| 19 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 20 | "amadan.net/oficina/equipo/internal/runner/provider/claude" |
| 21 | "amadan.net/oficina/equipo/internal/wire" |
| 22 | ) |
| 23 | |
| 24 | // fakeClaude stands in for the 276 MB darwin binary: the tests check the |
| 25 | // pin's handling, so a test CLI carries its own digest (claudeFor). |
| 26 | func fakeClaude() []byte { |
| 27 | return bytes.Repeat([]byte("#!/bin/sh\necho fake claude 2.1.312\n"), 1024) |
| 28 | } |
| 29 | |
| 30 | func digest(b []byte) string { |
| 31 | s := sha256.Sum256(b) |
| 32 | return hex.EncodeToString(s[:]) |
| 33 | } |
| 34 | |
| 35 | // claudeFor is the pinned CLI under installDir, pinned to body's digest. |
| 36 | // Its home is installDir's parent (a test's own temporary directory); |
| 37 | // both are made 0700, as a umask of 002 leaves t.TempDir's group-writable. |
| 38 | func claudeFor(installDir string, body []byte) ClaudeCLI { |
| 39 | for _, d := range []string{filepath.Dir(installDir), installDir} { |
| 40 | if err := os.Chmod(d, 0700); err != nil { |
| 41 | panic(err) |
| 42 | } |
| 43 | } |
| 44 | c := NewClaudeCLI(filepath.Dir(installDir), installDir) |
| 45 | c.pin = digest(body) |
| 46 | return c |
| 47 | } |
| 48 | |
| 49 | // serve answers every fetch with body and records the URLs asked. |
| 50 | func serve(body []byte, urls *[]string) ClaudeFetch { |
| 51 | return func(_ context.Context, url string) (io.ReadCloser, error) { |
| 52 | if urls != nil { |
| 53 | *urls = append(*urls, url) |
| 54 | } |
| 55 | return io.NopCloser(bytes.NewReader(body)), nil |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func noFetch(t *testing.T) ClaudeFetch { |
| 60 | return func(context.Context, string) (io.ReadCloser, error) { |
| 61 | t.Fatal("fetched although the pinned CLI is in place") |
| 62 | return nil, errors.New("no") |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // watched calls atEOF once, when the download has been read to its end: |
| 67 | // what is on disk then is what the provisioner wrote before it decided. |
| 68 | type watched struct { |
| 69 | r io.Reader |
| 70 | atEOF func() |
| 71 | n int64 |
| 72 | } |
| 73 | |
| 74 | func (w *watched) Read(p []byte) (int, error) { |
| 75 | n, err := w.r.Read(p) |
| 76 | w.n += int64(n) |
| 77 | if err == io.EOF && w.atEOF != nil { |
| 78 | w.atEOF() |
| 79 | w.atEOF = nil |
| 80 | } |
| 81 | return n, err |
| 82 | } |
| 83 | |
| 84 | func (w *watched) Close() error { return nil } |
| 85 | |
| 86 | // entries are the names under dir and their modes ("" for dir itself); a |
| 87 | // missing dir has none. |
| 88 | func entries(t *testing.T, dir string) map[string]fs.FileMode { |
| 89 | t.Helper() |
| 90 | out := map[string]fs.FileMode{} |
| 91 | err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error { |
| 92 | if errors.Is(err, fs.ErrNotExist) && path == dir { |
| 93 | return fs.SkipDir |
| 94 | } |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | if path == dir { |
| 99 | return nil |
| 100 | } |
| 101 | info, err := os.Lstat(path) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | rel, _ := filepath.Rel(dir, path) |
| 106 | out[filepath.ToSlash(rel)] = info.Mode() |
| 107 | return nil |
| 108 | }) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | return out |
| 113 | } |
| 114 | |
| 115 | // The Mac's pin is the official 2.1.312 darwin-arm64 binary (task 1, S1): |
| 116 | // nothing else on darwin has one, and the Linux pins did not move. |
| 117 | func TestClaudePinDarwin(t *testing.T) { |
| 118 | const want = "6b5ec691195e551b02c41df9f7d723f4ea61eaa03c2df90acefe3736f1aeb620" |
| 119 | if claude.ExecutableSHA256DarwinArm64 != want { |
| 120 | t.Fatalf("darwin pin %q", claude.ExecutableSHA256DarwinArm64) |
| 121 | } |
| 122 | if pin, ok := claude.ReleasePinFor("darwin", "arm64"); !ok || pin != want { |
| 123 | t.Fatalf("darwin/arm64: %q %v", pin, ok) |
| 124 | } |
| 125 | for _, arch := range []string{"amd64", "386", "riscv64", ""} { |
| 126 | if pin, ok := claude.ReleasePinFor("darwin", arch); ok || pin != "" { |
| 127 | t.Fatalf("darwin/%s has a pin %q", arch, pin) |
| 128 | } |
| 129 | } |
| 130 | for _, p := range []struct{ goos, goarch, pin string }{{"linux", "amd64", claude.ExecutableSHA256}, {"linux", "arm64", claude.ExecutableSHA256Arm64}} { |
| 131 | if pin, ok := claude.ReleasePinFor(p.goos, p.goarch); !ok || pin != p.pin { |
| 132 | t.Fatalf("%s/%s: %q %v", p.goos, p.goarch, pin, ok) |
| 133 | } |
| 134 | } |
| 135 | if _, ok := claude.ReleasePinFor("windows", "amd64"); ok { |
| 136 | t.Fatal("windows has a pin") |
| 137 | } |
| 138 | // The Mac worker's CLI checks against exactly that pin. |
| 139 | if got := NewClaudeCLI("/Users/u", "/Users/u/.local/lib/equipo-runner"); got.pin != want || got.home != "/Users/u" || got.installDir != "/Users/u/.local/lib/equipo-runner" { |
| 140 | t.Fatalf("NewClaudeCLI %+v", got) |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // One changed byte is refused before the file is ever executable, and the |
| 145 | // refused download leaves nothing behind. |
| 146 | func TestProvisionChecksDigestBeforeExec(t *testing.T) { |
| 147 | dir := t.TempDir() |
| 148 | body := fakeClaude() |
| 149 | bad := slices.Clone(body) |
| 150 | bad[len(bad)/2] ^= 1 |
| 151 | cli := claudeFor(dir, body) |
| 152 | for _, try := range [][]byte{bad, body} { |
| 153 | var during map[string]fs.FileMode |
| 154 | fetch := func(context.Context, string) (io.ReadCloser, error) { |
| 155 | return &watched{r: bytes.NewReader(try), atEOF: func() { during = entries(t, ClaudeDir(dir)) }}, nil |
| 156 | } |
| 157 | err := cli.Provision(context.Background(), fetch) |
| 158 | if len(during) != 1 { |
| 159 | t.Fatalf("the download was not staged as one file: %v", during) |
| 160 | } |
| 161 | for name, mode := range during { |
| 162 | if mode.Perm()&0111 != 0 || !mode.IsRegular() { |
| 163 | t.Fatalf("%s is %v while it downloads, before its digest is known", name, mode) |
| 164 | } |
| 165 | } |
| 166 | if bytes.Equal(try, bad) { |
| 167 | if !errors.Is(err, ErrClaudeRefused) { |
| 168 | t.Fatalf("one changed byte: %v", err) |
| 169 | } |
| 170 | if left := entries(t, ClaudeDir(dir)); len(left) != 0 { |
| 171 | t.Fatalf("a refused download left %v", left) |
| 172 | } |
| 173 | if _, err := cli.Verify(); err == nil { |
| 174 | t.Fatal("verified after a refused download") |
| 175 | } |
| 176 | continue |
| 177 | } |
| 178 | if err != nil { |
| 179 | t.Fatalf("the pinned bytes: %v", err) |
| 180 | } |
| 181 | path, err := cli.Verify() |
| 182 | if err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | info, err := os.Lstat(path) |
| 186 | if err != nil || info.Mode().Perm() != 0700 { |
| 187 | t.Fatalf("placed %v %v", info.Mode(), err) |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // A 512 MB bound; written only at tools/claude-2.1.312/claude under the |
| 193 | // install dir, mode 0700, through no symlink; a wrong file in place is |
| 194 | // replaced, a right one is kept. |
| 195 | func TestProvisionBoundsAndPlace(t *testing.T) { |
| 196 | if MaxClaudeBinary != 512<<20 { |
| 197 | t.Fatalf("bound %d", MaxClaudeBinary) |
| 198 | } |
| 199 | if got := NewClaudeCLI("/", "/i").bound(); got != MaxClaudeBinary { |
| 200 | t.Fatalf("the worker's bound is %d", got) |
| 201 | } |
| 202 | ctx := context.Background() |
| 203 | body := fakeClaude() |
| 204 | |
| 205 | t.Run("bound", func(t *testing.T) { |
| 206 | dir := t.TempDir() |
| 207 | cli := claudeFor(dir, body) |
| 208 | cli.limit = int64(len(body)) - 1 |
| 209 | w := &watched{r: io.MultiReader(bytes.NewReader(body), bytes.NewReader(make([]byte, 1<<20)))} |
| 210 | if err := cli.Provision(ctx, func(context.Context, string) (io.ReadCloser, error) { return w, nil }); !errors.Is(err, ErrClaudeRefused) { |
| 211 | t.Fatalf("over the bound: %v", err) |
| 212 | } |
| 213 | if w.n > cli.limit+1 { |
| 214 | t.Fatalf("read %d bytes past a %d bound", w.n, cli.limit) |
| 215 | } |
| 216 | if left := entries(t, ClaudeDir(dir)); len(left) != 0 { |
| 217 | t.Fatalf("left %v", left) |
| 218 | } |
| 219 | cli.limit = int64(len(body)) |
| 220 | if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| 221 | t.Fatalf("at the bound: %v", err) |
| 222 | } |
| 223 | }) |
| 224 | |
| 225 | t.Run("place", func(t *testing.T) { |
| 226 | dir := t.TempDir() |
| 227 | cli := claudeFor(dir, body) |
| 228 | if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| 229 | t.Fatal(err) |
| 230 | } |
| 231 | want := filepath.Join(dir, "tools", "claude-2.1.312", "claude") |
| 232 | if ClaudeDir(dir) != filepath.Dir(want) { |
| 233 | t.Fatalf("ClaudeDir %q", ClaudeDir(dir)) |
| 234 | } |
| 235 | if path, err := cli.Verify(); err != nil || path != want { |
| 236 | t.Fatalf("Verify %q %v", path, err) |
| 237 | } |
| 238 | got := entries(t, dir) |
| 239 | wantModes := map[string]fs.FileMode{"tools": fs.ModeDir | 0700, "tools/claude-2.1.312": fs.ModeDir | 0700, "tools/claude-2.1.312/claude": 0700} |
| 240 | if len(got) != len(wantModes) { |
| 241 | t.Fatalf("wrote %v", got) |
| 242 | } |
| 243 | for name, mode := range wantModes { |
| 244 | if got[name] != mode { |
| 245 | t.Fatalf("%s is %v, want %v (all: %v)", name, got[name], mode, got) |
| 246 | } |
| 247 | } |
| 248 | // In place and right: kept, nothing fetched. |
| 249 | if err := cli.Provision(ctx, noFetch(t)); err != nil { |
| 250 | t.Fatal(err) |
| 251 | } |
| 252 | // In place and wrong: replaced. |
| 253 | if err := os.WriteFile(want, append(slices.Clone(body), '\n'), 0700); err != nil { |
| 254 | t.Fatal(err) |
| 255 | } |
| 256 | var urls []string |
| 257 | if err := cli.Provision(ctx, serve(body, &urls)); err != nil || len(urls) != 1 { |
| 258 | t.Fatalf("a wrong file: %v %v", err, urls) |
| 259 | } |
| 260 | if raw, _ := os.ReadFile(want); !bytes.Equal(raw, body) { |
| 261 | t.Fatal("the wrong file was not replaced") |
| 262 | } |
| 263 | // A download a crash left behind goes. |
| 264 | stale := filepath.Join(ClaudeDir(dir), ".claude-download-stale") |
| 265 | if err := os.WriteFile(stale, []byte("half"), 0600); err != nil { |
| 266 | t.Fatal(err) |
| 267 | } |
| 268 | if err := cli.Provision(ctx, noFetch(t)); err != nil { |
| 269 | t.Fatal(err) |
| 270 | } |
| 271 | if _, err := os.Lstat(stale); !errors.Is(err, fs.ErrNotExist) { |
| 272 | t.Fatalf("a stale download is still there: %v", err) |
| 273 | } |
| 274 | }) |
| 275 | |
| 276 | t.Run("tools is a symlink", func(t *testing.T) { |
| 277 | dir, outside := t.TempDir(), t.TempDir() |
| 278 | if err := os.Symlink(outside, filepath.Join(dir, "tools")); err != nil { |
| 279 | t.Fatal(err) |
| 280 | } |
| 281 | cli := claudeFor(dir, body) |
| 282 | if err := cli.Provision(ctx, serve(body, nil)); err == nil { |
| 283 | t.Fatal("provisioned through a symlinked tools directory") |
| 284 | } |
| 285 | if left := entries(t, outside); len(left) != 0 { |
| 286 | t.Fatalf("wrote through the link: %v", left) |
| 287 | } |
| 288 | }) |
| 289 | |
| 290 | // os.Root alone refuses a link that leaves it; these stay inside, so |
| 291 | // only the provisioner's own refusal stops them. |
| 292 | t.Run("tools is a symlink inside the install dir", func(t *testing.T) { |
| 293 | dir := t.TempDir() |
| 294 | elsewhere := filepath.Join(dir, "elsewhere") |
| 295 | if err := os.Mkdir(elsewhere, 0700); err != nil { |
| 296 | t.Fatal(err) |
| 297 | } |
| 298 | if err := os.Symlink("elsewhere", filepath.Join(dir, "tools")); err != nil { |
| 299 | t.Fatal(err) |
| 300 | } |
| 301 | cli := claudeFor(dir, body) |
| 302 | if err := cli.Provision(ctx, serve(body, nil)); err == nil { |
| 303 | t.Fatal("provisioned through a symlinked tools directory") |
| 304 | } |
| 305 | if left := entries(t, elsewhere); len(left) != 0 { |
| 306 | t.Fatalf("wrote through the link: %v", left) |
| 307 | } |
| 308 | }) |
| 309 | |
| 310 | t.Run("the version directory is a symlink", func(t *testing.T) { |
| 311 | dir := t.TempDir() |
| 312 | elsewhere := filepath.Join(dir, "tools", "elsewhere") |
| 313 | if err := os.MkdirAll(elsewhere, 0700); err != nil { |
| 314 | t.Fatal(err) |
| 315 | } |
| 316 | if err := os.Symlink("elsewhere", ClaudeDir(dir)); err != nil { |
| 317 | t.Fatal(err) |
| 318 | } |
| 319 | cli := claudeFor(dir, body) |
| 320 | if err := cli.Provision(ctx, serve(body, nil)); err == nil { |
| 321 | t.Fatal("provisioned through a symlinked version directory") |
| 322 | } |
| 323 | if left := entries(t, elsewhere); len(left) != 0 { |
| 324 | t.Fatalf("wrote through the link: %v", left) |
| 325 | } |
| 326 | if _, err := cli.Verify(); err == nil { |
| 327 | t.Fatal("verified through a symlinked version directory") |
| 328 | } |
| 329 | }) |
| 330 | |
| 331 | t.Run("the binary is a symlink", func(t *testing.T) { |
| 332 | dir := t.TempDir() |
| 333 | cli := claudeFor(dir, body) |
| 334 | if err := cli.Provision(ctx, serve(body, nil)); err != nil { |
| 335 | t.Fatal(err) |
| 336 | } |
| 337 | // A link to a right copy beside it: never verified, replaced by a |
| 338 | // real file, the copy untouched. |
| 339 | path := ClaudePath(dir) |
| 340 | real := filepath.Join(ClaudeDir(dir), "claude.real") |
| 341 | if err := os.Rename(path, real); err != nil { |
| 342 | t.Fatal(err) |
| 343 | } |
| 344 | if err := os.Symlink("claude.real", path); err != nil { |
| 345 | t.Fatal(err) |
| 346 | } |
| 347 | if _, err := cli.Verify(); !errors.Is(err, ErrClaudeUnpinned) { |
| 348 | t.Fatalf("a symlink verified: %v", err) |
| 349 | } |
| 350 | var urls []string |
| 351 | if err := cli.Provision(ctx, serve(body, &urls)); err != nil || len(urls) != 1 { |
| 352 | t.Fatalf("a symlink in place: %v %v", err, urls) |
| 353 | } |
| 354 | if info, err := os.Lstat(path); err != nil || !info.Mode().IsRegular() { |
| 355 | t.Fatalf("still a link: %v %v", info, err) |
| 356 | } |
| 357 | if raw, err := os.ReadFile(real); err != nil || !bytes.Equal(raw, body) { |
| 358 | t.Fatalf("the link's target changed: %v", err) |
| 359 | } |
| 360 | }) |
| 361 | } |
| 362 | |
| 363 | // roundTrip stands in for the network: nothing in a test leaves the |
| 364 | // machine. |
| 365 | type roundTrip func(*http.Request) (*http.Response, error) |
| 366 | |
| 367 | func (f roundTrip) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) } |
| 368 | |
| 369 | // The URL comes from the version and platform constants; the official |
| 370 | // source is an HTTPS GET of it that follows no redirect; a local file |
| 371 | // stands in only in a test build. |
| 372 | func TestProvisionOnlyFromTheOfficialURL(t *testing.T) { |
| 373 | const official = "https://downloads.claude.ai/claude-code-releases/2.1.312/darwin-arm64/claude" |
| 374 | if ClaudeURL() != official || ClaudeURL() != ClaudeReleaseBase+"/"+provider.ClaudeVersion+"/"+ClaudePlatform+"/claude" { |
| 375 | t.Fatalf("ClaudeURL %q", ClaudeURL()) |
| 376 | } |
| 377 | ctx := context.Background() |
| 378 | body := fakeClaude() |
| 379 | var urls []string |
| 380 | if err := claudeFor(t.TempDir(), body).Provision(ctx, serve(body, &urls)); err != nil || !slices.Equal(urls, []string{official}) { |
| 381 | t.Fatalf("asked %v: %v", urls, err) |
| 382 | } |
| 383 | |
| 384 | defer func(was http.RoundTripper) { claudeTransport = was }(claudeTransport) |
| 385 | var asked []string |
| 386 | status, location := http.StatusOK, "" |
| 387 | claudeTransport = roundTrip(func(r *http.Request) (*http.Response, error) { |
| 388 | asked = append(asked, r.Method+" "+r.URL.String()) |
| 389 | h, code := http.Header{}, status |
| 390 | if r.URL.String() != official { |
| 391 | // A redirect's target answers, so a client that follows one |
| 392 | // shows here as a second request rather than looping. |
| 393 | code = http.StatusOK |
| 394 | } else if location != "" { |
| 395 | h.Set("Location", location) |
| 396 | } |
| 397 | return &http.Response{StatusCode: code, Header: h, Body: io.NopCloser(bytes.NewReader(body)), Request: r}, nil |
| 398 | }) |
| 399 | local := filepath.Join(t.TempDir(), "claude") |
| 400 | if err := os.WriteFile(local, body, 0600); err != nil { |
| 401 | t.Fatal(err) |
| 402 | } |
| 403 | getenv := func(k string) string { |
| 404 | if k == ClaudeStandInEnv { |
| 405 | return local |
| 406 | } |
| 407 | return "" |
| 408 | } |
| 409 | if err := claudeFor(t.TempDir(), body).Provision(ctx, OfficialClaude(getenv)); err != nil { |
| 410 | t.Fatal(err) |
| 411 | } |
| 412 | if claudeStandInBuild { |
| 413 | if len(asked) != 0 { |
| 414 | t.Fatalf("a test build with a stand-in fetched %v", asked) |
| 415 | } |
| 416 | } else if !slices.Equal(asked, []string{"GET " + official}) { |
| 417 | t.Fatalf("a release build asked %v, the stand-in must be ignored", asked) |
| 418 | } |
| 419 | |
| 420 | // The official source itself: no redirect followed, 200 only. |
| 421 | none := func(string) string { return "" } |
| 422 | asked, status, location = nil, http.StatusFound, "https://elsewhere.example/claude" |
| 423 | if err := claudeFor(t.TempDir(), body).Provision(ctx, OfficialClaude(none)); err == nil || !slices.Equal(asked, []string{"GET " + official}) { |
| 424 | t.Fatalf("a redirect: %v %v", err, asked) |
| 425 | } |
| 426 | asked, status, location = nil, http.StatusNotFound, "" |
| 427 | if err := claudeFor(t.TempDir(), body).Provision(ctx, OfficialClaude(none)); err == nil || len(asked) != 1 { |
| 428 | t.Fatalf("a 404: %v %v", err, asked) |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | // The launcher hashes the CLI again before each launch: a file swapped |
| 433 | // after a launch stops the next one with engineering_unavailable, and |
| 434 | // nothing starts. |
| 435 | func TestVerifyBeforeEveryLaunch(t *testing.T) { |
| 436 | dir := t.TempDir() |
| 437 | body := fakeClaude() |
| 438 | cli := claudeFor(dir, body) |
| 439 | if err := cli.Provision(context.Background(), serve(body, nil)); err != nil { |
| 440 | t.Fatal(err) |
| 441 | } |
| 442 | var started [][]string |
| 443 | start := func(_ context.Context, argv []string) error { |
| 444 | started = append(started, argv) |
| 445 | return nil |
| 446 | } |
| 447 | for range 2 { |
| 448 | if err := cli.Launch(context.Background(), start, "-p", "--output-format", "stream-json"); err != nil { |
| 449 | t.Fatal(err) |
| 450 | } |
| 451 | } |
| 452 | want := []string{ClaudePath(dir), "-p", "--output-format", "stream-json"} |
| 453 | if len(started) != 2 || !slices.Equal(started[0], want) || !slices.Equal(started[1], want) { |
| 454 | t.Fatalf("started %v", started) |
| 455 | } |
| 456 | unavailable := func(err error) bool { |
| 457 | var u *ClaudeUnavailable |
| 458 | return errors.As(err, &u) && u.Reason() == wire.ReasonEngineeringUnavailable |
| 459 | } |
| 460 | // Swapped in place, same size and mode, one byte changed. |
| 461 | swapped := slices.Clone(body) |
| 462 | swapped[0] ^= 1 |
| 463 | next := ClaudePath(dir) + ".next" |
| 464 | if err := os.WriteFile(next, swapped, 0700); err != nil { |
| 465 | t.Fatal(err) |
| 466 | } |
| 467 | if err := os.Rename(next, ClaudePath(dir)); err != nil { |
| 468 | t.Fatal(err) |
| 469 | } |
| 470 | if err := cli.Launch(context.Background(), start); !unavailable(err) { |
| 471 | t.Fatalf("a swapped CLI: %v", err) |
| 472 | } |
| 473 | // None in place at all, or no CLI configured: the same stop. |
| 474 | if err := os.Remove(ClaudePath(dir)); err != nil { |
| 475 | t.Fatal(err) |
| 476 | } |
| 477 | if err := cli.Launch(context.Background(), start); !unavailable(err) { |
| 478 | t.Fatalf("a missing CLI: %v", err) |
| 479 | } |
| 480 | if err := (ClaudeCLI{}).Launch(context.Background(), start); !unavailable(err) { |
| 481 | t.Fatalf("no CLI: %v", err) |
| 482 | } |
| 483 | if len(started) != 2 { |
| 484 | t.Fatalf("started an unverified CLI: %v", started) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // The capability report runs no claude at all: it states the pinned |
| 489 | // version only while the pinned file verifies. |
| 490 | func TestDiscoveryNeverRunsUnpinnedClaude(t *testing.T) { |
| 491 | dir := t.TempDir() |
| 492 | body := fakeClaude() |
| 493 | cli := claudeFor(dir, body) |
| 494 | var ran []string |
| 495 | x := func(ctx context.Context, name string, args ...string) (string, error) { |
| 496 | ran = append(ran, strings.Join(append([]string{name}, args...), " ")) |
| 497 | return recordedExec(t)(ctx, name, args...) |
| 498 | } |
| 499 | tools := Tools{Xcodebuild: "xcodebuild", Xcrun: "xcrun", Swift: "swift", Node: "node", SRT: "srt", Claude: cli} |
| 500 | if c := Capabilities(context.Background(), x, tools); c.Harness.Claude != "" { |
| 501 | t.Fatalf("claimed %q with nothing provisioned", c.Harness.Claude) |
| 502 | } |
| 503 | if err := cli.Provision(context.Background(), serve(body, nil)); err != nil { |
| 504 | t.Fatal(err) |
| 505 | } |
| 506 | c := Capabilities(context.Background(), x, tools) |
| 507 | if c.Harness.Claude != provider.ClaudeVersion || slices.Contains(c.Tags, wire.TagClaude) { |
| 508 | t.Fatalf("verified: harness %q tags %v", c.Harness.Claude, c.Tags) |
| 509 | } |
| 510 | if err := os.WriteFile(ClaudePath(dir), append(slices.Clone(body), 0), 0700); err != nil { |
| 511 | t.Fatal(err) |
| 512 | } |
| 513 | if c := Capabilities(context.Background(), x, tools); c.Harness.Claude != "" { |
| 514 | t.Fatalf("claimed %q for a changed file", c.Harness.Claude) |
| 515 | } |
| 516 | if len(ran) == 0 { |
| 517 | t.Fatal("the recorder saw nothing") |
| 518 | } |
| 519 | for _, cmd := range ran { |
| 520 | if strings.Contains(cmd, "claude") { |
| 521 | t.Fatalf("discovery ran %q", cmd) |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // The worker keeps the pinned CLI itself: fetched from the official URL |
| 527 | // only, again after a failure (warned, nothing claimed), refreshed when |
| 528 | // it starts verifying, fetched anew when it is swapped, never run. |
| 529 | func TestKeepClaude(t *testing.T) { |
| 530 | defer func(r, c time.Duration) { ClaudeRetry, ClaudeRecheck = r, c }(ClaudeRetry, ClaudeRecheck) |
| 531 | ClaudeRetry, ClaudeRecheck = time.Millisecond, time.Millisecond |
| 532 | dir := t.TempDir() |
| 533 | body := fakeClaude() |
| 534 | cli := claudeFor(dir, body) |
| 535 | ctx, cancel := context.WithCancel(context.Background()) |
| 536 | defer cancel() |
| 537 | var urls, warned, versions []string |
| 538 | fetch := func(_ context.Context, url string) (io.ReadCloser, error) { |
| 539 | urls = append(urls, url) |
| 540 | switch len(urls) { |
| 541 | case 1, 2: |
| 542 | if v := cli.Version(); v != "" { |
| 543 | t.Errorf("claimed %q before any fetch succeeded", v) |
| 544 | } |
| 545 | return nil, errors.New("offline") |
| 546 | case 4: |
| 547 | cancel() |
| 548 | } |
| 549 | return io.NopCloser(bytes.NewReader(body)), nil |
| 550 | } |
| 551 | refresh := func() { |
| 552 | versions = append(versions, cli.Version()) |
| 553 | // Swapped once it verified: found at the next check. |
| 554 | if err := os.WriteFile(ClaudePath(dir), append(slices.Clone(body), 1), 0700); err != nil { |
| 555 | t.Error(err) |
| 556 | } |
| 557 | } |
| 558 | done := make(chan struct{}) |
| 559 | go func() { |
| 560 | KeepClaude(ctx, cli, fetch, refresh, func(msg string, _ ...any) { warned = append(warned, msg) }) |
| 561 | close(done) |
| 562 | }() |
| 563 | select { |
| 564 | case <-done: |
| 565 | case <-time.After(10 * time.Second): |
| 566 | t.Fatal("KeepClaude did not stop with its context") |
| 567 | } |
| 568 | if len(urls) != 4 || slices.ContainsFunc(urls, func(u string) bool { return u != ClaudeURL() }) { |
| 569 | t.Fatalf("asked %v", urls) |
| 570 | } |
| 571 | if len(warned) != 2 || !slices.Equal(versions, []string{provider.ClaudeVersion}) { |
| 572 | t.Fatalf("warned %v versions %v", warned, versions) |
| 573 | } |
| 574 | if cli.Version() != provider.ClaudeVersion { |
| 575 | t.Fatal("the swapped CLI was not fetched again") |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | // Env-gated, run on docs-t1 with task 1's download (never executed: it |
| 580 | // is a darwin binary): the official 2.1.312 darwin-arm64 build matches |
| 581 | // the compiled pin and provisions as the Mac would. |
| 582 | func TestClaudeDarwinBinaryMatchesPin(t *testing.T) { |
| 583 | path := os.Getenv("EQUIPO_CLAUDE_DARWIN_BINARY") |
| 584 | if path == "" { |
| 585 | t.Skip("EQUIPO_CLAUDE_DARWIN_BINARY not set") |
| 586 | } |
| 587 | f, err := os.Open(path) |
| 588 | if err != nil { |
| 589 | t.Fatal(err) |
| 590 | } |
| 591 | defer f.Close() |
| 592 | h := sha256.New() |
| 593 | if _, err := io.Copy(h, f); err != nil { |
| 594 | t.Fatal(err) |
| 595 | } |
| 596 | if got := hex.EncodeToString(h.Sum(nil)); got != claude.ExecutableSHA256DarwinArm64 { |
| 597 | t.Fatalf("%s is %s, the pin is %s", path, got, claude.ExecutableSHA256DarwinArm64) |
| 598 | } |
| 599 | home, dir := install(t) |
| 600 | cli := NewClaudeCLI(home, dir) |
| 601 | var urls []string |
| 602 | fetch := func(_ context.Context, url string) (io.ReadCloser, error) { |
| 603 | urls = append(urls, url) |
| 604 | return os.Open(path) |
| 605 | } |
| 606 | if err := cli.Provision(context.Background(), fetch); err != nil || !slices.Equal(urls, []string{ClaudeURL()}) { |
| 607 | t.Fatalf("provision: %v %v", err, urls) |
| 608 | } |
| 609 | if cli.Version() != "2.1.312" { |
| 610 | t.Fatal("the provisioned binary does not verify") |
| 611 | } |
| 612 | } |
| 613 | |