···
316
+
// TestUserRepository_GetByDIDs tests the batch user retrieval functionality
317
+
func TestUserRepository_GetByDIDs(t *testing.T) {
318
+
db := setupTestDB(t)
320
+
if err := db.Close(); err != nil {
321
+
t.Logf("Failed to close database: %v", err)
325
+
userRepo := postgres.NewUserRepository(db)
326
+
ctx := context.Background()
328
+
// Create test users
329
+
user1 := &users.User{
330
+
DID: "did:plc:getbydids1",
331
+
Handle: "user1.test",
332
+
PDSURL: "https://pds1.example.com",
334
+
user2 := &users.User{
335
+
DID: "did:plc:getbydids2",
336
+
Handle: "user2.test",
337
+
PDSURL: "https://pds2.example.com",
339
+
user3 := &users.User{
340
+
DID: "did:plc:getbydids3",
341
+
Handle: "user3.test",
342
+
PDSURL: "https://pds3.example.com",
345
+
_, err := userRepo.Create(ctx, user1)
347
+
t.Fatalf("Failed to create user1: %v", err)
349
+
_, err = userRepo.Create(ctx, user2)
351
+
t.Fatalf("Failed to create user2: %v", err)
353
+
_, err = userRepo.Create(ctx, user3)
355
+
t.Fatalf("Failed to create user3: %v", err)
358
+
t.Run("Empty array returns empty map", func(t *testing.T) {
359
+
result, err := userRepo.GetByDIDs(ctx, []string{})
361
+
t.Errorf("Expected no error for empty array, got: %v", err)
364
+
t.Error("Expected non-nil map, got nil")
366
+
if len(result) != 0 {
367
+
t.Errorf("Expected empty map, got length: %d", len(result))
371
+
t.Run("Single DID returns one user", func(t *testing.T) {
372
+
result, err := userRepo.GetByDIDs(ctx, []string{"did:plc:getbydids1"})
374
+
t.Fatalf("Failed to get user by DID: %v", err)
376
+
if len(result) != 1 {
377
+
t.Errorf("Expected 1 user, got %d", len(result))
379
+
if user, found := result["did:plc:getbydids1"]; !found {
380
+
t.Error("Expected user1 to be in result")
381
+
} else if user.Handle != "user1.test" {
382
+
t.Errorf("Expected handle user1.test, got %s", user.Handle)
386
+
t.Run("Multiple DIDs returns multiple users", func(t *testing.T) {
387
+
result, err := userRepo.GetByDIDs(ctx, []string{
388
+
"did:plc:getbydids1",
389
+
"did:plc:getbydids2",
390
+
"did:plc:getbydids3",
393
+
t.Fatalf("Failed to get users by DIDs: %v", err)
395
+
if len(result) != 3 {
396
+
t.Errorf("Expected 3 users, got %d", len(result))
398
+
if result["did:plc:getbydids1"].Handle != "user1.test" {
399
+
t.Errorf("User1 handle mismatch")
401
+
if result["did:plc:getbydids2"].Handle != "user2.test" {
402
+
t.Errorf("User2 handle mismatch")
404
+
if result["did:plc:getbydids3"].Handle != "user3.test" {
405
+
t.Errorf("User3 handle mismatch")
409
+
t.Run("Missing DIDs not in result map", func(t *testing.T) {
410
+
result, err := userRepo.GetByDIDs(ctx, []string{
411
+
"did:plc:getbydids1",
412
+
"did:plc:nonexistent",
415
+
t.Fatalf("Failed to get users by DIDs: %v", err)
417
+
if len(result) != 1 {
418
+
t.Errorf("Expected 1 user (missing not included), got %d", len(result))
420
+
if _, found := result["did:plc:nonexistent"]; found {
421
+
t.Error("Expected nonexistent user to not be in result")
425
+
t.Run("Preserves all user fields correctly", func(t *testing.T) {
426
+
result, err := userRepo.GetByDIDs(ctx, []string{"did:plc:getbydids1"})
428
+
t.Fatalf("Failed to get user by DID: %v", err)
430
+
user := result["did:plc:getbydids1"]
431
+
if user.DID != "did:plc:getbydids1" {
432
+
t.Errorf("DID mismatch: expected did:plc:getbydids1, got %s", user.DID)
434
+
if user.Handle != "user1.test" {
435
+
t.Errorf("Handle mismatch: expected user1.test, got %s", user.Handle)
437
+
if user.PDSURL != "https://pds1.example.com" {
438
+
t.Errorf("PDSURL mismatch: expected https://pds1.example.com, got %s", user.PDSURL)
440
+
if user.CreatedAt.IsZero() {
441
+
t.Error("CreatedAt should not be zero")
443
+
if user.UpdatedAt.IsZero() {
444
+
t.Error("UpdatedAt should not be zero")
448
+
t.Run("Validates batch size limit", func(t *testing.T) {
449
+
// Create array exceeding MaxBatchSize (1000)
450
+
largeDIDs := make([]string, 1001)
451
+
for i := 0; i < 1001; i++ {
452
+
largeDIDs[i] = fmt.Sprintf("did:plc:test%d", i)
455
+
_, err := userRepo.GetByDIDs(ctx, largeDIDs)
457
+
t.Error("Expected error for batch size exceeding limit, got nil")
459
+
if !strings.Contains(err.Error(), "exceeds maximum") {
460
+
t.Errorf("Expected batch size error, got: %v", err)
464
+
t.Run("Validates DID format", func(t *testing.T) {
465
+
invalidDIDs := []string{
466
+
"did:plc:getbydids1",
467
+
"invalid-did", // Invalid DID format
470
+
_, err := userRepo.GetByDIDs(ctx, invalidDIDs)
472
+
t.Error("Expected error for invalid DID format, got nil")
474
+
if !strings.Contains(err.Error(), "invalid DID format") {
475
+
t.Errorf("Expected invalid DID format error, got: %v", err)
// TestHandleValidation tests atProto handle validation rules
func TestHandleValidation(t *testing.T) {