| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestDesktopFrameTransitionRetry(t *testing.T) { |
| 10 | for _, tc := range []struct { |
| 11 | code string |
| 12 | failures, calls int |
| 13 | fatal bool |
| 14 | }{ |
| 15 | {"desktop_target_changed", 2, 3, false}, |
| 16 | {"desktop_empty_frame", 1, 2, false}, |
| 17 | {"desktop_capture_busy", 1, 2, false}, |
| 18 | {"desktop_target_changed", 8, 6, true}, |
| 19 | {"desktop_capture_permission_required", 1, 1, true}, |
| 20 | {"desktop_registration_invalid", 1, 1, true}, |
| 21 | {"requires_gui_login", 1, 1, true}, |
| 22 | } { |
| 23 | t.Run(tc.code, func(t *testing.T) { |
| 24 | calls := 0 |
| 25 | var frame windowsWindowFrame |
| 26 | err := requestDesktopFrame(context.Background(), func(input, output any) error { |
| 27 | if input.(map[string]any)["action"] != "frame" { |
| 28 | t.Fatal("retried mutation") |
| 29 | } |
| 30 | calls++ |
| 31 | if calls <= tc.failures { |
| 32 | return windowsWindowFailure{tc.code} |
| 33 | } |
| 34 | *output.(*windowsWindowFrame) = windowsWindowFrame{Target: "fresh", Width: 2, Height: 1} |
| 35 | return nil |
| 36 | }, map[string]any{"action": "frame"}, &frame) |
| 37 | if calls != tc.calls || (err != nil) != tc.fatal { |
| 38 | t.Fatalf("calls=%d error=%v", calls, err) |
| 39 | } |
| 40 | if !tc.fatal && frame.Target != "fresh" { |
| 41 | t.Fatal("missing fresh validated frame") |
| 42 | } |
| 43 | }) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestDesktopFrameTransitionCancellation(t *testing.T) { |
| 48 | ctx, cancel := context.WithCancel(context.Background()) |
| 49 | calls := 0 |
| 50 | err := requestDesktopFrame(ctx, func(any, any) error { calls++; cancel(); return windowsWindowFailure{"desktop_capture_busy"} }, nil, nil) |
| 51 | if !errors.Is(err, context.Canceled) || calls != 1 { |
| 52 | t.Fatalf("calls=%d error=%v", calls, err) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | func TestDesktopFrameTransportFailureNotRetried(t *testing.T) { |
| 57 | failure := errors.New("disconnected") |
| 58 | calls := 0 |
| 59 | err := requestDesktopFrame(context.Background(), func(any, any) error { calls++; return failure }, nil, nil) |
| 60 | if err != failure || calls != 1 { |
| 61 | t.Fatal("transport failure hidden") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestDesktopViewerDropsOldInputUntilFullFrame(t *testing.T) { |
| 66 | calls, released := 0, 0 |
| 67 | unchanged := true |
| 68 | request := desktopViewerRequest(context.Background(), func(input, output any) error { |
| 69 | calls++ |
| 70 | if input.(map[string]any)["action"] == "frame" { |
| 71 | if _, exists := input.(map[string]any)["after"]; exists { |
| 72 | t.Fatal("must request a full fresh frame") |
| 73 | } |
| 74 | *output.(*windowsWindowFrame) = windowsWindowFrame{Unchanged: unchanged, Target: "new"} |
| 75 | return nil |
| 76 | } |
| 77 | return windowsWindowFailure{"desktop_target_changed"} |
| 78 | }, func() error { released++; return nil }) |
| 79 | if err := request(map[string]any{"action": "pointer"}, nil); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | |
| 83 | if released != 1 { |
| 84 | t.Fatal("held input was not released") |
| 85 | } |
| 86 | for _, action := range []string{"pointer", "key", "text"} { |
| 87 | if err := request(map[string]any{"action": action}, nil); err != nil { |
| 88 | t.Fatal(err) |
| 89 | } |
| 90 | } |
| 91 | if calls != 1 || released != 1 { |
| 92 | t.Fatal("stale input was replayed") |
| 93 | } |
| 94 | var frame windowsWindowFrame |
| 95 | if err := request(map[string]any{"action": "frame", "after": "old"}, &frame); err != nil { |
| 96 | t.Fatal(err) |
| 97 | } |
| 98 | request(map[string]any{"action": "text"}, nil) |
| 99 | if calls != 2 { |
| 100 | t.Fatal("unchanged frame resumed input") |
| 101 | } |
| 102 | unchanged = false |
| 103 | if err := request(map[string]any{"action": "frame", "after": "old"}, &frame); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | request(map[string]any{"action": "key"}, nil) |
| 107 | if calls != 4 { |
| 108 | t.Fatal("fresh frame did not resume new input") |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestDesktopViewerPreservesFatalErrors(t *testing.T) { |
| 113 | for _, code := range []string{"requires_gui_login", "desktop_registration_invalid", "desktop_pointer_invalid"} { |
| 114 | request := desktopViewerRequest(context.Background(), func(any, any) error { return windowsWindowFailure{code} }, |
| 115 | func() error { t.Fatal("released input for", code); return nil }) |
| 116 | if request(map[string]any{"action": "pointer"}, nil) == nil { |
| 117 | t.Fatal("hidden security failure", code) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestDesktopViewerFailsWhenHeldInputCannotBeReleased(t *testing.T) { |
| 123 | failure := errors.New("relay unavailable") |
| 124 | request := desktopViewerRequest(context.Background(), func(any, any) error { return windowsWindowFailure{"desktop_target_changed"} }, |
| 125 | func() error { return failure }) |
| 126 | |
| 127 | if err := request(map[string]any{"action": "key"}, nil); err != failure { |
| 128 | t.Fatalf("stuck input hidden: %v", err) |
| 129 | } |
| 130 | } |