| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "image/png" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 14 | ) |
| 15 | |
| 16 | |
| 17 | |
| 18 | func TestWindowsBrowserEditorNativeE2E(t *testing.T) { |
| 19 | profile, artifacts := os.Getenv("CAGE_TEST_WINDOWS_WORKBENCH_PROFILE"), os.Getenv("CAGE_TEST_WINDOWS_BROWSER_ARTIFACTS") |
| 20 | if runtime.GOOS != "windows" || profile == "" || artifacts == "" || os.Getenv("CAGE_TEST_WINDOWS_BROWSER_NATIVE") != "1" { |
| 21 | t.Skip("requires the existing paired Windows QA profile and explicit native browser opt-in") |
| 22 | } |
| 23 | binding, err := guestos.LoadWindowsProfile(profile) |
| 24 | if err != nil || binding == nil { |
| 25 | t.Fatal("paired QA profile unavailable", err) |
| 26 | } |
| 27 | s := &Server{profile: profile, configPath: filepath.Join(profile, "env.json"), windowsGuest: binding, ready: true} |
| 28 | s.lifetimeContext() |
| 29 | defer s.lifeCancel() |
| 30 | defer s.closeProjectShares() |
| 31 | defer s.closeWindowsBrowser() |
| 32 | ctx, cancel := context.WithTimeout(s.lifetimeContext(), 5*time.Minute) |
| 33 | defer cancel() |
| 34 | if err = os.MkdirAll(artifacts, 0700); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | projectA, projectB := t.TempDir(), t.TempDir() |
| 38 | for _, project := range []string{projectA, projectB} { |
| 39 | |
| 40 | |
| 41 | if err = os.Mkdir(filepath.Join(project, ".vscode"), 0700); err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | if err = os.WriteFile(filepath.Join(project, ".vscode", "settings.json"), []byte("{\"files.autoSave\":\"off\"}\n"), 0600); err != nil { |
| 45 | t.Fatal(err) |
| 46 | } |
| 47 | if err = os.WriteFile(filepath.Join(project, "first.txt"), []byte("Native editor original\n"), 0600); err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | if err = s.ensureProjectShare(project); err != nil { |
| 51 | t.Fatal("production live share failed", err) |
| 52 | } |
| 53 | } |
| 54 | a, err := s.windowsProjectForHost(projectA) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | b, err := s.windowsProjectForHost(projectB) |
| 59 | if err != nil { |
| 60 | t.Fatal(err) |
| 61 | } |
| 62 | client := s.nativeBrowserClient() |
| 63 | call := func(route string, input any, output any) { |
| 64 | t.Helper() |
| 65 | method := "POST" |
| 66 | if route == "/frame" || route == "/tabs" { |
| 67 | method = "GET" |
| 68 | } |
| 69 | data, err := client.request(ctx, method, route, input) |
| 70 | if err != nil { |
| 71 | t.Fatal("native browser operation failed", route, err) |
| 72 | } |
| 73 | if output != nil && json.Unmarshal(data, output) != nil { |
| 74 | t.Fatal("native browser response invalid", route) |
| 75 | } |
| 76 | } |
| 77 | frame := func(name string) windowsWindowFrame { |
| 78 | t.Helper() |
| 79 | var value windowsWindowFrame |
| 80 | call("/frame", nil, &value) |
| 81 | image, err := nativeFrameImage(value) |
| 82 | if err != nil { |
| 83 | t.Fatal("real native frame invalid", err) |
| 84 | } |
| 85 | file, err := os.Create(filepath.Join(artifacts, name+".png")) |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | err = png.Encode(file, image) |
| 90 | closeErr := file.Close() |
| 91 | if err != nil || closeErr != nil { |
| 92 | t.Fatal("native frame evidence write failed") |
| 93 | } |
| 94 | return value |
| 95 | } |
| 96 | chord := func(id, value string, ctrl, shift bool) { |
| 97 | t.Helper() |
| 98 | f := frame("editor-input-target") |
| 99 | if f.ID != id { |
| 100 | t.Fatal("native editor input target changed") |
| 101 | } |
| 102 | symbol := uint32(value[0]) |
| 103 | if value == "Enter" { |
| 104 | symbol = 0xff0d |
| 105 | } |
| 106 | if value == "End" { |
| 107 | symbol = 0xff57 |
| 108 | } |
| 109 | if value == "Home" { |
| 110 | symbol = 0xff50 |
| 111 | } |
| 112 | if value == "Delete" { |
| 113 | symbol = 0xffff |
| 114 | } |
| 115 | emit := func(key uint32, down bool) { |
| 116 | call("/input", map[string]any{"kind": "key", "target": f.Target, "keysym": key, "down": down}, nil) |
| 117 | } |
| 118 | if ctrl { |
| 119 | emit(0xffe3, true) |
| 120 | } |
| 121 | if shift { |
| 122 | emit(0xffe1, true) |
| 123 | } |
| 124 | emit(symbol, true) |
| 125 | emit(symbol, false) |
| 126 | if shift { |
| 127 | emit(0xffe1, false) |
| 128 | } |
| 129 | if ctrl { |
| 130 | emit(0xffe3, false) |
| 131 | } |
| 132 | } |
| 133 | key := func(id, value string, ctrl bool) { chord(id, value, ctrl, false) } |
| 134 | insert := func(id, value string) { call("/insert-text", map[string]any{"id": id, "text": value}, nil) } |
| 135 | var first, second struct { |
| 136 | ID string `json:"id"` |
| 137 | } |
| 138 | open := func(project *windowsProjectBinding, output any) { |
| 139 | call("/editor", map[string]string{"project_id": project.ID, "grant": project.Grant}, output) |
| 140 | } |
| 141 | open(a, &first) |
| 142 | defer func() { |
| 143 | _, _ = client.request(context.Background(), "POST", "/close", map[string]string{"id": first.ID}) |
| 144 | }() |
| 145 | time.Sleep(1500 * time.Millisecond) |
| 146 | opened := frame("editor-01-open") |
| 147 | for _, overlay := range opened.Overlays { |
| 148 | |
| 149 | |
| 150 | if overlay.X > opened.Width/2 && overlay.Y == 0 && overlay.Width < opened.Width && overlay.Height < opened.Height/2 { |
| 151 | for _, mask := range []int{1, 0} { |
| 152 | call("/input", map[string]any{"kind": "pointer", "target": opened.Target, "x": opened.Width - 68, "y": 56, "mask": mask}, nil) |
| 153 | } |
| 154 | break |
| 155 | } |
| 156 | } |
| 157 | opened = frame("editor-01-startup-ready") |
| 158 | if os.Getenv("CAGE_TEST_WINDOWS_EDITOR_PREFERENCES_ONLY") == "1" { |
| 159 | |
| 160 | |
| 161 | time.Sleep(2 * time.Second) |
| 162 | frame("prefs-01-default") |
| 163 | user := func(id, name string) { |
| 164 | chord(id, "p", true, true) |
| 165 | time.Sleep(200 * time.Millisecond) |
| 166 | insert(id, "Preferences: Open User Settings (JSON)") |
| 167 | time.Sleep(200 * time.Millisecond) |
| 168 | key(id, "Enter", false) |
| 169 | time.Sleep(500 * time.Millisecond) |
| 170 | f := frame(name) |
| 171 | |
| 172 | |
| 173 | for _, mask := range []int{1, 0} { |
| 174 | call("/input", map[string]any{"kind": "pointer", "target": f.Target, "x": f.Width / 3, "y": f.Height / 4, "mask": mask}, nil) |
| 175 | } |
| 176 | } |
| 177 | user(first.ID, "prefs-02-a-empty") |
| 178 | key(first.ID, "a", true) |
| 179 | insert(first.ID, "{\"security.workspace.trust.enabled\":true,\"editor.fontSize\":19}\n") |
| 180 | |
| 181 | |
| 182 | key(first.ID, "End", true) |
| 183 | chord(first.ID, "Home", false, true) |
| 184 | key(first.ID, "Delete", false) |
| 185 | key(first.ID, "s", true) |
| 186 | time.Sleep(500 * time.Millisecond) |
| 187 | frame("prefs-03-a-saved") |
| 188 | var restarted struct { |
| 189 | ID string `json:"id"` |
| 190 | } |
| 191 | call("/editor-restart", map[string]string{"project_id": a.ID, "grant": a.Grant}, &restarted) |
| 192 | if restarted.ID != first.ID { |
| 193 | t.Fatal("editor preference restart replaced the owned tab") |
| 194 | } |
| 195 | time.Sleep(2 * time.Second) |
| 196 | frame("prefs-04-a-restarted") |
| 197 | user(first.ID, "prefs-05-a-read-back") |
| 198 | open(b, &second) |
| 199 | defer func() { |
| 200 | _, _ = client.request(context.Background(), "POST", "/close", map[string]string{"id": second.ID}) |
| 201 | }() |
| 202 | time.Sleep(2 * time.Second) |
| 203 | user(second.ID, "prefs-06-b-read-back") |
| 204 | t.Log("preference-only native UI evidence captured; inspect actual A saved/restarted User JSON and fresh B User JSON") |
| 205 | return |
| 206 | } |
| 207 | |
| 208 | |
| 209 | for _, mask := range []int{1, 0} { |
| 210 | call("/input", map[string]any{"kind": "pointer", "target": opened.Target, "x": 300, "y": 260, "mask": mask}, nil) |
| 211 | } |
| 212 | time.Sleep(250 * time.Millisecond) |
| 213 | key(first.ID, "p", true) |
| 214 | time.Sleep(200 * time.Millisecond) |
| 215 | frame("editor-01a-quick-open") |
| 216 | insert(first.ID, "first.txt") |
| 217 | time.Sleep(200 * time.Millisecond) |
| 218 | key(first.ID, "Enter", false) |
| 219 | time.Sleep(400 * time.Millisecond) |
| 220 | frame("editor-02-file") |
| 221 | key(first.ID, "a", true) |
| 222 | insert(first.ID, "Native editor saved\n") |
| 223 | key(first.ID, "s", true) |
| 224 | deadline := time.Now().Add(4 * time.Second) |
| 225 | for { |
| 226 | data, readErr := os.ReadFile(filepath.Join(projectA, "first.txt")) |
| 227 | if readErr == nil && string(data) == "Native editor saved\n" { |
| 228 | break |
| 229 | } |
| 230 | if time.Now().After(deadline) { |
| 231 | frame("editor-save-failed") |
| 232 | t.Fatal("actual editor save did not reach the granted host file") |
| 233 | } |
| 234 | time.Sleep(100 * time.Millisecond) |
| 235 | } |
| 236 | insert(first.ID, "CAGE_UNSAVED_NATIVE_BUFFER") |
| 237 | frame("editor-03-unsaved") |
| 238 | open(b, &second) |
| 239 | defer func() { |
| 240 | _, _ = client.request(context.Background(), "POST", "/close", map[string]string{"id": second.ID}) |
| 241 | }() |
| 242 | var returned struct { |
| 243 | ID string `json:"id"` |
| 244 | } |
| 245 | open(a, &returned) |
| 246 | if returned.ID != first.ID { |
| 247 | t.Fatal("switching projects replaced the existing editor tab") |
| 248 | } |
| 249 | frame("editor-04-returned-unsaved") |
| 250 | if data, _ := os.ReadFile(filepath.Join(projectA, "first.txt")); string(data) != "Native editor saved\n" { |
| 251 | t.Fatal("unsaved buffer unexpectedly changed the host file") |
| 252 | } |
| 253 | call("/split", map[string]string{"id": first.ID, "left": first.ID, "right": second.ID}, nil) |
| 254 | frame("editor-05-split") |
| 255 | s.mu.Lock() |
| 256 | share := s.projectShares[projectB] |
| 257 | s.mu.Unlock() |
| 258 | share.close() |
| 259 | deadline = time.Now().Add(4 * time.Second) |
| 260 | for { |
| 261 | _, err = client.request(ctx, "GET", "/frame", nil) |
| 262 | if err != nil { |
| 263 | break |
| 264 | } |
| 265 | if time.Now().After(deadline) { |
| 266 | t.Fatal("split still returned a frame containing the revoked editor") |
| 267 | } |
| 268 | time.Sleep(100 * time.Millisecond) |
| 269 | } |
| 270 | if _, err = client.request(ctx, "POST", "/editor", map[string]string{"project_id": b.ID, "grant": b.Grant}); err == nil { |
| 271 | t.Fatal("a retired real project grant reopened the editor") |
| 272 | } |
| 273 | call("/split", map[string]string{"id": first.ID}, nil) |
| 274 | |
| 275 | |
| 276 | |
| 277 | chord(first.ID, "p", true, true) |
| 278 | time.Sleep(200 * time.Millisecond) |
| 279 | insert(first.ID, "Preferences: Open User Settings (JSON)") |
| 280 | time.Sleep(200 * time.Millisecond) |
| 281 | key(first.ID, "Enter", false) |
| 282 | time.Sleep(400 * time.Millisecond) |
| 283 | frame("editor-06-own-user-settings") |
| 284 | key(first.ID, "a", true) |
| 285 | insert(first.ID, "{\"security.workspace.trust.enabled\":true}\n") |
| 286 | key(first.ID, "s", true) |
| 287 | time.Sleep(300 * time.Millisecond) |
| 288 | call("/editor-restart", map[string]string{"project_id": a.ID, "grant": a.Grant}, &returned) |
| 289 | time.Sleep(1000 * time.Millisecond) |
| 290 | frame("editor-07-explicit-user-true") |
| 291 | if returned.ID != first.ID { |
| 292 | t.Fatal("explicit editor restart replaced the owned tab") |
| 293 | } |
| 294 | t.Log("real native editor: production share, file open/save, same tab and dirty buffer retained across project switch, split and revoked grant denied; inspect PNGs for default unrestricted mode and explicit browser User=true after restart") |
| 295 | } |