| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestItemCollection_Append(t *testing.T) { |
| 9 | t.Skipf("TODO") |
| 10 | } |
| 11 | |
| 12 | func TestItemCollection_Collection(t *testing.T) { |
| 13 | t.Skipf("TODO") |
| 14 | } |
| 15 | |
| 16 | func TestItemCollection_GetID(t *testing.T) { |
| 17 | t.Skipf("TODO") |
| 18 | } |
| 19 | |
| 20 | func TestItemCollection_GetLink(t *testing.T) { |
| 21 | t.Skipf("TODO") |
| 22 | } |
| 23 | |
| 24 | func TestItemCollection_GetType(t *testing.T) { |
| 25 | t.Skipf("TODO") |
| 26 | } |
| 27 | |
| 28 | func TestItemCollection_IsLink(t *testing.T) { |
| 29 | t.Skipf("TODO") |
| 30 | } |
| 31 | |
| 32 | func TestItemCollection_IsObject(t *testing.T) { |
| 33 | t.Skipf("TODO") |
| 34 | } |
| 35 | |
| 36 | func TestItemCollection_First(t *testing.T) { |
| 37 | t.Skipf("TODO") |
| 38 | } |
| 39 | |
| 40 | func TestItemCollection_Count(t *testing.T) { |
| 41 | t.Skipf("TODO") |
| 42 | } |
| 43 | |
| 44 | func TestItemCollection_Contains(t *testing.T) { |
| 45 | t.Skipf("TODO") |
| 46 | } |
| 47 | |
| 48 | func TestItemCollection_IsCollection(t *testing.T) { |
| 49 | t.Skipf("TODO") |
| 50 | } |
| 51 | |
| 52 | func TestToItemCollection(t *testing.T) { |
| 53 | t.Skipf("TODO") |
| 54 | } |
| 55 | |
| 56 | func TestItemCollection_Remove(t *testing.T) { |
| 57 | tests := []struct { |
| 58 | name string |
| 59 | items ItemCollection |
| 60 | toRemove ItemCollection |
| 61 | remaining ItemCollection |
| 62 | }{ |
| 63 | { |
| 64 | name: "empty_collection_nil_item", |
| 65 | items: ItemCollection{}, |
| 66 | toRemove: nil, |
| 67 | remaining: ItemCollection{}, |
| 68 | }, |
| 69 | { |
| 70 | name: "empty_collection_non_nil_item", |
| 71 | items: ItemCollection{}, |
| 72 | toRemove: ItemCollection{&Object{}}, |
| 73 | remaining: ItemCollection{}, |
| 74 | }, |
| 75 | { |
| 76 | name: "non_empty_collection_nil_item", |
| 77 | items: ItemCollection{ |
| 78 | &Object{ID: "test"}, |
| 79 | }, |
| 80 | toRemove: nil, |
| 81 | remaining: ItemCollection{ |
| 82 | &Object{ID: "test"}, |
| 83 | }, |
| 84 | }, |
| 85 | { |
| 86 | name: "non_empty_collection_non_contained_item_empty_ID", |
| 87 | items: ItemCollection{ |
| 88 | &Object{ID: "test"}, |
| 89 | }, |
| 90 | toRemove: ItemCollection{&Object{}}, |
| 91 | remaining: ItemCollection{ |
| 92 | &Object{ID: "test"}, |
| 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | name: "non_empty_collection_non_contained_item", |
| 97 | items: ItemCollection{ |
| 98 | &Object{ID: "test"}, |
| 99 | }, |
| 100 | toRemove: ItemCollection{&Object{ID: "test123"}}, |
| 101 | remaining: ItemCollection{ |
| 102 | &Object{ID: "test"}, |
| 103 | }, |
| 104 | }, |
| 105 | { |
| 106 | name: "non_empty_collection_just_contained_item", |
| 107 | items: ItemCollection{ |
| 108 | &Object{ID: "test"}, |
| 109 | }, |
| 110 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 111 | remaining: ItemCollection{}, |
| 112 | }, |
| 113 | { |
| 114 | name: "non_empty_collection_contained_item_first_pos", |
| 115 | items: ItemCollection{ |
| 116 | &Object{ID: "test"}, |
| 117 | &Object{ID: "test123"}, |
| 118 | }, |
| 119 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 120 | remaining: ItemCollection{ |
| 121 | &Object{ID: "test123"}, |
| 122 | }, |
| 123 | }, |
| 124 | { |
| 125 | name: "non_empty_collection_contained_item_not_first_pos", |
| 126 | items: ItemCollection{ |
| 127 | &Object{ID: "test123"}, |
| 128 | &Object{ID: "test"}, |
| 129 | &Object{ID: "test321"}, |
| 130 | }, |
| 131 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 132 | remaining: ItemCollection{ |
| 133 | &Object{ID: "test123"}, |
| 134 | &Object{ID: "test321"}, |
| 135 | }, |
| 136 | }, |
| 137 | { |
| 138 | name: "non_empty_collection_contained_item_last_pos", |
| 139 | items: ItemCollection{ |
| 140 | &Object{ID: "test123"}, |
| 141 | &Object{ID: "test"}, |
| 142 | }, |
| 143 | toRemove: ItemCollection{&Object{ID: "test"}}, |
| 144 | remaining: ItemCollection{ |
| 145 | &Object{ID: "test123"}, |
| 146 | }, |
| 147 | }, |
| 148 | } |
| 149 | for _, tt := range tests { |
| 150 | t.Run(tt.name, func(t *testing.T) { |
| 151 | tt.items.Remove(tt.toRemove...) |
| 152 | |
| 153 | if tt.remaining.Count() != tt.items.Count() { |
| 154 | t.Errorf("Post Remove() %T has count %d different than expected %d", tt.items, tt.items.Count(), tt.remaining.Count()) |
| 155 | } |
| 156 | for _, it := range tt.toRemove { |
| 157 | if tt.items.Contains(it) { |
| 158 | t.Errorf("Post Remove() was still able to find %s in %T Items %v", it.GetLink(), tt.items, tt.items) |
| 159 | } |
| 160 | } |
| 161 | for _, it := range tt.remaining { |
| 162 | if !tt.items.Contains(it) { |
| 163 | t.Errorf("Post Remove() unable to find %s in %T Items %v", it.GetLink(), tt.items, tt.items) |
| 164 | } |
| 165 | } |
| 166 | }) |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | func TestItemCollectionDeduplication(t *testing.T) { |
| 171 | tests := []struct { |
| 172 | name string |
| 173 | args []*ItemCollection |
| 174 | want ItemCollection |
| 175 | remaining []*ItemCollection |
| 176 | }{ |
| 177 | { |
| 178 | name: "empty", |
| 179 | }, |
| 180 | { |
| 181 | name: "no-overlap", |
| 182 | args: []*ItemCollection{ |
| 183 | { |
| 184 | IRI("https://example.com"), |
| 185 | IRI("https://example.com/2"), |
| 186 | }, |
| 187 | { |
| 188 | IRI("https://example.com/1"), |
| 189 | }, |
| 190 | }, |
| 191 | want: ItemCollection{ |
| 192 | IRI("https://example.com"), |
| 193 | IRI("https://example.com/2"), |
| 194 | IRI("https://example.com/1"), |
| 195 | }, |
| 196 | remaining: []*ItemCollection{ |
| 197 | { |
| 198 | IRI("https://example.com"), |
| 199 | IRI("https://example.com/2"), |
| 200 | }, |
| 201 | { |
| 202 | IRI("https://example.com/1"), |
| 203 | }, |
| 204 | }, |
| 205 | }, |
| 206 | { |
| 207 | name: "some-overlap", |
| 208 | args: []*ItemCollection{ |
| 209 | { |
| 210 | IRI("https://example.com"), |
| 211 | IRI("https://example.com/2"), |
| 212 | }, |
| 213 | { |
| 214 | IRI("https://example.com/1"), |
| 215 | IRI("https://example.com/2"), |
| 216 | }, |
| 217 | }, |
| 218 | want: ItemCollection{ |
| 219 | IRI("https://example.com"), |
| 220 | IRI("https://example.com/2"), |
| 221 | IRI("https://example.com/1"), |
| 222 | }, |
| 223 | remaining: []*ItemCollection{ |
| 224 | { |
| 225 | IRI("https://example.com"), |
| 226 | IRI("https://example.com/2"), |
| 227 | }, |
| 228 | { |
| 229 | IRI("https://example.com/1"), |
| 230 | }, |
| 231 | }, |
| 232 | }, |
| 233 | { |
| 234 | name: "test from spammy", |
| 235 | args: []*ItemCollection{ |
| 236 | { |
| 237 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4/followers"), |
| 238 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 239 | }, |
| 240 | { |
| 241 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 242 | }, |
| 243 | { |
| 244 | IRI("https://example.dev"), |
| 245 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 246 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 247 | }, |
| 248 | }, |
| 249 | want: ItemCollection{ |
| 250 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4/followers"), |
| 251 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 252 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 253 | IRI("https://example.dev"), |
| 254 | }, |
| 255 | remaining: []*ItemCollection{ |
| 256 | { |
| 257 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4/followers"), |
| 258 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 259 | }, |
| 260 | { |
| 261 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 262 | }, |
| 263 | { |
| 264 | IRI("https://example.dev"), |
| 265 | }, |
| 266 | }, |
| 267 | }, |
| 268 | { |
| 269 | name: "different order for spammy test", |
| 270 | args: []*ItemCollection{ |
| 271 | { |
| 272 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4/followers"), |
| 273 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 274 | }, |
| 275 | { |
| 276 | IRI("https://example.dev"), |
| 277 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 278 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 279 | }, |
| 280 | { |
| 281 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 282 | }, |
| 283 | }, |
| 284 | want: ItemCollection{ |
| 285 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4/followers"), |
| 286 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 287 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 288 | IRI("https://example.dev"), |
| 289 | }, |
| 290 | remaining: []*ItemCollection{ |
| 291 | { |
| 292 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4/followers"), |
| 293 | IRI("https://www.w3.org/ns/activitystreams#Public"), |
| 294 | }, |
| 295 | { |
| 296 | IRI("https://example.dev"), |
| 297 | IRI("https://example.dev/a801139a-0d9a-4703-b0a5-9d14ae1438e4"), |
| 298 | }, |
| 299 | {}, |
| 300 | }, |
| 301 | }, |
| 302 | } |
| 303 | for _, tt := range tests { |
| 304 | t.Run(tt.name, func(t *testing.T) { |
| 305 | if got := ItemCollectionDeduplication(tt.args...); !tt.want.Equals(got) { |
| 306 | t.Errorf("ItemCollectionDeduplication() = %v, want %v", got, tt.want) |
| 307 | } |
| 308 | if len(tt.remaining) != len(tt.args) { |
| 309 | t.Errorf("ItemCollectionDeduplication() arguments count %d, want %d", len(tt.args), len(tt.remaining)) |
| 310 | } |
| 311 | for i, remArg := range tt.remaining { |
| 312 | arg := tt.args[i] |
| 313 | if !remArg.Equals(arg) { |
| 314 | t.Errorf("ItemCollectionDeduplication() argument at pos %d = %v, want %v", i, arg, remArg) |
| 315 | } |
| 316 | } |
| 317 | }) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | func TestToItemCollection1(t *testing.T) { |
| 322 | tests := []struct { |
| 323 | name string |
| 324 | it Item |
| 325 | want *ItemCollection |
| 326 | wantErr bool |
| 327 | }{ |
| 328 | { |
| 329 | name: "empty", |
| 330 | }, |
| 331 | { |
| 332 | name: "IRIs to ItemCollection", |
| 333 | it: IRIs{"https://example.com", "https://example.com/example"}, |
| 334 | want: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 335 | wantErr: false, |
| 336 | }, |
| 337 | { |
| 338 | name: "ItemCollection to ItemCollection", |
| 339 | it: ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 340 | want: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 341 | wantErr: false, |
| 342 | }, |
| 343 | { |
| 344 | name: "*ItemCollection to ItemCollection", |
| 345 | it: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 346 | want: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 347 | wantErr: false, |
| 348 | }, |
| 349 | { |
| 350 | name: "Collection to ItemCollection", |
| 351 | it: &Collection{Items: ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}}, |
| 352 | want: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 353 | wantErr: false, |
| 354 | }, |
| 355 | { |
| 356 | name: "CollectionPage to ItemCollection", |
| 357 | it: &CollectionPage{Items: ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}}, |
| 358 | want: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 359 | wantErr: false, |
| 360 | }, |
| 361 | { |
| 362 | name: "OrderedCollection to ItemCollection", |
| 363 | it: &OrderedCollection{OrderedItems: ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}}, |
| 364 | want: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 365 | wantErr: false, |
| 366 | }, |
| 367 | { |
| 368 | name: "OrderedCollectionPage to ItemOrderedCollection", |
| 369 | it: &OrderedCollectionPage{OrderedItems: ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}}, |
| 370 | want: &ItemCollection{IRI("https://example.com"), IRI("https://example.com/example")}, |
| 371 | wantErr: false, |
| 372 | }, |
| 373 | } |
| 374 | for _, tt := range tests { |
| 375 | t.Run(tt.name, func(t *testing.T) { |
| 376 | got, err := ToItemCollection(tt.it) |
| 377 | if (err != nil) != tt.wantErr { |
| 378 | t.Errorf("ToItemCollection() error = %v, wantErr %v", err, tt.wantErr) |
| 379 | return |
| 380 | } |
| 381 | if !reflect.DeepEqual(got, tt.want) { |
| 382 | t.Errorf("ToItemCollection() got = %v, want %v", got, tt.want) |
| 383 | } |
| 384 | }) |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | func TestItemCollection_IRIs(t *testing.T) { |
| 389 | tests := []struct { |
| 390 | name string |
| 391 | i ItemCollection |
| 392 | want IRIs |
| 393 | }{ |
| 394 | { |
| 395 | name: "empty", |
| 396 | i: nil, |
| 397 | want: nil, |
| 398 | }, |
| 399 | { |
| 400 | name: "one item", |
| 401 | i: ItemCollection{ |
| 402 | &Object{ID: "https://example.com"}, |
| 403 | }, |
| 404 | want: IRIs{"https://example.com"}, |
| 405 | }, |
| 406 | { |
| 407 | name: "two items", |
| 408 | i: ItemCollection{ |
| 409 | &Object{ID: "https://example.com"}, |
| 410 | &Actor{ID: "https://example.com/~jdoe"}, |
| 411 | }, |
| 412 | want: IRIs{"https://example.com", "https://example.com/~jdoe"}, |
| 413 | }, |
| 414 | { |
| 415 | name: "mixed items", |
| 416 | i: ItemCollection{ |
| 417 | &Object{ID: "https://example.com"}, |
| 418 | IRI("https://example.com/666"), |
| 419 | &Actor{ID: "https://example.com/~jdoe"}, |
| 420 | }, |
| 421 | want: IRIs{"https://example.com", "https://example.com/666", "https://example.com/~jdoe"}, |
| 422 | }, |
| 423 | } |
| 424 | for _, tt := range tests { |
| 425 | t.Run(tt.name, func(t *testing.T) { |
| 426 | if got := tt.i.IRIs(); !reflect.DeepEqual(got, tt.want) { |
| 427 | t.Errorf("IRIs() = %v, want %v", got, tt.want) |
| 428 | } |
| 429 | }) |
| 430 | } |
| 431 | } |