| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "os" |
| 6 | "os/exec" |
| 7 | "path/filepath" |
| 8 | "runtime" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestClaudeDesktopSnapshotFailureReason(t *testing.T) { |
| 13 | if runtime.GOOS == "windows" { |
| 14 | t.Skip("snapshot executes in the Linux guest; fixture requires POSIX executables") |
| 15 | } |
| 16 | if _, err := exec.LookPath("python3"); err != nil { |
| 17 | t.Skip("python3 unavailable") |
| 18 | } |
| 19 | dir := t.TempDir() |
| 20 | for name, script := range map[string]string{ |
| 21 | "dpkg-query": "#!/bin/sh\nprintf 'install ok installed\\n4.4212.0\\n'\n", |
| 22 | "systemctl": "#!/bin/sh\nif [ \"$1\" = is-active ]; then printf '%s\\n' \"$TEST_ACTIVE\"; else printf '%s\\n' \"$TEST_RESULT\"; fi\n", |
| 23 | } { |
| 24 | if err := os.WriteFile(filepath.Join(dir, name), []byte(script), 0755); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | } |
| 28 | for _, tc := range []struct{ active, result, want string }{ |
| 29 | {"failed", "oom-kill", "oom-kill"}, {"failed", "exit-code", ""}, {"active", "oom-kill", ""}, |
| 30 | } { |
| 31 | cmd := exec.Command("sh", "-c", claudeDesktopSnapshot) |
| 32 | cmd.Env = append(os.Environ(), "PATH="+dir+":"+os.Getenv("PATH"), "TEST_ACTIVE="+tc.active, "TEST_RESULT="+tc.result) |
| 33 | out, err := cmd.CombinedOutput() |
| 34 | var state claudeDesktopState |
| 35 | if err != nil || json.Unmarshal(out, &state) != nil { |
| 36 | t.Fatalf("snapshot: %v %s", err, out) |
| 37 | } |
| 38 | if state.FailureReason != tc.want || state.Failed != (tc.active == "failed") || state.Running != (tc.active == "active") { |
| 39 | t.Fatalf("%+v: %+v", tc, state) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func TestClaudeDesktopControlBoundary(t *testing.T) { |
| 45 | s := testServer(t) |
| 46 | for _, path := range []string{"/api/claude-desktop/install", "/api/claude-desktop/start", "/api/claude-desktop/stop"} { |
| 47 | if w := request(s, "POST", path, s.origin, "{}", false); w.Code != 401 { |
| 48 | t.Fatalf("%s without session: %d", path, w.Code) |
| 49 | } |
| 50 | if w := request(s, "POST", path, "https://claude.ai", "{}", true); w.Code != 403 { |
| 51 | t.Fatalf("%s cross-origin: %d", path, w.Code) |
| 52 | } |
| 53 | if w := request(s, "POST", path, s.origin, `{"command":"touch /tmp/untrusted"}`, true); w.Code != 400 { |
| 54 | t.Fatalf("%s accepted arbitrary command: %d", path, w.Code) |
| 55 | } |
| 56 | if w := request(s, "POST", path, s.origin, "{}", true); w.Code != 409 { |
| 57 | t.Fatalf("%s must require running VM: %d", path, w.Code) |
| 58 | } |
| 59 | } |
| 60 | for _, body := range []string{`{"width":1,"height":1000}`, `{"width":8193,"height":1000}`, `{"width":1000,"height":8193}`, `{"width":"1024;command","height":768}`, |
| 61 | `{"width":1000,"height":800,"scale":0.5}`, `{"width":1000,"height":800,"scale":4.5}`, `{"width":1000,"height":800,"scale":"2;command"}`} { |
| 62 | if w := request(s, "POST", "/api/claude-desktop/resize", s.origin, body, true); w.Code != 400 { |
| 63 | t.Fatalf("invalid geometry: %d", w.Code) |
| 64 | } |
| 65 | } |
| 66 | } |