| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "slices" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | func TestProjectPickerConsumerAddsOnceAndReusesAuthorizedDirectory(t *testing.T) { |
| 13 | s := testServer(t) |
| 14 | s.ready, s.networkPaused = true, true |
| 15 | a, _ := filepath.EvalSymlinks(t.TempDir()) |
| 16 | b, _ := filepath.EvalSymlinks(t.TempDir()) |
| 17 | s.preferences.Workspace = a |
| 18 | if err := s.rememberSharedProject(a); err != nil { |
| 19 | t.Fatal(err) |
| 20 | } |
| 21 | share := &projectShare{done: make(chan struct{})} |
| 22 | s.projectShares = map[string]*projectShare{a: {done: make(chan struct{})}, b: share} |
| 23 | terminal := &terminalSession{id: "existing", workspace: a} |
| 24 | s.terminals.sessions = map[string]*terminalSession{terminal.id: terminal} |
| 25 | config, _ := os.ReadFile(s.configPath) |
| 26 | call := func(path string, add bool) string { |
| 27 | t.Helper() |
| 28 | body, _ := json.Marshal(map[string]any{"workspace": path, "add": add}) |
| 29 | w := request(s, "POST", "/api/projects/open", s.origin, string(body), true) |
| 30 | if w.Code != 200 { |
| 31 | t.Fatal(w.Code, w.Body.String()) |
| 32 | } |
| 33 | var result struct { |
| 34 | Workspace string `json:"workspace"` |
| 35 | } |
| 36 | if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil { |
| 37 | t.Fatal(err) |
| 38 | } |
| 39 | return result.Workspace |
| 40 | } |
| 41 | body, _ := json.Marshal(map[string]any{"workspace": b}) |
| 42 | if w := request(s, "POST", "/api/projects/open", s.origin, string(body), true); w.Code != 400 { |
| 43 | t.Fatal("consumer granted an unregistered directory", w.Code, w.Body.String()) |
| 44 | } |
| 45 | if call(b, true) != b || call(b, false) != b || call(b, true) != b { |
| 46 | t.Fatal("project path changed") |
| 47 | } |
| 48 | alias := filepath.Join(t.TempDir(), "project-link") |
| 49 | if err := os.Symlink(b, alias); err == nil { |
| 50 | if call(alias, false) != b { |
| 51 | t.Fatal("authorized alias did not use canonical project") |
| 52 | } |
| 53 | } |
| 54 | projects, err := s.projectPaths() |
| 55 | if err != nil || !slices.Equal(projects, []string{s.environmentDir(), a, b}) { |
| 56 | t.Fatal("list diverged or repeated grants", projects, err) |
| 57 | } |
| 58 | reloaded := testServer(t) |
| 59 | reloaded.profile = s.profile |
| 60 | reloaded.preferences = s.preferences |
| 61 | if saved, err := reloaded.projectPaths(); err != nil || !slices.Equal(projects, saved) { |
| 62 | t.Fatal("project list did not persist", saved, err) |
| 63 | } |
| 64 | if s.workspace() != a || s.projectShares[b] != share || terminal.closed || !s.ready || !s.networkPaused { |
| 65 | t.Fatal("consumer selection changed existing context") |
| 66 | } |
| 67 | after, _ := os.ReadFile(s.configPath) |
| 68 | if !bytes.Equal(config, after) { |
| 69 | t.Fatal("project selection rewrote network configuration") |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestProjectPickerWorkbenchActivationPreservesOtherPreferences(t *testing.T) { |
| 74 | s := testServer(t) |
| 75 | s.ready = true |
| 76 | root, _ := filepath.EvalSymlinks(t.TempDir()) |
| 77 | s.projectShares = map[string]*projectShare{root: {done: make(chan struct{})}} |
| 78 | s.preferences = Preferences{Workspace: s.environmentDir(), Keyboard: "mac", AutoTimezone: true} |
| 79 | original := s.preferences |
| 80 | body, _ := json.Marshal(map[string]any{"workspace": root, "add": true, "activate": true}) |
| 81 | w := request(s, "POST", "/api/projects/open", s.origin, string(body), true) |
| 82 | if w.Code != 200 { |
| 83 | t.Fatal(w.Code, w.Body.String()) |
| 84 | } |
| 85 | var saved Preferences |
| 86 | data, err := os.ReadFile(filepath.Join(s.profile, "workbench.json")) |
| 87 | if err != nil || json.Unmarshal(data, &saved) != nil { |
| 88 | t.Fatal("activation was not persisted", err) |
| 89 | } |
| 90 | original.Workspace = root |
| 91 | if s.preferences != original || saved != original { |
| 92 | t.Fatal("activation changed unrelated preferences", saved, s.preferences) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestProjectPickerRejectsLifecycleRacesAndUntrustedRequests(t *testing.T) { |
| 97 | s := testServer(t) |
| 98 | s.ready = true |
| 99 | root, _ := filepath.EvalSymlinks(t.TempDir()) |
| 100 | body, _ := json.Marshal(map[string]any{"workspace": root, "add": true}) |
| 101 | for _, c := range []struct { |
| 102 | origin string |
| 103 | auth bool |
| 104 | code int |
| 105 | }{{s.origin, false, 401}, {"https://example.com", true, 403}} { |
| 106 | if w := request(s, "POST", "/api/projects/open", c.origin, string(body), c.auth); w.Code != c.code { |
| 107 | t.Fatal(w.Code, w.Body.String()) |
| 108 | } |
| 109 | } |
| 110 | s.op.Lock() |
| 111 | for _, c := range []struct{ endpoint, body string }{ |
| 112 | {"/api/projects/open", string(body)}, |
| 113 | {"/api/input/files/directory", `{"target":"desktop","id":"` + testBrowserFileID + `","workspace":"` + filepath.ToSlash(root) + `"}`}, |
| 114 | } { |
| 115 | w := request(s, "POST", c.endpoint, s.origin, c.body, true) |
| 116 | if w.Code != 409 { |
| 117 | s.op.Unlock() |
| 118 | t.Fatal("project access raced lifecycle operation", w.Code, w.Body.String()) |
| 119 | } |
| 120 | } |
| 121 | s.op.Unlock() |
| 122 | if _, err := os.Stat(filepath.Join(s.profile, "projects.json")); !os.IsNotExist(err) { |
| 123 | t.Fatal("busy request granted project access") |
| 124 | } |
| 125 | s.ready = false |
| 126 | if w := request(s, "POST", "/api/projects/open", s.origin, string(body), true); w.Code != 409 { |
| 127 | t.Fatal(w.Code, w.Body.String()) |
| 128 | } |
| 129 | s.account.lock() |
| 130 | if w := request(s, "POST", "/api/projects/open", s.origin, string(body), true); w.Code != 423 { |
| 131 | t.Fatal("locked account could authorize project", w.Code) |
| 132 | } |
| 133 | } |