| 1 | |
| 2 | |
| 3 | package workbench |
| 4 | |
| 5 | import ( |
| 6 | "encoding/json" |
| 7 | "io" |
| 8 | "net/http" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "slices" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "al.essio.dev/pkg/shellescape" |
| 16 | ) |
| 17 | |
| 18 | func TestProjectRemovalRefreshesExternalSessions(t *testing.T) { |
| 19 | s, id, project := metadataFixture(t) |
| 20 | s.terminals.sessions = nil |
| 21 | home, _ := filepath.EvalSymlinks(t.TempDir()) |
| 22 | bin := filepath.Join(home, "bin") |
| 23 | if err := os.Mkdir(bin, 0700); err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | const cli = "#!/bin/sh\n[ \"$#\" -eq 2 ] && [ \"$1\" = agents ] && [ \"$2\" = --json ] || exit 2\ncat \"$HOME/agents.json\"\n" |
| 27 | if err := os.WriteFile(filepath.Join(bin, "claude"), []byte(cli), 0700); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := os.WriteFile(filepath.Join(home, ".bash_profile"), []byte("export PATH="+shellescape.Quote(bin)+":/usr/bin:/bin\n"), 0600); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | mobileTestSSH(t, s, home, []string{"HOME=" + home, "PATH=" + bin + ":/usr/bin:/bin", "CLAUDE_CONFIG_DIR=" + filepath.Join(home, ".claude")}) |
| 34 | record, err := s.mobileCLI.record(s.profile, id) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | history := filepath.Join(home, ".claude", "projects", "fixture", record.CLIID+".jsonl") |
| 39 | if err := os.MkdirAll(filepath.Dir(history), 0700); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | row, _ := json.Marshal(map[string]any{"cwd": guestPath(project), "type": "user", "message": map[string]string{"content": "keep history"}}) |
| 43 | if err := os.WriteFile(history, append(row, '\n'), 0600); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | registry := filepath.Join(home, "agents.json") |
| 47 | if err := writePrivate(registry, []map[string]any{{"sessionId": record.CLIID, "pid": os.Getpid()}}); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | s.ready = true |
| 51 | s.browserControl = browserClientForTest(t, s, browserTransport(func(r *http.Request) (*http.Response, error) { |
| 52 | return &http.Response{StatusCode: 200, Header: http.Header{}, Body: io.NopCloser(strings.NewReader(`{"tabs":[]}`))}, nil |
| 53 | })) |
| 54 | removeProjectRequest(t, s, project, 409) |
| 55 | if !s.mobileCLI.running[id] { |
| 56 | t.Fatal("removal did not refresh the previously empty process cache") |
| 57 | } |
| 58 | if err := os.WriteFile(registry, []byte("invalid JSON"), 0600); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | removeProjectRequest(t, s, project, 409) |
| 62 | if err := writePrivate(registry, []any{}); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | removeProjectRequest(t, s, project, 200) |
| 66 | paths, err := s.projectPaths() |
| 67 | if err != nil || slices.Contains(paths, project) { |
| 68 | t.Fatal("finished session blocked removal", err) |
| 69 | } |
| 70 | if _, err := os.Stat(history); err != nil { |
| 71 | t.Fatal("guest history changed", err) |
| 72 | } |
| 73 | } |