| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/google/uuid" |
| 14 | ) |
| 15 | |
| 16 | func TestMobileCLIRecoveryDoesNotReplayAndChangesEpoch(t *testing.T) { |
| 17 | s := testServer(t) |
| 18 | project, _ := filepath.EvalSymlinks(t.TempDir()) |
| 19 | if err := writePrivate(filepath.Join(s.profile, "environment.json"), map[string]string{"directory": project}); err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | id := uuid.NewString() |
| 23 | old, err := s.mobileCLI.create(s.profile, project, id) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | lookup, err := s.mobileCLI.record(s.profile, id) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | if lookup.CLIID == id { |
| 32 | t.Fatal("caller selected the runtime transcript ID") |
| 33 | } |
| 34 | lookup.Turns = append(lookup.Turns, mobileTurn{ID: uuid.NewString(), Prompt: "test accepted request", Status: "running"}) |
| 35 | lookup.Resume = true |
| 36 | lookup.changed() |
| 37 | path := mobileRecordPath(s.profile, id) |
| 38 | if err = writePrivate(path, lookup); err != nil { |
| 39 | t.Fatal(err) |
| 40 | } |
| 41 | s.mobileCLI = mobileCLIStore{} |
| 42 | url := "/api/mobile/cli/sessions/" + id |
| 43 | w := request(s, "GET", url+"?after=3&epoch="+old.Generation, "", "", true) |
| 44 | if w.Code != 200 { |
| 45 | t.Fatalf("recovery returned %d", w.Code) |
| 46 | } |
| 47 | var recovered mobileSession |
| 48 | if err = json.Unmarshal(w.Body.Bytes(), &recovered); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if recovered.Generation == old.Generation || recovered.Turns[0].Status != "interrupted" { |
| 52 | t.Fatal("unfinished turn was not reconciled") |
| 53 | } |
| 54 | message, _ := json.Marshal(map[string]string{"request_id": lookup.Turns[0].ID, "text": lookup.Turns[0].Prompt}) |
| 55 | if rep := request(s, "POST", url+"/messages", s.origin, string(message), true); rep.Code != 200 { |
| 56 | t.Fatal("same request should resolve to its accepted result", rep.Code) |
| 57 | } |
| 58 | message, _ = json.Marshal(map[string]string{"request_id": lookup.Turns[0].ID, "text": "other prompt"}) |
| 59 | if rep := request(s, "POST", url+"/messages", s.origin, string(message), true); rep.Code != 409 { |
| 60 | t.Fatal("request ID reused for different content") |
| 61 | } |
| 62 | if err = os.WriteFile(path, []byte("{broken"), 0600); err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if _, err = s.mobileCLI.create(s.profile, project, id); err == nil { |
| 66 | t.Fatal("corrupted record was overwritten") |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestMobileCLIAccountAndConnectionBoundaries(t *testing.T) { |
| 71 | s := testServer(t) |
| 72 | path := "/api/mobile/cli/projects" |
| 73 | if w := request(s, "GET", path, "", "", false); w.Code != 401 { |
| 74 | t.Fatal("unauthenticated project read", w.Code) |
| 75 | } |
| 76 | if w := request(s, "POST", "/api/mobile/cli/sessions", "https://outside.example", "{}", true); w.Code != 403 { |
| 77 | t.Fatal("cross-origin create", w.Code) |
| 78 | } |
| 79 | s.remote = newRemoteGateway(s.profile) |
| 80 | s.remote.setRemote(true) |
| 81 | |
| 82 | |
| 83 | if w := request(s, "GET", path, "", "", true); w.Code != 200 { |
| 84 | t.Fatal("mobile route escaped host scope", w.Code) |
| 85 | } |
| 86 | s.clientOnly = true |
| 87 | if w := request(s, "GET", path, "", "", true); w.Code != 409 { |
| 88 | t.Fatal("client-only host allowed work", w.Code) |
| 89 | } |
| 90 | s.clientOnly = false |
| 91 | s.account.lock() |
| 92 | if w := request(s, "GET", path, "", "", true); w.Code != http.StatusLocked { |
| 93 | t.Fatal("locked account allowed read", w.Code) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestMobileCLILaunchScopeAndOldApproval(t *testing.T) { |
| 98 | for _, command := range []string{"claude --dangerously-skip-permissions", "ccs a;id", "ccs --yes", "bash -c anything", "claude | other"} { |
| 99 | if _, err := mobileLaunchArgs(command, claudeSelection{}); err == nil { |
| 100 | t.Fatal("accepted launcher", command) |
| 101 | } |
| 102 | } |
| 103 | for _, command := range []string{"claude", "ccs klink"} { |
| 104 | if _, err := mobileLaunchArgs(command, claudeSelection{}); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | } |
| 108 | s := testServer(t) |
| 109 | project, _ := filepath.EvalSymlinks(t.TempDir()) |
| 110 | if err := writePrivate(filepath.Join(s.profile, "environment.json"), map[string]string{"directory": project}); err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | id := uuid.NewString() |
| 114 | if _, err := s.mobileCLI.create(s.profile, project, id); err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | record, err := s.mobileCLI.record(s.profile, id) |
| 118 | if err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | turnID, approvalID := uuid.NewString(), uuid.NewString() |
| 122 | record.Turns = []mobileTurn{{ID: turnID, Status: "running", Approval: &mobileApproval{ID: approvalID, Tool: "Bash"}}} |
| 123 | ctx, cancel := context.WithCancel(context.Background()) |
| 124 | defer cancel() |
| 125 | decision := &mobileDecision{ctx: ctx, reply: make(chan bool, 1)} |
| 126 | s.mobileCLI.active = map[string]*mobileRun{id: {record: record, pending: decision, cancel: cancel}} |
| 127 | answer := func(approval string) int { |
| 128 | body, _ := json.Marshal(map[string]any{"request_id": turnID, "approval_id": approval, "allow": true}) |
| 129 | return request(s, "POST", "/api/mobile/cli/sessions/"+id+"/approval", s.origin, string(body), true).Code |
| 130 | } |
| 131 | if answer(uuid.NewString()) != 409 { |
| 132 | t.Fatal("stale approval accepted") |
| 133 | } |
| 134 | if answer(approvalID) != 200 || !<-decision.reply { |
| 135 | t.Fatal("exact approval not delivered") |
| 136 | } |
| 137 | if answer(approvalID) != 409 { |
| 138 | t.Fatal("decision replay accepted") |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func TestDesktopCLIViewUsesOnlyItsLocalWorkload(t *testing.T) { |
| 143 | gateway, local := gatewayFixture(t) |
| 144 | gateway.desktopEntry = true |
| 145 | d := gateway.remote.desktop |
| 146 | generation := chooseLocal(t, d, d.context().ConnectionID) |
| 147 | path := "/api/mobile/cli/projects" |
| 148 | if w := gatewayRequest(gateway, path, generation); w.Code != http.StatusOK || local.commands.Load() != 1 { |
| 149 | t.Fatalf("desktop local view failed: %d, requests %d", w.Code, local.commands.Load()) |
| 150 | } |
| 151 | for _, id := range []string{"", "stale-generation"} { |
| 152 | if w := gatewayRequest(gateway, path, id); w.Code != http.StatusConflict || local.commands.Load() != 1 { |
| 153 | t.Fatalf("stale view reached workload: %d", w.Code) |
| 154 | } |
| 155 | } |
| 156 | d.mu.Lock() |
| 157 | d.active.mode = "remote" |
| 158 | d.mu.Unlock() |
| 159 | if w := gatewayRequest(gateway, path, generation); w.Code != http.StatusForbidden || local.commands.Load() != 1 { |
| 160 | t.Fatalf("mobile operation sent to SSH workload: %d", w.Code) |
| 161 | } |
| 162 | if w := gatewayRequest(gateway, "/api/mobile-pair", generation); w.Code != http.StatusForbidden || local.commands.Load() != 1 { |
| 163 | t.Fatalf("phone pairing management reached the SSH workload: %d", w.Code) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestMobileCLIStopFromPreviousUnlock(t *testing.T) { |
| 168 | s := testServer(t) |
| 169 | project, _ := filepath.EvalSymlinks(t.TempDir()) |
| 170 | if err := writePrivate(filepath.Join(s.profile, "environment.json"), map[string]string{"directory": project}); err != nil { |
| 171 | t.Fatal(err) |
| 172 | } |
| 173 | id, turnID := uuid.NewString(), uuid.NewString() |
| 174 | if _, err := s.mobileCLI.create(s.profile, project, id); err != nil { |
| 175 | t.Fatal(err) |
| 176 | } |
| 177 | record, err := s.mobileCLI.record(s.profile, id) |
| 178 | if err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | record.Turns = []mobileTurn{{ID: turnID, Status: "running"}} |
| 182 | run, cancel := context.WithCancel(context.Background()) |
| 183 | defer cancel() |
| 184 | s.mobileCLI.active = map[string]*mobileRun{id: {record: record, cancel: cancel}} |
| 185 | stop := func(epoch context.Context) int { |
| 186 | req := httptest.NewRequest("POST", "/", strings.NewReader(`{"request_id":"`+turnID+`"}`)) |
| 187 | req.SetPathValue("id", id) |
| 188 | req.Header.Set("Content-Type", "application/json") |
| 189 | w := httptest.NewRecorder() |
| 190 | s.mobileStop(w, req.WithContext(context.WithValue(req.Context(), mobileEpochKey{}, epoch))) |
| 191 | return w.Code |
| 192 | } |
| 193 | oldEpoch, _ := s.account.access() |
| 194 | s.account.lock() |
| 195 | s.account.setKey(make([]byte, 32)) |
| 196 | if code := stop(oldEpoch); code != http.StatusLocked || run.Err() != nil { |
| 197 | t.Fatalf("expired request stopped active turn: HTTP %d, %v", code, run.Err()) |
| 198 | } |
| 199 | newEpoch, _ := s.account.access() |
| 200 | if code := stop(newEpoch); code != http.StatusOK || run.Err() == nil { |
| 201 | t.Fatalf("current request failed to stop active turn: HTTP %d, %v", code, run.Err()) |
| 202 | } |
| 203 | } |