| 1 | package activitypub |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/google/go-cmp/cmp" |
| 8 | ) |
| 9 | |
| 10 | func TestIntransitiveActivityNew(t *testing.T) { |
| 11 | testValue := ID("test") |
| 12 | var testType ActivityVocabularyType = "Arrive" |
| 13 | |
| 14 | a := IntransitiveActivityNew(testValue, testType) |
| 15 | |
| 16 | if a.ID != testValue { |
| 17 | t.Errorf("IntransitiveActivity Id '%v' different than expected '%v'", a.ID, testValue) |
| 18 | } |
| 19 | if !a.Match(testType) { |
| 20 | t.Errorf("IntransitiveActivity Type '%v' different than expected '%v'", a.GetType(), testType) |
| 21 | } |
| 22 | |
| 23 | g := IntransitiveActivityNew(testValue, "") |
| 24 | |
| 25 | if g.ID != testValue { |
| 26 | t.Errorf("IntransitiveActivity Id '%v' different than expected '%v'", g.ID, testValue) |
| 27 | } |
| 28 | if !g.Match(IntransitiveActivityType) { |
| 29 | t.Errorf("IntransitiveActivity Type '%v' different than expected '%v'", g.GetType(), IntransitiveActivityType) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func TestIntransitiveActivityRecipients(t *testing.T) { |
| 34 | bob := PersonNew("bob") |
| 35 | alice := PersonNew("alice") |
| 36 | foo := OrganizationNew("foo") |
| 37 | bar := GroupNew("bar") |
| 38 | |
| 39 | a := IntransitiveActivityNew("test", "t") |
| 40 | |
| 41 | a.To.Append(bob) |
| 42 | a.To.Append(alice) |
| 43 | a.To.Append(foo) |
| 44 | a.To.Append(bar) |
| 45 | if len(a.To) != 4 { |
| 46 | t.Errorf("%T.To should have exactly 4(four) elements, not %d", a, len(a.To)) |
| 47 | } |
| 48 | |
| 49 | a.To.Append(bar) |
| 50 | a.To.Append(alice) |
| 51 | a.To.Append(foo) |
| 52 | a.To.Append(bob) |
| 53 | if len(a.To) != 4 { |
| 54 | t.Errorf("%T.To should have exactly 4(eight) elements, not %d", a, len(a.To)) |
| 55 | } |
| 56 | |
| 57 | a.Recipients() |
| 58 | if len(a.To) != 4 { |
| 59 | t.Errorf("%T.To should have exactly 4(four) elements, not %d", a, len(a.To)) |
| 60 | } |
| 61 | |
| 62 | b := ActivityNew("t", "test", nil) |
| 63 | |
| 64 | b.To.Append(bar) |
| 65 | b.To.Append(alice) |
| 66 | b.To.Append(foo) |
| 67 | b.To.Append(bob) |
| 68 | b.Bto.Append(bar) |
| 69 | b.Bto.Append(alice) |
| 70 | b.Bto.Append(foo) |
| 71 | b.Bto.Append(bob) |
| 72 | b.CC.Append(bar) |
| 73 | b.CC.Append(alice) |
| 74 | b.CC.Append(foo) |
| 75 | b.CC.Append(bob) |
| 76 | b.BCC.Append(bar) |
| 77 | b.BCC.Append(alice) |
| 78 | b.BCC.Append(foo) |
| 79 | b.BCC.Append(bob) |
| 80 | |
| 81 | b.Recipients() |
| 82 | if len(b.To) != 4 { |
| 83 | t.Errorf("%T.To should have exactly 4(four) elements, not %d", b, len(b.To)) |
| 84 | } |
| 85 | if len(b.Bto) != 0 { |
| 86 | t.Errorf("%T.Bto should have exactly 0(zero) elements, not %d", b, len(b.Bto)) |
| 87 | } |
| 88 | if len(b.CC) != 0 { |
| 89 | t.Errorf("%T.CC should have exactly 0(zero) elements, not %d", b, len(b.CC)) |
| 90 | } |
| 91 | if len(b.BCC) != 0 { |
| 92 | t.Errorf("%T.BCC should have exactly 0(zero) elements, not %d", b, len(b.BCC)) |
| 93 | } |
| 94 | var err error |
| 95 | recIds := make([]ID, 0) |
| 96 | err = checkDedup(b.To, &recIds) |
| 97 | if err != nil { |
| 98 | t.Error(err) |
| 99 | } |
| 100 | err = checkDedup(b.Bto, &recIds) |
| 101 | if err != nil { |
| 102 | t.Error(err) |
| 103 | } |
| 104 | err = checkDedup(b.CC, &recIds) |
| 105 | if err != nil { |
| 106 | t.Error(err) |
| 107 | } |
| 108 | err = checkDedup(b.BCC, &recIds) |
| 109 | if err != nil { |
| 110 | t.Error(err) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | func TestIntransitiveActivity_GetLink(t *testing.T) { |
| 115 | i := IntransitiveActivityNew("test", QuestionType) |
| 116 | |
| 117 | if i.GetID() != "test" { |
| 118 | t.Errorf("%T should return an empty %T object. Received %#v", i, i, i) |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func TestIntransitiveActivity_GetObject(t *testing.T) { |
| 123 | i := IntransitiveActivityNew("test", QuestionType) |
| 124 | |
| 125 | if i.GetID() != "test" || !i.Match(QuestionType) { |
| 126 | t.Errorf("%T should not return an empty %T object. Received %#v", i, i, i) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestIntransitiveActivity_IsLink(t *testing.T) { |
| 131 | i := IntransitiveActivityNew("test", QuestionType) |
| 132 | |
| 133 | if i.IsLink() { |
| 134 | t.Errorf("%T should not respond true to IsLink", i) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func TestIntransitiveActivity_IsObject(t *testing.T) { |
| 139 | i := IntransitiveActivityNew("test", ActivityType) |
| 140 | |
| 141 | if !i.IsObject() { |
| 142 | t.Errorf("%T should respond true to IsObject", i) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestIntransitiveActivity_Recipients(t *testing.T) { |
| 147 | to := PersonNew("bob") |
| 148 | o := ObjectNew(ArticleType) |
| 149 | cc := PersonNew("alice") |
| 150 | |
| 151 | o.ID = "something" |
| 152 | |
| 153 | c := IntransitiveActivityNew("act", IntransitiveActivityType) |
| 154 | c.To.Append(to) |
| 155 | c.CC.Append(cc) |
| 156 | c.BCC.Append(cc) |
| 157 | |
| 158 | c.Recipients() |
| 159 | |
| 160 | var err error |
| 161 | recIds := make([]ID, 0) |
| 162 | err = checkDedup(c.To, &recIds) |
| 163 | if err != nil { |
| 164 | t.Error(err) |
| 165 | } |
| 166 | err = checkDedup(c.Bto, &recIds) |
| 167 | if err != nil { |
| 168 | t.Error(err) |
| 169 | } |
| 170 | err = checkDedup(c.CC, &recIds) |
| 171 | if err != nil { |
| 172 | t.Error(err) |
| 173 | } |
| 174 | err = checkDedup(c.BCC, &recIds) |
| 175 | if err != nil { |
| 176 | t.Error(err) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | func TestIntransitiveActivity_GetID(t *testing.T) { |
| 181 | a := IntransitiveActivityNew("test", IntransitiveActivityType) |
| 182 | |
| 183 | if a.GetID() != "test" { |
| 184 | t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), a.GetID()) |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | func TestIntransitiveActivity_GetType(t *testing.T) { |
| 189 | { |
| 190 | a := IntransitiveActivityNew("test", IntransitiveActivityType) |
| 191 | if !a.Match(IntransitiveActivityType) { |
| 192 | t.Errorf("GetType should return %q for %T, received %q", IntransitiveActivityType, a, a.GetType()) |
| 193 | } |
| 194 | } |
| 195 | { |
| 196 | a := IntransitiveActivityNew("test", ArriveType) |
| 197 | if !a.Match(ArriveType) { |
| 198 | t.Errorf("GetType should return %q for %T, received %q", ArriveType, a, a.GetType()) |
| 199 | } |
| 200 | } |
| 201 | { |
| 202 | a := IntransitiveActivityNew("test", QuestionType) |
| 203 | if !a.Match(QuestionType) { |
| 204 | t.Errorf("GetType should return %q for %T, received %q", QuestionType, a, a.GetType()) |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | func TestToIntransitiveActivity(t *testing.T) { |
| 210 | tests := []struct { |
| 211 | name string |
| 212 | it LinkOrIRI |
| 213 | want *IntransitiveActivity |
| 214 | wantErr error |
| 215 | }{ |
| 216 | { |
| 217 | name: "empty", |
| 218 | }, |
| 219 | { |
| 220 | name: "Valid IntransitiveActivity", |
| 221 | it: IntransitiveActivity{ID: "test", Type: TravelType}, |
| 222 | want: &IntransitiveActivity{ID: "test", Type: TravelType}, |
| 223 | }, |
| 224 | { |
| 225 | name: "Valid *IntransitiveActivity", |
| 226 | it: &IntransitiveActivity{ID: "test", Type: ArriveType}, |
| 227 | want: &IntransitiveActivity{ID: "test", Type: ArriveType}, |
| 228 | }, |
| 229 | { |
| 230 | name: "Valid Question", |
| 231 | it: Question{ID: "test", Type: QuestionType}, |
| 232 | want: &IntransitiveActivity{ID: "test", Type: QuestionType}, |
| 233 | }, |
| 234 | { |
| 235 | name: "Valid *Question", |
| 236 | it: &Question{ID: "test", Type: QuestionType}, |
| 237 | want: &IntransitiveActivity{ID: "test", Type: QuestionType}, |
| 238 | }, |
| 239 | { |
| 240 | name: "IRI", |
| 241 | it: IRI("https://example.com"), |
| 242 | wantErr: ErrorInvalidType[IntransitiveActivity](IRI("")), |
| 243 | }, |
| 244 | { |
| 245 | name: "Activity", |
| 246 | it: &Activity{ID: "test", Type: UpdateType}, |
| 247 | want: &IntransitiveActivity{ID: "test", Type: UpdateType}, |
| 248 | }, |
| 249 | { |
| 250 | name: "IRIs", |
| 251 | it: IRIs{IRI("https://example.com")}, |
| 252 | wantErr: ErrorInvalidType[IntransitiveActivity](IRIs{}), |
| 253 | }, |
| 254 | { |
| 255 | name: "ItemCollection", |
| 256 | it: ItemCollection{}, |
| 257 | wantErr: ErrorInvalidType[IntransitiveActivity](ItemCollection{}), |
| 258 | }, |
| 259 | { |
| 260 | name: "Object", |
| 261 | it: &Object{ID: "test", Type: ArticleType}, |
| 262 | wantErr: ErrorInvalidType[IntransitiveActivity](&Object{}), |
| 263 | }, |
| 264 | { |
| 265 | name: "Actor", |
| 266 | it: &Actor{ID: "test", Type: PersonType}, |
| 267 | wantErr: ErrorInvalidType[IntransitiveActivity](&Person{}), |
| 268 | }, |
| 269 | } |
| 270 | for _, tt := range tests { |
| 271 | t.Run(tt.name, func(t *testing.T) { |
| 272 | got, err := ToIntransitiveActivity(tt.it) |
| 273 | if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) { |
| 274 | t.Errorf("ToIntransitiveActivity() error = %v, wantErr %v", err, tt.wantErr) |
| 275 | return |
| 276 | } |
| 277 | if !cmp.Equal(got, tt.want) { |
| 278 | t.Errorf("ToIntransitiveActivity() got = %s", cmp.Diff(tt.want, got)) |
| 279 | } |
| 280 | }) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestIntransitiveActivity_Clean(t *testing.T) { |
| 285 | t.Skipf("TODO") |
| 286 | } |
| 287 | |
| 288 | func TestIntransitiveActivity_IsCollection(t *testing.T) { |
| 289 | t.Skipf("TODO") |
| 290 | } |
| 291 | |
| 292 | func TestIntransitiveActivity_UnmarshalJSON(t *testing.T) { |
| 293 | t.Skipf("TODO") |
| 294 | } |
| 295 | |
| 296 | func TestArriveNew(t *testing.T) { |
| 297 | testValue := ID("test") |
| 298 | |
| 299 | a := ArriveNew(testValue) |
| 300 | |
| 301 | if a.ID != testValue { |
| 302 | t.Errorf("Activity Id '%v' different than expected '%v'", a.ID, testValue) |
| 303 | } |
| 304 | if !a.Match(ArriveType) { |
| 305 | t.Errorf("Activity Type '%v' different than expected '%v'", a.GetType(), ArriveType) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | func TestTravelNew(t *testing.T) { |
| 310 | testValue := ID("test") |
| 311 | |
| 312 | a := TravelNew(testValue) |
| 313 | |
| 314 | if a.ID != testValue { |
| 315 | t.Errorf("Activity Id '%v' different than expected '%v'", a.ID, testValue) |
| 316 | } |
| 317 | if !a.Match(TravelType) { |
| 318 | t.Errorf("Activity Type '%v' different than expected '%v'", a.GetType(), TravelType) |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | func TestIntransitiveActivity_Equals(t *testing.T) { |
| 323 | type fields struct { |
| 324 | ID ID |
| 325 | Type Typer |
| 326 | Name NaturalLanguageValues |
| 327 | Attachment Item |
| 328 | AttributedTo Item |
| 329 | Audience ItemCollection |
| 330 | Content NaturalLanguageValues |
| 331 | Context Item |
| 332 | MediaType MimeType |
| 333 | EndTime time.Time |
| 334 | Generator Item |
| 335 | Icon Item |
| 336 | Image Item |
| 337 | InReplyTo Item |
| 338 | Location Item |
| 339 | Preview Item |
| 340 | Published time.Time |
| 341 | Replies Item |
| 342 | StartTime time.Time |
| 343 | Summary NaturalLanguageValues |
| 344 | Tag ItemCollection |
| 345 | Updated time.Time |
| 346 | URL Item |
| 347 | To ItemCollection |
| 348 | Bto ItemCollection |
| 349 | CC ItemCollection |
| 350 | BCC ItemCollection |
| 351 | Duration time.Duration |
| 352 | Likes Item |
| 353 | Shares Item |
| 354 | Source Source |
| 355 | Actor Item |
| 356 | Target Item |
| 357 | Result Item |
| 358 | Origin Item |
| 359 | Instrument Item |
| 360 | } |
| 361 | tests := []struct { |
| 362 | name string |
| 363 | fields fields |
| 364 | arg Item |
| 365 | want bool |
| 366 | }{ |
| 367 | { |
| 368 | name: "equal-empty-intransitive-activity", |
| 369 | fields: fields{}, |
| 370 | arg: IntransitiveActivity{}, |
| 371 | want: true, |
| 372 | }, |
| 373 | { |
| 374 | name: "equal-intransitive-activity-just-id", |
| 375 | fields: fields{ID: "test"}, |
| 376 | arg: IntransitiveActivity{ID: "test"}, |
| 377 | want: true, |
| 378 | }, |
| 379 | { |
| 380 | name: "equal-intransitive-activity-id", |
| 381 | fields: fields{ID: "test", URL: IRI("example.com")}, |
| 382 | arg: IntransitiveActivity{ID: "test"}, |
| 383 | want: true, |
| 384 | }, |
| 385 | { |
| 386 | name: "equal-false-with-id-and-url", |
| 387 | fields: fields{ID: "test"}, |
| 388 | arg: IntransitiveActivity{ID: "test", URL: IRI("example.com")}, |
| 389 | want: false, |
| 390 | }, |
| 391 | { |
| 392 | name: "not a valid intransitive-activity", |
| 393 | fields: fields{ID: "http://example.com"}, |
| 394 | arg: Link{ID: "http://example.com"}, |
| 395 | want: false, |
| 396 | }, |
| 397 | } |
| 398 | for _, tt := range tests { |
| 399 | t.Run(tt.name, func(t *testing.T) { |
| 400 | a := IntransitiveActivity{ |
| 401 | ID: tt.fields.ID, |
| 402 | Type: tt.fields.Type, |
| 403 | Name: tt.fields.Name, |
| 404 | Attachment: tt.fields.Attachment, |
| 405 | AttributedTo: tt.fields.AttributedTo, |
| 406 | Audience: tt.fields.Audience, |
| 407 | Content: tt.fields.Content, |
| 408 | Context: tt.fields.Context, |
| 409 | MediaType: tt.fields.MediaType, |
| 410 | EndTime: tt.fields.EndTime, |
| 411 | Generator: tt.fields.Generator, |
| 412 | Icon: tt.fields.Icon, |
| 413 | Image: tt.fields.Image, |
| 414 | InReplyTo: tt.fields.InReplyTo, |
| 415 | Location: tt.fields.Location, |
| 416 | Preview: tt.fields.Preview, |
| 417 | Published: tt.fields.Published, |
| 418 | Replies: tt.fields.Replies, |
| 419 | StartTime: tt.fields.StartTime, |
| 420 | Summary: tt.fields.Summary, |
| 421 | Tag: tt.fields.Tag, |
| 422 | Updated: tt.fields.Updated, |
| 423 | URL: tt.fields.URL, |
| 424 | To: tt.fields.To, |
| 425 | Bto: tt.fields.Bto, |
| 426 | CC: tt.fields.CC, |
| 427 | BCC: tt.fields.BCC, |
| 428 | Duration: tt.fields.Duration, |
| 429 | Likes: tt.fields.Likes, |
| 430 | Shares: tt.fields.Shares, |
| 431 | Source: tt.fields.Source, |
| 432 | Actor: tt.fields.Actor, |
| 433 | Target: tt.fields.Target, |
| 434 | Result: tt.fields.Result, |
| 435 | Origin: tt.fields.Origin, |
| 436 | Instrument: tt.fields.Instrument, |
| 437 | } |
| 438 | if got := a.Equals(tt.arg); got != tt.want { |
| 439 | t.Errorf("Equal() = %v, want %v", got, tt.want) |
| 440 | } |
| 441 | }) |
| 442 | } |
| 443 | } |