text: const Broker = require('../index')
  const { MemberMetadata, MemberAssignment } = require('../../consumer/assignerProtocol')
  
  const {
    secureRandom,
    createConnectionPool,
    newLogger,
    createTopic,
    retryProtocol,
  } = require('testHelpers')
  
  describe('Broker > Heartbeat', () => {
    let topicName, groupId, seedBroker, broker, groupCoordinator
  
    beforeEach(async () => {
      topicName = `test-topic-${secureRandom()}`
      groupId = `consumer-group-id-${secureRandom()}`
  
      seedBroker = new Broker({
        connectionPool: createConnectionPool(),
        logger: newLogger(),
      })
      await seedBroker.connect()
      await createTopic({ topic: topicName })
  
      const metadata = await retryProtocol(
        'LEADER_NOT_AVAILABLE',
        async () => await seedBroker.metadata([topicName])
      )
  
      // Find leader of partition
      const partitionBroker = metadata.topicMetadata[0].partitionMetadata[0].leader
      const newBrokerData = metadata.brokers.find(b => b.nodeId === partitionBroker)
  
      // Connect to the correct broker to produce message
      broker = new Broker({
        connectionPool: createConnectionPool(newBrokerData),
        logger: newLogger(),
      })
      await broker.connect()
  
      const {
        coordinator: { host, port },
      } = await retryProtocol(
        'GROUP_COORDINATOR_NOT_AVAILABLE',
        async () => await seedBroker.findGroupCoordinator({ groupId })
      )
  
      groupCoordinator = new Broker({
        connectionPool: createConnectionPool({ host, port }),
        logger: newLogger(),
      })
      await groupCoordinator.connect()
    })
  
    afterEach(async () => {
      seedBroker && (await seedBroker.disconnect())
      broker && (await broker.disconnect())
      groupCoordinator && (await groupCoordinator.disconnect())
    })
  
    test('request', async () => {
      const { generationId, memberId } = await groupCoordinator.joinGroup({
        groupId,
        sessionTimeout: 30000,
        groupProtocols: [
          {
            name: 'AssignerName',
            metadata: MemberMetadata.encode({ version: 1, topics: [topicName] }),
          },
        ],
      })
  
      const memberAssignment = MemberAssignment.encode({
        version: 1,
        assignment: { [topicName]: [0] },
      })
  
      const groupAssignment = [{ memberId, memberAssignment }]
      await groupCoordinator.syncGroup({
        groupId,
        generationId,
        memberId,
        groupAssignment,
      })
  
      const response = await groupCoordinator.heartbeat({
        groupId,
        groupGenerationId: generationId,
        memberId,
      })
  
      expect(response).toEqual({
        clientSideThrottleTime: expect.optional(0),
        throttleTime: 0,
        errorCode: 0,
      })
    })
  })
  