|
| 1 | +// Copyright (c) 2026 Lark Technologies Pte. Ltd. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package im |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "encoding/json" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/larksuite/cli/internal/httpmock" |
| 13 | +) |
| 14 | + |
| 15 | +func TestBuildFeedCardTimeSensitiveBody(t *testing.T) { |
| 16 | + runtime := newTestRuntimeContext(t, map[string]string{ |
| 17 | + "user-ids": "ou_1, ou_2", |
| 18 | + }, map[string]bool{ |
| 19 | + "time-sensitive": true, |
| 20 | + }) |
| 21 | + |
| 22 | + body, err := buildFeedCardTimeSensitiveBody(runtime) |
| 23 | + if err != nil { |
| 24 | + t.Fatalf("buildFeedCardTimeSensitiveBody() error = %v", err) |
| 25 | + } |
| 26 | + userIDs, _ := body["user_ids"].([]string) |
| 27 | + if len(userIDs) != 2 || userIDs[0] != "ou_1" || userIDs[1] != "ou_2" { |
| 28 | + t.Fatalf("user_ids = %#v", body["user_ids"]) |
| 29 | + } |
| 30 | + if body["time_sensitive"] != true { |
| 31 | + t.Fatalf("time_sensitive = %#v", body["time_sensitive"]) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func TestBuildFeedCardTimeSensitiveBodyAcceptsFalse(t *testing.T) { |
| 36 | + runtime := newTestRuntimeContext(t, map[string]string{ |
| 37 | + "user-ids": "ou_1", |
| 38 | + }, map[string]bool{ |
| 39 | + "time-sensitive": false, |
| 40 | + }) |
| 41 | + |
| 42 | + body, err := buildFeedCardTimeSensitiveBody(runtime) |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("buildFeedCardTimeSensitiveBody() error = %v", err) |
| 45 | + } |
| 46 | + if body["time_sensitive"] != false { |
| 47 | + t.Fatalf("time_sensitive = %#v", body["time_sensitive"]) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestBuildFeedCardTimeSensitiveBodyValidation(t *testing.T) { |
| 52 | + invalidOpenIDErr := invalidOpenIDError(t) |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + strFlags map[string]string |
| 56 | + boolFlags map[string]bool |
| 57 | + wantErr string |
| 58 | + }{ |
| 59 | + { |
| 60 | + name: "missing time sensitive", |
| 61 | + strFlags: map[string]string{"user-ids": "ou_1"}, |
| 62 | + wantErr: "--time-sensitive is required", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "invalid recipient open id", |
| 66 | + strFlags: map[string]string{"user-ids": "bad_user"}, |
| 67 | + boolFlags: map[string]bool{"time-sensitive": true}, |
| 68 | + wantErr: invalidOpenIDErr, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "union id bypasses open id prefix validation", |
| 72 | + strFlags: map[string]string{"user-ids": "onion_1", "user-id-type": "union_id"}, |
| 73 | + boolFlags: map[string]bool{"time-sensitive": true}, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + runtime := newTestRuntimeContext(t, tt.strFlags, tt.boolFlags) |
| 80 | + _, err := buildFeedCardTimeSensitiveBody(runtime) |
| 81 | + if tt.wantErr == "" { |
| 82 | + if err != nil { |
| 83 | + t.Fatalf("unexpected error = %v", err) |
| 84 | + } |
| 85 | + return |
| 86 | + } |
| 87 | + if err == nil || !strings.Contains(err.Error(), tt.wantErr) { |
| 88 | + t.Fatalf("error = %v, want substring %q", err, tt.wantErr) |
| 89 | + } |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestFeedCardIDForTimeSensitiveValidation(t *testing.T) { |
| 95 | + runtime := newTestRuntimeContext(t, map[string]string{"feed-card-id": " "}, nil) |
| 96 | + if _, err := feedCardIDForTimeSensitive(runtime); err == nil || !strings.Contains(err.Error(), "--feed-card-id is required") { |
| 97 | + t.Fatalf("feedCardIDForTimeSensitive() error = %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + runtime = newTestRuntimeContext(t, map[string]string{"feed-card-id": "bot_time_sentive"}, nil) |
| 101 | + if _, err := feedCardIDForTimeSensitive(runtime); err == nil || |
| 102 | + !strings.Contains(err.Error(), "conflicts with the bot time sensitive endpoint") { |
| 103 | + t.Fatalf("feedCardIDForTimeSensitive() reserved segment error = %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + runtime = newTestRuntimeContext(t, map[string]string{"feed-card-id": "om_123"}, nil) |
| 107 | + if _, err := feedCardIDForTimeSensitive(runtime); err == nil || |
| 108 | + !strings.Contains(err.Error(), `starting with "oc_"`) { |
| 109 | + t.Fatalf("feedCardIDForTimeSensitive() non-group feed card error = %v", err) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestFeedCardTimeSensitiveRequestPathRejectsReservedSegment(t *testing.T) { |
| 114 | + runtime := newTestRuntimeContext(t, map[string]string{"feed-card-id": "bot_time_sentive"}, nil) |
| 115 | + if _, _, err := feedCardTimeSensitiveRequestPath(runtime); err == nil || |
| 116 | + !strings.Contains(err.Error(), "conflicts with the bot time sensitive endpoint") { |
| 117 | + t.Fatalf("feedCardTimeSensitiveRequestPath() error = %v", err) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestFeedCardTimeSensitiveDryRunShape(t *testing.T) { |
| 122 | + botRuntime := newTestRuntimeContext(t, map[string]string{ |
| 123 | + "user-ids": "ou_1", |
| 124 | + }, map[string]bool{ |
| 125 | + "time-sensitive": true, |
| 126 | + }) |
| 127 | + botGot := mustMarshalDryRun(t, ImFeedCardBotTimeSensitive.DryRun(context.Background(), botRuntime)) |
| 128 | + if !strings.Contains(botGot, `"method":"PATCH"`) || |
| 129 | + !strings.Contains(botGot, `"/open-apis/im/v2/feed_cards/bot_time_sentive"`) || |
| 130 | + !strings.Contains(botGot, `"user_id_type":"open_id"`) || |
| 131 | + !strings.Contains(botGot, `"time_sensitive":true`) { |
| 132 | + t.Fatalf("ImFeedCardBotTimeSensitive.DryRun() = %s", botGot) |
| 133 | + } |
| 134 | + |
| 135 | + cardRuntime := newTestRuntimeContext(t, map[string]string{ |
| 136 | + "feed-card-id": "oc_dryrun", |
| 137 | + "user-ids": "ou_1", |
| 138 | + }, map[string]bool{ |
| 139 | + "time-sensitive": false, |
| 140 | + }) |
| 141 | + cardGot := mustMarshalDryRun(t, ImFeedCardTimeSensitive.DryRun(context.Background(), cardRuntime)) |
| 142 | + if !strings.Contains(cardGot, `"method":"PATCH"`) || |
| 143 | + !strings.Contains(cardGot, `"/open-apis/im/v2/feed_cards/oc_dryrun"`) || |
| 144 | + !strings.Contains(cardGot, `"time_sensitive":false`) { |
| 145 | + t.Fatalf("ImFeedCardTimeSensitive.DryRun() = %s", cardGot) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestFeedCardBotTimeSensitiveExecute(t *testing.T) { |
| 150 | + factory, stdout, reg := newIMExecuteFactory(t) |
| 151 | + stub := &httpmock.Stub{ |
| 152 | + Method: "PATCH", |
| 153 | + URL: feedCardBotTimeSensitivePath, |
| 154 | + Body: map[string]interface{}{ |
| 155 | + "code": 0, |
| 156 | + "msg": "ok", |
| 157 | + "data": map[string]interface{}{}, |
| 158 | + }, |
| 159 | + } |
| 160 | + reg.Register(stub) |
| 161 | + |
| 162 | + err := mountAndRunIMShortcut(t, ImFeedCardBotTimeSensitive, []string{ |
| 163 | + "+feed-card-bot-time-sensitive", |
| 164 | + "--user-ids", "ou_1", |
| 165 | + "--time-sensitive", |
| 166 | + }, factory, stdout) |
| 167 | + if err != nil { |
| 168 | + t.Fatalf("mountAndRunIMShortcut() error = %v", err) |
| 169 | + } |
| 170 | + if !strings.Contains(stdout.String(), `"requested_user_count": 1`) || !strings.Contains(stdout.String(), `"time_sensitive": true`) { |
| 171 | + t.Fatalf("stdout = %s", stdout.String()) |
| 172 | + } |
| 173 | + |
| 174 | + var captured map[string]interface{} |
| 175 | + if err := json.Unmarshal(stub.CapturedBody, &captured); err != nil { |
| 176 | + t.Fatalf("captured body JSON = %s, err=%v", string(stub.CapturedBody), err) |
| 177 | + } |
| 178 | + if captured["time_sensitive"] != true { |
| 179 | + t.Fatalf("captured body = %#v", captured) |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func TestFeedCardTimeSensitiveExecute(t *testing.T) { |
| 184 | + factory, stdout, reg := newIMExecuteFactory(t) |
| 185 | + stub := &httpmock.Stub{ |
| 186 | + Method: "PATCH", |
| 187 | + URL: "/open-apis/im/v2/feed_cards/oc_123", |
| 188 | + Body: map[string]interface{}{ |
| 189 | + "code": 0, |
| 190 | + "msg": "ok", |
| 191 | + "data": map[string]interface{}{}, |
| 192 | + }, |
| 193 | + } |
| 194 | + reg.Register(stub) |
| 195 | + |
| 196 | + err := mountAndRunIMShortcut(t, ImFeedCardTimeSensitive, []string{ |
| 197 | + "+feed-card-time-sensitive", |
| 198 | + "--feed-card-id", "oc_123", |
| 199 | + "--user-ids", "ou_1", |
| 200 | + "--time-sensitive=false", |
| 201 | + }, factory, stdout) |
| 202 | + if err != nil { |
| 203 | + t.Fatalf("mountAndRunIMShortcut() error = %v", err) |
| 204 | + } |
| 205 | + if !strings.Contains(stdout.String(), `"feed_card_id": "oc_123"`) || !strings.Contains(stdout.String(), `"time_sensitive": false`) { |
| 206 | + t.Fatalf("stdout = %s", stdout.String()) |
| 207 | + } |
| 208 | + |
| 209 | + var captured map[string]interface{} |
| 210 | + if err := json.Unmarshal(stub.CapturedBody, &captured); err != nil { |
| 211 | + t.Fatalf("captured body JSON = %s, err=%v", string(stub.CapturedBody), err) |
| 212 | + } |
| 213 | + userIDs, _ := captured["user_ids"].([]interface{}) |
| 214 | + if len(userIDs) != 1 || captured["time_sensitive"] != false { |
| 215 | + t.Fatalf("captured body = %#v", captured) |
| 216 | + } |
| 217 | +} |
0 commit comments