| 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 mockOrderedCollection(items ...Item) OrderedCollection { |
| 12 | cc := OrderedCollection{ |
| 13 | ID: IRIf("https://example.com", Inbox), |
| 14 | Type: OrderedCollectionType, |
| 15 | } |
| 16 | if len(items) == 0 { |
| 17 | cc.OrderedItems = make(ItemCollection, 0) |
| 18 | } else { |
| 19 | cc.OrderedItems = items |
| 20 | cc.TotalItems = uint(len(items)) |
| 21 | } |
| 22 | return cc |
| 23 | } |
| 24 | |
| 25 | func TestOrderedCollectionNew(t *testing.T) { |
| 26 | testValue := ID("test") |
| 27 | |
| 28 | c := OrderedCollectionNew(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(OrderedCollectionType) { |
| 34 | t.Errorf("APObject Type '%v' different than expected '%v'", c.GetType(), OrderedCollectionType) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestOrderedCollection_Append(t *testing.T) { |
| 39 | tests := []struct { |
| 40 | name string |
| 41 | col OrderedCollection |
| 42 | it []Item |
| 43 | wantErr error |
| 44 | }{ |
| 45 | { |
| 46 | name: "empty", |
| 47 | col: mockOrderedCollection(), |
| 48 | it: ItemCollection{}, |
| 49 | }, |
| 50 | { |
| 51 | name: "add one item", |
| 52 | col: mockOrderedCollection(), |
| 53 | it: ItemCollection{ |
| 54 | Object{ID: ID("grrr")}, |
| 55 | }, |
| 56 | }, |
| 57 | { |
| 58 | name: "add multiple items", |
| 59 | col: mockOrderedCollection(), |
| 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.OrderedItems = tt.col.OrderedItems[: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.OrderedItems.Contains(it) { |
| 81 | t.Errorf("Post Append() unable to find %s in %T Items %v", tt.col, it.GetLink(), tt.col.OrderedItems) |
| 82 | } |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestOrderedCollection_Collection(t *testing.T) { |
| 89 | id := ID("test") |
| 90 | |
| 91 | o := OrderedCollectionNew(id) |
| 92 | |
| 93 | if !reflect.DeepEqual(o.Collection(), o.OrderedItems) { |
| 94 | t.Errorf("Collection items should be equal %v %v", o.Collection(), o.OrderedItems) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestOrderedCollection_GetID(t *testing.T) { |
| 99 | id := ID("test") |
| 100 | |
| 101 | c := OrderedCollectionNew(id) |
| 102 | |
| 103 | if c.GetID() != id { |
| 104 | t.Errorf("GetID should return %q, received %q", id, c.GetID()) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestOrderedCollection_GetLink(t *testing.T) { |
| 109 | id := ID("test") |
| 110 | link := IRI(id) |
| 111 | |
| 112 | c := OrderedCollectionNew(id) |
| 113 | |
| 114 | if c.GetLink() != link { |
| 115 | t.Errorf("GetLink should return %q, received %q", link, c.GetLink()) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestOrderedCollection_GetType(t *testing.T) { |
| 120 | id := ID("test") |
| 121 | |
| 122 | c := OrderedCollectionNew(id) |
| 123 | |
| 124 | if !c.Match(OrderedCollectionType) { |
| 125 | t.Errorf("OrderedCollection Type should be %q, received %q", OrderedCollectionType, c.GetType()) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func TestOrderedCollection_IsLink(t *testing.T) { |
| 130 | id := ID("test") |
| 131 | |
| 132 | c := OrderedCollectionNew(id) |
| 133 | |
| 134 | if c.IsLink() != false { |
| 135 | t.Errorf("OrderedCollection should not be a link, received %t", c.IsLink()) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | func TestOrderedCollection_IsObject(t *testing.T) { |
| 140 | id := ID("test") |
| 141 | |
| 142 | c := OrderedCollectionNew(id) |
| 143 | |
| 144 | if c.IsObject() != true { |
| 145 | t.Errorf("OrderedCollection should be an object, received %t", c.IsObject()) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestOrderedCollection_UnmarshalJSON(t *testing.T) { |
| 150 | c := OrderedCollection{} |
| 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.OrderedItems) > 0 { |
| 176 | t.Errorf("Unmarshaled object should have empty OrderedItems, received %v", c.OrderedItems) |
| 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 TestOrderedCollection_Count(t *testing.T) { |
| 193 | id := ID("test") |
| 194 | |
| 195 | c := OrderedCollectionNew(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.OrderedItems) > 0 { |
| 201 | t.Errorf("Empty object should have empty Items, received %v", c.OrderedItems) |
| 202 | } |
| 203 | if c.Count() != uint(len(c.OrderedItems)) { |
| 204 | t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.OrderedItems)) |
| 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.OrderedItems)) { |
| 212 | t.Errorf("%T.Count() returned %d, expected %d", c, c.Count(), len(c.OrderedItems)) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func TestOnOrderedCollection(t *testing.T) { |
| 217 | t.Skipf("TODO") |
| 218 | } |
| 219 | |
| 220 | func TestToOrderedCollection(t *testing.T) { |
| 221 | tests := []struct { |
| 222 | name string |
| 223 | it LinkOrIRI |
| 224 | want *OrderedCollection |
| 225 | wantErr error |
| 226 | }{ |
| 227 | { |
| 228 | name: "empty", |
| 229 | }, |
| 230 | { |
| 231 | name: "Valid OrderedCollection", |
| 232 | it: OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 233 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 234 | }, |
| 235 | { |
| 236 | name: "Valid *OrderedCollection", |
| 237 | it: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 238 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 239 | }, |
| 240 | { |
| 241 | name: "Valid OrderedCollectionPage", |
| 242 | it: OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 243 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionPageType}, |
| 244 | }, |
| 245 | { |
| 246 | name: "Valid *OrderedCollectionPage", |
| 247 | it: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 248 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionPageType}, |
| 249 | }, |
| 250 | { |
| 251 | name: "Valid OrderedCollection", |
| 252 | it: OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 253 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 254 | }, |
| 255 | { |
| 256 | name: "Valid *OrderedCollection", |
| 257 | it: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 258 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 259 | }, |
| 260 | { |
| 261 | name: "Valid OrderedCollectionPage", |
| 262 | it: OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 263 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionPageType}, |
| 264 | }, |
| 265 | { |
| 266 | name: "Valid *OrderedCollectionPage", |
| 267 | it: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 268 | want: &OrderedCollection{ID: "test", Type: OrderedCollectionPageType}, |
| 269 | }, |
| 270 | { |
| 271 | name: "IRI", |
| 272 | it: IRI("https://example.com"), |
| 273 | wantErr: ErrorInvalidType[OrderedCollection](IRI("")), |
| 274 | }, |
| 275 | { |
| 276 | name: "IRIs", |
| 277 | it: IRIs{IRI("https://example.com")}, |
| 278 | wantErr: ErrorInvalidType[OrderedCollection](IRIs{}), |
| 279 | }, |
| 280 | { |
| 281 | name: "ItemCollection", |
| 282 | it: ItemCollection{}, |
| 283 | wantErr: ErrorInvalidType[OrderedCollection](ItemCollection{}), |
| 284 | }, |
| 285 | { |
| 286 | name: "Object", |
| 287 | it: &Object{ID: "test", Type: ArticleType}, |
| 288 | wantErr: ErrorInvalidType[OrderedCollection](&Object{}), |
| 289 | }, |
| 290 | { |
| 291 | name: "Activity", |
| 292 | it: &Activity{ID: "test", Type: CreateType}, |
| 293 | wantErr: ErrorInvalidType[OrderedCollection](&Activity{}), |
| 294 | }, |
| 295 | { |
| 296 | name: "IntransitiveActivity", |
| 297 | it: &IntransitiveActivity{ID: "test", Type: ArriveType}, |
| 298 | wantErr: ErrorInvalidType[OrderedCollection](&IntransitiveActivity{}), |
| 299 | }, |
| 300 | } |
| 301 | for _, tt := range tests { |
| 302 | t.Run(tt.name, func(t *testing.T) { |
| 303 | got, err := ToOrderedCollection(tt.it) |
| 304 | if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 305 | t.Errorf("ToOrderedCollection() error = %v, wantErr %v", err, tt.wantErr) |
| 306 | return |
| 307 | } |
| 308 | if !cmp.Equal(got, tt.want) { |
| 309 | t.Errorf("ToOrderedCollection() got = %s", cmp.Diff(tt.want, got)) |
| 310 | } |
| 311 | }) |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | func TestOrderedCollection_Contains(t *testing.T) { |
| 316 | t.Skipf("TODO") |
| 317 | } |
| 318 | |
| 319 | func TestOrderedCollection_MarshalJSON(t *testing.T) { |
| 320 | t.Skipf("TODO") |
| 321 | } |
| 322 | |
| 323 | func TestOrderedCollection_ItemMatches(t *testing.T) { |
| 324 | t.Skipf("TODO") |
| 325 | } |
| 326 | |
| 327 | func TestOrderedCollection_IsCollection(t *testing.T) { |
| 328 | t.Skipf("TODO") |
| 329 | } |
| 330 | |
| 331 | func TestOrderedCollection_Remove(t *testing.T) { |
| 332 | tests := []struct { |
| 333 | name string |
| 334 | col OrderedCollection |
| 335 | toRemove ItemCollection |
| 336 | remaining ItemCollection |
| 337 | }{ |
| 338 | { |
| 339 | name: "empty", |
| 340 | col: mockOrderedCollection(), |
| 341 | }, |
| 342 | { |
| 343 | name: "remove one item from empty collection", |
| 344 | col: mockOrderedCollection(), |
| 345 | toRemove: ItemCollection{Object{ID: ID("grrr")}}, |
| 346 | }, |
| 347 | { |
| 348 | name: "remove all from collection", |
| 349 | col: mockOrderedCollection( |
| 350 | Object{ID: ID("grrr")}, |
| 351 | Activity{ID: ID("one")}, |
| 352 | Actor{ID: ID("jdoe")}, |
| 353 | ), |
| 354 | toRemove: ItemCollection{ |
| 355 | Object{ID: ID("grrr")}, |
| 356 | Activity{ID: ID("one")}, |
| 357 | Actor{ID: ID("jdoe")}, |
| 358 | }, |
| 359 | remaining: ItemCollection{}, |
| 360 | }, |
| 361 | { |
| 362 | name: "empty_collection_non_nil_item", |
| 363 | col: mockOrderedCollection(), |
| 364 | toRemove: ItemCollection{&Object{}}, |
| 365 | remaining: ItemCollection{}, |
| 366 | }, |
| 367 | { |
| 368 | name: "non_empty_collection_nil_item", |
| 369 | col: mockOrderedCollection(&Object{ID: "test"}), |
| 370 | toRemove: nil, |
| 371 | remaining: ItemCollection{&Object{ID: "test"}}, |
| 372 | }, |
| 373 | { |
| 374 | name: "non_empty_collection_non_contained_item_empty_ID", |
| 375 | col: mockOrderedCollection(&Object{ID: "test"}), |
| 376 | toRemove: ItemCollection{&Object{}}, |
| 377 | remaining: ItemCollection{ |
| 378 | &Object{ID: "test"}, |
| 379 | }, |
| 380 | }, |
| 381 | { |
| 382 | name: "non_empty_collection_non_contained_item", |
| 383 | col: mockOrderedCollection(&Object{ID: "test"}), |
| 384 | toRemove: ItemCollection{&Object{ID: "test123"}}, |
| 385 | remaining: ItemCollection{ |
| 386 | &Object{ID: "test"}, |
| 387 | }, |
| 388 | }, |
| 389 | { |
| 390 | name: "non_empty_collection_just_contained_item", |
| 391 | col: mockOrderedCollection(&Object{ID: "test"}), |
| 392 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 393 | remaining: ItemCollection{}, |
| 394 | }, |
| 395 | { |
| 396 | name: "non_empty_collection_contained_item_first_pos", |
| 397 | col: mockOrderedCollection( |
| 398 | &Object{ID: "test"}, |
| 399 | &Object{ID: "test123"}, |
| 400 | ), |
| 401 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 402 | remaining: ItemCollection{ |
| 403 | &Object{ID: "test123"}, |
| 404 | }, |
| 405 | }, |
| 406 | { |
| 407 | name: "non_empty_collection_contained_item_not_first_pos", |
| 408 | col: mockOrderedCollection( |
| 409 | &Object{ID: "test123"}, |
| 410 | &Object{ID: "test"}, |
| 411 | &Object{ID: "test321"}, |
| 412 | ), |
| 413 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 414 | remaining: ItemCollection{ |
| 415 | &Object{ID: "test123"}, |
| 416 | &Object{ID: "test321"}, |
| 417 | }, |
| 418 | }, |
| 419 | { |
| 420 | name: "non_empty_collection_contained_item_last_pos", |
| 421 | col: mockOrderedCollection( |
| 422 | &Object{ID: "test123"}, |
| 423 | &Object{ID: "test"}, |
| 424 | ), |
| 425 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 426 | remaining: ItemCollection{ |
| 427 | &Object{ID: "test123"}, |
| 428 | }, |
| 429 | }, |
| 430 | } |
| 431 | for _, tt := range tests { |
| 432 | t.Run(tt.name, func(t *testing.T) { |
| 433 | tt.col.Remove(tt.toRemove...) |
| 434 | |
| 435 | if tt.col.TotalItems != uint(len(tt.remaining)) { |
| 436 | t.Errorf("Post Remove() %T TotalItems %d different than expected %d", tt.col, tt.col.TotalItems, len(tt.remaining)) |
| 437 | } |
| 438 | for _, it := range tt.remaining { |
| 439 | if !tt.col.OrderedItems.Contains(it) { |
| 440 | t.Errorf("Post Remove() unable to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.OrderedItems) |
| 441 | } |
| 442 | } |
| 443 | for _, it := range tt.toRemove { |
| 444 | if tt.col.OrderedItems.Contains(it) { |
| 445 | t.Errorf("Post Remove() was still able to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.OrderedItems) |
| 446 | } |
| 447 | } |
| 448 | }) |
| 449 | } |
| 450 | } |