|
| 1 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package whiteboard |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "errors" |
| 10 | + "strings" |
| 11 | + "sync" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/larksuite/cli/internal/event" |
| 15 | +) |
| 16 | + |
| 17 | +// recordedCall captures a single APIClient invocation for assertion. |
| 18 | +type recordedCall struct { |
| 19 | + method string |
| 20 | + path string |
| 21 | + body interface{} |
| 22 | +} |
| 23 | + |
| 24 | +// fakeAPIClient is a minimal event.APIClient stub that records calls and |
| 25 | +// can be configured to fail when the request path matches errOnPath. |
| 26 | +type fakeAPIClient struct { |
| 27 | + mu sync.Mutex |
| 28 | + calls []recordedCall |
| 29 | + errOnPath string |
| 30 | +} |
| 31 | + |
| 32 | +// CallAPI records the invocation and optionally returns a simulated error |
| 33 | +// when the path contains the configured errOnPath substring. |
| 34 | +func (f *fakeAPIClient) CallAPI(_ context.Context, method, path string, body interface{}) (json.RawMessage, error) { |
| 35 | + f.mu.Lock() |
| 36 | + defer f.mu.Unlock() |
| 37 | + f.calls = append(f.calls, recordedCall{method: method, path: path, body: body}) |
| 38 | + if f.errOnPath != "" && strings.Contains(path, f.errOnPath) { |
| 39 | + return nil, errors.New("simulated subscribe failure") |
| 40 | + } |
| 41 | + return json.RawMessage(`{}`), nil |
| 42 | +} |
| 43 | + |
| 44 | +// TestWhiteboardSubscriptionPreConsume_MissingWhiteboardID verifies that the |
| 45 | +// PreConsume hook fails fast with an actionable error when whiteboard_id |
| 46 | +// is absent from the params map. |
| 47 | +func TestWhiteboardSubscriptionPreConsume_MissingWhiteboardID(t *testing.T) { |
| 48 | + t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) |
| 49 | + |
| 50 | + pc := whiteboardSubscriptionPreConsume(eventTypeWhiteboardUpdated) |
| 51 | + cleanup, err := pc(context.Background(), &fakeAPIClient{}, map[string]string{}) |
| 52 | + if err == nil { |
| 53 | + t.Fatalf("expected error when whiteboard_id missing") |
| 54 | + } |
| 55 | + if cleanup != nil { |
| 56 | + t.Fatalf("expected nil cleanup on error") |
| 57 | + } |
| 58 | + if !strings.Contains(err.Error(), "whiteboard_id") { |
| 59 | + t.Fatalf("error should mention whiteboard_id, got: %v", err) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// TestWhiteboardSubscriptionPreConsume_NilRuntime verifies that PreConsume |
| 64 | +// returns an error when the runtime APIClient dependency is missing. |
| 65 | +func TestWhiteboardSubscriptionPreConsume_NilRuntime(t *testing.T) { |
| 66 | + t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) |
| 67 | + |
| 68 | + pc := whiteboardSubscriptionPreConsume(eventTypeWhiteboardUpdated) |
| 69 | + _, err := pc(context.Background(), nil, map[string]string{"whiteboard_id": "wb1"}) |
| 70 | + if err == nil { |
| 71 | + t.Fatalf("expected error when runtime client is nil") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestWhiteboardSubscriptionPreConsume_SubscribeError verifies that a |
| 76 | +// failed subscribe call surfaces the error and skips registering a cleanup, |
| 77 | +// so no spurious unsubscribe is invoked. |
| 78 | +func TestWhiteboardSubscriptionPreConsume_SubscribeError(t *testing.T) { |
| 79 | + t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) |
| 80 | + |
| 81 | + pc := whiteboardSubscriptionPreConsume(eventTypeWhiteboardUpdated) |
| 82 | + rt := &fakeAPIClient{errOnPath: "/subscribe"} |
| 83 | + cleanup, err := pc(context.Background(), rt, map[string]string{"whiteboard_id": "wb1"}) |
| 84 | + if err == nil { |
| 85 | + t.Fatalf("expected error from subscribe call") |
| 86 | + } |
| 87 | + if cleanup != nil { |
| 88 | + t.Fatalf("expected nil cleanup when subscribe fails") |
| 89 | + } |
| 90 | + // only the failed subscribe call should have been made; no unsubscribe. |
| 91 | + if len(rt.calls) != 1 { |
| 92 | + t.Fatalf("expected exactly 1 call (subscribe), got %d", len(rt.calls)) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// TestWhiteboardSubscriptionPreConsume_SubscribeAndCleanup verifies the full |
| 97 | +// happy-path: subscribe is called once with the correct method/path/body, |
| 98 | +// and the returned cleanup invokes the matching unsubscribe. |
| 99 | +func TestWhiteboardSubscriptionPreConsume_SubscribeAndCleanup(t *testing.T) { |
| 100 | + t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) |
| 101 | + |
| 102 | + pc := whiteboardSubscriptionPreConsume(eventTypeWhiteboardUpdated) |
| 103 | + rt := &fakeAPIClient{} |
| 104 | + cleanup, err := pc(context.Background(), rt, map[string]string{"whiteboard_id": "wb1"}) |
| 105 | + if err != nil { |
| 106 | + t.Fatalf("unexpected error: %v", err) |
| 107 | + } |
| 108 | + if cleanup == nil { |
| 109 | + t.Fatalf("expected non-nil cleanup") |
| 110 | + } |
| 111 | + |
| 112 | + if len(rt.calls) != 1 { |
| 113 | + t.Fatalf("expected 1 call after subscribe, got %d", len(rt.calls)) |
| 114 | + } |
| 115 | + got := rt.calls[0] |
| 116 | + if got.method != "POST" { |
| 117 | + t.Errorf("subscribe method: got %q, want POST", got.method) |
| 118 | + } |
| 119 | + wantSubPath := "/open-apis/board/v1/whiteboards/wb1/subscribe" |
| 120 | + if got.path != wantSubPath { |
| 121 | + t.Errorf("subscribe path: got %q, want %q", got.path, wantSubPath) |
| 122 | + } |
| 123 | + body, _ := got.body.(map[string]string) |
| 124 | + if body["event_type"] != eventTypeWhiteboardUpdated { |
| 125 | + t.Errorf("subscribe body event_type: got %q, want %q", body["event_type"], eventTypeWhiteboardUpdated) |
| 126 | + } |
| 127 | + |
| 128 | + cleanup() |
| 129 | + if len(rt.calls) != 2 { |
| 130 | + t.Fatalf("expected 2 calls after cleanup, got %d", len(rt.calls)) |
| 131 | + } |
| 132 | + got2 := rt.calls[1] |
| 133 | + if got2.method != "POST" { |
| 134 | + t.Errorf("unsubscribe method: got %q, want POST", got2.method) |
| 135 | + } |
| 136 | + wantUnsubPath := "/open-apis/board/v1/whiteboards/wb1/unsubscribe" |
| 137 | + if got2.path != wantUnsubPath { |
| 138 | + t.Errorf("unsubscribe path: got %q, want %q", got2.path, wantUnsubPath) |
| 139 | + } |
| 140 | + body2, _ := got2.body.(map[string]string) |
| 141 | + if body2["event_type"] != eventTypeWhiteboardUpdated { |
| 142 | + t.Errorf("unsubscribe body event_type: got %q, want %q", body2["event_type"], eventTypeWhiteboardUpdated) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// TestWhiteboardSubscriptionPreConsume_PathSegmentEncoded verifies that |
| 147 | +// whiteboard_id values containing reserved URL characters are properly |
| 148 | +// path-segment encoded so they cannot escape into adjacent path segments. |
| 149 | +func TestWhiteboardSubscriptionPreConsume_PathSegmentEncoded(t *testing.T) { |
| 150 | + t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) |
| 151 | + |
| 152 | + pc := whiteboardSubscriptionPreConsume(eventTypeWhiteboardUpdated) |
| 153 | + rt := &fakeAPIClient{} |
| 154 | + // 含特殊字符的 whiteboard_id 应被 path-segment 编码,避免越界到其他 path 段。 |
| 155 | + _, err := pc(context.Background(), rt, map[string]string{"whiteboard_id": "wb/1?evil"}) |
| 156 | + if err != nil { |
| 157 | + t.Fatalf("unexpected error: %v", err) |
| 158 | + } |
| 159 | + if len(rt.calls) != 1 { |
| 160 | + t.Fatalf("expected 1 call, got %d", len(rt.calls)) |
| 161 | + } |
| 162 | + if strings.Contains(rt.calls[0].path, "wb/1?evil") { |
| 163 | + t.Errorf("whiteboard_id was not encoded; path: %s", rt.calls[0].path) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// TestWhiteboardUpdatedV1HasPreConsume ensures the registered EventKey for |
| 168 | +// board.whiteboard.updated_v1 wires the PreConsume hook and declares the |
| 169 | +// required whiteboard_id parameter. |
| 170 | +func TestWhiteboardUpdatedV1HasPreConsume(t *testing.T) { |
| 171 | + t.Setenv("LARKSUITE_CLI_CONFIG_DIR", t.TempDir()) |
| 172 | + |
| 173 | + keys := Keys() |
| 174 | + for _, k := range keys { |
| 175 | + if k.Key == eventTypeWhiteboardUpdated { |
| 176 | + if k.PreConsume == nil { |
| 177 | + t.Fatalf("EventKey %s should have PreConsume hook", eventTypeWhiteboardUpdated) |
| 178 | + } |
| 179 | + if len(k.Params) == 0 { |
| 180 | + t.Fatalf("EventKey %s should declare whiteboard_id param", eventTypeWhiteboardUpdated) |
| 181 | + } |
| 182 | + var found bool |
| 183 | + for _, p := range k.Params { |
| 184 | + if p.Name == "whiteboard_id" && p.Required { |
| 185 | + found = true |
| 186 | + } |
| 187 | + } |
| 188 | + if !found { |
| 189 | + t.Fatalf("EventKey %s must declare required whiteboard_id param", eventTypeWhiteboardUpdated) |
| 190 | + } |
| 191 | + return |
| 192 | + } |
| 193 | + } |
| 194 | + t.Fatalf("EventKey %s not registered", eventTypeWhiteboardUpdated) |
| 195 | +} |
| 196 | + |
| 197 | +// 确保 event.APIClient 接口与本测试 mock 一致。 |
| 198 | +var _ event.APIClient = (*fakeAPIClient)(nil) |
0 commit comments