<commit_before>const {
  createCluster,
  secureRandom,
  createTopic,
  createModPartitioner,
  newLogger,
} = require('testHelpers')

const createProducer = require('../../producer')

describe('Cluster > fetchTopicsOffset', () => {
  let cluster, topic, producer

  const sendSampleMessages = async () => {
    await producer.send({
      topic,
      acks: 1,
      messages: [
        { key: 'k1', value: 'v1' },
        { key: 'k2', value: 'v2' },
        { key: 'k3', value: 'v3' },
        { key: 'k4', value: 'v4' },
      ],
    })
  }

  beforeEach(async () => {
    topic = `test-topic-${secureRandom()}`
    cluster = createCluster()

    await createTopic({ topic, partitions: 3 })
    await cluster.connect()
    await cluster.addTargetTopic(topic)

    producer = createProducer({
      cluster,
      logger: newLogger(),
      createPartitioner: createModPartitioner,
    })

    await producer.connect()
    await sendSampleMessages()
  })

  afterEach(async () => {
    await producer.disconnect()
    cluster && (await cluster.disconnect())
  })

  test('returns latest offsets by default', async () => {
    const result = await cluster.fetchTopicsOffset([
      {
        topic,
        partitions: [{ partition: 0 }, { partition: 1 }, { partition: 2 }],
      },
    ])

    expect(result).toEqual([
      {<commit_msg>Make sure the producer is initialized in the test before disconnecting<commit_after>const {
  createCluster,
  secureRandom,
  createTopic,
  createModPartitioner,
  newLogger,
} = require('testHelpers')

const createProducer = require('../../producer')

describe('Cluster > fetchTopicsOffset', () => {
  let cluster, topic, producer

  const sendSampleMessages = async () => {
    await producer.send({
      topic,
      acks: 1,
      messages: [
        { key: 'k1', value: 'v1' },
        { key: 'k2', value: 'v2' },
        { key: 'k3', value: 'v3' },
        { key: 'k4', value: 'v4' },
      ],
    })
  }

  beforeEach(async () => {
    topic = `test-topic-${secureRandom()}`
    cluster = createCluster()

    await createTopic({ topic, partitions: 3 })
    await cluster.connect()
    await cluster.addTargetTopic(topic)

    producer = createProducer({
      cluster,
      logger: newLogger(),
      createPartitioner: createModPartitioner,
    })

    await producer.connect()
    await sendSampleMessages()
  })

  afterEach(async () => {
    producer && (await producer.disconnect())
    cluster && (await cluster.disconnect())
  })

  test('returns latest offsets by default', async () => {
    const result = await cluster.fetchTopicsOffset([
      {
        topic,
        partitions: [{ partition: 0 }, { partition: 1 }, { partition: 2 }],
      },
    ])

    expect(result).toEqual([
      {
{'fraction_non_alphanumeric': 0.1502722323049002, 'fraction_numerical': 0.009437386569872959, 'mean_word_length': 2.6993288590604028, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1683725', 'n_tokens_mistral': 848, 'n_tokens_neox': 837, 'n_words': 193}