public CompletableFuture<Account> removeDevice(final Account account, final byte deviceId) {
    if (deviceId == Device.PRIMARY_ID) {
      throw new IllegalArgumentException("Cannot remove primary device");
    }

    return accountLockManager.withLockAsync(List.of(account.getNumber()),
        () -> removeDevice(account.getIdentifier(IdentityType.ACI), deviceId, MAX_UPDATE_ATTEMPTS),
        accountLockExecutor);
  }
@Test
  void testRemoveDevice() {
    final Device primaryDevice = new Device();
    primaryDevice.setId(Device.PRIMARY_ID);

    final Device linkedDevice = new Device();
    linkedDevice.setId((byte) (Device.PRIMARY_ID + 1));

    Account account = AccountsHelper.generateTestAccount("+14152222222", List.of(primaryDevice, linkedDevice));

    when(accounts.getByAccountIdentifierAsync(account.getUuid()))
        .thenReturn(CompletableFuture.completedFuture(Optional.of(account)));

    when(keysManager.deleteSingleUsePreKeys(any(), anyByte())).thenReturn(CompletableFuture.completedFuture(null));
    when(messagesManager.clear(any(), anyByte())).thenReturn(CompletableFuture.completedFuture(null));

    assertTrue(account.getDevice(linkedDevice.getId()).isPresent());

    account = accountsManager.removeDevice(account, linkedDevice.getId()).join();

    assertFalse(account.getDevice(linkedDevice.getId()).isPresent());
    verify(messagesManager, times(2)).clear(account.getUuid(), linkedDevice.getId());
    verify(keysManager, times(2)).deleteSingleUsePreKeys(account.getUuid(), linkedDevice.getId());
    verify(keysManager).buildWriteItemsForRemovedDevice(account.getUuid(), account.getPhoneNumberIdentifier(), linkedDevice.getId());
    verify(clientPublicKeysManager).buildTransactWriteItemForDeletion(account.getUuid(), linkedDevice.getId());
    verify(clientPresenceManager).disconnectPresence(account.getUuid(), linkedDevice.getId());
  }