提交记录修改internal/workbench/windows_browser_client_test.go+50 −3看文件
| @@ -6,10 +6,12 @@ import ( | ||
| 6 | 6 | "context" |
| 7 | 7 | "encoding/base64" |
| 8 | 8 | "encoding/json" |
| 9 | 新增: "errors" | |
| 9 | 10 | "fmt" |
| 10 | 11 | "io" |
| 11 | 12 | "os" |
| 12 | 13 | "os/exec" |
| 14 | 新增: "path/filepath" | |
| 13 | 15 | "strings" |
| 14 | 16 | "testing" |
| 15 | 17 | "time" |
| @@ -50,6 +52,13 @@ func TestWindowsBrowserRelayHelper(t *testing.T) { | ||
| 50 | 52 | result = map[string]any{"active": "tab", "tabs": []any{}} |
| 51 | 53 | case "/events": |
| 52 | 54 | result = map[string]any{"sequence": 2, "events": []any{}} |
| 55 | 新增: if request.Input.After == 2 { | |
| 56 | 新增: if acknowledgement := os.Getenv("CAGE_BROWSER_EVENT_ACK"); acknowledgement != "" { | |
| 57 | 新增: if err := os.WriteFile(acknowledgement, []byte("2"), 0600); err != nil { | |
| 58 | 新增: os.Exit(5) | |
| 59 | 新增: } | |
| 60 | 新增: } | |
| 61 | 新增: } | |
| 53 | 62 | if request.Input.After < 2 { |
| 54 | 63 | result = map[string]any{"sequence": 2, "state": map[string]any{"active": "tab", "tabs": []any{}}, "events": []any{ |
| 55 | 64 | map[string]any{"sequence": 2, "type": "clipboard", "value": map[string]any{"id": "tab", "text": "复制🙂", "context": "0123456789abcdef0123456789abcdef", "document": "current-document"}}, |
| @@ -201,13 +210,51 @@ func TestWindowsBrowserCanceledFrameCannotBecomeNextViewerReply(t *testing.T) { | ||
| 201 | 210 | |
| 202 | 211 | func TestNativeBrowserEventsPreserveCopyContextAndSequence(t *testing.T) { |
| 203 | 212 | client := fixtureWindowsBrowserClient(t) |
| 213 | 新增: acknowledgement := filepath.Join(t.TempDir(), "event-cursor") | |
| 214 | 新增: start := client.start | |
| 215 | 新增: client.start = func(ctx context.Context) (*exec.Cmd, error) { | |
| 216 | 新增: command, err := start(ctx) | |
| 217 | 新增: if err == nil { | |
| 218 | 新增: command.Env = append(command.Env, "CAGE_BROWSER_EVENT_ACK="+acknowledgement) | |
| 219 | 新增: } | |
| 220 | 新增: return command, err | |
| 221 | 新增: } | |
| 204 | 222 | server := &Server{windowsBrowserControl: client} |
| 205 | 删除: ctx, cancel := context.WithTimeout(t.Context(), 650*time.Millisecond) | |
| 223 | 新增: ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second) | |
| 206 | 224 | defer cancel() |
| 207 | 225 | stream := server.subscribeNativeBrowser(ctx) |
| 208 | 226 | defer stream.Close() |
| 209 | 删除: data, err := io.ReadAll(stream) | |
| 210 | 删除: if err != nil { | |
| 227 | 新增: type readResult struct { | |
| 228 | 新增: data []byte | |
| 229 | 新增: err error | |
| 230 | 新增: } | |
| 231 | 新增: finished := make(chan readResult, 1) | |
| 232 | 新增: go func() { data, err := io.ReadAll(stream); finished <- readResult{data, err} }() | |
| 233 | 新增: ticker := time.NewTicker(10 * time.Millisecond) | |
| 234 | 新增: defer ticker.Stop() | |
| 235 | 新增: for { | |
| 236 | 新增: if data, err := os.ReadFile(acknowledgement); err == nil && string(data) == "2" { | |
| 237 | 新增: break | |
| 238 | 新增: } | |
| 239 | 新增: select { | |
| 240 | 新增: case <-ctx.Done(): | |
| 241 | 新增: t.Fatal("subscriber did not advance the acknowledged event cursor", ctx.Err()) | |
| 242 | 新增: case result := <-finished: | |
| 243 | 新增: t.Fatal("event stream ended before cursor acknowledgement", result.err) | |
| 244 | 新增: case <-ticker.C: | |
| 245 | 新增: } | |
| 246 | 新增: } | |
| 247 | 新增: // End only after a second poll has proved the previous sequence was retained. | |
| 248 | 新增: // Cancellation may race that poll's reply; either EOF or Canceled is normal. | |
| 249 | 新增: cancel() | |
| 250 | 新增: var result readResult | |
| 251 | 新增: select { | |
| 252 | 新增: case result = <-finished: | |
| 253 | 新增: case <-time.After(5 * time.Second): | |
| 254 | 新增: t.Fatal("event stream did not stop after cancellation") | |
| 255 | 新增: } | |
| 256 | 新增: data, err := result.data, result.err | |
| 257 | 新增: if err != nil && !errors.Is(err, context.Canceled) { | |
| 211 | 258 | t.Fatal(err) |
| 212 | 259 | } |
| 213 | 260 | wanted := "id: 2\nevent: clipboard\ndata: " |

