Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

2.9 KB
Raw Blame
import "package:ente_contacts/contacts.dart" as contacts;
import "package:flutter_test/flutter_test.dart";
import "package:photos/models/api/collection/user.dart";
import "package:photos/models/collection/collection.dart";
import "package:photos/models/user_details.dart";
import "package:photos/services/contacts/direct_contact_users.dart";

void main() {
  test("includes only direct contact sources", () {
    final users = buildDirectContactUsers(
      ownerUserId: 1,
      ownerEmail: "[email protected]",
      collections: [
        _collection(
          owner: User(id: 1, email: "[email protected]"),
          sharees: [User(id: 2, email: "[email protected]")],
        ),
        _collection(
          owner: User(id: 3, email: "[email protected]"),
          sharees: [
            User(id: 1, email: "[email protected]"),
            User(id: 4, email: "[email protected]"),
          ],
        ),
      ],
      familyMembers: [
        _familyMember(
          "[email protected]",
          FamilyMemberStatus.accepted,
          userID: 6,
        ),
        _familyMember("[email protected]", FamilyMemberStatus.invited),
      ],
      savedContacts: [
        _savedContact(id: "contact-1", userID: 5, email: "[email protected]"),
      ],
    );

    expect(users.map((user) => user.email).toSet(), {
      "[email protected]",
      "[email protected]",
      "[email protected]",
      "[email protected]",
    });
    expect(
      users.singleWhere((user) => user.email == "[email protected]").id,
      6,
    );
  });

  test("deduplicates by user id after an email change", () {
    final users = buildDirectContactUsers(
      ownerUserId: 1,
      ownerEmail: "[email protected]",
      collections: [
        _collection(
          owner: User(id: 1, email: "[email protected]"),
          sharees: [User(id: 5, email: "[email protected]")],
        ),
      ],
      familyMembers: const [],
      savedContacts: [
        _savedContact(
          id: "contact-1",
          userID: 5,
          email: "[email protected]",
          name: "Same",
        ),
      ],
    );

    expect(users.single.email, "[email protected]");
  });
}

contacts.ContactRecord _savedContact({
  required String id,
  required int userID,
  required String? email,
  String name = "Saved",
}) {
  return contacts.ContactRecord(
    id: id,
    contactUserId: userID,
    email: email,
    name: name,
    profilePictureAttachmentId: null,
    isDeleted: false,
    createdAt: 1,
    updatedAt: 1,
  );
}

Collection _collection({required User owner, required List<User> sharees}) {
  return Collection(
    1,
    owner,
    "",
    null,
    "Album",
    null,
    null,
    CollectionType.album,
    CollectionAttributes(),
    sharees,
    const [],
    0,
  );
}

FamilyMember _familyMember(
  String email,
  FamilyMemberStatus status, {
  int? userID,
}) {
  return FamilyMember(email, 0, "family-id", userID, false, status, null);
}