| 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 mockOrderedCollectionPage(items ...Item) OrderedCollectionPage { |
| 12 | cc := OrderedCollectionPage{ |
| 13 | ID: IRIf("https://example.com", Inbox), |
| 14 | Type: OrderedCollectionPageType, |
| 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 TestOrderedCollectionPageNew(t *testing.T) { |
| 26 | testValue := ID("test") |
| 27 | |
| 28 | c := OrderedCollectionNew(testValue) |
| 29 | p := OrderedCollectionPageNew(c) |
| 30 | if reflect.DeepEqual(p, c) { |
| 31 | t.Errorf("Invalid ordered collection parent '%v'", p.PartOf) |
| 32 | } |
| 33 | if p.PartOf != c.GetLink() { |
| 34 | t.Errorf("Invalid collection '%v'", p.PartOf) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func TestOrderedCollectionPage_UnmarshalJSON(t *testing.T) { |
| 39 | p := OrderedCollectionPage{} |
| 40 | |
| 41 | dataEmpty := []byte("{}") |
| 42 | p.UnmarshalJSON(dataEmpty) |
| 43 | if p.ID != "" { |
| 44 | t.Errorf("Unmarshaled object should have empty ID, received %q", p.ID) |
| 45 | } |
| 46 | if HasTypes(p) { |
| 47 | t.Errorf("Unmarshaled object should have empty Type, received %q", p.GetType()) |
| 48 | } |
| 49 | if p.AttributedTo != nil { |
| 50 | t.Errorf("Unmarshaled object should have empty AttributedTo, received %q", p.AttributedTo) |
| 51 | } |
| 52 | if len(p.Name) != 0 { |
| 53 | t.Errorf("Unmarshaled object should have empty Name, received %q", p.Name) |
| 54 | } |
| 55 | if len(p.Summary) != 0 { |
| 56 | t.Errorf("Unmarshaled object should have empty Summary, received %q", p.Summary) |
| 57 | } |
| 58 | if len(p.Content) != 0 { |
| 59 | t.Errorf("Unmarshaled object should have empty Content, received %q", p.Content) |
| 60 | } |
| 61 | if p.TotalItems != 0 { |
| 62 | t.Errorf("Unmarshaled object should have empty TotalItems, received %d", p.TotalItems) |
| 63 | } |
| 64 | if len(p.OrderedItems) > 0 { |
| 65 | t.Errorf("Unmarshaled object should have empty OrderedItems, received %v", p.OrderedItems) |
| 66 | } |
| 67 | if p.URL != nil { |
| 68 | t.Errorf("Unmarshaled object should have empty URL, received %v", p.URL) |
| 69 | } |
| 70 | if !p.Published.IsZero() { |
| 71 | t.Errorf("Unmarshaled object should have empty Published, received %q", p.Published) |
| 72 | } |
| 73 | if !p.StartTime.IsZero() { |
| 74 | t.Errorf("Unmarshaled object should have empty StartTime, received %q", p.StartTime) |
| 75 | } |
| 76 | if !p.Updated.IsZero() { |
| 77 | t.Errorf("Unmarshaled object should have empty Updated, received %q", p.Updated) |
| 78 | } |
| 79 | if p.PartOf != nil { |
| 80 | t.Errorf("Unmarshaled object should have empty PartOf, received %q", p.PartOf) |
| 81 | } |
| 82 | if p.Current != nil { |
| 83 | t.Errorf("Unmarshaled object should have empty Current, received %q", p.Current) |
| 84 | } |
| 85 | if p.First != nil { |
| 86 | t.Errorf("Unmarshaled object should have empty First, received %q", p.First) |
| 87 | } |
| 88 | if p.Last != nil { |
| 89 | t.Errorf("Unmarshaled object should have empty Last, received %q", p.Last) |
| 90 | } |
| 91 | if p.Next != nil { |
| 92 | t.Errorf("Unmarshaled object should have empty Next, received %q", p.Next) |
| 93 | } |
| 94 | if p.Prev != nil { |
| 95 | t.Errorf("Unmarshaled object should have empty Prev, received %q", p.Prev) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | func TestOrderedCollectionPage_Append(t *testing.T) { |
| 100 | tests := []struct { |
| 101 | name string |
| 102 | col OrderedCollectionPage |
| 103 | it []Item |
| 104 | wantErr error |
| 105 | }{ |
| 106 | { |
| 107 | name: "empty", |
| 108 | col: mockOrderedCollectionPage(), |
| 109 | it: ItemCollection{}, |
| 110 | }, |
| 111 | { |
| 112 | name: "add one item", |
| 113 | col: mockOrderedCollectionPage(), |
| 114 | it: ItemCollection{ |
| 115 | Object{ID: ID("grrr")}, |
| 116 | }, |
| 117 | }, |
| 118 | { |
| 119 | name: "add multiple items", |
| 120 | col: mockOrderedCollectionPage(), |
| 121 | it: ItemCollection{ |
| 122 | Object{ID: ID("grrr")}, |
| 123 | Activity{ID: ID("one")}, |
| 124 | Actor{ID: ID("jdoe")}, |
| 125 | }, |
| 126 | }, |
| 127 | } |
| 128 | for _, tt := range tests { |
| 129 | t.Run(tt.name, func(t *testing.T) { |
| 130 | defer func() { |
| 131 | tt.col.OrderedItems = tt.col.OrderedItems[:0] |
| 132 | tt.col.TotalItems = 0 |
| 133 | }() |
| 134 | if err := tt.col.Append(tt.it...); (err != nil) && errors.Is(err, tt.wantErr) { |
| 135 | t.Errorf("Append() error = %v, wantErr %v", err, tt.wantErr) |
| 136 | } |
| 137 | if tt.col.TotalItems != uint(len(tt.it)) { |
| 138 | t.Errorf("Post Append() %T TotalItems %d different than added count %d", tt.col, tt.col.TotalItems, len(tt.it)) |
| 139 | } |
| 140 | for _, it := range tt.it { |
| 141 | if !tt.col.OrderedItems.Contains(it) { |
| 142 | t.Errorf("Post Append() unable to find %s in %T Items %v", tt.col, it.GetLink(), tt.col.OrderedItems) |
| 143 | } |
| 144 | } |
| 145 | }) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | func TestOrderedCollectionPage_Collection(t *testing.T) { |
| 150 | id := ID("test") |
| 151 | |
| 152 | c := OrderedCollectionNew(id) |
| 153 | p := OrderedCollectionPageNew(c) |
| 154 | |
| 155 | if !reflect.DeepEqual(p.Collection(), p.OrderedItems) { |
| 156 | t.Errorf("Collection items should be equal %v %v", p.Collection(), p.OrderedItems) |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | func TestOrderedCollectionPage_Contains(t *testing.T) { |
| 161 | t.Skipf("TODO") |
| 162 | } |
| 163 | |
| 164 | func TestOrderedCollectionPage_Count(t *testing.T) { |
| 165 | id := ID("test") |
| 166 | |
| 167 | c := OrderedCollectionNew(id) |
| 168 | p := OrderedCollectionPageNew(c) |
| 169 | |
| 170 | if p.TotalItems != 0 { |
| 171 | t.Errorf("Empty object should have empty TotalItems, received %d", p.TotalItems) |
| 172 | } |
| 173 | if len(p.OrderedItems) > 0 { |
| 174 | t.Errorf("Empty object should have empty Items, received %v", p.OrderedItems) |
| 175 | } |
| 176 | if p.Count() != uint(len(p.OrderedItems)) { |
| 177 | t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.OrderedItems)) |
| 178 | } |
| 179 | |
| 180 | _ = p.Append(IRI("test")) |
| 181 | if p.TotalItems != 1 { |
| 182 | t.Errorf("Object should have %d TotalItems, received %d", 1, p.TotalItems) |
| 183 | } |
| 184 | if p.Count() != uint(len(p.OrderedItems)) { |
| 185 | t.Errorf("%T.Count() returned %d, expected %d", c, p.Count(), len(p.OrderedItems)) |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | func TestToOrderedCollectionPage(t *testing.T) { |
| 190 | tests := []struct { |
| 191 | name string |
| 192 | it LinkOrIRI |
| 193 | want *OrderedCollectionPage |
| 194 | wantErr error |
| 195 | }{ |
| 196 | { |
| 197 | name: "empty", |
| 198 | }, |
| 199 | { |
| 200 | name: "Valid OrderedCollectionPage", |
| 201 | it: OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 202 | want: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 203 | }, |
| 204 | { |
| 205 | name: "Valid *OrderedCollectionPage", |
| 206 | it: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 207 | want: &OrderedCollectionPage{ID: "test", Type: OrderedCollectionPageType}, |
| 208 | }, |
| 209 | { |
| 210 | name: "Valid CollectionPage", |
| 211 | it: CollectionPage{ID: "test", Type: CollectionPageType}, |
| 212 | want: &OrderedCollectionPage{ID: "test", Type: CollectionPageType}, |
| 213 | }, |
| 214 | { |
| 215 | name: "Valid *CollectionPage", |
| 216 | it: &CollectionPage{ID: "test", Type: CollectionPageType}, |
| 217 | want: &OrderedCollectionPage{ID: "test", Type: CollectionPageType}, |
| 218 | }, |
| 219 | { |
| 220 | name: "Valid Collection", |
| 221 | it: Collection{ID: "test", Type: CollectionType}, |
| 222 | wantErr: ErrorInvalidType[OrderedCollectionPage](Collection{}), |
| 223 | }, |
| 224 | { |
| 225 | name: "Valid *Collection", |
| 226 | it: &Collection{ID: "test", Type: CollectionType}, |
| 227 | wantErr: ErrorInvalidType[OrderedCollectionPage](new(Collection)), |
| 228 | }, |
| 229 | { |
| 230 | name: "Valid OrderedCollection", |
| 231 | it: OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 232 | wantErr: ErrorInvalidType[OrderedCollectionPage](OrderedCollection{}), |
| 233 | }, |
| 234 | { |
| 235 | name: "Valid *OrderedCollection", |
| 236 | it: &OrderedCollection{ID: "test", Type: OrderedCollectionType}, |
| 237 | wantErr: ErrorInvalidType[OrderedCollectionPage](new(OrderedCollection)), |
| 238 | }, |
| 239 | { |
| 240 | name: "IRI", |
| 241 | it: IRI("https://example.com"), |
| 242 | wantErr: ErrorInvalidType[OrderedCollectionPage](IRI("")), |
| 243 | }, |
| 244 | { |
| 245 | name: "IRIs", |
| 246 | it: IRIs{IRI("https://example.com")}, |
| 247 | wantErr: ErrorInvalidType[OrderedCollectionPage](IRIs{}), |
| 248 | }, |
| 249 | { |
| 250 | name: "ItemCollection", |
| 251 | it: ItemCollection{}, |
| 252 | wantErr: ErrorInvalidType[OrderedCollectionPage](ItemCollection{}), |
| 253 | }, |
| 254 | { |
| 255 | name: "Object", |
| 256 | it: &Object{ID: "test", Type: ArticleType}, |
| 257 | wantErr: ErrorInvalidType[OrderedCollectionPage](&Object{}), |
| 258 | }, |
| 259 | { |
| 260 | name: "Activity", |
| 261 | it: &Activity{ID: "test", Type: CreateType}, |
| 262 | wantErr: ErrorInvalidType[OrderedCollectionPage](&Activity{}), |
| 263 | }, |
| 264 | { |
| 265 | name: "IntransitiveActivity", |
| 266 | it: &IntransitiveActivity{ID: "test", Type: ArriveType}, |
| 267 | wantErr: ErrorInvalidType[OrderedCollectionPage](&IntransitiveActivity{}), |
| 268 | }, |
| 269 | } |
| 270 | for _, tt := range tests { |
| 271 | t.Run(tt.name, func(t *testing.T) { |
| 272 | got, err := ToOrderedCollectionPage(tt.it) |
| 273 | if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 274 | t.Errorf("ToOrderedCollectionPage() error = %v, wantErr %v", err, tt.wantErr) |
| 275 | return |
| 276 | } |
| 277 | if !cmp.Equal(got, tt.want) { |
| 278 | t.Errorf("ToOrderedCollectionPage() got = %s", cmp.Diff(tt.want, got)) |
| 279 | } |
| 280 | }) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestOrderedCollectionPage_Remove(t *testing.T) { |
| 285 | tests := []struct { |
| 286 | name string |
| 287 | col OrderedCollectionPage |
| 288 | toRemove ItemCollection |
| 289 | remaining ItemCollection |
| 290 | }{ |
| 291 | { |
| 292 | name: "empty", |
| 293 | col: mockOrderedCollectionPage(), |
| 294 | }, |
| 295 | { |
| 296 | name: "remove one item from empty collection", |
| 297 | col: mockOrderedCollectionPage(), |
| 298 | toRemove: ItemCollection{Object{ID: ID("grrr")}}, |
| 299 | }, |
| 300 | { |
| 301 | name: "remove all from collection", |
| 302 | col: mockOrderedCollectionPage( |
| 303 | Object{ID: ID("grrr")}, |
| 304 | Activity{ID: ID("one")}, |
| 305 | Actor{ID: ID("jdoe")}, |
| 306 | ), |
| 307 | toRemove: ItemCollection{ |
| 308 | Object{ID: ID("grrr")}, |
| 309 | Activity{ID: ID("one")}, |
| 310 | Actor{ID: ID("jdoe")}, |
| 311 | }, |
| 312 | remaining: ItemCollection{}, |
| 313 | }, |
| 314 | { |
| 315 | name: "empty_collection_non_nil_item", |
| 316 | col: mockOrderedCollectionPage(), |
| 317 | toRemove: ItemCollection{&Object{}}, |
| 318 | remaining: ItemCollection{}, |
| 319 | }, |
| 320 | { |
| 321 | name: "non_empty_collection_nil_item", |
| 322 | col: mockOrderedCollectionPage(&Object{ID: "test"}), |
| 323 | toRemove: nil, |
| 324 | remaining: ItemCollection{&Object{ID: "test"}}, |
| 325 | }, |
| 326 | { |
| 327 | name: "non_empty_collection_non_contained_item_empty_ID", |
| 328 | col: mockOrderedCollectionPage(&Object{ID: "test"}), |
| 329 | toRemove: ItemCollection{&Object{}}, |
| 330 | remaining: ItemCollection{ |
| 331 | &Object{ID: "test"}, |
| 332 | }, |
| 333 | }, |
| 334 | { |
| 335 | name: "non_empty_collection_non_contained_item", |
| 336 | col: mockOrderedCollectionPage(&Object{ID: "test"}), |
| 337 | toRemove: ItemCollection{&Object{ID: "test123"}}, |
| 338 | remaining: ItemCollection{ |
| 339 | &Object{ID: "test"}, |
| 340 | }, |
| 341 | }, |
| 342 | { |
| 343 | name: "non_empty_collection_just_contained_item", |
| 344 | col: mockOrderedCollectionPage(&Object{ID: "test"}), |
| 345 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 346 | remaining: ItemCollection{}, |
| 347 | }, |
| 348 | { |
| 349 | name: "non_empty_collection_contained_item_first_pos", |
| 350 | col: mockOrderedCollectionPage( |
| 351 | &Object{ID: "test"}, |
| 352 | &Object{ID: "test123"}, |
| 353 | ), |
| 354 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 355 | remaining: ItemCollection{ |
| 356 | &Object{ID: "test123"}, |
| 357 | }, |
| 358 | }, |
| 359 | { |
| 360 | name: "non_empty_collection_contained_item_not_first_pos", |
| 361 | col: mockOrderedCollectionPage( |
| 362 | &Object{ID: "test123"}, |
| 363 | &Object{ID: "test"}, |
| 364 | &Object{ID: "test321"}, |
| 365 | ), |
| 366 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 367 | remaining: ItemCollection{ |
| 368 | &Object{ID: "test123"}, |
| 369 | &Object{ID: "test321"}, |
| 370 | }, |
| 371 | }, |
| 372 | { |
| 373 | name: "non_empty_collection_contained_item_last_pos", |
| 374 | col: mockOrderedCollectionPage( |
| 375 | &Object{ID: "test123"}, |
| 376 | &Object{ID: "test"}, |
| 377 | ), |
| 378 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 379 | remaining: ItemCollection{ |
| 380 | &Object{ID: "test123"}, |
| 381 | }, |
| 382 | }, |
| 383 | } |
| 384 | for _, tt := range tests { |
| 385 | t.Run(tt.name, func(t *testing.T) { |
| 386 | tt.col.Remove(tt.toRemove...) |
| 387 | |
| 388 | if tt.col.TotalItems != uint(len(tt.remaining)) { |
| 389 | t.Errorf("Post Remove() %T TotalItems %d different than expected %d", tt.col, tt.col.TotalItems, len(tt.remaining)) |
| 390 | } |
| 391 | for _, it := range tt.remaining { |
| 392 | if !tt.col.OrderedItems.Contains(it) { |
| 393 | t.Errorf("Post Remove() unable to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.OrderedItems) |
| 394 | } |
| 395 | } |
| 396 | for _, it := range tt.toRemove { |
| 397 | if tt.col.OrderedItems.Contains(it) { |
| 398 | t.Errorf("Post Remove() was still able to find %s in %T Items %v", it.GetLink(), tt.col, tt.col.OrderedItems) |
| 399 | } |
| 400 | } |
| 401 | }) |
| 402 | } |
| 403 | } |