| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/go-ap/errors" |
| 8 | "github.com/google/go-cmp/cmp" |
| 9 | ) |
| 10 | |
| 11 | func mockCollection(items ...Item) Collection { |
| 12 | cc := Collection{ |
| 13 | ID: IRIf("https://example.com", Inbox), |
| 14 | Type: CollectionType, |
| 15 | } |
| 16 | if len(items) == 0 { |
| 17 | cc.Items = make(ItemCollection, 0) |
| 18 | } else { |
| 19 | cc.Items = items |
| 20 | cc.TotalItems = uint(len(items)) |
| 21 | } |
| 22 | return cc |
| 23 | } |
| 24 | |
| 25 | func TestCollectionNew(t *testing.T) { |
| 26 | testValue := ID("test") |
| 27 | |
| 28 | c := CollectionNew(testValue) |
| 29 | |
| 30 | if c.ID != testValue { |
| 31 | t.Errorf("APObject Id '%v' different than expected '%v'", c.ID, testValue) |
| 32 | } |
| 33 | if !c.Match(CollectionType) { |
| 34 | t.Errorf("APObject Type '%v' different than expected '%v'", c.GetType(), CollectionType) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestCollection_Append(t *testing.T) { |
| 39 | tests := []struct { |
| 40 | name string |
| 41 | col Collection |
| 42 | it []Item |
| 43 | wantErr error |
| 44 | }{ |
| 45 | { |
| 46 | name: "empty", |
| 47 | col: mockCollection(), |
| 48 | it: ItemCollection{}, |
| 49 | }, |
| 50 | { |
| 51 | name: "add one item", |
| 52 | col: mockCollection(), |
| 53 | it: ItemCollection{ |
| 54 | Object{ID: ID("grrr")}, |
| 55 | }, |
| 56 | }, |
| 57 | { |
| 58 | name: "add multiple items", |
| 59 | col: mockCollection(), |
| 60 | it: ItemCollection{ |
| 61 | Object{ID: ID("grrr")}, |
| 62 | Activity{ID: ID("one")}, |
| 63 | Actor{ID: ID("jdoe")}, |
| 64 | }, |
| 65 | }, |
| 66 | } |
| 67 | for _, tt := range tests { |
| 68 | t.Run(tt.name, func(t *testing.T) { |
| 69 | defer func() { |
| 70 | tt.col.Items = tt.col.Items[:0] |
| 71 | tt.col.TotalItems = 0 |
| 72 | }() |
| 73 | if err := tt.col.Append(tt.it...); (err != nil) && errors.Is(err, tt.wantErr) { |
| 74 | t.Errorf("Append() error = %v, wantErr %v", err, tt.wantErr) |
| 75 | } |
| 76 | if tt.col.TotalItems != uint(len(tt.it)) { |
| 77 | t.Errorf("Post Append() %T TotalItems %d different than added count %d", tt.col, tt.col.TotalItems, len(tt.it)) |
| 78 | } |
| 79 | for _, it := range tt.it { |
| 80 | if !tt.col.Items.Contains(it) { |
| 81 | t.Errorf("Post Append() unable to find %s in %T Items %v", tt.col, it.GetLink(), tt.col.Items) |
| 82 | } |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestCollection_Collection(t *testing.T) { |
| 89 | id := ID("test") |
| 90 | |
| 91 | c := CollectionNew(id) |
| 92 | |
| 93 | if !reflect.DeepEqual(c.Collection(), c.Items) { |
| 94 | t.Errorf("Collection items should be equal %v %v", c.Collection(), c.Items) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestCollection_GetID(t *testing.T) { |
| 99 | id := ID("test") |
| 100 | |
| 101 | c := CollectionNew(id) |
| 102 | |
| 103 | if c.GetID() != id { |
| 104 | t.Errorf("GetID should return %s, received %s", id, c.GetID()) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestCollection_GetLink(t *testing.T) { |
| 109 | id := ID("test") |
| 110 | link := IRI(id) |
| 111 | |
| 112 | c := CollectionNew(id) |
| 113 | |
| 114 | if c.GetLink() != link { |
| 115 | t.Errorf("GetLink should return %q, received %q", link, c.GetLink()) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestCollection_GetType(t *testing.T) { |
| 120 | id := ID("test") |
| 121 | |
| 122 | c := CollectionNew(id) |
| 123 | |
| 124 | if !c.Match(CollectionType) { |
| 125 | t.Errorf("Collection Type should be %q, received %q", CollectionType, c.GetType()) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestCollection_IsLink(t *testing.T) { |
| 130 | id := ID("test") |
| 131 | |
| 132 | c := CollectionNew(id) |
| 133 | |
| 134 | if c.IsLink() != false { |
| 135 | t.Errorf("Collection should not be a link, received %t", c.IsLink()) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestCollection_IsObject(t *testing.T) { |
| 140 | id := ID("test") |
| 141 | |
| 142 | c := CollectionNew(id) |
| 143 | |
| 144 | if c.IsObject() != true { |
| 145 | t.Errorf("Collection should be an object, received %t", c.IsObject()) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestCollection_UnmarshalJSON(t *testing.T) { |
| 150 | c := Collection{} |
| 151 | |
| 152 | dataEmpty := []byte("{}") |
| 153 | c.UnmarshalJSON(dataEmpty) |
| 154 | if c.ID != "" { |
| 155 | t.Errorf("Unmarshaled object should have empty ID, received %q", c.ID) |
| 156 | } |
| 157 | if HasTypes(c) { |
| 158 | t.Errorf("Unmarshaled object should have empty Type, received %q", c.GetType()) |
| 159 | } |
| 160 | if c.AttributedTo != nil { |
| 161 | t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", c.AttributedTo) |
| 162 | } |
| 163 | if len(c.Name) != 0 { |
| 164 | t.Errorf("Unmarshaled object should have empty Name, received %q", c.Name) |
| 165 | } |
| 166 | if len(c.Summary) != 0 { |
| 167 | t.Errorf("Unmarshaled object should have empty Summary, received %q", c.Summary) |
| 168 | } |
| 169 | if len(c.Content) != 0 { |
| 170 | t.Errorf("Unmarshaled object should have empty Content, received %q", c.Content) |
| 171 | } |
| 172 | if c.TotalItems != 0 { |
| 173 | t.Errorf("Unmarshaled object should have empty TotalItems, received %d", c.TotalItems) |
| 174 | } |
| 175 | if len(c.Items) > 0 { |
| 176 | t.Errorf("Unmarshaled object should have empty Items, received %v", c.Items) |
| 177 | } |
| 178 | if c.URL != nil { |
| 179 | t.Errorf("Unmarshaled object should have empty URL, received %v", c.URL) |
| 180 | } |
| 181 | if !c.Published.IsZero() { |
| 182 | t.Errorf("Unmarshaled object should have empty Published, received %q", c.Published) |
| 183 | } |
| 184 | if !c.StartTime.IsZero() { |
| 185 | t.Errorf("Unmarshaled object should have empty StartTime, received %q", c.StartTime) |
| 186 | } |
| 187 | if !c.Updated.IsZero() { |
| 188 | t.Errorf("Unmarshaled object should have empty Updated, received %q", c.Updated) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func TestCollection_Count(t *testing.T) { |
| 193 | id := ID("test") |
| 194 | |
| 195 | c := CollectionNew(id) |
| 196 | |
| 197 | if c.TotalItems != 0 { |
| 198 | t.Errorf("Empty object should have empty TotalItems, received %d", c.TotalItems) |
| 199 | } |
| 200 | if len(c.Items) > 0 { |
| 201 | t.Errorf("Empty object should have empty Items, received %v", c.Items) |
| 202 | } |
| 203 | if c.Count() != uint(len(c.Items)) { |
| 204 | t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.Items)) |
| 205 | } |
| 206 | |
| 207 | _ = c.Append(IRI("test")) |
| 208 | if c.TotalItems != 1 { |
| 209 | t.Errorf("Object should have %d TotalItems, received %d", 1, c.TotalItems) |
| 210 | } |
| 211 | if c.Count() != uint(len(c.Items)) { |
| 212 | t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.Items)) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func TestCollection_Contains(t *testing.T) { |
| 217 | t.Skipf("TODO") |
| 218 | } |
| 219 | |
| 220 | func TestCollection_IsCollection(t *testing.T) { |
| 221 | t.Skipf("TODO") |
| 222 | } |
| 223 | |
| 224 | func TestFollowersNew(t *testing.T) { |
| 225 | t.Skipf("TODO") |
| 226 | } |
| 227 | |
| 228 | func TestFollowingNew(t *testing.T) { |
| 229 | t.Skipf("TODO") |
| 230 | } |
| 231 | |
| 232 | func TestCollection_MarshalJSON(t *testing.T) { |
| 233 | t.Skipf("TODO") |
| 234 | } |
| 235 | |
| 236 | func TestCollection_ItemMatches(t *testing.T) { |
| 237 | t.Skipf("TODO") |
| 238 | } |
| 239 | |
| 240 | func TestToCollection(t *testing.T) { |
| 241 | tests := []struct { |
| 242 | name string |
| 243 | it LinkOrIRI |
| 244 | want *Collection |
| 245 | wantErr error |
| 246 | }{ |
| 247 | { |
| 248 | name: "empty", |
| 249 | }, |
| 250 | { |
| 251 | name: "Valid Collection", |
| 252 | it: Collection{ID: "test", Type: CollectionType}, |
| 253 | want: &Collection{ID: "test", Type: CollectionType}, |
| 254 | }, |
| 255 | { |
| 256 | name: "Valid *Collection", |
| 257 | it: &Collection{ID: "test", Type: CollectionType}, |
| 258 | want: &Collection{ID: "test", Type: CollectionType}, |
| 259 | }, |
| 260 | { |
| 261 | name: "Valid CollectionPage", |
| 262 | it: CollectionPage{ID: "test", Type: CollectionPageType}, |
| 263 | want: &Collection{ID: "test", Type: CollectionPageType}, |
| 264 | }, |
| 265 | { |
| 266 | name: "Valid *CollectionPage", |
| 267 | it: &CollectionPage{ID: "test", Type: CollectionPageType}, |
| 268 | want: &Collection{ID: "test", Type: CollectionPageType}, |
| 269 | }, |
| 270 | { |
| 271 | name: "Valid OrderedCollection", |
| 272 | it: OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 273 | want: &Collection{ID: "test", Type: OrderedCollectionType}, |
| 274 | }, |
| 275 | { |
| 276 | name: "Valid *OrderedCollection", |
| 277 | it: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 278 | want: &Collection{ID: "test", Type: OrderedCollectionType}, |
| 279 | }, |
| 280 | { |
| 281 | name: "Valid OrderedCollectionPage", |
| 282 | it: OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 283 | want: &Collection{ID: "test", Type: OrderedCollectionPageType}, |
| 284 | }, |
| 285 | { |
| 286 | name: "Valid *OrderedCollectionPage", |
| 287 | it: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 288 | want: &Collection{ID: "test", Type: OrderedCollectionPageType}, |
| 289 | }, |
| 290 | { |
| 291 | name: "IRI", |
| 292 | it: IRI("https://example.com"), |
| 293 | wantErr: ErrorInvalidType[Collection](IRI("")), |
| 294 | }, |
| 295 | { |
| 296 | name: "IRIs", |
| 297 | it: IRIs{IRI("https://example.com")}, |
| 298 | wantErr: ErrorInvalidType[Collection](IRIs{}), |
| 299 | }, |
| 300 | { |
| 301 | name: "ItemCollection", |
| 302 | it: ItemCollection{}, |
| 303 | wantErr: ErrorInvalidType[Collection](ItemCollection{}), |
| 304 | }, |
| 305 | { |
| 306 | name: "Object", |
| 307 | it: &Object{ID: "test", Type: ArticleType}, |
| 308 | wantErr: ErrorInvalidType[Collection](&Object{}), |
| 309 | }, |
| 310 | { |
| 311 | name: "Activity", |
| 312 | it: &Activity{ID: "test", Type: CreateType}, |
| 313 | wantErr: ErrorInvalidType[Collection](&Activity{}), |
| 314 | }, |
| 315 | { |
| 316 | name: "IntransitiveActivity", |
| 317 | it: &IntransitiveActivity{ID: "test", Type: ArriveType}, |
| 318 | wantErr: ErrorInvalidType[Collection](&IntransitiveActivity{}), |
| 319 | }, |
| 320 | } |
| 321 | for _, tt := range tests { |
| 322 | t.Run(tt.name, func(t *testing.T) { |
| 323 | got, err := ToCollection(tt.it) |
| 324 | if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 325 | t.Errorf("ToCollection() error = %v, wantErr %v", err, tt.wantErr) |
| 326 | return |
| 327 | } |
| 328 | if !cmp.Equal(got, tt.want) { |
| 329 | t.Errorf("ToCollection() got = %s", cmp.Diff(tt.want, got)) |
| 330 | } |
| 331 | }) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | func TestCollection_Equals(t *testing.T) { |
| 336 | tests := []struct { |
| 337 | name string |
| 338 | fields Collection |
| 339 | item Item |
| 340 | want bool |
| 341 | }{ |
| 342 | { |
| 343 | name: "collection with two items", |
| 344 | fields: Collection{ |
| 345 | ID: "https://example.com/1", |
| 346 | Type: CollectionType, |
| 347 | First: IRI("https://example.com/1?first"), |
| 348 | Items: ItemCollection{ |
| 349 | Object{ID: "https://example.com/1/1", Type: NoteType}, |
| 350 | Object{ID: "https://example.com/1/3", Type: ImageType}, |
| 351 | }, |
| 352 | }, |
| 353 | item: &Collection{ |
| 354 | ID: "https://example.com/1", |
| 355 | Type: CollectionType, |
| 356 | First: IRI("https://example.com/1?first"), |
| 357 | Items: ItemCollection{ |
| 358 | Object{ID: "https://example.com/1/1", Type: NoteType}, |
| 359 | Object{ID: "https://example.com/1/3", Type: ImageType}, |
| 360 | }, |
| 361 | }, |
| 362 | want: true, |
| 363 | }, |
| 364 | } |
| 365 | for _, tt := range tests { |
| 366 | t.Run(tt.name, func(t *testing.T) { |
| 367 | if got := tt.fields.Equals(tt.item); got != tt.want { |
| 368 | t.Errorf("Equal() = %v, want %v", got, tt.want) |
| 369 | } |
| 370 | }) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | func TestCollection_Remove(t *testing.T) { |
| 375 | tests := []struct { |
| 376 | name string |
| 377 | col Collection |
| 378 | toRemove ItemCollection |
| 379 | remaining ItemCollection |
| 380 | }{ |
| 381 | { |
| 382 | name: "empty", |
| 383 | col: mockCollection(), |
| 384 | }, |
| 385 | { |
| 386 | name: "remove one item from empty collection", |
| 387 | col: mockCollection(), |
| 388 | toRemove: ItemCollection{Object{ID: ID("grrr")}}, |
| 389 | }, |
| 390 | { |
| 391 | name: "remove all from collection", |
| 392 | col: mockCollection( |
| 393 | Object{ID: ID("grrr")}, |
| 394 | Activity{ID: ID("one")}, |
| 395 | Actor{ID: ID("jdoe")}, |
| 396 | ), |
| 397 | toRemove: ItemCollection{ |
| 398 | Object{ID: ID("grrr")}, |
| 399 | Activity{ID: ID("one")}, |
| 400 | Actor{ID: ID("jdoe")}, |
| 401 | }, |
| 402 | remaining: ItemCollection{}, |
| 403 | }, |
| 404 | { |
| 405 | name: "empty_collection_non_nil_item", |
| 406 | col: mockCollection(), |
| 407 | toRemove: ItemCollection{&Object{}}, |
| 408 | remaining: ItemCollection{}, |
| 409 | }, |
| 410 | { |
| 411 | name: "non_empty_collection_nil_item", |
| 412 | col: mockCollection(&Object{ID: "test"}), |
| 413 | toRemove: nil, |
| 414 | remaining: ItemCollection{&Object{ID: "test"}}, |
| 415 | }, |
| 416 | { |
| 417 | name: "non_empty_collection_non_contained_item_empty_ID", |
| 418 | col: mockCollection(&Object{ID: "test"}), |
| 419 | toRemove: ItemCollection{&Object{}}, |
| 420 | remaining: ItemCollection{ |
| 421 | &Object{ID: "test"}, |
| 422 | }, |
| 423 | }, |
| 424 | { |
| 425 | name: "non_empty_collection_non_contained_item", |
| 426 | col: mockCollection(&Object{ID: "test"}), |
| 427 | toRemove: ItemCollection{&Object{ID: "test123"}}, |
| 428 | remaining: ItemCollection{ |
| 429 | &Object{ID: "test"}, |
| 430 | }, |
| 431 | }, |
| 432 | { |
| 433 | name: "non_empty_collection_just_contained_item", |
| 434 | col: mockCollection(&Object{ID: "test"}), |
| 435 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 436 | remaining: ItemCollection{}, |
| 437 | }, |
| 438 | { |
| 439 | name: "non_empty_collection_contained_item_first_pos", |
| 440 | col: mockCollection( |
| 441 | &Object{ID: "test"}, |
| 442 | &Object{ID: "test123"}, |
| 443 | ), |
| 444 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 445 | remaining: ItemCollection{ |
| 446 | &Object{ID: "test123"}, |
| 447 | }, |
| 448 | }, |
| 449 | { |
| 450 | name: "non_empty_collection_contained_item_not_first_pos", |
| 451 | col: mockCollection( |
| 452 | &Object{ID: "test123"}, |
| 453 | &Object{ID: "test"}, |
| 454 | &Object{ID: "test321"}, |
| 455 | ), |
| 456 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 457 | remaining: ItemCollection{ |
| 458 | &Object{ID: "test123"}, |
| 459 | &Object{ID: "test321"}, |
| 460 | }, |
| 461 | }, |
| 462 | { |
| 463 | name: "non_empty_collection_contained_item_last_pos", |
| 464 | col: mockCollection( |
| 465 | &Object{ID: "test123"}, |
| 466 | &Object{ID: "test"}, |
| 467 | ), |
| 468 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 469 | remaining: ItemCollection{ |
| 470 | &Object{ID: "test123"}, |
| 471 | }, |
| 472 | }, |
| 473 | } |
| 474 | for _, tt := range tests { |
| 475 | t.Run(tt.name, func(t *testing.T) { |
| 476 | tt.col.Remove(tt.toRemove...) |
| 477 | |
| 478 | if tt.col.TotalItems != uint(len(tt.remaining)) { |
| 479 | t.Errorf("Post Remove() %T TotalItems %d different than expected %d", tt.col, tt.col.TotalItems, len(tt.remaining)) |
| 480 | } |
| 481 | for _, it := range tt.remaining { |
| 482 | if !tt.col.Items.Contains(it) { |
| 483 | t.Errorf("Post Remove() unable to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.Items) |
| 484 | } |
| 485 | } |
| 486 | for _, it := range tt.toRemove { |
| 487 | if tt.col.Items.Contains(it) { |
| 488 | t.Errorf("Post Remove() was still able to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.Items) |
| 489 | } |
| 490 | } |
| 491 | }) |
| 492 | } |
| 493 | } |