-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathposts_read_test.go
More file actions
516 lines (445 loc) · 15 KB
/
posts_read_test.go
File metadata and controls
516 lines (445 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
package threads
import (
"context"
"net/http"
"testing"
)
func TestGetPost_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{
"id": "123456",
"text": "Hello world",
"media_type": "TEXT",
"permalink": "https://threads.net/@user/post/123456",
"username": "testuser",
"timestamp": "2026-01-15T10:30:00+0000"
}`))
post, err := client.GetPost(context.Background(), ConvertToPostID("123456"))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if post.ID != "123456" {
t.Errorf("expected ID 123456, got %s", post.ID)
}
if post.Text != "Hello world" {
t.Errorf("expected text 'Hello world', got %s", post.Text)
}
if post.Username != "testuser" {
t.Errorf("expected username 'testuser', got %s", post.Username)
}
}
func TestGetPost_InvalidID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetPost(context.Background(), PostID(""))
if err == nil {
t.Fatal("expected error for empty post ID")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestGetPost_NotFound(t *testing.T) {
client := testClient(t, jsonHandler(404, `{"error":{"message":"Object does not exist","type":"OAuthException","code":100}}`))
_, err := client.GetPost(context.Background(), ConvertToPostID("nonexistent"))
if err == nil {
t.Fatal("expected error for 404")
}
if !IsAPIError(err) {
t.Errorf("expected APIError, got %T", err)
}
}
func TestGetPost_ServerError(t *testing.T) {
client := testClient(t, jsonHandler(500, `{"error":{"message":"Internal error","type":"OAuthException","code":2}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetPost(context.Background(), ConvertToPostID("123"))
if err == nil {
t.Fatal("expected error for 500")
}
if !IsAPIError(err) {
t.Errorf("expected APIError, got %T", err)
}
}
func TestGetPost_AuthenticationRequired(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_ = client.ClearToken()
_, err := client.GetPost(context.Background(), ConvertToPostID("123"))
if err == nil {
t.Fatal("expected error when not authenticated")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestGetUserPosts_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{
"data": [
{"id": "1", "text": "Post 1"},
{"id": "2", "text": "Post 2"}
],
"paging": {"cursors": {"after": "cursor123"}}
}`))
resp, err := client.GetUserPosts(context.Background(), ConvertToUserID("12345"), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 2 {
t.Errorf("expected 2 posts, got %d", len(resp.Data))
}
if resp.Paging.Cursors == nil || resp.Paging.Cursors.After != "cursor123" {
t.Error("expected paging cursor")
}
}
func TestGetUserPosts_InvalidUserID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetUserPosts(context.Background(), UserID(""), nil)
if err == nil {
t.Fatal("expected error for empty user ID")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestGetPublishingLimits_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{
"data": [{
"quota_usage": 5,
"config": {"quota_total": 250, "quota_duration": 86400},
"reply_quota_usage": 10,
"reply_config": {"quota_total": 1000, "quota_duration": 86400}
}]
}`))
limits, err := client.GetPublishingLimits(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if limits.QuotaUsage != 5 {
t.Errorf("expected quota_usage 5, got %d", limits.QuotaUsage)
}
if limits.Config.QuotaTotal != 250 {
t.Errorf("expected quota_total 250, got %d", limits.Config.QuotaTotal)
}
}
func TestGetUserMentions_Success(t *testing.T) {
client := testClient(t, jsonHandler(200, `{
"data": [{"id": "1", "text": "@user mentioned you"}],
"paging": {}
}`))
resp, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 1 {
t.Errorf("expected 1 mention, got %d", len(resp.Data))
}
}
func TestGetUserGhostPosts_Success(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
fields := r.URL.Query().Get("fields")
if fields != GhostPostFields {
t.Errorf("expected ghost post fields, got %s", fields)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{
"data": [{"id": "1", "text": "Ghost!", "ghost_post_status": "active"}],
"paging": {}
}`))
}
client := testClient(t, http.HandlerFunc(handler))
resp, err := client.GetUserGhostPosts(context.Background(), ConvertToUserID("12345"), nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 1 {
t.Errorf("expected 1 ghost post, got %d", len(resp.Data))
}
}
func TestGetUserMentions_InvalidUserID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetUserMentions(context.Background(), UserID(""), nil)
if err == nil {
t.Fatal("expected error for empty user ID")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestGetUserMentions_NotAuthenticated(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_ = client.ClearToken()
_, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error when not authenticated")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestGetUserMentions_WithPagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
limit := r.URL.Query().Get("limit")
before := r.URL.Query().Get("before")
after := r.URL.Query().Get("after")
if limit != "10" {
t.Errorf("expected limit=10, got %s", limit)
}
if before != "cursor_before" {
t.Errorf("expected before=cursor_before, got %s", before)
}
if after != "cursor_after" {
t.Errorf("expected after=cursor_after, got %s", after)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"data": [{"id": "1"}], "paging": {}}`))
}
client := testClient(t, http.HandlerFunc(handler))
_, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), &PostsOptions{
Limit: 10,
Before: "cursor_before",
After: "cursor_after",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetUserMentions_WithSinceUntil(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
since := r.URL.Query().Get("since")
until := r.URL.Query().Get("until")
if since != "1700000000" {
t.Errorf("expected since=1700000000, got %s", since)
}
if until != "1700100000" {
t.Errorf("expected until=1700100000, got %s", until)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"data": [{"id": "1"}], "paging": {}}`))
}
client := testClient(t, http.HandlerFunc(handler))
_, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), &PostsOptions{
Since: 1700000000,
Until: 1700100000,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetUserMentions_NotFound(t *testing.T) {
client := testClient(t, jsonHandler(404, `{"error":{"message":"not found","type":"OAuthException","code":100}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error for 404")
}
}
func TestGetUserMentions_Forbidden(t *testing.T) {
client := testClient(t, jsonHandler(403, `{"error":{"message":"access denied","type":"OAuthException","code":200}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error for 403")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestGetUserMentions_ServerError(t *testing.T) {
client := testClient(t, jsonHandler(500, `{"error":{"message":"internal error","type":"OAuthException","code":2}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error for 500")
}
}
func TestGetUserMentions_InvalidSinceTimestamp(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetUserMentions(context.Background(), ConvertToUserID("12345"), &PostsOptions{
Since: 100, // Below MinSearchTimestamp
})
if err == nil {
t.Fatal("expected error for invalid since timestamp")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestGetUserGhostPosts_InvalidUserID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_, err := client.GetUserGhostPosts(context.Background(), UserID(""), nil)
if err == nil {
t.Fatal("expected error for empty user ID")
}
if !IsValidationError(err) {
t.Errorf("expected ValidationError, got %T", err)
}
}
func TestGetUserGhostPosts_NotAuthenticated(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_ = client.ClearToken()
_, err := client.GetUserGhostPosts(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error when not authenticated")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestGetUserGhostPosts_WithPagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
limit := r.URL.Query().Get("limit")
before := r.URL.Query().Get("before")
if limit != "5" {
t.Errorf("expected limit=5, got %s", limit)
}
if before != "cursor_abc" {
t.Errorf("expected before=cursor_abc, got %s", before)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"data": [{"id": "1"}], "paging": {}}`))
}
client := testClient(t, http.HandlerFunc(handler))
_, err := client.GetUserGhostPosts(context.Background(), ConvertToUserID("12345"), &PaginationOptions{
Limit: 5,
Before: "cursor_abc",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetUserGhostPosts_NotFound(t *testing.T) {
client := testClient(t, jsonHandler(404, `{"error":{"message":"not found","type":"OAuthException","code":100}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetUserGhostPosts(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error for 404")
}
}
func TestGetUserGhostPosts_ServerError(t *testing.T) {
client := testClient(t, jsonHandler(500, `{"error":{"message":"internal error","type":"OAuthException","code":2}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetUserGhostPosts(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error for 500")
}
}
func TestGetPublishingLimits_NotAuthenticated(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
_ = client.ClearToken()
_, err := client.GetPublishingLimits(context.Background())
if err == nil {
t.Fatal("expected error when not authenticated")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestGetPublishingLimits_EmptyUserID(t *testing.T) {
client := testClient(t, jsonHandler(200, `{}`))
client.mu.Lock()
client.tokenInfo.UserID = ""
client.mu.Unlock()
_, err := client.GetPublishingLimits(context.Background())
if err == nil {
t.Fatal("expected error for empty user ID")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}
func TestGetPublishingLimits_APIError(t *testing.T) {
client := testClient(t, jsonHandler(500, `{"error":{"message":"internal error","type":"OAuthException","code":2}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetPublishingLimits(context.Background())
if err == nil {
t.Fatal("expected error for 500")
}
}
func TestGetPublishingLimits_EmptyData(t *testing.T) {
client := testClient(t, jsonHandler(200, `{"data":[]}`))
_, err := client.GetPublishingLimits(context.Background())
if err == nil {
t.Fatal("expected error for empty data")
}
}
func TestGetPublishingLimits_MalformedResponse(t *testing.T) {
client := testClient(t, jsonHandler(200, `not json`))
_, err := client.GetPublishingLimits(context.Background())
if err == nil {
t.Fatal("expected error for malformed response")
}
}
func TestGetUserPosts_WithPagination(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
limit := r.URL.Query().Get("limit")
before := r.URL.Query().Get("before")
after := r.URL.Query().Get("after")
if limit != "10" {
t.Errorf("expected limit=10, got %s", limit)
}
if before != "before_cursor" {
t.Errorf("expected before=before_cursor, got %s", before)
}
if after != "after_cursor" {
t.Errorf("expected after=after_cursor, got %s", after)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"data": [{"id": "1"}], "paging": {}}`))
}
client := testClient(t, http.HandlerFunc(handler))
resp, err := client.GetUserPosts(context.Background(), ConvertToUserID("12345"), &PaginationOptions{
Limit: 10,
Before: "before_cursor",
After: "after_cursor",
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(resp.Data) != 1 {
t.Errorf("expected 1 post, got %d", len(resp.Data))
}
}
func TestGetUserPostsWithOptions_WithTimeFilters(t *testing.T) {
handler := func(w http.ResponseWriter, r *http.Request) {
since := r.URL.Query().Get("since")
until := r.URL.Query().Get("until")
if since != "1700000000" {
t.Errorf("expected since=1700000000, got %s", since)
}
if until != "1700100000" {
t.Errorf("expected until=1700100000, got %s", until)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte(`{"data": [{"id": "1"}], "paging": {}}`))
}
client := testClient(t, http.HandlerFunc(handler))
_, err := client.GetUserPostsWithOptions(context.Background(), ConvertToUserID("12345"), &PostsOptions{
Since: 1700000000,
Until: 1700100000,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func TestGetUserPostsWithOptions_NotFound(t *testing.T) {
client := testClient(t, jsonHandler(404, `{"error":{"message":"not found","type":"OAuthException","code":100}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetUserPostsWithOptions(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error for 404")
}
}
func TestGetUserPostsWithOptions_Forbidden(t *testing.T) {
client := testClient(t, jsonHandler(403, `{"error":{"message":"access denied","type":"OAuthException","code":200}}`))
client.config.RetryConfig.MaxRetries = 0
_, err := client.GetUserPostsWithOptions(context.Background(), ConvertToUserID("12345"), nil)
if err == nil {
t.Fatal("expected error for 403")
}
if !IsAuthenticationError(err) {
t.Errorf("expected AuthenticationError, got %T", err)
}
}